예제 #1
0
 /// <summary>A helper to FlateDecode.</summary>
 /// <param name="input">the input data</param>
 /// <param name="strict">
 /// <CODE>true</CODE> to read a correct stream. <CODE>false</CODE>
 /// to try to read a corrupted stream
 /// </param>
 /// <returns>the decoded data</returns>
 public static byte[] FlateDecode(byte[] input, bool strict)
 {
     using (MemoryStream stream = new MemoryStream(input)) {
         using (ZInflaterInputStream zip = new ZInflaterInputStream(stream)) {
             MemoryStream output = new MemoryStream();
             byte[]       b      = new byte[strict ? 4092 : 1];
             try {
                 int n;
                 while ((n = zip.Read(b, 0, b.Length)) > 0)
                 {
                     output.Write(b, 0, n);
                 }
                 zip.Dispose();
                 output.Dispose();
                 return(output.ToArray());
             }
             catch {
                 if (strict)
                 {
                     return(null);
                 }
                 return(output.ToArray());
             }
         }
     }
 }
예제 #2
0
        public static byte[] FlateDecode(byte[] inp, bool strict = true)
        {
            MemoryStream         stream = new MemoryStream(inp);
            ZInflaterInputStream zip    = new ZInflaterInputStream(stream);
            MemoryStream         outp   = new MemoryStream();

            byte[] b = new byte[strict ? 4092 : 1];
            try
            {
                int n;
                while ((n = zip.Read(b, 0, b.Length)) > 0)
                {
                    outp.Write(b, 0, n);
                }
                zip.Close();
                outp.Close();
                return(outp.ToArray());
            }
            catch
            {
                if (strict)
                {
                    return(null);
                }
                return(outp.ToArray());
            }
        }
예제 #3
0
        /// <summary>A helper to flateDecode.</summary>
        /// <param name="in">the input data</param>
        /// <param name="strict">
        ///
        /// <see langword="true"/>
        /// to read a correct stream.
        /// <see langword="false"/>
        /// to try to read a corrupted stream.
        /// </param>
        /// <returns>the decoded data</returns>
        public static byte[] FlateDecode(byte[] @in, bool strict)
        {
            MemoryStream         stream = new MemoryStream(@in);
            ZInflaterInputStream zip    = new ZInflaterInputStream(stream);
            MemoryStream         @out   = new MemoryStream();

            byte[] b = new byte[strict ? 4092 : 1];
            try {
                int n;
                while ((n = zip.Read(b)) >= 0)
                {
                    @out.Write(b, 0, n);
                }
                zip.Dispose();
                @out.Dispose();
                return(@out.ToArray());
            }
            catch (Exception) {
                if (strict)
                {
                    return(null);
                }
                return(@out.ToArray());
            }
        }
예제 #4
0
        /// <summary>A helper to flateDecode.</summary>
        /// <param name="in">the input data</param>
        /// <param name="strict">
        ///
        /// <see langword="true"/>
        /// to read a correct stream.
        /// <see langword="false"/>
        /// to try to read a corrupted stream.
        /// </param>
        /// <param name="out">the out stream which will be used to write the bytes.</param>
        /// <returns>the decoded data</returns>
        protected internal static byte[] FlateDecodeInternal(byte[] @in, bool strict, MemoryStream @out)
        {
            MemoryStream         stream = new MemoryStream(@in);
            ZInflaterInputStream zip    = new ZInflaterInputStream(stream);

            byte[] b = new byte[strict ? 4092 : 1];
            try {
                int n;
                while ((n = zip.Read(b)) >= 0)
                {
                    @out.Write(b, 0, n);
                }
                zip.Dispose();
                @out.Dispose();
                return(@out.ToArray());
            }
            catch (MemoryLimitsAwareException e) {
                throw;
            }
            catch (Exception) {
                if (strict)
                {
                    return(null);
                }
                return(@out.ToArray());
            }
        }
예제 #5
0
파일: FlateUtility.cs 프로젝트: Daoting/dt
        public static byte[] FlateDecode(byte[] inp, bool strict)
        {
            MemoryStream         stream  = new MemoryStream(inp);
            ZInflaterInputStream @this   = new ZInflaterInputStream(stream);
            MemoryStream         stream3 = new MemoryStream();

            byte[] buffer = new byte[strict ? 0xffc : 1];
            try
            {
                int num;
                while ((num = @this.Read(buffer, 0, buffer.Length)) > 0)
                {
                    stream3.Write(buffer, 0, num);
                }
                @this.Dispose();
                stream3.Dispose();
                return(stream3.ToArray());
            }
            catch
            {
                if (strict)
                {
                    return(null);
                }
                return(stream3.ToArray());
            }
        }
예제 #6
0
        /**
         * Return the uncompressed content, throwing an exception if the data size
         * is greater than the passed in limit. If the content is exceeded getCause()
         * on the CMSException will contain a StreamOverflowException
         *
         * @param limit maximum number of bytes to read
         * @return the content read
         * @throws CMSException if there is an exception uncompressing the data.
         */
        public byte[] GetContent(int limit)
        {
            CompressedData comData = CompressedData.GetInstance(contentInfo.Content);
            ContentInfo    content = comData.EncapContentInfo;

            Asn1OctetString bytes = (Asn1OctetString)content.Content;

            ZInflaterInputStream zIn = new ZInflaterInputStream(new MemoryStream(bytes.GetOctets(), false));

            try
            {
                return(CmsUtilities.StreamToByteArray(zIn, limit));
            }
            catch (IOException e)
            {
                throw new CmsException("exception reading compressed stream.", e);
            }
        }
예제 #7
0
        /**
         * Return the uncompressed content.
         *
         * @return the uncompressed content
         * @throws CmsException if there is an exception uncompressing the data.
         */
        public byte[] GetContent()
        {
            CompressedData comData = CompressedData.GetInstance(contentInfo.Content);
            ContentInfo    content = comData.EncapContentInfo;

            Asn1OctetString      bytes = (Asn1OctetString)content.Content;
            ZInflaterInputStream zIn   = new ZInflaterInputStream(bytes.GetOctetStream());

            try
            {
                return(CmsUtilities.StreamToByteArray(zIn));
            }
            catch (IOException e)
            {
                throw new CmsException("exception reading compressed stream.", e);
            }
            finally
            {
                zIn.Close();
            }
        }
예제 #8
0
        /// <summary>A helper to flateDecode.</summary>
        /// <param name="in">the input data</param>
        /// <param name="out">the out stream which will be used to write the bytes.</param>
        /// <returns>the decoded data</returns>
        private static byte[] FlateDecode(byte[] @in, MemoryStream @out)
        {
            MemoryStream         stream = new MemoryStream(@in);
            ZInflaterInputStream zip    = new ZInflaterInputStream(stream);

            byte[] b = new byte[4092];
            try {
                int n;
                while ((n = zip.Read(b)) >= 0)
                {
                    @out.Write(b, 0, n);
                }
                zip.Dispose();
                @out.Dispose();
                return(@out.ToArray());
            }
            catch (MemoryLimitsAwareException e) {
                throw;
            }
            catch (Exception) {
                return(null);
            }
        }
예제 #9
0
        public static byte[] Convert(byte[] woffBytes)
        {
            int srcPos  = 0;
            int destPos = 0;

            // signature
            if (BytesToUInt(woffBytes, srcPos) != woffSignature)
            {
                throw new ArgumentException();
            }
            srcPos += 4;
            byte[] flavor = new byte[4];
            Array.Copy(woffBytes, srcPos, flavor, 0, 4);
            srcPos += 4;
            // length
            if (BytesToUInt(woffBytes, srcPos) != woffBytes.Length)
            {
                throw new ArgumentException();
            }
            srcPos += 4;
            byte[] numTables = new byte[2];
            Array.Copy(woffBytes, srcPos, numTables, 0, 2);
            srcPos += 2;
            // reserved
            if (BytesToUShort(woffBytes, srcPos) != 0)
            {
                throw new ArgumentException();
            }
            srcPos += 2;
            long totalSfntSize = BytesToUInt(woffBytes, srcPos);

            srcPos += 4;
            // majorVersion
            srcPos += 2;
            // minorVersion
            srcPos += 2;
            // metaOffset
            srcPos += 4;
            // metaLength
            srcPos += 4;
            // metaOrigLength
            srcPos += 4;
            // privOffset
            srcPos += 4;
            // privLength
            srcPos += 4;
            // assuming font won't be larger than 2GB
            byte[] otfBytes = new byte[(int)totalSfntSize];
            Array.Copy(flavor, 0, otfBytes, destPos, 4);
            destPos += 4;
            Array.Copy(numTables, 0, otfBytes, destPos, 2);
            destPos += 2;
            int entrySelector = -1;
            int searchRange   = -1;
            int numTablesVal  = BytesToUShort(numTables, 0);

            for (int i = 0; i < 17; ++i)
            {
                int powOfTwo = (int)Math.Pow(2, i);
                if (powOfTwo > numTablesVal)
                {
                    entrySelector = i;
                    searchRange   = powOfTwo * 16;
                    break;
                }
            }
            if (entrySelector < 0)
            {
                throw new ArgumentException();
            }
            otfBytes[destPos]     = (byte)(searchRange >> 8);
            otfBytes[destPos + 1] = (byte)(searchRange);
            destPos              += 2;
            otfBytes[destPos]     = (byte)(entrySelector >> 8);
            otfBytes[destPos + 1] = (byte)(entrySelector);
            destPos              += 2;
            int rangeShift = numTablesVal * 16 - searchRange;

            otfBytes[destPos]     = (byte)(rangeShift >> 8);
            otfBytes[destPos + 1] = (byte)(rangeShift);
            destPos += 2;
            int outTableOffset = destPos;
            IList <WoffConverter.TableDirectory> tdList = new List <WoffConverter.TableDirectory>(numTablesVal);

            for (int i = 0; i < numTablesVal; ++i)
            {
                WoffConverter.TableDirectory td = new WoffConverter.TableDirectory();
                Array.Copy(woffBytes, srcPos, td.tag, 0, 4);
                srcPos   += 4;
                td.offset = BytesToUInt(woffBytes, srcPos);
                srcPos   += 4;
                if (td.offset % 4 != 0)
                {
                    throw new ArgumentException();
                }
                td.compLength = BytesToUInt(woffBytes, srcPos);
                srcPos       += 4;
                Array.Copy(woffBytes, srcPos, td.origLength, 0, 4);
                td.origLengthVal = BytesToUInt(td.origLength, 0);
                srcPos          += 4;
                Array.Copy(woffBytes, srcPos, td.origChecksum, 0, 4);
                srcPos += 4;
                tdList.Add(td);
                outTableOffset += 4 * 4;
            }
            foreach (WoffConverter.TableDirectory td in tdList)
            {
                Array.Copy(td.tag, 0, otfBytes, destPos, 4);
                destPos += 4;
                Array.Copy(td.origChecksum, 0, otfBytes, destPos, 4);
                destPos              += 4;
                otfBytes[destPos]     = (byte)(outTableOffset >> 24);
                otfBytes[destPos + 1] = (byte)(outTableOffset >> 16);
                otfBytes[destPos + 2] = (byte)(outTableOffset >> 8);
                otfBytes[destPos + 3] = (byte)(outTableOffset);
                destPos              += 4;
                Array.Copy(td.origLength, 0, otfBytes, destPos, 4);
                destPos        += 4;
                td.outOffset    = outTableOffset;
                outTableOffset += (int)td.origLengthVal;
                if (outTableOffset % 4 != 0)
                {
                    outTableOffset += 4 - outTableOffset % 4;
                }
            }
            if (outTableOffset != totalSfntSize)
            {
                throw new ArgumentException();
            }
            foreach (WoffConverter.TableDirectory td in tdList)
            {
                byte[] compressedData = new byte[(int)td.compLength];
                byte[] uncompressedData;
                Array.Copy(woffBytes, (int)td.offset, compressedData, 0, (int)td.compLength);
                int expectedUncompressedLen = (int)td.origLengthVal;
                if (td.compLength > td.origLengthVal)
                {
                    throw new ArgumentException();
                }
                if (td.compLength != td.origLengthVal)
                {
                    MemoryStream         stream = new MemoryStream(compressedData);
                    ZInflaterInputStream zip    = new ZInflaterInputStream(stream);
                    uncompressedData = new byte[expectedUncompressedLen];
                    int bytesRead = 0;
                    while (expectedUncompressedLen - bytesRead > 0)
                    {
                        int readRes = zip.JRead(uncompressedData, bytesRead, expectedUncompressedLen - bytesRead);
                        if (readRes < 0)
                        {
                            throw new ArgumentException();
                        }
                        bytesRead += readRes;
                    }
                    if (zip.Read() >= 0)
                    {
                        throw new ArgumentException();
                    }
                }
                else
                {
                    uncompressedData = compressedData;
                }
                Array.Copy(uncompressedData, 0, otfBytes, td.outOffset, expectedUncompressedLen);
            }
            return(otfBytes);
        }