public void Run(ICommandInteraction writer, [Values(-1L, 0L, 1L, 2L, 3L)] NumericToken level, LiteralToken backendName, LiteralToken peripheralName) { if (!SetLogLevel((LogLevel)level.Value, backendName.Value, peripheralName.Value)) { writer.WriteError(string.Format("Could not find emulation element or backend")); } }
public void EscapeStateTokenizeNumeric() { var lexer = new Lexer(); var tokens = lexer.Tokenize("\\d"); var expected = new NumericToken(); Assert.AreEqual(tokens.Last(), expected); }
protected override NumericToken Evaluate(NumericToken l, NumericToken r) { try { return(l / r); } catch (ArithmeticException e) { throw new EvaluationException($"Division by zero @ char {r.Index}", e); } }
public long GetFirstCrossReferenceOffset(IInputBytes bytes, ISeekableTokenScanner scanner, bool isLenientParsing) { if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } if (scanner == null) { throw new ArgumentNullException(nameof(scanner)); } var fileLength = bytes.Length; var offsetFromEnd = fileLength < EndOfFileSearchRange ? (int)fileLength : EndOfFileSearchRange; var startPosition = fileLength - offsetFromEnd; bytes.Seek(startPosition); var startXrefPosition = GetStartXrefPosition(bytes, offsetFromEnd); scanner.Seek(startXrefPosition); if (!scanner.TryReadToken(out OperatorToken startXrefToken) || startXrefToken.Data != "startxref") { throw new InvalidOperationException($"The start xref position we found was not correct. Found {startXrefPosition} but it was occupied by token {scanner.CurrentToken}."); } NumericToken numeric = null; while (scanner.MoveNext()) { if (scanner.CurrentToken is NumericToken token) { numeric = token; break; } if (!(scanner.CurrentToken is CommentToken)) { throw new PdfDocumentFormatException($"Found an unexpected token following 'startxref': {scanner.CurrentToken}."); } } if (numeric == null) { throw new PdfDocumentFormatException($"Could not find the numeric value following 'startxref'. Searching from position {startXrefPosition}."); } return(numeric.Long); }
private static void WriteNumber(NumericToken number, Stream outputStream) { if (!number.HasDecimalPlaces) { WriteInt(number.Int, outputStream); } else { var bytes = OtherEncodings.StringAsLatin1Bytes(number.Data.ToString("G", CultureInfo.InvariantCulture)); outputStream.Write(bytes, 0, bytes.Length); } WriteWhitespace(outputStream); }
/// <summary> /// Performs the evaluation of the specified operator token /// </summary> private static NumericToken Evaluate(OperatorToken token, Stack <Token> operands) { try { var arguments = new NumericToken[token.Value.Operands]; for (var i = 0; i < token.Value.Operands; i++) { arguments[i] = (NumericToken)operands.Pop(); } return(token.Value.Evaluate(arguments)); } catch (InvalidOperationException e) when(!operands.Any()) { throw new EvaluationException( $"Insufficient operands for operator '{token.Value}' @ char: {token.Index}", e); } }
/// <summary> /// /// </summary> /// <param name="numericTokenId"></param> /// <returns></returns> public NumericToken SetUsed(long numericTokenId, bool used) { NumericToken nt = Find(numericTokenId); using (var c = Connections.GetConnections.GetConnection()) using (var cmd = c.CreateCommand()) { c.Open(); cmd.CommandText = "UPDATE NumericTokens SET Used = @2 WHERE NumericTokenId = @1"; cmd.Parameters.AddWithValue("@1", numericTokenId); cmd.Parameters.AddWithValue("@2", used); cmd.ExecuteNonQuery(); } return(Find(numericTokenId)); }
private static CharacterIdentifierSystemInfo GetCharacterIdentifier(DictionaryToken dictionary, bool isLenientParsing) { string GetErrorMessage(string missingKey) { return($"No {missingKey} found in the CIDSystemInfo dictionary: " + dictionary); } if (!dictionary.TryGet(NameToken.Registry, out var registry) || !(registry is StringToken registryString)) { if (isLenientParsing) { registryString = new StringToken("Adobe"); } else { throw new InvalidOperationException(GetErrorMessage("registry")); } } if (!dictionary.TryGet(NameToken.Ordering, out var ordering) || !(ordering is StringToken orderingString)) { if (isLenientParsing) { orderingString = new StringToken(""); } else { throw new InvalidOperationException(GetErrorMessage("ordering")); } } if (!dictionary.TryGet(NameToken.Supplement, out var supplement) || !(supplement is NumericToken supplementNumeric)) { if (isLenientParsing) { supplementNumeric = new NumericToken(0); } else { throw new InvalidOperationException(GetErrorMessage("supplement")); } } return(new CharacterIdentifierSystemInfo(registryString.Data, orderingString.Data, supplementNumeric.Int)); }
private static CharacterIdentifierSystemInfo GetCharacterIdentifier(DictionaryToken dictionary) { if (!dictionary.TryGet(NameToken.Registry, out var registry) || !(registry is StringToken registryString)) { registryString = new StringToken("Adobe"); } if (!dictionary.TryGet(NameToken.Ordering, out var ordering) || !(ordering is StringToken orderingString)) { orderingString = new StringToken(string.Empty); } if (!dictionary.TryGet(NameToken.Supplement, out var supplement) || !(supplement is NumericToken supplementNumeric)) { supplementNumeric = new NumericToken(0); } return(new CharacterIdentifierSystemInfo(registryString.Data, orderingString.Data, supplementNumeric.Int)); }
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token) { token = null; StringBuilder characters; if ((currentByte >= Zero && currentByte <= Nine) || currentByte == '-' || currentByte == '+' || currentByte == '.') { characters = stringBuilder; characters.Append((char)currentByte); } else { return(false); } while (inputBytes.MoveNext()) { var b = inputBytes.CurrentByte; if ((b >= Zero && b <= Nine) || b == '-' || b == '+' || b == '.' || b == 'E' || b == 'e') { characters.Append((char)b); } else { break; } } try { var str = characters.ToString(); characters.Clear(); switch (str) { case "-1": token = NumericToken.MinusOne; return(true); case "-": case ".": case "0": case "0000": token = NumericToken.Zero; return(true); case "1": token = NumericToken.One; return(true); case "2": token = NumericToken.Two; return(true); case "3": token = NumericToken.Three; return(true); case "4": token = NumericToken.Four; return(true); case "5": token = NumericToken.Five; return(true); case "6": token = NumericToken.Six; return(true); case "7": token = NumericToken.Seven; return(true); case "8": token = NumericToken.Eight; return(true); case "9": token = NumericToken.Nine; return(true); case "10": token = NumericToken.Ten; return(true); case "11": token = NumericToken.Eleven; return(true); case "12": token = NumericToken.Twelve; return(true); case "13": token = NumericToken.Thirteen; return(true); case "14": token = NumericToken.Fourteen; return(true); case "15": token = NumericToken.Fifteen; return(true); case "16": token = NumericToken.Sixteen; return(true); case "17": token = NumericToken.Seventeen; return(true); case "18": token = NumericToken.Eighteen; return(true); case "19": token = NumericToken.Nineteen; return(true); case "20": token = NumericToken.Twenty; return(true); case "100": token = NumericToken.OneHundred; return(true); case "500": token = NumericToken.FiveHundred; return(true); case "1000": token = NumericToken.OneThousand; return(true); default: if (!decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) { return(false); } token = new NumericToken(value); return(true); } } catch (FormatException) { return(false); } catch (OverflowException) { return(false); } }
public bool MoveNext() { if (isDisposed) { throw new ObjectDisposedException(nameof(PdfTokenScanner)); } // Read until we find object-number generation obj, e.g. "69 420 obj". int tokensRead = 0; while (coreTokenScanner.MoveNext() && !Equals(coreTokenScanner.CurrentToken, OperatorToken.StartObject)) { if (coreTokenScanner.CurrentToken is CommentToken) { continue; } tokensRead++; previousTokens[0] = previousTokens[1]; previousTokenPositions[0] = previousTokenPositions[1]; previousTokens[1] = coreTokenScanner.CurrentToken; previousTokenPositions[1] = coreTokenScanner.CurrentTokenStart; } // We only read partial tokens. if (tokensRead < 2) { return(false); } var startPosition = previousTokenPositions[0]; var objectNumber = previousTokens[0] as NumericToken; var generation = previousTokens[1] as NumericToken; if (objectNumber == null || generation == null) { // Handle case where the scanner correctly reads most of an object token but includes too much of the first token // specifically %%EOF1 0 obj where scanning starts from 'F'. if (generation != null && previousTokens[0] is OperatorToken op) { var match = EndsWithNumberRegex.Match(op.Data); if (match.Success && int.TryParse(match.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var number)) { startPosition = previousTokenPositions[0] + match.Index; objectNumber = new NumericToken(number); } else { return(false); } } else { return(false); } } // Read all tokens between obj and endobj. while (coreTokenScanner.MoveNext() && !Equals(coreTokenScanner.CurrentToken, OperatorToken.EndObject)) { if (coreTokenScanner.CurrentToken is CommentToken) { continue; } if (ReferenceEquals(coreTokenScanner.CurrentToken, OperatorToken.StartObject)) { // This should never happen. Debug.Assert(false, "Encountered a start object 'obj' operator before the end of the previous object."); return(false); } if (ReferenceEquals(coreTokenScanner.CurrentToken, OperatorToken.StartStream)) { var streamIdentifier = new IndirectReference(objectNumber.Long, generation.Int); // Prevent an infinite loop where a stream's length references the stream or the stream's offset. var getLengthFromFile = !(callingObject.HasValue && callingObject.Value.Equals(streamIdentifier)); var outerCallingObject = callingObject; try { callingObject = streamIdentifier; // Read stream: special case. if (TryReadStream(coreTokenScanner.CurrentTokenStart, getLengthFromFile, out var stream)) { readTokens.Clear(); readTokens.Add(stream); } } finally { callingObject = outerCallingObject; } } else { readTokens.Add(coreTokenScanner.CurrentToken); } previousTokens[0] = previousTokens[1]; previousTokenPositions[0] = previousTokenPositions[1]; previousTokens[1] = coreTokenScanner.CurrentToken; previousTokenPositions[1] = coreTokenScanner.CurrentPosition; } if (!ReferenceEquals(coreTokenScanner.CurrentToken, OperatorToken.EndObject)) { readTokens.Clear(); return(false); } var reference = new IndirectReference(objectNumber.Long, generation.Int); IToken token; if (readTokens.Count == 3 && readTokens[0] is NumericToken objNum && readTokens[1] is NumericToken genNum && ReferenceEquals(readTokens[2], OperatorToken.R)) { // I have no idea if this can ever happen. token = new IndirectReferenceToken(new IndirectReference(objNum.Long, genNum.Int)); }
public MarkedContentElement Build(IPdfTokenScanner pdfScanner) { var mcid = -1; if (properties.TryGet(NameToken.Mcid, pdfScanner, out NumericToken mcidToken)) { mcid = mcidToken.Int; } var language = GetOptional(NameToken.Lang, pdfScanner); var actualText = GetOptional(NameToken.ActualText, pdfScanner); var alternateDescription = GetOptional(NameToken.Alternate, pdfScanner); var expandedForm = GetOptional(NameToken.E, pdfScanner); if (name != NameToken.Artifact) { return(new MarkedContentElement(mcid, name, properties, language, actualText, alternateDescription, expandedForm, false, Children, letters, paths, images, number)); } var artifactType = ArtifactMarkedContentElement.ArtifactType.Unknown; if (properties.TryGet(NameToken.Type, pdfScanner, out IDataToken <string> typeToken) && Enum.TryParse(typeToken.Data, true, out ArtifactMarkedContentElement.ArtifactType parsedType)) { artifactType = parsedType; } var subType = GetOptional(NameToken.Subtype, pdfScanner); var attributeOwners = GetOptional(NameToken.O, pdfScanner); var boundingBox = default(PdfRectangle?); if (properties.TryGet(NameToken.Bbox, pdfScanner, out ArrayToken arrayToken)) { NumericToken left = null; NumericToken bottom = null; NumericToken right = null; NumericToken top = null; if (arrayToken.Length == 4) { left = arrayToken[0] as NumericToken; bottom = arrayToken[1] as NumericToken; right = arrayToken[2] as NumericToken; top = arrayToken[3] as NumericToken; } else if (arrayToken.Length == 6) { left = arrayToken[2] as NumericToken; bottom = arrayToken[3] as NumericToken; right = arrayToken[4] as NumericToken; top = arrayToken[5] as NumericToken; } if (left != null && bottom != null && right != null && top != null) { boundingBox = new PdfRectangle(left.Double, bottom.Double, right.Double, top.Double); } } var attached = new List <NameToken>(); if (properties.TryGet(NameToken.Attached, out ArrayToken attachedToken)) { foreach (var token in attachedToken.Data) { if (token is NameToken aName) { attached.Add(aName); } } } return(new ArtifactMarkedContentElement(mcid, name, properties, language, actualText, alternateDescription, expandedForm, artifactType, subType, attributeOwners, boundingBox, attached, Children, letters, paths, images, number)); }
public NumericConsequentRepeatEveryNode(Token repeatEvery, NumericToken number, WordToken partOfDatetime) : base(repeatEvery, partOfDatetime) { _number = number; }
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token) { token = null; StringBuilder characters; if ((currentByte >= '0' && currentByte <= '9') || currentByte == '-' || currentByte == '+' || currentByte == '.') { characters = new StringBuilder(); characters.Append((char)currentByte); } else { return(false); } while (inputBytes.MoveNext()) { var b = inputBytes.CurrentByte; var c = (char)b; if (char.IsDigit(c) || c == '-' || c == '+' || c == '.' || c == 'E' || c == 'e') { characters.Append(c); } else { break; } } decimal value; try { if (characters.Length == 1 && (characters[0] == '-' || characters[0] == '.')) { value = 0; } else { value = decimal.Parse(characters.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture); } } catch (FormatException) { return(false); } catch (OverflowException) { return(false); } token = new NumericToken(value); return(true); }
/// <summary> /// Adds the PNG image represented by the input stream at the specified location. /// </summary> public AddedImage AddPng(Stream pngStream, PdfRectangle placementRectangle) { var png = Png.Open(pngStream); byte[] data; var pixelBuffer = new byte[3]; using (var memoryStream = new MemoryStream()) { for (var rowIndex = 0; rowIndex < png.Height; rowIndex++) { for (var colIndex = 0; colIndex < png.Width; colIndex++) { var pixel = png.GetPixel(colIndex, rowIndex); pixelBuffer[0] = pixel.R; pixelBuffer[1] = pixel.G; pixelBuffer[2] = pixel.B; memoryStream.Write(pixelBuffer, 0, pixelBuffer.Length); } } data = memoryStream.ToArray(); } var widthToken = new NumericToken(png.Width); var heightToken = new NumericToken(png.Height); IndirectReferenceToken smaskReference = null; if (png.HasAlphaChannel && documentBuilder.ArchiveStandard != PdfAStandard.A1B && documentBuilder.ArchiveStandard != PdfAStandard.A1A) { var smaskData = new byte[data.Length / 3]; for (var rowIndex = 0; rowIndex < png.Height; rowIndex++) { for (var colIndex = 0; colIndex < png.Width; colIndex++) { var pixel = png.GetPixel(colIndex, rowIndex); var index = rowIndex * png.Width + colIndex; smaskData[index] = pixel.A; } } var compressedSmask = DataCompresser.CompressBytes(smaskData); // Create a soft-mask. var smaskDictionary = new Dictionary <NameToken, IToken> { { NameToken.Type, NameToken.Xobject }, { NameToken.Subtype, NameToken.Image }, { NameToken.Width, widthToken }, { NameToken.Height, heightToken }, { NameToken.ColorSpace, NameToken.Devicegray }, { NameToken.BitsPerComponent, new NumericToken(png.Header.BitDepth) }, { NameToken.Decode, new ArrayToken(new IToken[] { new NumericToken(0), new NumericToken(1) }) }, { NameToken.Length, new NumericToken(compressedSmask.Length) }, { NameToken.Filter, NameToken.FlateDecode } }; smaskReference = documentBuilder.AddImage(new DictionaryToken(smaskDictionary), compressedSmask); } var compressed = DataCompresser.CompressBytes(data); var imgDictionary = new Dictionary <NameToken, IToken> { { NameToken.Type, NameToken.Xobject }, { NameToken.Subtype, NameToken.Image }, { NameToken.Width, widthToken }, { NameToken.Height, heightToken }, { NameToken.BitsPerComponent, new NumericToken(png.Header.BitDepth) }, { NameToken.ColorSpace, NameToken.Devicergb }, { NameToken.Filter, NameToken.FlateDecode }, { NameToken.Length, new NumericToken(compressed.Length) } }; if (smaskReference != null) { imgDictionary.Add(NameToken.Smask, smaskReference); } var reference = documentBuilder.AddImage(new DictionaryToken(imgDictionary), compressed); var resources = pageDictionary.GetOrCreateDict(NameToken.Resources); var xObjects = resources.GetOrCreateDict(NameToken.Xobject); var key = NameToken.Create($"I{imageKey++}"); xObjects[key] = reference; currentStream.Add(Push.Value); // This needs to be the placement rectangle. currentStream.Add(new ModifyCurrentTransformationMatrix(new[] { (decimal)placementRectangle.Width, 0, 0, (decimal)placementRectangle.Height, (decimal)placementRectangle.BottomLeft.X, (decimal)placementRectangle.BottomLeft.Y })); currentStream.Add(new InvokeNamedXObject(key)); currentStream.Add(Pop.Value); return(new AddedImage(reference.Data, png.Width, png.Height)); }
/// <summary> /// Determines whether the specified object is equal to the current object /// </summary> /// <returns>true if the specified object is equal to the current object; otherwise, false</returns> /// <param name="other">The object to compare with the current object</param> protected bool Equals(NumericToken other) => Value == other.Value;
public static INumericLiteral Parse(NumericToken numericToken) { string numberCharacters = (string)numericToken.Literal !; if (numericToken.IsFractional) { // TODO: This is a mess. We currently treat all floating point values as _double_, which is insane. We // TODO: should probably have a "use smallest possible type" logic as below for integers, for floating point // TODO: values as well. We could also consider supporting `decimal` while we're at it. // The explicit IFormatProvider is required to ensure we use 123.45 format, regardless of host OS // language/region settings. See #263 for more details. return(new FloatingPointLiteral <double>(Double.Parse(numberCharacters, CultureInfo.InvariantCulture))); } else { // Any potential preceding '-' character has already been taken care of at this stage => we can treat // the number as an unsigned value. However, we still try to coerce it to the smallest signed or // unsigned integer type in which it will fit (but never smaller than 32-bit). This coincidentally // follows the same semantics as how C# does it, for simplicity. BigInteger value = numericToken.NumberBase switch { NumericToken.Base.DECIMAL => BigInteger.Parse(numberCharacters, numericToken.NumberStyles), NumericToken.Base.BINARY => Convert.ToUInt64(numberCharacters, 2), NumericToken.Base.OCTAL => Convert.ToUInt64(numberCharacters, 8), NumericToken.Base.HEXADECIMAL => // Quoting from // https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.parse?view=net-5.0#System_Numerics_BigInteger_Parse_System_ReadOnlySpan_System_Char__System_Globalization_NumberStyles_System_IFormatProvider_ // // If value is a hexadecimal string, the Parse(String, NumberStyles) method interprets value as a // negative number stored by using two's complement representation if its first two hexadecimal // digits are greater than or equal to 0x80. In other words, the method interprets the highest-order // bit of the first byte in value as the sign bit. To make sure that a hexadecimal string is // correctly interpreted as a positive number, the first digit in value must have a value of zero. // // We presume that all hexadecimals should be treated as positive numbers for now. BigInteger.Parse('0' + numberCharacters, numericToken.NumberStyles), _ => throw new InvalidOperationException($"Base {(int)numericToken.NumberBase} not supported") }; if (value <= Int32.MaxValue) { return(new IntegerLiteral <int>((int)value)); } else if (value <= UInt32.MaxValue) { return(new IntegerLiteral <uint>((uint)value)); } else if (value <= Int64.MaxValue) { return(new IntegerLiteral <long>((long)value)); } else if (value <= UInt64.MaxValue) { return(new IntegerLiteral <ulong>((ulong)value)); } else // Anything else remains a BigInteger { return(new IntegerLiteral <BigInteger>(value)); } } }
protected override NumericToken Evaluate(NumericToken r) => -- r;
public void Run(ICommandInteraction writer, [Values("set")] LiteralToken action, NumericToken number) { var machines = EmulationManager.Instance.CurrentEmulation.Machines.ToArray(); if (machines.Length > number.Value && number.Value >= 0) { SetCurrentMachine(machines[number.Value]); } else { writer.WriteError("Wrong machine number. Type {0} to show a list of available machines.".FormatWith(Name)); } }
public void Run(ICommandInteraction writer, StringToken message, [Values(-1L, 0L, 1L, 2L, 3L)] NumericToken level) { InnerLog((LogLevel)(int)level.Value, message.Value); }
public void Run([Values(-1L, 0L, 1L, 2L, 3L)] NumericToken level) { SetLogLevel((LogLevel)level.Value); }
public NumericToken SetUsed(NumericToken nt, bool used) { return(SetUsed(nt.NumericTokenId, used)); }
protected abstract NumericToken Evaluate(NumericToken l, NumericToken r);
public void Run(ICommandInteraction writer, [Values(TraceEnableCommand)] LiteralToken enable, LiteralToken cpuToken, StringToken functionName, BooleanToken traceReturn, NumericToken numberOfParameters) { Execute(writer, cpuToken, functionName.Value, traceReturn.Value, (int)numberOfParameters.Value); }
protected override NumericToken Evaluate(NumericToken l, NumericToken r) => l || r;
public IToken Take() { NumericToken nt = GetAvailable(); return(SetUsed(nt, true)); }