/// <summary>
        /// Decodes the specified data.
        /// </summary>
        public override byte[] Decode(byte[] data, FilterParms parms)
        {
            MemoryStream msInput  = new MemoryStream(data);
            MemoryStream msOutput = new MemoryStream();

            InflaterInputStream iis = new InflaterInputStream(msInput, new Inflater(false));
            int cbRead;

            byte[] abResult = new byte[32768];
            do
            {
                cbRead = iis.Read(abResult, 0, abResult.Length);
                if (cbRead > 0)
                {
                    msOutput.Write(abResult, 0, cbRead);
                }
            }while (cbRead > 0);
            iis.Close();
            msOutput.Flush();
            if (msOutput.Length >= 0)
            {
                if (parms.DecodeParms != null)
                {
                    return(StreamDecoder.Decode(msOutput.ToArray(), parms.DecodeParms));
                }
                return(msOutput.ToArray());
            }
            return(null);
        }
Example #2
0
        /// <summary>
        /// Decodes to a raw string.
        /// </summary>
        public virtual string DecodeToString(byte[] data, FilterParms parms)
        {
            byte[] bytes = Decode(data, parms);
            string text  = PdfEncoders.RawEncoding.GetString(bytes, 0, bytes.Length);

            return(text);
        }
Example #3
0
        /// <summary>
        /// Decodes to a raw string with the specified filter.
        /// </summary>
        public static string DecodeToString(byte[] data, string filterName, FilterParms parms)
        {
            Filter filter = GetFilter(filterName);

            if (filter != null)
            {
                return(filter.DecodeToString(data, parms));
            }
            return(null);
        }
        /// <summary>
        /// Decodes the specified data.
        /// </summary>
        public override byte[] Decode(byte[] data, FilterParms parms)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            data = RemoveWhiteSpace(data);
            int count = data.Length;

            // Ignore EOD (end of data) character.
            // EOD can be anywhere in the stream, but makes sense only at the end of the stream.
            if (count > 0 && data[count - 1] == '>')
            {
                --count;
            }
            if (count % 2 == 1)
            {
                count++;
                byte[] temp = data;
                data = new byte[count];
                temp.CopyTo(data, 0);
            }
            count >>= 1;
            byte[] bytes = new byte[count];
            for (int i = 0, j = 0; i < count; i++)
            {
                // Must support 0-9, A-F, a-f - "Any other characters cause an error."
                byte hi = data[j++];
                byte lo = data[j++];
                if (hi >= 'a' && hi <= 'f')
                {
                    hi -= 32;
                }
                if (lo >= 'a' && lo <= 'f')
                {
                    lo -= 32;
                }
                // TODO Throw on invalid characters. Stop when encountering EOD. Add one more byte if EOD is the lo byte.
                bytes[i] = (byte)((hi > '9' ? hi - '7' /*'A' + 10*/: hi - '0') * 16 + (lo > '9' ? lo - '7' /*'A' + 10*/: lo - '0'));
            }
            return(bytes);
        }
Example #5
0
 /// <summary>
 /// When implemented in a derived class decodes the specified data.
 /// </summary>
 public abstract byte[] Decode(byte[] data, FilterParms parms);
Example #6
0
        /// <summary>
        /// Decodes the specified data.
        /// </summary>
        public override byte[] Decode(byte[] data, FilterParms parms)
        {
            MemoryStream msInput  = new MemoryStream(data);
            MemoryStream msOutput = new MemoryStream();

#if NET_ZIP
            // See http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97064
            // It seems to work when skipping the first two bytes.
            byte header;   // 0x30 0x59
            header = (byte)msInput.ReadByte();
            //Debug.Assert(header == 48);
            header = (byte)msInput.ReadByte();
            //Debug.Assert(header == 89);
            DeflateStream zip = new DeflateStream(msInput, CompressionMode.Decompress, true);
            int           cbRead;
            byte[]        abResult = new byte[1024];
            do
            {
                cbRead = zip.Read(abResult, 0, abResult.Length);
                if (cbRead > 0)
                {
                    msOutput.Write(abResult, 0, cbRead);
                }
            }while (cbRead > 0);
            zip.Close();
            msOutput.Flush();
            if (msOutput.Length >= 0)
            {
                msOutput.Capacity = (int)msOutput.Length;
                return(msOutput.GetBuffer());
            }
            return(null);
#else
            InflaterInputStream iis = new InflaterInputStream(msInput, new Inflater(false));
            int    cbRead;
            byte[] abResult = new byte[32768];
            do
            {
                cbRead = iis.Read(abResult, 0, abResult.Length);
                if (cbRead > 0)
                {
                    msOutput.Write(abResult, 0, cbRead);
                }
            }while (cbRead > 0);
#if UWP
            iis.Dispose();
#else
            iis.Close();
#endif
            msOutput.Flush();
            if (msOutput.Length >= 0)
            {
#if NETFX_CORE || UWP || PORTABLE
                return(msOutput.ToArray());
#else
                msOutput.Capacity = (int)msOutput.Length;
                return(msOutput.GetBuffer());
#endif
            }
            return(null);
#endif
        }
        /// <summary>
        /// Decodes the specified data.
        /// </summary>
        public override byte[] Decode(byte[] data, FilterParms parms)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int idx;
            int length = data.Length;
            int zCount = 0;
            int idxOut = 0;

            for (idx = 0; idx < length; idx++)
            {
                char ch = (char)data[idx];
                if (ch >= '!' && ch <= 'u')
                {
                    data[idxOut++] = (byte)ch;
                }
                else if (ch == 'z')
                {
                    data[idxOut++] = (byte)ch;
                    zCount++;
                }
                else if (ch == '~')
                {
                    if ((char)data[idx + 1] != '>')
                    {
                        throw new ArgumentException("Illegal character.", "data");
                    }
                    break;
                }
                // ingnore unknown character
            }
            // Loop not ended with break?
            if (idx == length)
            {
                throw new ArgumentException("Illegal character.", "data");
            }

            length = idxOut;
            int nonZero   = length - zCount;
            int byteCount = 4 * (zCount + (nonZero / 5)); // full 4 byte blocks

            int remainder = nonZero % 5;

            if (remainder == 1)
            {
                throw new InvalidOperationException("Illegal character.");
            }

            if (remainder != 0)
            {
                byteCount += remainder - 1;
            }

            byte[] output = new byte[byteCount];

            idxOut = 0;
            idx    = 0;
            while (idx + 4 < length)
            {
                char ch = (char)data[idx];
                if (ch == 'z')
                {
                    idx++;
                    idxOut += 4;
                }
                else
                {
                    // TODO: check
                    long value =
                        (long)(data[idx++] - '!') * (85 * 85 * 85 * 85) +
                        (uint)(data[idx++] - '!') * (85 * 85 * 85) +
                        (uint)(data[idx++] - '!') * (85 * 85) +
                        (uint)(data[idx++] - '!') * 85 +
                        (uint)(data[idx++] - '!');

                    if (value > UInt32.MaxValue)
                    {
                        throw new InvalidOperationException("Value of group greater than 2 power 32 - 1.");
                    }

                    output[idxOut++] = (byte)(value >> 24);
                    output[idxOut++] = (byte)(value >> 16);
                    output[idxOut++] = (byte)(value >> 8);
                    output[idxOut++] = (byte)value;
                }
            }

            // I have found no appropriate algorithm, so I write my own. In some rare cases the value must not
            // increased by one, but I cannot found a general formula or a proof.
            // All possible cases are tested programmatically.
            if (remainder == 2) // one byte
            {
                uint value =
                    (uint)(data[idx++] - '!') * (85 * 85 * 85 * 85) +
                    (uint)(data[idx] - '!') * (85 * 85 * 85);

                // Always increase if not zero (tried out).
                if (value != 0)
                {
                    value += 0x01000000;
                }

                output[idxOut] = (byte)(value >> 24);
            }
            else if (remainder == 3) // two bytes
            {
                int  idxIn = idx;
                uint value =
                    (uint)(data[idx++] - '!') * (85 * 85 * 85 * 85) +
                    (uint)(data[idx++] - '!') * (85 * 85 * 85) +
                    (uint)(data[idx] - '!') * (85 * 85);

                if (value != 0)
                {
                    value &= 0xFFFF0000;
                    uint val = value / (85 * 85);
                    byte c3  = (byte)(val % 85 + '!');
                    val /= 85;
                    byte c2 = (byte)(val % 85 + '!');
                    val /= 85;
                    byte c1 = (byte)(val + '!');
                    if (c1 != data[idxIn] || c2 != data[idxIn + 1] || c3 != data[idxIn + 2])
                    {
                        value += 0x00010000;
                        //Count2++;
                    }
                }
                output[idxOut++] = (byte)(value >> 24);
                output[idxOut]   = (byte)(value >> 16);
            }
            else if (remainder == 4) // three bytes
            {
                int  idxIn = idx;
                uint value =
                    (uint)(data[idx++] - '!') * (85 * 85 * 85 * 85) +
                    (uint)(data[idx++] - '!') * (85 * 85 * 85) +
                    (uint)(data[idx++] - '!') * (85 * 85) +
                    (uint)(data[idx] - '!') * 85;

                if (value != 0)
                {
                    value &= 0xFFFFFF00;
                    uint val = value / 85;
                    byte c4  = (byte)(val % 85 + '!');
                    val /= 85;
                    byte c3 = (byte)(val % 85 + '!');
                    val /= 85;
                    byte c2 = (byte)(val % 85 + '!');
                    val /= 85;
                    byte c1 = (byte)(val + '!');
                    if (c1 != data[idxIn] || c2 != data[idxIn + 1] || c3 != data[idxIn + 2] || c4 != data[idxIn + 3])
                    {
                        value += 0x00000100;
                        //Count3++;
                    }
                }
                output[idxOut++] = (byte)(value >> 24);
                output[idxOut++] = (byte)(value >> 16);
                output[idxOut]   = (byte)(value >> 8);
            }
            return(output);
        }
Example #8
0
        /// <summary>
        /// Decodes the specified data.
        /// </summary>
        public override byte[] Decode(byte[] data, FilterParms parms)
        {
            if (data[0] == 0x00 && data[1] == 0x01)
            {
                throw new Exception("LZW flavour not supported.");
            }

            MemoryStream outputStream = new MemoryStream();

            InitializeDictionary();

            _data        = data;
            _bytePointer = 0;
            _nextData    = 0;
            _nextBits    = 0;
            int code, oldCode = 0;

            byte[] str;

            while ((code = NextCode) != 257)
            {
                if (code == 256)
                {
                    InitializeDictionary();
                    code = NextCode;
                    if (code == 257)
                    {
                        break;
                    }
                    outputStream.Write(_stringTable[code], 0, _stringTable[code].Length);
                    oldCode = code;
                }
                else
                {
                    if (code < _tableIndex)
                    {
                        str = _stringTable[code];
                        outputStream.Write(str, 0, str.Length);
                        AddEntry(_stringTable[oldCode], str[0]);
                        oldCode = code;
                    }
                    else
                    {
                        str = _stringTable[oldCode];
                        outputStream.Write(str, 0, str.Length);
                        AddEntry(str, str[0]);
                        oldCode = code;
                    }
                }
            }

            if (outputStream.Length >= 0)
            {
#if !NETFX_CORE && !UWP && !PORTABLE
                outputStream.Capacity = (int)outputStream.Length;
                return(outputStream.GetBuffer());
#else
                return(outputStream.ToArray());
#endif
            }
            return(null);
        }
Example #9
0
        /// <summary>
        /// Decodes the specified data.
        /// </summary>
        public override byte[] Decode(byte[] data, FilterParms parms)
        {
            if (data[0] == 0x00 && data[1] == 0x01)
            {
                throw new Exception("LZW flavour not supported.");
            }

            MemoryStream outputStream = new MemoryStream();

            InitializeDictionary();

            _data        = data;
            _bytePointer = 0;
            _nextData    = 0;
            _nextBits    = 0;
            int code, oldCode = 0;

            byte[] str;

            while ((code = NextCode) != 257)
            {
                if (code == 256)
                {
                    InitializeDictionary();
                    code = NextCode;
                    if (code == 257)
                    {
                        break;
                    }
                    outputStream.Write(_stringTable[code], 0, _stringTable[code].Length);
                    oldCode = code;
                }
                else
                {
                    if (code < _tableIndex)
                    {
                        str = _stringTable[code];
                        outputStream.Write(str, 0, str.Length);
                        AddEntry(_stringTable[oldCode], str[0]);
                        oldCode = code;
                    }
                    else
                    {
                        str = _stringTable[oldCode];
                        outputStream.Write(str, 0, str.Length);
                        AddEntry(str, str[0]);
                        oldCode = code;
                    }
                }
            }

            if (outputStream.Length >= 0)
            {
                if (parms.DecodeParms != null)
                {
                    return(StreamDecoder.Decode(outputStream.ToArray(), parms.DecodeParms));
                }
                return(outputStream.ToArray());
            }
            return(null);
        }