Beispiel #1
0
        /// <summary>
        /// Set the position of this stream.
        /// </summary>
        /// <param name="offset">Byte offset relative to origin.</param>
        /// <param name="origin">Reference point.</param>
        /// <returns>New position of the stream.</returns>
        public override long Seek(long offset, SeekOrigin origin)
        {
            long target;

            switch (origin)
            {
            case SeekOrigin.Current:
                target = Position + offset;
                break;

            case SeekOrigin.Begin:
                target = offset;
                break;

            case SeekOrigin.End:
                target = file.Length() + offset;
                break;

            default:
                throw new ArgumentException("Invalid origin");
            }
            if ((target < 0) || (target > file.Length()))
            {
                throw new IOException("Seek outside file limits");
            }
            file.Seek(target);
            return(Position);
        }
Beispiel #2
0
        public static byte[] readFile(File file)
        {
            // Open file
            var f = new RandomAccessFile(file, "r");

            try
            {
                // Get and check length
                long longlength = f.Length();
                var  length     = (int)longlength;

                if (length != longlength)
                {
                    throw new IOException("Filesize exceeds allowed size");
                }
                // Read file and return data
                byte[] data = new byte[length];
                f.ReadFully(data);
                return(data);
            }
            finally
            {
                f.Close();
            }
        }
Beispiel #3
0
        public static byte [] readFile(Java.IO.File file)
        {
            // Open file
            var f = new RandomAccessFile(file, "r");

            try {
                // Get and check length
                long longlength = f.Length();
                var  length     = (int)longlength;

                if (length != longlength)
                {
                    throw new Java.IO.IOException("Filesize exceeds allowed size");
                }
                // Read file and return data
                byte [] data = new byte [length];
                f.ReadFully(data);
                return(data);
            } catch (Exception ex) {
                System.Diagnostics.Debug.Write(ex);
                return(new byte [0]);
            } finally {
                f.Close();
            }
        }
        public static byte[] readFile(Java.IO.File file)
        {
            // Open file
            RandomAccessFile f = new RandomAccessFile(file, "r");

            try
            {
                // Get and check length
                long longlength = f.Length();
                int  length     = (int)longlength;
                if (length != longlength)
                {
                    throw new Java.IO.IOException("Tamanho do arquivo excede o permitido!");
                }
                // Read file and return data
                byte[] data = new byte[length];
                f.ReadFully(data);
                return(data);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex);
                return(new byte[0]);
            }
            finally
            {
                f.Close();
            }
        }
Beispiel #5
0
 /// <exception cref="System.IO.IOException"></exception>
 private void DoOpen()
 {
     try
     {
         if (invalid)
         {
             throw new PackInvalidException(packFile);
         }
         lock (readLock)
         {
             fd     = new RandomAccessFile(packFile, "r");
             length = fd.Length();
             OnOpenPack();
         }
     }
     catch (IOException ioe)
     {
         OpenFail();
         throw;
     }
     catch (RuntimeException re)
     {
         OpenFail();
         throw;
     }
     catch (Error re)
     {
         OpenFail();
         throw;
     }
 }
Beispiel #6
0
 protected internal virtual void readToc()
 {
     toc = new Dictionary <int, int>();
     nextFreeBufferedSectorNumber = 0;
     tocDirty = false;
     try
     {
         tocFile.seek(0);
         long Length = tocFile.Length();
         if (Length >= 4)
         {
             numSectors = tocFile.readInt();
             for (long i = 4; i < Length; i += 8)
             {
                 int sectorNumber         = tocFile.readInt();
                 int bufferedSectorNumber = tocFile.readInt();
                 toc[sectorNumber]            = bufferedSectorNumber;
                 nextFreeBufferedSectorNumber = System.Math.Max(nextFreeBufferedSectorNumber, bufferedSectorNumber + 1);
             }
         }
         else if (sectorDevice != null)
         {
             numSectors = sectorDevice.NumSectors;
         }
     }
     catch (IOException e)
     {
         Console.WriteLine("readToc", e);
     }
 }
        public static byte[] ReadFileFromPath(File file)
        {
            // Open file
            var f = new RandomAccessFile(file, "r");

            try
            {
                // Get and check length
                var longlength = f.Length();
                var length     = (int)longlength;
                if (length != longlength)
                {
                    throw new IOException("Tamanho do arquivo excede o permitido!");
                }
                // Read file and return data
                var data = new byte[length];
                f.ReadFully(data);
                return(data);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex);
                return(null);
            }
            finally
            {
                f.Close();
                f.Dispose();
            }
        }
Beispiel #8
0
        /// <exception cref="System.IO.IOException"/>
        public static FsImageProto.FileSummary LoadSummary(RandomAccessFile file)
        {
            int  FileLengthFieldSize = 4;
            long fileLength          = file.Length();

            file.Seek(fileLength - FileLengthFieldSize);
            int summaryLength = file.ReadInt();

            if (summaryLength <= 0)
            {
                throw new IOException("Negative length of the file");
            }
            file.Seek(fileLength - FileLengthFieldSize - summaryLength);
            byte[] summaryBytes = new byte[summaryLength];
            file.ReadFully(summaryBytes);
            FsImageProto.FileSummary summary = FsImageProto.FileSummary.ParseDelimitedFrom(new
                                                                                           ByteArrayInputStream(summaryBytes));
            if (summary.GetOndiskVersion() != FileVersion)
            {
                throw new IOException("Unsupported file version " + summary.GetOndiskVersion());
            }
            if (!NameNodeLayoutVersion.Supports(LayoutVersion.Feature.ProtobufFormat, summary
                                                .GetLayoutVersion()))
            {
                throw new IOException("Unsupported layout version " + summary.GetLayoutVersion());
            }
            return(summary);
        }
 /// <exception cref="System.IO.IOException"/>
 public RandomAccessFileReader([NotNull] RandomAccessFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException();
     }
     _file   = file;
     _length = _file.Length();
 }
Beispiel #10
0
        //private static String readInstallationFile(File installation)
        //{
        //    RandomAccessFile f = new RandomAccessFile(installation, "r");
        //    byte[] bytes = new byte[(int)f.Length()];
        //    f.ReadFully(bytes);
        //    f.Close();
        //    return new Java.Lang.String(bytes).ToString();
        //}
        private static String readInstallationFile(Java.IO.File installation)
        {
            RandomAccessFile f = new RandomAccessFile(installation, "r");

            byte[] bytes = new byte[(int)f.Length()];
            f.ReadFully(bytes);
            f.Close();
            return(new Java.Lang.String(bytes).ToString());
        }
Beispiel #11
0
        public byte[] getAsBinary(string key)
        {
            RandomAccessFile raFile     = null;
            bool             removeFile = false;

            try
            {
                File file = mCache.get(key);

                if (!file.Exists())
                {
                    return(null);
                }

                raFile = new RandomAccessFile(file, "r");
                byte[] byteArray = new byte[(int)raFile.Length()];

                raFile.Read(byteArray);

                if (!Utils.isDue(byteArray))
                {
                    return(Utils.clearDateInfo(byteArray));
                }
                else
                {
                    removeFile = true;

                    return(null);
                }
            }
            catch (System.Exception ex)
            {
                VPNLog.d("ACache", ex.ToString());

                return(null);
            }
            finally
            {
                if (raFile != null)
                {
                    try
                    {
                        raFile.Dispose();
                    }
                    catch (IOException ex)
                    {
                        ex.PrintStackTrace();
                    }
                }

                if (removeFile)
                {
                    remove(key);
                }
            }
        }
 private MP4Parser(System.String path)
 {
     mFile = new RandomAccessFile(new File(path), "r");
     try {
         parse("", mFile.Length());
     } catch (Java.Lang.Exception e) {
         e.PrintStackTrace();
         throw new IOException("Parse error: malformed mp4 file");
     }
 }
        private static string readInstallationFile(File installation)
        {
            RandomAccessFile f = new RandomAccessFile(installation, "r");

            byte[] bytes = new byte[(int)f.Length()];
            f.ReadFully(bytes);
            f.Close();
            System.Text.UTF8Encoding en = new UTF8Encoding();
            return(en.GetString(bytes));
        }
Beispiel #14
0
        /// <summary>
        /// Open a random access file for the given path, more and access.
        /// </summary>
        private static RandomAccessFile Open(string path, FileMode mode, FileAccess access)
        {
            var file = new Java.Io.File(path);

            switch (mode)
            {
            case FileMode.CreateNew:
                if (file.Exists())
                {
                    throw new IOException("File already exists");
                }
                break;

            case FileMode.Open:
                if (!file.Exists())
                {
                    throw new FileNotFoundException(path);
                }
                break;

            case FileMode.Append:
                access = FileAccess.Write;
                break;
            }

            switch (mode)
            {
            case FileMode.Create:
            case FileMode.CreateNew:
            case FileMode.OpenOrCreate:
                if (access == FileAccess.Read)
                {
                    //create empty file, so it can be opened again with read only right,
                    //otherwise an FilNotFoundException is thrown.
                    var additinalAccessFile = new RandomAccessFile(file, "rw");
                }
                break;
            }

            var jMode            = (access == FileAccess.Read) ? "r" : "rw";
            var randomAccessFile = new RandomAccessFile(file, jMode);

            switch (mode)
            {
            case FileMode.Truncate:
                randomAccessFile.SetLength(0);
                break;

            case FileMode.Append:
                randomAccessFile.Seek(randomAccessFile.Length());
                break;
            }

            return(randomAccessFile);
        }
 /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
 public override long GetLength()
 {
     try
     {
         return(_delegate.Length());
     }
     catch (IOException e)
     {
         throw new Db4oIOException(e);
     }
 }
Beispiel #16
0
 /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
 public virtual long Length()
 {
     try
     {
         return(_file.Length());
     }
     catch (IOException e)
     {
         throw new Db4oIOException(e);
     }
 }
Beispiel #17
0
 /// <exception cref="System.IO.IOException"/>
 public static bool CheckFileFormat(RandomAccessFile file)
 {
     if (file.Length() < FSImageFormatProtobuf.Loader.MinimumFileLength)
     {
         return(false);
     }
     byte[] magic = new byte[MagicHeader.Length];
     file.ReadFully(magic);
     if (!Arrays.Equals(MagicHeader, magic))
     {
         return(false);
     }
     return(true);
 }
Beispiel #18
0
        /// <summary>TC7: Corrupted replicas are present.</summary>
        /// <exception cref="System.IO.IOException">an exception might be thrown</exception>
        /// <exception cref="System.Exception"/>
        private void TestTC7(bool appendToNewBlock)
        {
            short repl = 2;
            Path  p    = new Path("/TC7/foo" + (appendToNewBlock ? "0" : "1"));

            System.Console.Out.WriteLine("p=" + p);
            //a. Create file with replication factor of 2. Write half block of data. Close file.
            int len1 = (int)(BlockSize / 2);

            {
                FSDataOutputStream @out = fs.Create(p, false, buffersize, repl, BlockSize);
                AppendTestUtil.Write(@out, 0, len1);
                @out.Close();
            }
            DFSTestUtil.WaitReplication(fs, p, repl);
            //b. Log into one datanode that has one replica of this block.
            //   Find the block file on this datanode and truncate it to zero size.
            LocatedBlocks locatedblocks = fs.dfs.GetNamenode().GetBlockLocations(p.ToString()
                                                                                 , 0L, len1);

            NUnit.Framework.Assert.AreEqual(1, locatedblocks.LocatedBlockCount());
            LocatedBlock  lb  = locatedblocks.Get(0);
            ExtendedBlock blk = lb.GetBlock();

            NUnit.Framework.Assert.AreEqual(len1, lb.GetBlockSize());
            DatanodeInfo[] datanodeinfos = lb.GetLocations();
            NUnit.Framework.Assert.AreEqual(repl, datanodeinfos.Length);
            DataNode dn = cluster.GetDataNode(datanodeinfos[0].GetIpcPort());
            FilePath f  = DataNodeTestUtils.GetBlockFile(dn, blk.GetBlockPoolId(), blk.GetLocalBlock
                                                             ());
            RandomAccessFile raf = new RandomAccessFile(f, "rw");

            AppendTestUtil.Log.Info("dn=" + dn + ", blk=" + blk + " (length=" + blk.GetNumBytes
                                        () + ")");
            NUnit.Framework.Assert.AreEqual(len1, raf.Length());
            raf.SetLength(0);
            raf.Close();
            //c. Open file in "append mode".  Append a new block worth of data. Close file.
            int len2 = (int)BlockSize;

            {
                FSDataOutputStream @out = appendToNewBlock ? fs.Append(p, EnumSet.Of(CreateFlag.Append
                                                                                     , CreateFlag.NewBlock), 4096, null) : fs.Append(p);
                AppendTestUtil.Write(@out, len1, len2);
                @out.Close();
            }
            //d. Reopen file and read two blocks worth of data.
            AppendTestUtil.Check(fs, p, len1 + len2);
        }
Beispiel #19
0
        /// <summary>
        /// Reads installation ID from file.
        /// </summary>
        /// <param name="installation"><see cref="File"/> to read ID from</param>
        /// <returns>Application installation ID</returns>
        private static string ReadInstallationFile(File installation)
        {
            var f     = new RandomAccessFile(installation, "r");
            var bytes = new byte[(int)f.Length()];

            try
            {
                f.ReadFully(bytes);
            }
            finally
            {
                f.Close();
            }
            return(Encoding.UTF8.GetString(bytes));
        }
Beispiel #20
0
        private void appendFileData(SaveData data)
        {
            RandomAccessFile randomAccessFile;

            try
            {
                randomAccessFile = new RandomAccessFile(lastSaveFile.AbsolutePath, "rw");
                long length = randomAccessFile.Length();

                randomAccessFile.Seek(length);
                randomAccessFile.Write(data.NeedParseData, data.OffSet, data.PlayoffSize);
            }
            catch (Exception)
            {
            }
        }
        public override byte[] GetServerBlob()
        {
#if SILVERLIGHT
            if (this.UseElevatedTrust)
            {
                string     filePath     = siaqodb.GetDBPath() + System.IO.Path.DirectorySeparatorChar + "anchor_" + CacheController.ControllerBehavior.ScopeName + ".anc";
                FileStream phisicalFile = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                byte[]     fullFile     = new byte[phisicalFile.Length];
                phisicalFile.Read(fullFile, 0, fullFile.Length);
                phisicalFile.Close();
                return(fullFile);
            }
            else
            {
                IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

                IsolatedStorageFileStream phisicalFile = new IsolatedStorageFileStream(siaqodb.GetDBPath() + System.IO.Path.DirectorySeparatorChar + "anchor_" + CacheController.ControllerBehavior.ScopeName + ".anc", FileMode.OpenOrCreate, FileAccess.ReadWrite, isf);
                byte[] fullFile = new byte[phisicalFile.Length];

                phisicalFile.Read(fullFile, 0, fullFile.Length);
                phisicalFile.Close();
                return(fullFile);
            }
#elif MONODROID
            string           filePath     = siaqodb.GetDBPath() + System.IO.Path.DirectorySeparatorChar + "anchor_" + CacheController.ControllerBehavior.ScopeName + ".anc";
            RandomAccessFile phisicalFile = new RandomAccessFile(filePath, "rw");
            byte[]           fullFile     = new byte[phisicalFile.Length()];
            phisicalFile.Read(fullFile, 0, fullFile.Length);
            phisicalFile.Close();
            return(fullFile);
#else
            string     filePath     = siaqodb.GetDBPath() + System.IO.Path.DirectorySeparatorChar + "anchor_" + CacheController.ControllerBehavior.ScopeName + ".anc";
            FileStream phisicalFile = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            byte[]     fullFile     = new byte[phisicalFile.Length];
            phisicalFile.Read(fullFile, 0, fullFile.Length);
            phisicalFile.Close();
            return(fullFile);
#endif
        }
Beispiel #22
0
        public static void Main(string[] args)
        {
            try
            {
                RandomAccessFile inToc  = new RandomAccessFile("tmp/umdbuffer.toc", "r");
                RandomAccessFile inIso  = new RandomAccessFile("tmp/umdbuffer.iso", "r");
                RandomAccessFile outIso = new RandomAccessFile("tmp/umd.iso", "rw");

                int numSectors = inToc.readInt();
                Console.WriteLine(string.Format("numSectors={0:D}", numSectors));
                sbyte[] buffer = new sbyte[sectorLength];
                for (int i = 4; i < inToc.Length(); i += 8)
                {
                    int sectorNumber         = inToc.readInt();
                    int bufferedSectorNumber = inToc.readInt();
                    inIso.seek(bufferedSectorNumber * (long)sectorLength);
                    inIso.readFully(buffer);

                    outIso.seek(sectorNumber * (long)sectorLength);
                    outIso.write(buffer);
                }
                inToc.close();
                inIso.close();
                outIso.close();
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
        }
 //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
 //ORIGINAL LINE: public FileMediaDataSource(java.io.File file) throws java.io.IOException
 public FileMediaDataSource(File file)
 {
     mFile     = new RandomAccessFile(file, "r");
     mFileSize = mFile.Length();
 }
Beispiel #24
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestCustomShuffleTransfer()
        {
            FilePath absLogDir = new FilePath("target", typeof(TestFadvisedFileRegion).Name +
                                              "LocDir").GetAbsoluteFile();
            string testDirPath = StringUtils.Join(Path.Separator, new string[] { absLogDir.GetAbsolutePath
                                                                                     (), "testCustomShuffleTransfer" });
            FilePath testDir = new FilePath(testDirPath);

            testDir.Mkdirs();
            System.Console.Out.WriteLine(testDir.GetAbsolutePath());
            FilePath inFile  = new FilePath(testDir, "fileIn.out");
            FilePath outFile = new FilePath(testDir, "fileOut.out");

            //Initialize input file
            byte[] initBuff = new byte[FileSize];
            Random rand     = new Random();

            rand.NextBytes(initBuff);
            FileOutputStream @out = new FileOutputStream(inFile);

            try
            {
                @out.Write(initBuff);
            }
            finally
            {
                IOUtils.Cleanup(Log, @out);
            }
            //define position and count to read from a file region.
            int position = 2 * 1024 * 1024;
            int count    = 4 * 1024 * 1024 - 1;
            RandomAccessFile    inputFile  = null;
            RandomAccessFile    targetFile = null;
            WritableByteChannel target     = null;
            FadvisedFileRegion  fileRegion = null;

            try
            {
                inputFile  = new RandomAccessFile(inFile.GetAbsolutePath(), "r");
                targetFile = new RandomAccessFile(outFile.GetAbsolutePath(), "rw");
                target     = targetFile.GetChannel();
                NUnit.Framework.Assert.AreEqual(FileSize, inputFile.Length());
                //create FadvisedFileRegion
                fileRegion = new FadvisedFileRegion(inputFile, position, count, false, 0, null, null
                                                    , 1024, false);
                //test corner cases
                CustomShuffleTransferCornerCases(fileRegion, target, count);
                long pos = 0;
                long size;
                while ((size = fileRegion.CustomShuffleTransfer(target, pos)) > 0)
                {
                    pos += size;
                }
                //assert size
                NUnit.Framework.Assert.AreEqual(count, (int)pos);
                NUnit.Framework.Assert.AreEqual(count, targetFile.Length());
            }
            finally
            {
                if (fileRegion != null)
                {
                    fileRegion.ReleaseExternalResources();
                }
                IOUtils.Cleanup(Log, target);
                IOUtils.Cleanup(Log, targetFile);
                IOUtils.Cleanup(Log, inputFile);
            }
            //Read the target file and verify that copy is done correctly
            byte[]          buff = new byte[FileSize];
            FileInputStream @in  = new FileInputStream(outFile);

            try
            {
                int total = @in.Read(buff, 0, count);
                NUnit.Framework.Assert.AreEqual(count, total);
                for (int i = 0; i < count; i++)
                {
                    NUnit.Framework.Assert.AreEqual(initBuff[position + i], buff[i]);
                }
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
            }
            //delete files and folders
            inFile.Delete();
            outFile.Delete();
            testDir.Delete();
            absLogDir.Delete();
        }
Beispiel #25
0
        public PBPFileSectorDevice(RandomAccessFile fileAccess) : base(fileAccess)
        {
            try
            {
                int magic   = endianSwap32(fileAccess.readInt());
                int version = endianSwap32(fileAccess.readInt());
                offsetParamSFO = endianSwap32(fileAccess.readInt());
                offsetIcon0    = endianSwap32(fileAccess.readInt());
                offsetIcon1    = endianSwap32(fileAccess.readInt());
                offsetPic0     = endianSwap32(fileAccess.readInt());
                offsetPic1     = endianSwap32(fileAccess.readInt());
                offsetSnd0     = endianSwap32(fileAccess.readInt());
                offsetPspData  = endianSwap32(fileAccess.readInt());
                offsetPsarData = endianSwap32(fileAccess.readInt());
                if (magic != 0x50425000)
                {
                    throw new IOException(string.Format("Invalid PBP header 0x{0:X8}", magic));
                }
                if (version != 0x00010000 && version != 0x00000100 && version != 0x00010001)
                {
                    throw new IOException(string.Format("Invalid PBP version 0x{0:X8}", version));
                }
                fileAccess.seek(offsetPsarData);
                sbyte[] header   = new sbyte[256];
                int     readSize = fileAccess.read(header);
                if (readSize != header.Length)
                {
                    int psarDataLength = (int)(fileAccess.Length() - offsetPsarData);
                    if (psarDataLength != 0 && psarDataLength != 16)
                    {
                        throw new IOException(string.Format("Invalid PBP header"));
                    }
                }
                else if (header[0] == (sbyte)'N' && header[1] == (sbyte)'P' && header[2] == (sbyte)'U' && header[3] == (sbyte)'M' && header[4] == (sbyte)'D' && header[5] == (sbyte)'I' && header[6] == (sbyte)'M' && header[7] == (sbyte)'G')
                {
                    CryptoEngine cryptoEngine = new CryptoEngine();
                    amctrl = cryptoEngine.AMCTRLEngine;

                    AMCTRL.BBMac_Ctx    macContext    = new AMCTRL.BBMac_Ctx();
                    AMCTRL.BBCipher_Ctx cipherContext = new AMCTRL.BBCipher_Ctx();

                    // getKey
                    amctrl.hleDrmBBMacInit(macContext, 3);
                    amctrl.hleDrmBBMacUpdate(macContext, header, 0xC0);
                    sbyte[] macKeyC0 = new sbyte[16];
                    Array.Copy(header, 0xC0, macKeyC0, 0, macKeyC0.Length);
                    vkey = amctrl.GetKeyFromBBMac(macContext, macKeyC0);

                    // decrypt NP header
                    sbyte[] cipherData = new sbyte[0x60];
                    Array.Copy(header, 0x40, cipherData, 0, cipherData.Length);
                    Array.Copy(header, 0xA0, hkey, 0, hkey.Length);
                    amctrl.hleDrmBBCipherInit(cipherContext, 1, 2, hkey, vkey);
                    amctrl.hleDrmBBCipherUpdate(cipherContext, cipherData, cipherData.Length);
                    amctrl.hleDrmBBCipherFinal(cipherContext);

                    int lbaStart = Utilities.readUnaligned32(cipherData, 0x14);
                    int lbaEnd   = Utilities.readUnaligned32(cipherData, 0x24);
                    numSectors = lbaEnd + 1;
                    lbaSize    = numSectors - lbaStart;
                    blockLBAs  = Utilities.readUnaligned32(header, 0x0C);
                    blockSize  = blockLBAs * ISectorDevice_Fields.sectorLength;
                    numBlocks  = (lbaSize + blockLBAs - 1) / blockLBAs;

                    blockBuffer = new sbyte[blockSize];
                    tempBuffer  = new sbyte[blockSize];

                    table = new TableInfo[numBlocks];

                    int tableOffset = Utilities.readUnaligned32(cipherData, 0x2C);
                    fileAccess.seek(offsetPsarData + tableOffset);
                    sbyte[] tableBytes = new sbyte[numBlocks * 32];
                    readSize = fileAccess.read(tableBytes);
                    if (readSize != tableBytes.Length)
                    {
                        Console.WriteLine(string.Format("Could not read table with size {0:D} (readSize={1:D})", tableBytes.Length, readSize));
                    }

                    IntBuffer tableInts = ByteBuffer.wrap(tableBytes).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
                    for (int i = 0; i < numBlocks; i++)
                    {
                        int p0 = tableInts.get();
                        int p1 = tableInts.get();
                        int p2 = tableInts.get();
                        int p3 = tableInts.get();
                        int p4 = tableInts.get();
                        int p5 = tableInts.get();
                        int p6 = tableInts.get();
                        int p7 = tableInts.get();
                        int k0 = p0 ^ p1;
                        int k1 = p1 ^ p2;
                        int k2 = p0 ^ p3;
                        int k3 = p2 ^ p3;

                        TableInfo tableInfo = new TableInfo();
                        Array.Copy(tableBytes, i * 32, tableInfo.mac, 0, tableInfo.mac.Length);
                        tableInfo.offset  = p4 ^ k3;
                        tableInfo.size    = p5 ^ k1;
                        tableInfo.flags   = p6 ^ k2;
                        tableInfo.unknown = p7 ^ k0;
                        table[i]          = tableInfo;
                    }

                    currentBlock = -1;
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("Reading PBP", e);
            }
        }
Beispiel #26
0
        /// <summary>
        /// Open a random access file for the given path, more and access.
        /// </summary>
        private static RandomAccessFile Open(string path, FileMode mode, FileAccess access)
        {
            var file = new Java.Io.File(path);
            switch (mode)
            {
                case FileMode.CreateNew:
                    if (file.Exists())
                        throw new IOException("File already exists");
                    break;
                case FileMode.Open:
                    if (!file.Exists())
                        throw new FileNotFoundException(path);
                    break;
                case FileMode.Append:
                    access = FileAccess.Write;
                    break;
            }

            switch (mode)
            {
                case FileMode.Create:
                case FileMode.CreateNew:
                case FileMode.OpenOrCreate:
                    if (access == FileAccess.Read)
                    {
                        //create empty file, so it can be opened again with read only right, 
                        //otherwise an FilNotFoundException is thrown.
                        var additinalAccessFile = new RandomAccessFile(file, "rw");
                    }
                    break;
            }

            var jMode = (access == FileAccess.Read) ? "r" : "rw";
            var randomAccessFile = new RandomAccessFile(file, jMode);
            switch (mode)
            {
                case FileMode.Truncate:
                    randomAccessFile.SetLength(0);
                    break;
                case FileMode.Append:
                    randomAccessFile.Seek(randomAccessFile.Length());
                    break;
            }

            return randomAccessFile;
        }