コード例 #1
0
        public static ICCProfile Load(byte[] data)
        {
            var profile = new ICCProfile();
            var header  = new ICCHeader();
            var buffer  = new Bytes.Buffer(data);

            header.ProfileSize                  = buffer.ReadUnsignedInt();
            header.CMMTypeSignature             = buffer.ReadUnsignedInt();
            header.ProfileVersionNumber.Major   = (byte)buffer.ReadByte();
            header.ProfileVersionNumber.Minor   = (byte)buffer.ReadByte();
            header.ProfileVersionNumber.Reserv1 = (byte)buffer.ReadByte();
            header.ProfileVersionNumber.Reserv2 = (byte)buffer.ReadByte();
            header.ProfileDeviceClassSignature  = (ICCProfileDeviceSignatures)buffer.ReadUnsignedInt();
            header.ColorSpaceOfData             = (ICCColorSpaceSignatures)buffer.ReadUnsignedInt();
            header.ProfileConnectionSpace       = (ICCColorSpaceSignatures)buffer.ReadUnsignedInt();
            header.DateCreated.Load(buffer);
            header.acsp = buffer.ReadUnsignedInt();
            header.PrimaryPlatformSignature = (ICCPrimaryPlatformSignatures)buffer.ReadUnsignedInt();
            header.Flags = (ICCProfileFlags)buffer.ReadUnsignedInt();
            header.DeviceManufacturer = buffer.ReadUnsignedInt();
            header.DeviceModel        = buffer.ReadUnsignedInt();
            header.DeviceAttributes.Load(buffer);
            header.RenderingIntent.Intents  = buffer.ReadUnsignedShort();
            header.RenderingIntent.Reserved = buffer.ReadUnsignedShort();
            header.XYZ.Load(buffer);
            header.ProfileCreatorSignature = buffer.ReadUnsignedInt();
            header.FutureUse = new byte[44];
            buffer.Read(header.FutureUse);
            profile.Header = header;
            var tagCount = buffer.ReadUnsignedInt();

            for (int i = 0; i < tagCount; i++)
            {
                var tag = new ICCTagTable();
                tag.Signature               = (ICCTagTypes)buffer.ReadUnsignedInt();
                tag.Offset                  = buffer.ReadUnsignedInt();
                tag.ElementSize             = buffer.ReadUnsignedInt();
                profile.Tags[tag.Signature] = tag;
            }
            foreach (var tagTable in profile.Tags.Values)
            {
                buffer.Seek(tagTable.Offset);
                var key = buffer.ReadUnsignedInt();
                if (Types.TryGetValue(key, out var type))
                {
                    tagTable.Tag         = (ICCTag)Activator.CreateInstance(type, tagTable);
                    tagTable.Tag.Profile = profile;
                    tagTable.Tag.Load(buffer);
                }
            }
            return(profile);
        }
コード例 #2
0
 public override void Load(Bytes.Buffer buffer)
 {
     buffer.Seek(Table.Offset);
     buffer.ReadUnsignedInt();
     buffer.ReadUnsignedInt();
     Count           = buffer.ReadUnsignedInt();
     Description     = System.Text.Encoding.ASCII.GetString(buffer.ReadBytes((int)Count));
     UnicodeCode     = buffer.ReadUnsignedInt();
     UnicodeCount    = buffer.ReadUnsignedInt();
     ScriptcodeCode  = buffer.ReadUnsignedShort();
     ScriptcodeCount = (byte)buffer.ReadByte();
     MacDescription  = System.Text.Encoding.ASCII.GetString(buffer.ReadBytes(67));
 }
コード例 #3
0
ファイル: PfbParser.cs プロジェクト: alexandrvslv/pdf-clown
        /**
         * Parse the pfb-array.
         * @param pfb   The pfb-Array
         * @throws IOException in an IO-error occurs.
         */
        private void ParsePfb(byte[] pfb)
        {
            using (var input = new Bytes.Buffer(pfb))
            {
                pfbdata = new byte[pfb.Length - PFB_HEADER_LENGTH];
                lengths = new int[PFB_RECORDS.Length];
                int pointer = 0;
                for (int records = 0; records < PFB_RECORDS.Length; records++)
                {
                    if (input.ReadByte() != START_MARKER)
                    {
                        throw new IOException("Start marker missing");
                    }

                    if (input.ReadByte() != PFB_RECORDS[records])
                    {
                        throw new IOException("Incorrect record type");
                    }

                    int size = input.ReadByte();
                    size            += input.ReadByte() << 8;
                    size            += input.ReadByte() << 16;
                    size            += input.ReadByte() << 24;
                    lengths[records] = size;
                    if (pointer >= pfbdata.Length)
                    {
                        throw new EndOfStreamException("attempted to read past EOF");
                    }
                    int got = input.Read(pfbdata, pointer, size);
                    if (got < 0)
                    {
                        throw new EndOfStreamException();
                    }
                    pointer += got;
                }
            }
        }
コード例 #4
0
ファイル: LZWFilter.cs プロジェクト: alexandrvslv/pdf-clown
        public override byte[] Encode(Bytes.Buffer rawData, PdfDirectObject parameters, IDictionary <PdfName, PdfDirectObject> header)
        {
            List <byte[]> codeTable = CreateCodeTable();
            int           chunk     = 9;

            byte[] inputPattern = null;
            using (var output = new Bytes.Buffer())
            {
                output.WriteBits(CLEAR_TABLE, chunk);
                int foundCode = -1;
                int r;
                while ((r = rawData.ReadByte()) != -1)
                {
                    byte by = (byte)r;
                    if (inputPattern == null)
                    {
                        inputPattern = new byte[] { by };
                        foundCode    = by & 0xff;
                    }
                    else
                    {
                        inputPattern = inputPattern.SubArray(0, inputPattern.Length + 1);
                        inputPattern[inputPattern.Length - 1] = by;
                        int newFoundCode = FindPatternCode(codeTable, inputPattern);
                        if (newFoundCode == -1)
                        {
                            // use previous
                            chunk = CalculateChunk(codeTable.Count - 1, 1);
                            output.WriteBits(foundCode, chunk);
                            // create new table entry
                            codeTable.Add(inputPattern);

                            if (codeTable.Count == 4096)
                            {
                                // code table is full
                                output.WriteBits(CLEAR_TABLE, chunk);
                                codeTable = CreateCodeTable();
                            }

                            inputPattern = new byte[] { by };
                            foundCode    = by & 0xff;
                        }
                        else
                        {
                            foundCode = newFoundCode;
                        }
                    }
                }
                if (foundCode != -1)
                {
                    chunk = CalculateChunk(codeTable.Count - 1, 1);
                    output.WriteBits(foundCode, chunk);
                }

                // PPDFBOX-1977: the decoder wouldn't know that the encoder would output
                // an EOD as code, so he would have increased his own code table and
                // possibly adjusted the chunk. Therefore, the encoder must behave as
                // if the code table had just grown and thus it must be checked it is
                // needed to adjust the chunk, based on an increased table size parameter
                chunk = CalculateChunk(codeTable.Count, 1);

                output.WriteBits(EOD, chunk);

                // pad with 0
                output.WriteBits(0, 7);

                // must do or file will be empty :-(
                return(output.GetBuffer());
            }
        }
コード例 #5
0
 /**
  * Reads an ASCII char from the buffer.
  */
 private char GetChar()
 {
     return((char)buffer.ReadByte());
 }
コード例 #6
0
        bool GetMaybeValidDimensions(Bytes.Buffer data, IDictionary <PdfName, PdfDirectObject> dict)
        {
            var dictHeight = ((IPdfNumber)(dict[PdfName.Height] ?? dict[PdfName.H])).IntValue;
            var startPos   = data.Position;

            var validDimensions = true;
            var foundSOF        = false;
            var b = 0;

            while ((b = data.ReadByte()) != -1)
            {
                if (b != 0xff)
                {
                    // Not a valid marker.
                    continue;
                }
                switch (data.ReadByte())
                {
                case 0xc0:     // SOF0
                case 0xc1:     // SOF1
                case 0xc2:     // SOF2
                               // These three SOF{n} markers are the only ones that the built-in
                               // PDF.js JPEG decoder currently supports.
                    foundSOF = true;

                    data.Position += 2;     // Skip marker length.
                    data.Position += 1;     // Skip precision.
                    var scanLines      = data.ReadUnsignedShort();
                    var samplesPerLine = data.ReadUnsignedShort();

                    // Letting the browser handle the JPEG decoding, on the main-thread,
                    // will cause a *large* increase in peak memory usage since there's
                    // a handful of short-lived copies of the image data. For very big
                    // JPEG images, always let the PDF.js image decoder handle them to
                    // reduce overall memory usage during decoding (see issue 11694).
                    if (scanLines * samplesPerLine > 1e6)
                    {
                        validDimensions = false;
                        break;
                    }

                    // The "normal" case, where the image data and dictionary agrees.
                    if (scanLines == dictHeight)
                    {
                        break;
                    }
                    // A DNL (Define Number of Lines) marker is expected,
                    // which browsers (usually) cannot decode natively.
                    if (scanLines == 0)
                    {
                        validDimensions = false;
                        break;
                    }
                    // The dimensions of the image, among other properties, should
                    // always be taken from the image data *itself* rather than the
                    // XObject dictionary. However there's cases of corrupt images that
                    // browsers cannot decode natively, for example:
                    //  - JPEG images with DNL markers, where the SOF `scanLines`
                    //    parameter has an unexpected value (see issue 8614).
                    //  - JPEG images with too large SOF `scanLines` parameter, where
                    //    the EOI marker is encountered prematurely (see issue 10880).
                    // In an attempt to handle these kinds of corrupt images, compare
                    // the dimensions in the image data with the dictionary and *always*
                    // let the PDF.js JPEG decoder (rather than the browser) handle the
                    // image if the difference is larger than one order of magnitude
                    // (since that would generally suggest that something is off).
                    if (scanLines > dictHeight * 10)
                    {
                        validDimensions = false;
                        break;
                    }
                    break;

                case 0xc3:     // SOF3
                /* falls through */
                case 0xc5:     // SOF5
                case 0xc6:     // SOF6
                case 0xc7:     // SOF7
                /* falls through */
                case 0xc9:     // SOF9
                case 0xca:     // SOF10
                case 0xcb:     // SOF11
                /* falls through */
                case 0xcd:     // SOF13
                case 0xce:     // SOF14
                case 0xcf:     // SOF15
                    foundSOF = true;
                    break;

                case 0xc4:     // DHT
                case 0xcc:     // DAC
                /* falls through */
                case 0xda:     // SOS
                case 0xdb:     // DQT
                case 0xdc:     // DNL
                case 0xdd:     // DRI
                case 0xde:     // DHP
                case 0xdf:     // EXP
                /* falls through */
                case 0xe0:     // APP0
                case 0xe1:     // APP1
                case 0xe2:     // APP2
                case 0xe3:     // APP3
                case 0xe4:     // APP4
                case 0xe5:     // APP5
                case 0xe6:     // APP6
                case 0xe7:     // APP7
                case 0xe8:     // APP8
                case 0xe9:     // APP9
                case 0xea:     // APP10
                case 0xeb:     // APP11
                case 0xec:     // APP12
                case 0xed:     // APP13
                case 0xee:     // APP14
                case 0xef:     // APP15
                /* falls through */
                case 0xfe:     // COM
                    var markerLength = data.ReadUnsignedShort();
                    if (markerLength > 2)
                    {
                        data.Skip(markerLength - 2);     // Jump to the next marker.
                    }
                    else
                    {
                        // The marker length is invalid, resetting the stream position.
                        data.Skip(-2);
                    }
                    break;

                case 0xff:     // Fill byte.
                               // Avoid skipping a valid marker, resetting the stream position.
                    data.Skip(-1);
                    break;

                case 0xd9:     // EOI
                    foundSOF = true;
                    break;
                }
                if (foundSOF)
                {
                    break;
                }
            }
            // Finally, don't forget to reset the stream position.
            data.Position = startPos;

            return(validDimensions);
        }