Esempio n. 1
0
        /// <summary>
        /// Creates a PlexFAT instance inside an empty stream.
        /// </summary>
        /// <param name="fobj">The stream to store the filesystem in.</param>
        /// <returns>The resulting PlexFAT image.</returns>
        public static PlexFAT New(Stream fobj)
        {
            var ret = new PlexFAT();

            using (var write = Helpers.MakeBW(fobj))
            {
                ret.fobj   = fobj;
                ret.theFAT = new ushort[65536];

                // FAT itself
                for (int i = 0; i < 16; i++)
                {
                    write.Write(RESERVED);
                    ret.theFAT[i] = RESERVED;
                }

                // root directory FAT entry
                write.Write(END);
                ret.theFAT[16] = END;

                ret.Root = new Directory()
                {
                    vol         = ret,
                    entries     = new Dictionary <string, Tuple <ushort, int> >(),
                    firstSector = 16
                };

                // zero out rest of FAT and root dir
                write.Write(new byte[139230]);
            }
            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a PlexFAT instance from a stream.
        /// </summary>
        /// <param name="fobj">The stream to create the instance from.</param>
        /// <returns>The resulting PlexFAT image.</returns>
        public static PlexFAT FromStream(Stream fobj)
        {
            var ret = new PlexFAT();

            using (var read = Helpers.MakeBR(fobj))
            {
                // The FAT is an array of 65,536 ushorts. You don't get
                // much flexibility in read types in C# so I read bytes
                // and BlockCopy them to the array.
                ret.fobj   = fobj;
                ret.theFAT = new ushort[65536];
                Buffer.BlockCopy(read.ReadBytes(131072), 0, ret.theFAT, 0, 131072);
            }
            ret.Root = Directory.FromVol(ret);
            return(ret);
        }
Esempio n. 3
0
            /// <summary>
            /// Create a directory from a PlexFAT volume.
            /// </summary>
            /// <param name="vol">The PlexFAT volume from which the directory will be created.</param>
            /// <returns>The resulting directory.</returns>
            public static Directory FromVol(PlexFAT vol)
            {
                var ret = new Directory();

                ret.vol     = vol;
                ret.entries = new Dictionary <string, Tuple <ushort, int> >();
                ushort curSector = (ushort)(vol.fobj.Position / 8192);

                ret.firstSector = curSector;
                using (var read = Helpers.MakeBR(vol.fobj))
                {
                    int i = 0;
                    while (true)
                    {
                        int    entpos   = (int)vol.fobj.Position;
                        byte[] fnameRaw = read.ReadBytes(250);
                        int    fnameLength;
                        for (fnameLength = 0; fnameLength < 250 && fnameRaw[fnameLength] != 0; fnameLength++)
                        {
                        }
                        string fname       = Encoding.UTF8.GetString(fnameRaw, 0, fnameLength);
                        ushort firstSector = read.ReadUInt16();
                        if (fnameLength != 0)
                        {
                            ret.entries.Add(fname, Tuple.Create(firstSector, entpos));
                        }
                        vol.fobj.Position += 4; // don't read length here
                        i++;
                        if (i == 32)            // 32 entries fit in one sector
                        {
                            if (vol.theFAT[curSector] == END)
                            {
                                break;
                            }
                            i         = 0;
                            curSector = vol.theFAT[curSector];
                        }
                    }
                }
                return(ret);
            }
Esempio n. 4
0
 public LocalSubstream(PlexFAT vol, ushort firstSector, int lenpos)
 {
     using (var read = Helpers.MakeBR(vol.fobj))
     {
         this.vol          = vol;
         this.lenpos       = lenpos;
         this.possec       = 0;
         this.isecpos      = 0;
         vol.fobj.Position = lenpos;
         this.flen         = read.ReadInt32();
         this.sectorMap    = new List <ushort>();
         ushort sec = firstSector;
         while (sec != END)
         {
             if (sec == RESERVED)
             {
                 throw new InvalidOperationException("Reached a reserved block.");
             }
             this.sectorMap.Add(sec);
             sec = vol.theFAT[sec];
         }
     }
 }