Esempio n. 1
0
        public void Parse(NumericToken numeric, ITokenScanner scanner, CharacterMapBuilder builder, bool isLenientParsing)
        {
            for (var i = 0; i < numeric.Int; i++)
            {
                if (!scanner.TryReadToken(out HexToken startHexToken))
                {
                    throw new InvalidFontFormatException("Could not find the starting hex token for the CIDRange in this font.");
                }

                if (!scanner.TryReadToken(out HexToken endHexToken))
                {
                    throw new InvalidFontFormatException("Could not find the end hex token for the CIDRange in this font.");
                }

                if (!scanner.TryReadToken(out NumericToken mappedCode))
                {
                    throw new InvalidFontFormatException("Could not find the starting CID numeric token for the CIDRange in this font.");
                }

                var start = HexToken.ConvertHexBytesToInt(startHexToken);
                var end   = HexToken.ConvertHexBytesToInt(endHexToken);

                var range = new CidRange(start, end, mappedCode.Int);

                builder.AddCidRange(range);
            }
        }
Esempio n. 2
0
        private static IReadOnlyList <byte> ConvertHexToBinary(IReadOnlyList <byte> bytes)
        {
            var result = new List <byte>(bytes.Count / 2);

            var last   = '\0';
            var offset = 0;

            for (var i = 0; i < bytes.Count; i++)
            {
                var c = (char)bytes[i];
                if (!ReadHelper.IsHex(c))
                {
                    // TODO: do I need to assert this must be whitespace?
                    continue;
                }

                if (offset == 1)
                {
                    result.Add(HexToken.Convert(last, c));
                    offset = 0;
                }
                else
                {
                    offset++;
                }

                last = c;
            }

            return(result);
        }
Esempio n. 3
0
        public void MapsCorrectlyToInt(string input, int expected)
        {
            var token = new HexToken(input.ToCharArray());

            var value = HexToken.ConvertHexBytesToInt(token);

            Assert.Equal(expected, value);
        }
Esempio n. 4
0
        private static byte[] HexToBytes(string hex)
        {
            var result = new byte[hex.Length / 2];

            for (var i = 0; i < hex.Length; i += 2)
            {
                result[i / 2] = HexToken.Convert(hex[i], hex[i + 1]);
            }

            return(result);
        }
Esempio n. 5
0
        public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
        {
            token = null;

            if (currentByte != '<')
            {
                return(false);
            }

            var characters = new List <char>();

            while (inputBytes.MoveNext())
            {
                var current = inputBytes.CurrentByte;

                if (ReadHelper.IsWhitespace(current))
                {
                    continue;
                }

                if (current == '>')
                {
                    break;
                }

                if (!IsValidHexCharacter(current))
                {
                    return(false);
                }

                characters.Add((char)current);
            }

            token = new HexToken(characters);

            return(true);
        }
Esempio n. 6
0
        public void MapsCorrectlyToString(string input, string expected)
        {
            var token = new HexToken(input.ToCharArray());

            Assert.Equal(expected, token.Data);
        }
Esempio n. 7
0
        public void Issue443()
        {
            const string hex =
                @"00 0F 4A 43 42 31 33 36 36 31 32 32 37 2E 70 64 66 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 50 44 46 20 43 41 52 4F 01 00 FF FF FF FF 00 00 00 00 00 04 DF 28 00 00 00 00 AF 51 7E 82 AF 52 D7 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 81 81 03 0D 00 00 25 50 44 46 2D 31 2E 31 0A 25 E2 E3 CF D3 0D 0A 31 20 30 20 6F 62 6A";

            var bytes = hex.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(x => HexToken.Convert(x[0], x[1]));

            var str = OtherEncodings.BytesAsLatin1String(bytes.ToArray());

            var scanner = StringBytesTestConverter.Scanner(str);

            var result = FileHeaderParser.Parse(scanner.scanner, scanner.bytes, false, log);

            Assert.Equal(0, scanner.scanner.CurrentPosition);
            Assert.Equal(128, result.OffsetInFile);
            Assert.Equal(1.1m, result.Version);
            Assert.Equal("PDF-1.1", result.VersionString);
        }
Esempio n. 8
0
        private IToken DecryptInternal(IndirectReference reference, IToken token)
        {
            switch (token)
            {
            case StreamToken stream:
            {
                if (cryptHandler?.StreamDictionary?.IsIdentity == true ||
                    cryptHandler?.StreamDictionary?.Name == CryptDictionary.Method.None)
                {
                    // TODO: No idea if this is right.
                    return(token);
                }

                if (stream.StreamDictionary.TryGet(NameToken.Type, out NameToken typeName))
                {
                    if (NameToken.Xref.Equals(typeName))
                    {
                        return(token);
                    }

                    if (!encryptionDictionary.EncryptMetadata && NameToken.Metadata.Equals(typeName))
                    {
                        return(token);
                    }

                    // TODO: check unencrypted metadata
                }

                var streamDictionary = (DictionaryToken)DecryptInternal(reference, stream.StreamDictionary);

                var decrypted = DecryptData(stream.Data.ToArray(), reference);

                token = new StreamToken(streamDictionary, decrypted);

                break;
            }

            case StringToken stringToken:
            {
                if (cryptHandler?.StringDictionary?.IsIdentity == true ||
                    cryptHandler?.StringDictionary?.Name == CryptDictionary.Method.None)
                {
                    // TODO: No idea if this is right.
                    return(token);
                }

                var data = OtherEncodings.StringAsLatin1Bytes(stringToken.Data);

                var decrypted = DecryptData(data, reference);

                token = GetStringTokenFromDecryptedData(decrypted);

                break;
            }

            case HexToken hexToken:
            {
                var data = hexToken.Bytes.ToArray();

                var decrypted = DecryptData(data, reference);

                token = new HexToken(Hex.GetString(decrypted).ToCharArray());

                break;
            }

            case DictionaryToken dictionary:
            {
                // PDFBOX-2936: avoid orphan /CF dictionaries found in US govt "I-" files
                if (dictionary.TryGet(NameToken.Cf, out _))
                {
                    return(token);
                }

                var isSignatureDictionary = dictionary.TryGet(NameToken.Type, out NameToken typeName) &&
                                            (typeName.Equals(NameToken.Sig) || typeName.Equals(NameToken.DocTimeStamp));

                foreach (var keyValuePair in dictionary.Data)
                {
                    if (isSignatureDictionary && keyValuePair.Key == NameToken.Contents.Data)
                    {
                        continue;
                    }

                    if (keyValuePair.Value is StringToken || keyValuePair.Value is ArrayToken ||
                        keyValuePair.Value is DictionaryToken ||
                        keyValuePair.Value is HexToken)
                    {
                        var inner = DecryptInternal(reference, keyValuePair.Value);
                        dictionary = dictionary.With(keyValuePair.Key, inner);
                    }
                }

                token = dictionary;

                break;
            }

            case ArrayToken array:
            {
                var result = new IToken[array.Length];

                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = DecryptInternal(reference, array.Data[i]);
                }

                token = new ArrayToken(result);

                break;
            }
            }

            return(token);
        }
Esempio n. 9
0
 private static void WriteHex(HexToken hex, Stream stream)
 {
     stream.WriteByte(HexStart);
     stream.WriteText(hex.GetHexString());
     stream.WriteByte(HexEnd);
 }
Esempio n. 10
0
        private static byte[] GetHexBytes(params char[] characters)
        {
            var token = new HexToken(characters);

            return(token.Bytes.ToArray());
        }