Beispiel #1
0
        public byte[] GetEntryBytes(LhaEntry entry)
        {
            byte[] buffer = new byte[entry.GetOriginalSize()];
            LhaDecoderInputStream inputStream = GetInputStream(entry);

            inputStream.Read(buffer, 0, buffer.Length);
            return(buffer);
        }
 /**
  * Creates a new lha input stream.
  *
  * @param in
  *            the actual input stream
  * @param encoding
  *            character encoding name
  */
 public LhaInputStream(BinaryReader @in, Encoding encoding)
     : base(@in.BaseStream, encoding)
 {
     this.encoding = encoding;
     this.dis      = null;
     this.entry    = null;
     this.closed   = true;
 }
Beispiel #3
0
 /**
  * Creates a new lha input stream.
  *
  * @param in
  *            the actual input stream
  * @param entry
  *            the lha entry of current input stream
  */
 public LhaDecoderInputStream(BinaryReader @in, LhaEntry entry)
     : base(@in.BaseStream)
 {
     this.entry      = entry;
     this.crc        = new CRC16();
     this.skipBuffer = new byte[512];
     this.entryCount = entry.GetOriginalSize();
     this.decoder    = CreateDecoder(@in, entryCount, entry.GetMethod());
     crc.Reset();
 }
Beispiel #4
0
 /**
  * Closes the input stream.
  *
  * @throws IOException
  *             if an I/O error has occured
  */
 public override void Close()
 {
     if (decoder != null)
     {
         decoder.Close();
         decoder = null;
         entry   = null;
         crc     = null;
     }
 }
Beispiel #5
0
            public LhaFileInputStream(LhaFile file, LhaEntry entry)
                : base(file.binaryReader.BaseStream, file.encoding)
            {
                if (file == null || entry == null)
                {
                    throw new NullReferenceException();
                }
                this.file  = file;
                this.pos   = entry.GetOffset();
                this.count = entry.GetCompressedSize();

                file.binaryReader.BaseStream.Position = this.pos;
            }
        protected internal virtual LhaEntry ReadHeader_Lv2(byte[] @base)
        {
            LhaEntry e = new LhaEntry();

            e.SetMethod(encoding.GetString(@base, HDR2_OFF_METHOD, 5));
            e.SetCompressedSize(Get32(@base, HDR2_OFF_COMPSIZE));
            e.SetOriginalSize(Get32(@base, HDR2_OFF_ORIGSIZE));
            e.SetHeaderTimeStamp(Get32(@base, HDR2_OFF_TIMESTAMP));

            calcCRC.Update(@base, 0, @base.Length);

            byte[] buf = new byte[2];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read crc value)"));
            }
            e.SetCRC(Get16(buf, 0));
            calcCRC.Update(buf, 0, buf.Length);

            buf = new byte[1];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read os signature)"));
            }
            e.SetOS(buf[0]);
            calcCRC.Update(buf, 0, buf.Length);

            buf = new byte[2];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read ext)"));
            }
            calcCRC.Update(buf, 0, buf.Length);
            for (int next = Get16(buf, 0); next > 0; next = ReadExHeader(e, next))
            {
                ;
            }

            e.SetPath(dirName + fileName);

            return(e);
        }
        /**
         * Reads the next lha entry and positions stream at the beginning of the
         * entry data.
         *
         * @param name
         *            the name of the entry
         * @return the LzhEntry just read
         * @throws LhaException
         *             if a lha format error has occurred
         * @throws IOException
         *             if an I/O error has occured
         */
        public virtual LhaEntry GetNextEntry()
        {
            if (entry != null)
            {
                dis.CloseEntry();
            }
            LhaEntryReader lhaEntryReader = new LhaEntryReader(this, encoding);

            entry = lhaEntryReader.ReadHeader();

            if (entry != null)
            {
                dis    = new LhaDecoderInputStream(this, entry);
                closed = false;
            }
            else
            {
                dis    = null;
                closed = true;
            }
            return(entry);
        }
Beispiel #8
0
        /**
         * Make entry map in lha file.
         *
         * @throws LhaException
         *             if a lha format error has occurred
         * @throws IOException
         *             if an I/O error has occured
         */
        private void MakeEntryMap()
        {
            LhaEntryReader lhaEntryReader = new LhaEntryReader(binaryReader, encoding);

            this.size = 0;

            while (true)
            {
                LhaEntry e = lhaEntryReader.ReadHeader();

                if (e == null)
                {
                    break;
                }

                e.SetOffset(binaryReader.BaseStream.Position);
                entryList.Add(e);
                entryMap.Add(e.GetPath(), e);
                ++size;

                int skipSize = (int)e.GetCompressedSize();
                binaryReader.BaseStream.Position += skipSize;
            }
        }
Beispiel #9
0
 /**
  * Returns an input stream for reading the contents of the specified lha
  * file entry.
  *
  * @param entry
  *            the lha file entry
  * @return the input stream for reading the contents of the specified lha
  *         file entry
  * @throws IOException
  *             if an I/O error has occurred
  */
 public virtual LhaDecoderInputStream GetInputStream(LhaEntry entry)
 {
     return(new LhaDecoderInputStream(new LhaFileInputStream(this, entry), entry));
 }
        protected internal virtual int ReadExHeader(LhaEntry e, int size)
        {
            byte[] buf = new byte[size];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("header is broken"));
            }

            switch (buf[0])
            {
            case LhaEntry.EXHDR_SIG_COMMON:
                flagCRC = true;
                srcCRC  = Get16(buf, 1);
                buf[1]  = 0x00;
                buf[2]  = 0x00;
                break;

            case LhaEntry.EXHDR_SIG_FILENAME:
                fileName = encoding.GetString(buf, 1, size - 3);
                break;

            case LhaEntry.EXHDR_SIG_DIRNAME:
                StringBuilder dname = new StringBuilder();
                int           pi    = 0;
                for (int i = 1; i <= (size - 3); ++i)
                {
                    if (buf[i] == HD_CHR_DELIM_EXTRA)
                    {
                        dname.Append(encoding.GetString(buf, pi + 1, i - pi - 1));
                        dname.Append(Path.DirectorySeparatorChar);

                        pi = i;
                    }
                }
                dname.Append(encoding.GetString(buf, pi + 1, size - 3 - pi));
                dirName = dname.ToString();
                break;

            case LhaEntry.EXHDR_SIG_COMMENT:
                break;

            case LhaEntry.EXHDR_SIG_DOSATTR:
                break;

            case LhaEntry.EXHDR_SIG_DOSTIMES:
                break;

            case LhaEntry.EXHDR_SIG_UNIXPERM:
                break;

            case LhaEntry.EXHDR_SIG_UNIXID:
                break;

            case LhaEntry.EXHDR_SIG_UNIXGROUPNAME:
                break;

            case LhaEntry.EXHDR_SIG_UNIXUSERNAME:
                break;

            case LhaEntry.EXHDR_SIG_UNIXLMTIME:
                break;

            default:
                break;
            }

            calcCRC.Update(buf, 0, buf.Length);

            return(Get16(buf, size - 2));
        }
        protected internal virtual LhaEntry ReadHeader_Lv1(byte[] @base)
        {
            LhaEntry e = new LhaEntry();

            flagSum = true;

            srcSum = @base[HDR1_OFF_SUM];
            if (srcSum < 0)
            {
                srcSum += 256;
            }
            e.SetMethod(encoding.GetString(@base, HDR1_OFF_METHOD, 5));
            e.SetOriginalSize(Get32(@base, HDR1_OFF_ORIGSIZE));
            e.SetDosTimeStamp(Get32(@base, HDR1_OFF_TIMESTAMP));

            if (@base[HDR1_OFF_19] != 0x20)
            {
                throw (new LhaException("Lha header is broken (offset 19 is not 0x20)"));
            }

            calcSum.Update(@base, 2, @base.Length - 2);
            calcCRC.Update(@base, 0, @base.Length);

            byte[] buf = new byte[1];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read name size)"));
            }
            int nameSize = buf[0];

            calcSum.Update(buf, 0, buf.Length);
            calcCRC.Update(buf, 0, buf.Length);

            string name = "";

            if (nameSize > 0)
            {
                buf = new byte[nameSize];
                if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
                {
                    throw (new LhaException("Lha header is broken (cannot read name)"));
                }
                name = encoding.GetString(buf);
                calcSum.Update(buf, 0, buf.Length);
                calcCRC.Update(buf, 0, buf.Length);
            }

            buf = new byte[2];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read crc value)"));
            }
            e.SetCRC(Get16(buf, 0));
            calcSum.Update(buf, 0, buf.Length);
            calcCRC.Update(buf, 0, buf.Length);

            buf = new byte[1];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read os signature)"));
            }
            e.SetOS(buf[0]);
            calcSum.Update(buf, 0, buf.Length);
            calcCRC.Update(buf, 0, buf.Length);

            long extSize = 0;

            buf = new byte[2];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read ext)"));
            }
            calcSum.Update(buf, 0, buf.Length);
            calcCRC.Update(buf, 0, buf.Length);
            for (int next = Get16(buf, 0); next > 0; next = ReadExHeader(e, next))
            {
                extSize += next;
            }

            e.SetCompressedSize(Get32(@base, HDR0_OFF_COMPSIZE) - extSize);
            if (fileName.Length > 0)
            {
                name = dirName + fileName;
            }
            else
            {
                name = ConvertFilePath(name, e.GetOS());
            }

            e.SetPath(name);

            return(e);
        }
        protected internal virtual LhaEntry ReadHeader_Lv0(byte[] @base)
        {
            LhaEntry e = new LhaEntry();

            flagSum = true;

            int headerSize = @base[HDRU_OFF_HEADERSIZE];

            srcSum = @base[HDR0_OFF_SUM];
            if (srcSum < 0)
            {
                srcSum += 256;
            }
            e.SetMethod(encoding.GetString(@base, HDR0_OFF_METHOD, 5));
            e.SetCompressedSize(Get32(@base, HDR0_OFF_COMPSIZE));
            e.SetOriginalSize(Get32(@base, HDR0_OFF_ORIGSIZE));
            e.SetDosTimeStamp(Get32(@base, HDR0_OFF_TIMESTAMP));

            calcSum.Update(@base, 2, @base.Length - 2);

            byte[] buf = new byte[1];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (header size does'nt match)"));
            }
            int nameSize = buf[0];

            calcSum.Update(buf, 0, buf.Length);

            buf = new byte[nameSize];
            if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
            {
                throw (new LhaException("Lha header is broken (cannot read name)"));
            }
            string name = encoding.GetString(buf);

            calcSum.Update(buf, 0, buf.Length);

            int diff = headerSize - nameSize;

            if ((diff != 20) && (diff != 22) && (diff < 23))
            {
                throw (new LhaException("Lha header is broken (header size does'nt match)"));
            }

            e.SetOS(LhaEntry.OSID_SIG_GENERIC);

            if (diff >= 22)
            {
                buf = new byte[2];
                if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
                {
                    throw (new LhaException("Lha header is broken (cannot read crc value)"));
                }
                e.SetCRC(Get16(buf, 0));
                calcSum.Update(buf, 0, buf.Length);
            }

            if (diff >= 23)
            {
                buf = new byte[1];
                if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
                {
                    throw (new LhaException("Lha header is broken (cannot read os signature)"));
                }
                e.SetOS(buf[0]);
                calcSum.Update(buf, 0, buf.Length);
            }

            if (diff > 23)
            {
                buf = new byte[diff - 24];
                if (binaryReader.Read(buf, 0, buf.Length) != buf.Length)
                {
                    throw (new LhaException("Lha header is broken (cannot read ext)"));
                }
                calcSum.Update(buf, 0, buf.Length);
            }

            e.SetPath(ConvertFilePath(name, e.GetOS()));

            return(e);
        }