Exemple #1
0
        public Hashtable GetFontList()
        {
            Hashtable ttFonts = new Hashtable();

            try
            {
                DirectoryInfo di = new DirectoryInfo(GetFontDirectory());

                if (di.Exists)
                {
                    FileInfo[] fontFiles = di.GetFiles();

                    for (int i = 0; i < fontFiles.Length; i++)
                    {
                        string filename = fontFiles[i].FullName;
                        rf = new RandomAccessBuffer(filename);

                        rf.Seek(0);
                        int ttId = rf.ReadInt();

                        if (ttId != 0x00010000 && ttId != 0x4F54544F)
                        {
                            continue;
                        }


                        int num_tables = rf.ReadUnsignedShort();
                        rf.SkipBytes(6);
                        for (int k = 0; k < num_tables; ++k)
                        {
                            string tag = ReadStandardString(4);
                            rf.SkipBytes(4);
                            int[] table_location = new int[2];
                            table_location[0] = rf.ReadInt();
                            table_location[1] = rf.ReadInt();
                            tables[tag]       = table_location;
                        }

                        string[][] fullName   = GetNames(4);
                        string[][] familyName = GetNames(1);


                        ttFonts[fullName[0][3]] = filename;
                    }
                }


                //Console.WriteLine("Before : " + ttFonts.Count);

                ReadRegistry(ttFonts);

                //Console.WriteLine("After : " + ttFonts.Count);
            }
            catch (Exception)
            {
            }

            return(ttFonts);
        }
Exemple #2
0
        public IReadOnlyList <CosObject> Parse(PdfRawStream stream, CosObjectPool pool)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            //need to first parse the header.
            var numberOfObjects = stream.Dictionary.GetIntOrDefault(CosName.N);
            var objectNumbers   = new List <long>(numberOfObjects);

            var streamObjects = new List <CosObject>(numberOfObjects);

            var bytes = stream.Decode(filterProvider);

            var reader = new RandomAccessBuffer(bytes);

            for (int i = 0; i < numberOfObjects; i++)
            {
                long objectNumber = ObjectHelper.ReadObjectNumber(reader);
                // skip offset
                ReadHelper.ReadLong(reader);
                objectNumbers.Add(objectNumber);
            }

            CosObject obj;
            CosBase   cosObject;
            int       objectCounter = 0;

            while ((cosObject = baseParser.Parse(reader, pool)) != null)
            {
                obj = new CosObject(cosObject);
                obj.SetGenerationNumber(0);

                if (objectCounter >= objectNumbers.Count)
                {
                    log.Error("/ObjStm (object stream) has more objects than /N " + numberOfObjects);
                    break;
                }

                obj.SetObjectNumber(objectNumbers[objectCounter]);
                streamObjects.Add(obj);

                // According to the spec objects within an object stream shall not be enclosed
                // by obj/endobj tags, but there are some pdfs in the wild using those tags
                // skip endobject marker if present
                if (!reader.IsEof() && reader.Peek() == 'e')
                {
                    ReadHelper.ReadLine(reader);
                }

                objectCounter++;
            }

            return(streamObjects);
        }
Exemple #3
0
        public void SecondPartInvalidFormatFalse()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes("85 9t");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var _);

            Assert.False(result);
        }
Exemple #4
0
        public void TryReadIncorrectFormatMultiplePartsFalse()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes(@"76362 100 1000");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var _);

            Assert.False(result);
        }
Exemple #5
0
        public void ValidTrue()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes("12 32");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var definition);

            Assert.True(result);

            Assert.Equal(12, definition.FirstNumber);
            Assert.Equal(32, definition.Count);
        }
Exemple #6
0
        public void ValidWithLongTrue()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes("214748364700 6");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var definition);

            Assert.True(result);

            Assert.Equal(214748364700L, definition.FirstNumber);
            Assert.Equal(6, definition.Count);
        }
Exemple #7
0
        public static PdfDocument Open(byte[] fileBytes, ParsingOptions options = null)
        {
            var container = Bootstrapper.GenerateContainer(options?.Logger);

            var isLenientParsing = options?.UseLenientParsing ?? true;

            var reader = new RandomAccessBuffer(fileBytes);

            var inputBytes = new ByteArrayInputBytes(fileBytes);

            var tokenScanner = new CoreTokenScanner(inputBytes);

            var document = OpenDocument(reader, inputBytes, tokenScanner, container, isLenientParsing);

            return(document);
        }
        public void SearcherFindsCorrectObjects()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes(TestData);

            var reader = new RandomAccessBuffer(bytes);

            var searcher = new BruteForceSearcher(reader);

            var locations = searcher.GetObjectLocations();

            Assert.Equal(4, locations.Count);

            Assert.Equal(locations.Values, new long[]
            {
                TestData.IndexOf("2 0 obj", StringComparison.OrdinalIgnoreCase),
                TestData.IndexOf("3 0 obj", StringComparison.OrdinalIgnoreCase),
                TestData.IndexOf("4 0 obj", StringComparison.OrdinalIgnoreCase),
                TestData.IndexOf("5 0 obj", StringComparison.OrdinalIgnoreCase)
            });
        }
Exemple #9
0
        public static byte[] Decode(byte[] inputBytes, int predictor, int colors, int bitsPerComponent, int columns)
        {
            if (inputBytes == null)
            {
                throw new ArgumentNullException(nameof(inputBytes));
            }

            if (predictor == 1)
            {
                return(inputBytes);
            }

            int bitsPerPixel  = colors * bitsPerComponent;
            int bytesPerPixel = (bitsPerPixel + 7) / 8;
            int rowlength     = (columns * bitsPerPixel + 7) / 8;

            byte[] actline  = new byte[rowlength];
            byte[] lastline = new byte[rowlength];

            int linepredictor = predictor;

            var result = new List <byte>();

            using (var memoryStream = new MemoryStream())
                using (var output = new BinaryWriter(memoryStream))
                    using (var input = new RandomAccessBuffer(inputBytes))
                    {
                        while (input.Available() > 0)
                        {
                            // test for PNG predictor; each value >= 10 (not only 15) indicates usage of PNG predictor
                            if (predictor >= 10)
                            {
                                // PNG predictor; each row starts with predictor type (0, 1, 2, 3, 4)
                                // read per line predictor
                                linepredictor = input.Read();
                                if (linepredictor == -1)
                                {
                                    return(result.ToArray());
                                }
                                // add 10 to tread value 0 as 10, 1 as 11, ...
                                linepredictor += 10;
                            }

                            // read line
                            int i;
                            int offset = 0;
                            while (offset < rowlength && ((i = input.Read(actline, offset, rowlength - offset)) != -1))
                            {
                                offset += i;
                            }

                            // do prediction as specified input PNG-Specification 1.2
                            switch (linepredictor)
                            {
                            case 2:
                                // PRED TIFF SUB
                                if (bitsPerComponent == 8)
                                {
                                    // for 8 bits per component it is the same algorithm as PRED SUB of PNG format
                                    for (int p = bytesPerPixel; p < rowlength; p++)
                                    {
                                        int sub  = actline[p] & 0xff;
                                        int left = actline[p - bytesPerPixel] & 0xff;
                                        actline[p] = (byte)(sub + left);
                                    }
                                    break;
                                }
                                if (bitsPerComponent == 16)
                                {
                                    for (int p = bytesPerPixel; p < rowlength; p += 2)
                                    {
                                        int sub  = ((actline[p] & 0xff) << 8) + (actline[p + 1] & 0xff);
                                        int left = (((actline[p - bytesPerPixel] & 0xff) << 8)
                                                    + (actline[p - bytesPerPixel + 1] & 0xff));
                                        actline[p]     = (byte)(((sub + left) >> 8) & 0xff);
                                        actline[p + 1] = (byte)((sub + left) & 0xff);
                                    }
                                    break;
                                }
                                if (bitsPerComponent == 1 && colors == 1)
                                {
                                    // bytesPerPixel cannot be used:
                                    // "A row shall occupy a whole number of bytes, rounded up if necessary.
                                    // Samples and their components shall be packed into bytes
                                    // from high-order to low-order bits."
                                    for (int p = 0; p < rowlength; p++)
                                    {
                                        for (int bit = 7; bit >= 0; --bit)
                                        {
                                            int sub = (actline[p] >> bit) & 1;
                                            if (p == 0 && bit == 7)
                                            {
                                                continue;
                                            }
                                            int left;
                                            if (bit == 7)
                                            {
                                                // use bit #0 from previous byte
                                                left = actline[p - 1] & 1;
                                            }
                                            else
                                            {
                                                // use "previous" bit
                                                left = (actline[p] >> (bit + 1)) & 1;
                                            }
                                            if (((sub + left) & 1) == 0)
                                            {
                                                // reset bit
                                                actline[p] = (byte)(actline[p] & ~(1 << bit));
                                            }
                                            else
                                            {
                                                // set bit
                                                actline[p] = (byte)(actline[p] | (1 << bit));
                                            }
                                        }
                                    }
                                    break;
                                }
                                // everything else, i.e. bpc 2 and 4, but has been tested for bpc 1 and 8 too
                                int elements = columns * colors;
                                for (int p = colors; p < elements; ++p)
                                {
                                    int bytePosSub  = p * bitsPerComponent / 8;
                                    int bitPosSub   = 8 - p * bitsPerComponent % 8 - bitsPerComponent;
                                    int bytePosLeft = (p - colors) * bitsPerComponent / 8;
                                    int bitPosLeft  = 8 - (p - colors) * bitsPerComponent % 8 - bitsPerComponent;

                                    int sub  = GetBitSeq(actline[bytePosSub], bitPosSub, bitsPerComponent);
                                    int left = GetBitSeq(actline[bytePosLeft], bitPosLeft, bitsPerComponent);
                                    actline[bytePosSub] = (byte)CalcSetBitSeq(actline[bytePosSub], bitPosSub,
                                                                              bitsPerComponent,
                                                                              sub + left);
                                }
                                break;

                            case 10:
                                // PRED NONE
                                // do nothing
                                break;

                            case 11:
                                // PRED SUB
                                for (int p = bytesPerPixel; p < rowlength; p++)
                                {
                                    int sub  = actline[p];
                                    int left = actline[p - bytesPerPixel];
                                    actline[p] = (byte)(sub + left);
                                }
                                break;

                            case 12:
                                // PRED UP
                                for (int p = 0; p < rowlength; p++)
                                {
                                    int up    = actline[p] & 0xff;
                                    int prior = lastline[p] & 0xff;
                                    actline[p] = (byte)((up + prior) & 0xff);
                                }
                                break;

                            case 13:
                                // PRED AVG
                                for (int p = 0; p < rowlength; p++)
                                {
                                    int avg  = actline[p] & 0xff;
                                    int left = p - bytesPerPixel >= 0 ? actline[p - bytesPerPixel] & 0xff : 0;
                                    int up   = lastline[p] & 0xff;
                                    actline[p] = (byte)((avg + (left + up) / 2) & 0xff);
                                }
                                break;

                            case 14:
                                // PRED PAETH
                                for (int p = 0; p < rowlength; p++)
                                {
                                    int paeth = actline[p] & 0xff;
                                    int a     = p - bytesPerPixel >= 0 ? actline[p - bytesPerPixel] & 0xff : 0;  // left
                                    int b     = lastline[p] & 0xff;                                              // upper
                                    int c     = p - bytesPerPixel >= 0 ? lastline[p - bytesPerPixel] & 0xff : 0; // upperleft
                                    int value = a + b - c;
                                    int absa  = Math.Abs(value - a);
                                    int absb  = Math.Abs(value - b);
                                    int absc  = Math.Abs(value - c);

                                    if (absa <= absb && absa <= absc)
                                    {
                                        actline[p] = (byte)((paeth + a) & 0xff);
                                    }
                                    else if (absb <= absc)
                                    {
                                        actline[p] = (byte)((paeth + b) & 0xff);
                                    }
                                    else
                                    {
                                        actline[p] = (byte)((paeth + c) & 0xff);
                                    }
                                }

                                break;
                            }
                            Array.Copy(actline, 0, lastline, 0, rowlength);
                            output.Write(actline);
                        }

                        return(memoryStream.ToArray());
                    }
        }