Exemplo n.º 1
0
        public override int Read(byte[] buffer, int offset, int length)
        {
            // Todo: count already read bytes from previous Read calls and use it to compute remaining expected length
            int maxLength    = (int)Math.Min(length, ContentLength ?? length);
            int actualLength = RawStream.Read(buffer, offset, maxLength);

            return(actualLength);
        }
Exemplo n.º 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (m_Compressed)
            {
                long pos = m_Uncomp.Position;
                m_Uncomp.Seek(0, SeekOrigin.End);

                while (pos + count > m_Uncomp.Length && RawStream.Position + 8 < RawStream.Length)
                {
                    int block = Raw.ReadInt32();
                    int ucLen = Raw.ReadInt32();

                    if (block > 0x10000000 || block <= 0 || ucLen > 0x10000000 || ucLen <= 0)
                    {
                        break;
                    }

                    if (RawStream.Position + block > RawStream.Length)
                    {
                        break;
                    }

                    if (m_ReadBuff == null || m_ReadBuff.Length < block)
                    {
                        m_ReadBuff = new byte[block];
                    }

                    if (m_CompBuff == null || m_CompBuff.Length < ucLen)
                    {
                        m_CompBuff = new byte[ucLen];
                    }
                    else
                    {
                        ucLen = m_CompBuff.Length;
                    }

                    Raw.Read(m_ReadBuff, 0, block);

                    ZLibError error = ZLib.uncompress(m_CompBuff, ref ucLen, m_ReadBuff, block);
                    if (error != ZLibError.Z_OK)
                    {
                        throw new Exception("ZLib error uncompressing: " + error.ToString());
                    }

                    m_Uncomp.Write(m_CompBuff, 0, ucLen);
                }

                m_Uncomp.Position = pos;
                return(m_Uncomp.Read(buffer, offset, count));
            }
            else
            {
                return(RawStream.Read(buffer, offset, count));
            }
        }
Exemplo n.º 3
0
        internal NodeHeader ReadTree(ulong logical, byte level)
        {
            var physical = MapToPhysical(logical);

            RawStream.Seek((long)physical, SeekOrigin.Begin);
            var dataSize = level > 0 ? SuperBlock.NodeSize : SuperBlock.LeafSize;
            var buffer   = new byte[dataSize];

            RawStream.Read(buffer, 0, buffer.Length);
            var result = NodeHeader.Create(buffer, 0);

            VerifyChecksum(result.Checksum, buffer, 0x20, (int)dataSize - 0x20);
            return(result);
        }
Exemplo n.º 4
0
        public override int Read(byte[] buffer, int offset, int length)
        {
            if (EndOfDataReached)
            {
                return(0);
            }

            int totalRead = 0;

            while (totalRead < length)
            {
                if (CurrentChunkRemaining == 0)
                {
                    if (CurrentChunkLength > 0)
                    {
                        // Read the empty line that follows the previous chunk
                        HttpLineReader.ReadLine(RawStream);
                    }

                    NextChunk();

                    // Chunk length zero means end of data.
                    if (CurrentChunkLength == 0)
                    {
                        Close();
                        EndOfDataReached = true;
                        break;
                    }
                }

                int maxLength    = (int)Math.Min(length - totalRead, CurrentChunkRemaining);
                int actualLength = RawStream.Read(buffer, offset, maxLength);
                totalRead += actualLength;
                offset    += actualLength;

                CurrentChunkRemaining -= actualLength;

                if (actualLength == 0)
                {
                    break;
                }
            }

            return(totalRead);
        }
Exemplo n.º 5
0
        public override int Read(byte[] buffer, int offset, int length)
        {
            if (RemainingContentLength != null && RemainingContentLength <= 0)
            {
                Close();
                return(0);
            }

            int maxLength    = (int)Math.Min(length, RemainingContentLength ?? length);
            int actualLength = RawStream.Read(buffer, offset, maxLength);

            if (RemainingContentLength != null)
            {
                RemainingContentLength -= actualLength;
            }

            if (actualLength == 0)
            {
                Close();
            }

            return(actualLength);
        }
Exemplo n.º 6
0
        public JHead(DcRawState state, RawStream s, bool info_only, uint dng_version)
        {
            byte[] data = new byte[0x10000];

            restart = int.MaxValue;

            s.Read(data, 0, 2);

            if (data[1] != 0xd8)
            {
                // Error state (I think)
                throw new Exception("unexpected value in jpeg header");
                //return 0;
            }

            int tag;
            int len;

            do
            {
                s.Read(data, 0, 2 * 2);
                tag = data[0] << 8 | data[1];
                len = (data[2] << 8 | data[3]) - 2;
                if (tag <= 0xff00)
                {
                    // Non error
                    return;
                }

                s.Read(data, 0, len);
                switch (tag)
                {
                case 0xffc0:        // SOF0 - Start of Frame 0 - Baseline DCT
                case 0xffc3:        // SOF3 - Start of Frame 3 - Lossless (sequential)
                    if (tag == 0xffc3)
                    {
                        sraw = ((((data[7] >> 4) * (data[7] & 15) - 1) & 3) == 0) ? false : true;
                    }
                    bits = data[0];
                    high = data[1] << 8 | data[2];
                    wide = data[3] << 8 | data[4];
                    clrs = data[5] - (sraw ? 1 : 0);
                    if (len == 9 && dng_version == 0)
                    {
                        s.ReadByte();
                    }
                    break;

                case 0xffc4:        // DHT - Define Huffman Table
                    if (info_only)
                    {
                        break;
                    }

                    for (int dpi = 0; dpi < len && data[dpi] < 4;)
                    {
                        int idx = data[dpi];
                        dpi++;
                        huff[idx] = new HuffmanTree(data, ref dpi);
                    }
                    break;

                case 0xffda:        // SOS - Start of Scan
                    psv   = data[1 + data[0] * 2];
                    bits -= data[3 + data[0] * 2] & 15;
                    break;

                case 0xffdd:        // DRI - Define Restart Interval
                    restart = data[0] << 8 | data[1];
                    break;

                // <-- end of dcraw.c ported code (for this switch statement) -->

                // thumbnail image
                // for more unhandled tags, see: http://www.impulseadventure.com/photo/jpeg-decoder.html
                case 0xffd8:        // SOI - Start of Image
                case 0xffd9:        // EOI - End of Image
                case 0xffdb:        // DQT - Define Quantization Table
                    break;

                default:
                    Console.WriteLine("Unhandled jpeg tag: {0}", tag);
                    break;
                }
            } while (tag != 0xffda);

            if (info_only)
            {
                // No error
                //return 1;
                return;
            }

            if (sraw)
            {
                huff[3] = huff[2] = huff[1];
                huff[1] = huff[0];
            }

            row = new ushort[wide * clrs * 2];

            /*
             * for (int iii = 0; iii < huff.Length; iii++)
             * {
             *  Console.WriteLine("huff[{0}]", iii);
             *  Decode.DumpTable(huff[iii], 0);
             * }
             */

            //row = (ushort*) calloc(wide * clrs, 4);
            //merror(jh->row, "ljpeg_start()");
            // TODO: why do we need error handling here?
            s.ZeroAfterFF = true;
        }