internal static ReadOnlySequence <byte> SegmentInto(ReadOnlyMemory <byte> data, int segmentCount) { if (segmentCount < 2) { throw new ArgumentOutOfRangeException(nameof(segmentCount)); } int perSegment = data.Length / segmentCount; BufferSegment <byte> first; if (perSegment == 0 && data.Length > 0) { first = new BufferSegment <byte>(data.Slice(0, 1)); data = data.Slice(1); } else { first = new BufferSegment <byte>(data.Slice(0, perSegment)); data = data.Slice(perSegment); } BufferSegment <byte> last = first; segmentCount--; while (segmentCount > 1) { perSegment = data.Length / segmentCount; last = last.Append(data.Slice(0, perSegment)); data = data.Slice(perSegment); segmentCount--; } last = last.Append(data); return(new ReadOnlySequence <byte>(first, 0, last, data.Length)); }
public static bool TryGetBinaryValue( ReadOnlyMemory <byte> binaryToken, out ReadOnlyMemory <byte> binaryValue) { binaryValue = default; if (binaryToken.Length < 1) { return(false); } byte typeMarker = binaryToken.Span[0]; // trim off the type marker binaryToken = binaryToken.Slice(1); uint length; switch (typeMarker) { case JsonBinaryEncoding.TypeMarker.Binary1ByteLength: if (binaryToken.Length < JsonBinaryEncoding.OneByteLength) { return(false); } length = MemoryMarshal.Read <byte>(binaryToken.Span); binaryToken = binaryToken.Slice(JsonBinaryEncoding.OneByteLength); break; case JsonBinaryEncoding.TypeMarker.Binary2ByteLength: if (binaryToken.Length < JsonBinaryEncoding.TwoByteLength) { return(false); } length = MemoryMarshal.Read <ushort>(binaryToken.Span); binaryToken = binaryToken.Slice(JsonBinaryEncoding.TwoByteLength); break; case JsonBinaryEncoding.TypeMarker.Binary4ByteLength: if (binaryToken.Length < JsonBinaryEncoding.FourByteLength) { return(false); } length = MemoryMarshal.Read <uint>(binaryToken.Span); binaryToken = binaryToken.Slice(JsonBinaryEncoding.FourByteLength); break; default: return(false); } if (length > int.MaxValue) { return(false); } if (binaryToken.Length < length) { return(false); } binaryValue = binaryToken.Slice(0, (int)length); return(true); }
protected static ValueTask ProcessNextAsync(HttpMessage message, ReadOnlyMemory <HttpPipelinePolicy> pipeline) { return(pipeline.Span[0].ProcessAsync(message, pipeline.Slice(1))); }
protected override ReadOnlyMemory <byte> PreprocessData(ReadOnlyMemory <byte> data) { return(data.Slice(4)); }
internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory <byte> rebind, out Rfc3161TimeStampReq decoded) { decoded = default; AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); AsnValueReader defaultReader; AsnValueReader collectionReader; ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (!sequenceReader.TryReadInt32(out decoded.Version)) { sequenceReader.ThrowIfNotEmpty(); } System.Security.Cryptography.Pkcs.Asn1.MessageImprint.Decode(ref sequenceReader, rebind, out decoded.MessageImprint); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.ObjectIdentifier)) { decoded.ReqPolicy = sequenceReader.ReadObjectIdentifier(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Integer)) { tmpSpan = sequenceReader.ReadIntegerBytes(); decoded.Nonce = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Boolean)) { decoded.CertReq = sequenceReader.ReadBoolean(); } else { defaultReader = new AsnValueReader(DefaultCertReq, AsnEncodingRules.DER); decoded.CertReq = defaultReader.ReadBoolean(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { // Decode SEQUENCE OF for Extensions { collectionReader = sequenceReader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); var tmpList = new List <System.Security.Cryptography.Asn1.X509ExtensionAsn>(); System.Security.Cryptography.Asn1.X509ExtensionAsn tmpItem; while (collectionReader.HasData) { System.Security.Cryptography.Asn1.X509ExtensionAsn.Decode(ref collectionReader, rebind, out tmpItem); tmpList.Add(tmpItem); } decoded.Extensions = tmpList.ToArray(); } } sequenceReader.ThrowIfNotEmpty(); }
// used in Compute public static void ParseAuthorizationToken( string authorizationTokenString, out ReadOnlyMemory <char> typeOutput, out ReadOnlyMemory <char> versionOutput, out ReadOnlyMemory <char> tokenOutput) { typeOutput = default; versionOutput = default; tokenOutput = default; if (string.IsNullOrEmpty(authorizationTokenString)) { DefaultTrace.TraceError("Auth token missing"); throw new UnauthorizedException(RMResources.MissingAuthHeader); } if (authorizationTokenString.Length > AuthorizationHelper.MaxAuthorizationHeaderSize) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } authorizationTokenString = HttpUtility.UrlDecode(authorizationTokenString); // Format of the token being deciphered is // type=<master/resource/system>&ver=<version>&sig=<base64encodedstring> // Step 1. split the tokens into type/ver/token. // when parsing for the last token, I use , as a separator to skip any redundant authorization headers ReadOnlyMemory <char> authorizationToken = authorizationTokenString.AsMemory(); int typeSeparatorPosition = authorizationToken.Span.IndexOf('&'); if (typeSeparatorPosition == -1) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } ReadOnlyMemory <char> authType = authorizationToken.Slice(0, typeSeparatorPosition); authorizationToken = authorizationToken.Slice(typeSeparatorPosition + 1, authorizationToken.Length - typeSeparatorPosition - 1); int versionSepartorPosition = authorizationToken.Span.IndexOf('&'); if (versionSepartorPosition == -1) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } ReadOnlyMemory <char> version = authorizationToken.Slice(0, versionSepartorPosition); authorizationToken = authorizationToken.Slice(versionSepartorPosition + 1, authorizationToken.Length - versionSepartorPosition - 1); ReadOnlyMemory <char> token = authorizationToken; int tokenSeparatorPosition = authorizationToken.Span.IndexOf(','); if (tokenSeparatorPosition != -1) { token = authorizationToken.Slice(0, tokenSeparatorPosition); } // Step 2. For each token, split to get the right half of '=' // Additionally check for the left half to be the expected scheme type int typeKeyValueSepartorPosition = authType.Span.IndexOf('='); if (typeKeyValueSepartorPosition == -1 || !authType.Span.Slice(0, typeKeyValueSepartorPosition).SequenceEqual(Constants.Properties.AuthSchemaType.AsSpan()) || !authType.Span.Slice(0, typeKeyValueSepartorPosition).ToString().Equals(Constants.Properties.AuthSchemaType, StringComparison.OrdinalIgnoreCase)) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } ReadOnlyMemory <char> authTypeValue = authType.Slice(typeKeyValueSepartorPosition + 1); int versionKeyValueSeparatorPosition = version.Span.IndexOf('='); if (versionKeyValueSeparatorPosition == -1 || !version.Span.Slice(0, versionKeyValueSeparatorPosition).SequenceEqual(Constants.Properties.AuthVersion.AsSpan()) || !version.Slice(0, versionKeyValueSeparatorPosition).ToString().Equals(Constants.Properties.AuthVersion, StringComparison.OrdinalIgnoreCase)) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } ReadOnlyMemory <char> versionValue = version.Slice(versionKeyValueSeparatorPosition + 1); int tokenKeyValueSeparatorPosition = token.Span.IndexOf('='); if (tokenKeyValueSeparatorPosition == -1 || !token.Slice(0, tokenKeyValueSeparatorPosition).Span.SequenceEqual(Constants.Properties.AuthSignature.AsSpan()) || !token.Slice(0, tokenKeyValueSeparatorPosition).ToString().Equals(Constants.Properties.AuthSignature, StringComparison.OrdinalIgnoreCase)) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } ReadOnlyMemory <char> tokenValue = token.Slice(tokenKeyValueSeparatorPosition + 1); if (authTypeValue.IsEmpty || versionValue.IsEmpty || tokenValue.IsEmpty) { throw new UnauthorizedException(RMResources.InvalidAuthHeaderFormat); } typeOutput = authTypeValue; versionOutput = versionValue; tokenOutput = tokenValue; }
// {{else if a=b}} {{else}} {{/name}} // ^ // returns ^ ^ static ReadOnlyMemory <char> ParseElseStatement(this ReadOnlyMemory <char> literal, string blockName, out PageElseBlock statement) { var inStatements = 0; var pos = 0; statement = null; var statementPos = -1; var elseExpr = default(ReadOnlyMemory <char>); while (true) { pos = literal.IndexOf("{{", pos); if (pos == -1) { throw new SyntaxErrorException($"End block for 'else' not found."); } var c = literal.SafeGetChar(pos + 2); if (c == '#') { inStatements++; pos = literal.IndexOf("}}", pos) + 2; //end of expression } else if (c == '/') { if (inStatements == 0) { literal.Slice(pos + 2 + 1).ParseVarName(out var name); if (name.EqualsOrdinal(blockName)) { var body = ParseTemplatePage(literal.Slice(statementPos, pos - statementPos).TrimFirstNewLine()); statement = new PageElseBlock(elseExpr, body); return(literal.Slice(pos)); } } inStatements--; } else if (literal.Slice(pos + 2).StartsWith("else")) { if (inStatements == 0) { if (statementPos >= 0) { var bodyText = literal.Slice(statementPos, pos - statementPos).TrimFirstNewLine(); var body = ParseTemplatePage(bodyText); statement = new PageElseBlock(elseExpr, body); return(literal.Slice(pos)); } var endExprPos = literal.IndexOf("}}", pos); if (endExprPos == -1) { throw new SyntaxErrorException($"End expression for 'else' not found."); } var exprStartPos = pos + 2 + 4; //= {{else... elseExpr = literal.Slice(exprStartPos, endExprPos - exprStartPos).Trim(); statementPos = endExprPos + 2; } } pos += 2; } }
public static TryCatch <PartitionMapping <PartitionedToken> > TryGetInitializationInfo <PartitionedToken>( IReadOnlyList <PartitionKeyRange> partitionKeyRanges, IReadOnlyList <PartitionedToken> partitionedContinuationTokens) where PartitionedToken : IPartitionedToken { if (partitionKeyRanges == null) { throw new ArgumentNullException(nameof(partitionKeyRanges)); } if (partitionedContinuationTokens == null) { throw new ArgumentNullException(nameof(partitionedContinuationTokens)); } if (partitionKeyRanges.Count < 1) { throw new ArgumentException(nameof(partitionKeyRanges)); } if (partitionedContinuationTokens.Count < 1) { throw new ArgumentException(nameof(partitionKeyRanges)); } if (partitionedContinuationTokens.Count > partitionKeyRanges.Count) { throw new ArgumentException($"{nameof(partitionedContinuationTokens)} can not have more elements than {nameof(partitionKeyRanges)}."); } // Find the continuation token for the partition we left off on: PartitionedToken firstContinuationToken = partitionedContinuationTokens .OrderBy((partitionedToken) => partitionedToken.PartitionRange.Min) .First(); // Segment the ranges based off that: ReadOnlyMemory <PartitionKeyRange> sortedRanges = partitionKeyRanges .OrderBy((partitionKeyRange) => partitionKeyRange.MinInclusive) .ToArray(); PartitionKeyRange firstContinuationRange = new PartitionKeyRange { MinInclusive = firstContinuationToken.PartitionRange.Min, MaxExclusive = firstContinuationToken.PartitionRange.Max }; int matchedIndex = sortedRanges.Span.BinarySearch( firstContinuationRange, Comparer <PartitionKeyRange> .Create((range1, range2) => string.CompareOrdinal(range1.MinInclusive, range2.MinInclusive))); if (matchedIndex < 0) { return(TryCatch <PartitionMapping <PartitionedToken> > .FromException( new MalformedContinuationTokenException( $"{RMResources.InvalidContinuationToken} - Could not find continuation token: {firstContinuationToken}"))); } ReadOnlyMemory <PartitionKeyRange> partitionsLeftOfTarget = matchedIndex == 0 ? ReadOnlyMemory <PartitionKeyRange> .Empty : sortedRanges.Slice(start: 0, length: matchedIndex); ReadOnlyMemory <PartitionKeyRange> targetPartition = sortedRanges.Slice(start: matchedIndex, length: 1); ReadOnlyMemory <PartitionKeyRange> partitionsRightOfTarget = matchedIndex == sortedRanges.Length - 1 ? ReadOnlyMemory <PartitionKeyRange> .Empty : sortedRanges.Slice(start: matchedIndex + 1); // Create the continuation token mapping for each region. IReadOnlyDictionary <PartitionKeyRange, PartitionedToken> mappingForPartitionsLeftOfTarget = MatchRangesToContinuationTokens( partitionsLeftOfTarget, partitionedContinuationTokens); IReadOnlyDictionary <PartitionKeyRange, PartitionedToken> mappingForTargetPartition = MatchRangesToContinuationTokens( targetPartition, partitionedContinuationTokens); IReadOnlyDictionary <PartitionKeyRange, PartitionedToken> mappingForPartitionsRightOfTarget = MatchRangesToContinuationTokens( partitionsRightOfTarget, partitionedContinuationTokens); return(TryCatch <PartitionMapping <PartitionedToken> > .FromResult( new PartitionMapping <PartitionedToken>( partitionsLeftOfTarget : mappingForPartitionsLeftOfTarget, targetPartition : mappingForTargetPartition, partitionsRightOfTarget : mappingForPartitionsRightOfTarget))); }
public static List <PageFragment> ParseTemplate(this ScriptContext context, ReadOnlyMemory <char> text) { var to = new List <PageFragment>(); if (text.IsNullOrWhiteSpace()) { return(to); } int pos; var lastPos = 0; int nextPos() { var c1 = text.IndexOf("{{", lastPos); var c2 = text.IndexOf("{|", lastPos); if (c2 == -1) { return(c1); } return(c1 == -1 ? c2 : c1 < c2 ? c1 : c2); } while ((pos = nextPos()) != -1) { var block = text.Slice(lastPos, pos - lastPos); if (!block.IsNullOrEmpty()) { to.Add(new PageStringFragment(block)); } var varStartPos = pos + 2; if (varStartPos >= text.Span.Length) { throw new SyntaxErrorException($"Unterminated '{{{{' expression, near '{text.Slice(lastPos).DebugLiteral()}'"); } if (text.Span.SafeCharEquals(varStartPos - 1, '|')) // lang expression syntax {|lang ... |} https://flow.org/en/docs/types/objects/#toc-exact-object-types { var literal = text.Slice(varStartPos); ScriptLanguage lang = null; if (literal.SafeGetChar(0).IsValidVarNameChar()) { literal = literal.ParseVarName(out var langSpan); lang = context.GetScriptLanguage(langSpan.ToString()); if (lang != null) { var endPos = literal.IndexOf("|}"); if (endPos == -1) { throw new SyntaxErrorException($"Unterminated '|}}' expression, near '{text.Slice(varStartPos).DebugLiteral()}'"); } var exprStr = literal.Slice(0, endPos); var langExprFragment = lang.Parse(context, exprStr); to.AddRange(langExprFragment); } } if (lang == null) { var nextLastPos = text.IndexOf("|}", varStartPos) + 2; block = text.Slice(pos, nextLastPos - pos); if (!block.IsNullOrEmpty()) { to.Add(new PageStringFragment(block)); } } lastPos = text.IndexOf("|}", varStartPos) + 2; continue; } var firstChar = text.Span[varStartPos]; if (firstChar == '*') //comment { lastPos = text.IndexOf("*}}", varStartPos) + 3; if (text.Span.SafeCharEquals(lastPos, '\r')) { lastPos++; } if (text.Span.SafeCharEquals(lastPos, '\n')) { lastPos++; } } else if (firstChar == '#') //block statement { var literal = text.Slice(varStartPos + 1); literal = literal.ParseTemplateScriptBlock(context, out var blockFragment); var length = text.Length - pos - literal.Length; blockFragment.OriginalText = text.Slice(pos, length); lastPos = pos + length; to.Add(blockFragment); } else { var literal = text.Slice(varStartPos).Span; literal = literal.ParseJsExpression(out var expr, filterExpression: true); var filters = new List <JsCallExpression>(); if (!literal.StartsWith("}}")) { literal = literal.AdvancePastWhitespace(); if (literal.FirstCharEquals(FilterSep)) { literal = literal.AdvancePastPipeOperator(); while (true) { literal = literal.ParseJsCallExpression(out var filter, filterExpression: true); filters.Add(filter); literal = literal.AdvancePastWhitespace(); if (literal.IsNullOrEmpty()) { throw new SyntaxErrorException("Unterminated filter expression"); } if (literal.StartsWith("}}")) { literal = literal.Advance(2); break; } if (!literal.FirstCharEquals(FilterSep)) { throw new SyntaxErrorException( $"Expected pipeline operator '|>' but was {literal.DebugFirstChar()}"); } literal = literal.AdvancePastPipeOperator(); } } else if (!literal.AdvancePastWhitespace().IsNullOrEmpty()) { throw new SyntaxErrorException($"Unexpected syntax '{literal.ToString()}', Expected pipeline operator '|>'"); } } else { literal = literal.Advance(2); } var length = text.Length - pos - literal.Length; var originalText = text.Slice(pos, length); lastPos = pos + length; var varFragment = new PageVariableFragment(originalText, expr, filters); to.Add(varFragment); var newLineLen = literal.StartsWith("\n") ? 1 : literal.StartsWith("\r\n") ? 2 : 0; if (newLineLen > 0) { var lastExpr = varFragment.FilterExpressions?.LastOrDefault(); var filterName = lastExpr?.Name ?? varFragment?.InitialExpression?.Name ?? varFragment.Binding; if ((filterName != null && context.RemoveNewLineAfterFiltersNamed.Contains(filterName)) || expr is JsVariableDeclaration) { lastPos += newLineLen; } } } } if (lastPos != text.Length) { var lastBlock = lastPos == 0 ? text : text.Slice(lastPos); to.Add(new PageStringFragment(lastBlock)); } return(to); }
public static List <PageFragment> ParseScript(this ScriptContext context, ReadOnlyMemory <char> text) { var to = new List <PageFragment>(); ScriptLanguage scriptLanguage = null; ReadOnlyMemory <char> modifiers = default; ReadOnlyMemory <char> prevBlock = default; int startBlockPos = -1; var cursorPos = 0; var lastBlockPos = 0; const int delim = 3; // '```'.length while (text.TryReadLine(out var line, ref cursorPos)) { var lineLength = line.Length; line = line.AdvancePastWhitespace(); if (line.StartsWith("```")) { if (scriptLanguage != null && startBlockPos >= 0 && line.Slice(delim).AdvancePastWhitespace().IsEmpty) //is end block { var templateFragments = ScriptTemplate.Language.Parse(context, prevBlock); to.AddRange(templateFragments); var blockBody = text.ToLineStart(cursorPos, lineLength, startBlockPos); var blockFragments = scriptLanguage.Parse(context, blockBody, modifiers); to.AddRange(blockFragments); prevBlock = default; startBlockPos = -1; scriptLanguage = null; modifiers = null; lastBlockPos = cursorPos; continue; } if (line.SafeGetChar(delim).IsValidVarNameChar()) { line = line.Slice(delim).ParseVarName(out var blockNameSpan); var blockName = blockNameSpan.ToString(); scriptLanguage = context.GetScriptLanguage(blockName); if (scriptLanguage == null) { continue; } modifiers = line.AdvancePastChar('|'); var delimLen = text.Span.SafeCharEquals(cursorPos - 2, '\r') ? 2 : 1; prevBlock = text.Slice(lastBlockPos, cursorPos - lastBlockPos - lineLength - delimLen); startBlockPos = cursorPos; } } } var remainingBlock = text.Slice(lastBlockPos); if (!remainingBlock.IsEmpty) { var templateFragments = ScriptTemplate.Language.Parse(context, remainingBlock); to.AddRange(templateFragments); } return(to); }
public FSMMatch <N> Run(ReadOnlyMemory <char> source, LexerPosition lexerPosition) { ConsumeIgnored(source, lexerPosition); // End of token stream if (lexerPosition.Index >= source.Length) { return(new FSMMatch <N>(false)); } // Make a note of where current token starts var position = lexerPosition.Clone(); FSMMatch <N> result = null; var currentNode = Nodes[0]; while (lexerPosition.Index < source.Length) { var currentCharacter = source.At(lexerPosition); var currentValue = source.Slice(position.Index, lexerPosition.Index - position.Index + 1); currentNode = Move(currentNode, currentCharacter, currentValue); if (currentNode == null) { // No more viable transitions, so exit loop break; } if (currentNode.IsEnd) { // Remember the possible match result = new FSMMatch <N>(true, currentNode.Value, currentValue, position, currentNode.Id, lexerPosition, currentNode.IsLineEnding); } lexerPosition.Index++; lexerPosition.Column++; } if (result != null) { // Backtrack var length = result.Result.Value.Length; lexerPosition.Index = result.Result.Position.Index + length; lexerPosition.Column = result.Result.Position.Column + length; if (HasCallback(result.NodeId)) { result = Callbacks[result.NodeId](result); } return(result); } if (lexerPosition.Index >= source.Length) { // Failed on last character, so need to backtrack lexerPosition.Index -= 1; lexerPosition.Column -= 1; } var errorChar = source.Slice(lexerPosition.Index, 1); var ko = new FSMMatch <N>(false, default(N), errorChar, lexerPosition, -1, lexerPosition, false); return(ko); }
private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory <byte> rebind, out SubjectPublicKeyInfoAsn decoded) { decoded = default; AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref sequenceReader, rebind, out decoded.Algorithm); if (sequenceReader.TryReadPrimitiveBitString(out _, out tmpSpan)) { decoded.SubjectPublicKey = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else { decoded.SubjectPublicKey = sequenceReader.ReadBitString(out _); } sequenceReader.ThrowIfNotEmpty(); }
private static (ReadOnlyMemory <char> baseTypeName, List <ReadOnlyMemory <char> >?options) ParseTypeName(ReadOnlyMemory <char> typeName) { var typeNameSpan = typeName.Span; var pOpenIdx = typeNameSpan.IndexOf('('); if (pOpenIdx == 0) { throw new ClickHouseException(ClickHouseErrorCodes.InvalidTypeName, $"The name of the type (\"{typeNameSpan.ToString()}\") can't start with \"(\"."); } ReadOnlyMemory <char> baseTypeName; List <ReadOnlyMemory <char> >?options = null; if (pOpenIdx < 0) { baseTypeName = typeName.Trim(); } else { baseTypeName = typeName.Slice(0, pOpenIdx).Trim(); int count = 1; int currentIdx = pOpenIdx; int optionStartIdx = pOpenIdx + 1; ReadOnlySpan <char> significantChars = "(,)"; do { if (typeNameSpan.Length - 1 == currentIdx) { break; } var pNextIdx = typeNameSpan.Slice(currentIdx + 1).IndexOfAny(significantChars); if (pNextIdx < 0) { break; } pNextIdx += currentIdx + 1; currentIdx = pNextIdx; if (typeNameSpan[currentIdx] == '(') { ++count; } else if (typeNameSpan[currentIdx] == ')') { --count; if (count == 0) { break; } } else if (count == 1) { var currentOption = typeName.Slice(optionStartIdx, currentIdx - optionStartIdx).Trim(); optionStartIdx = currentIdx + 1; if (options != null) { options.Add(currentOption); } else { options = new List <ReadOnlyMemory <char> >(2) { currentOption } }; } } while (true); if (count != 0) { throw new ClickHouseException(ClickHouseErrorCodes.InvalidTypeName, $"The number of open parentheses doesn't match to the number of close parentheses in the type name \"{typeNameSpan.ToString()}\"."); } if (currentIdx != typeNameSpan.Length - 1) { var unexpectedString = typeNameSpan.Slice(currentIdx + 1); if (!unexpectedString.Trim().IsEmpty) { throw new ClickHouseException( ClickHouseErrorCodes.InvalidTypeName, $"There are unexpected characters (\"{unexpectedString.ToString()}\") in the type name \"{typeNameSpan.ToString()}\" after closing parenthesis."); } } var lastOption = typeName.Slice(optionStartIdx, currentIdx - optionStartIdx).Trim(); if (options != null) { options.Add(lastOption); } else { options = new List <ReadOnlyMemory <char> >(1) { lastOption } }; } return(baseTypeName, options); }
public static object ReadFieldValue(ReadOnlyMemory <byte> memory, out int bytesRead) { bytesRead = 1; ReadOnlyMemory <byte> slice = memory.Slice(1); switch ((char)memory.Span[0]) { case 'S': byte[] result = ReadLongstr(slice); bytesRead += result.Length + 4; return(result); case 'I': bytesRead += 4; return(NetworkOrderDeserializer.ReadInt32(slice)); case 'i': bytesRead += 4; return(NetworkOrderDeserializer.ReadUInt32(slice)); case 'D': bytesRead += 5; return(ReadDecimal(slice)); case 'T': bytesRead += 8; return(ReadTimestamp(slice)); case 'F': IDictionary <string, object> tableResult = ReadTable(slice, out int tableBytesRead); bytesRead += tableBytesRead; return(tableResult); case 'A': IList arrayResult = ReadArray(slice, out int arrayBytesRead); bytesRead += arrayBytesRead; return(arrayResult); case 'B': bytesRead += 1; return(slice.Span[0]); case 'b': bytesRead += 1; return((sbyte)slice.Span[0]); case 'd': bytesRead += 8; return(NetworkOrderDeserializer.ReadDouble(slice)); case 'f': bytesRead += 4; return(NetworkOrderDeserializer.ReadSingle(slice)); case 'l': bytesRead += 8; return(NetworkOrderDeserializer.ReadInt64(slice)); case 's': bytesRead += 2; return(NetworkOrderDeserializer.ReadInt16(slice)); case 't': bytesRead += 1; return(slice.Span[0] != 0); case 'x': byte[] binaryTableResult = ReadLongstr(slice); bytesRead += binaryTableResult.Length + 4; return(new BinaryTableValue(binaryTableResult)); case 'V': return(null); default: throw new SyntaxError($"Unrecognised type in table: {(char)memory.Span[0]}"); } }
private void ParsePrimitiveBitStringContents( ReadOnlyMemory <byte> source, out int unusedBitCount, out ReadOnlyMemory <byte> value, out byte normalizedLastByte) { // T-REC-X.690-201508 sec 9.2 if (RuleSet == AsnEncodingRules.CER && source.Length > MaxCERSegmentSize) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } // T-REC-X.690-201508 sec 8.6.2.3 if (source.Length == 0) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } ReadOnlySpan <byte> sourceSpan = source.Span; unusedBitCount = sourceSpan[0]; // T-REC-X.690-201508 sec 8.6.2.2 if (unusedBitCount > 7) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } if (source.Length == 1) { // T-REC-X.690-201508 sec 8.6.2.4 if (unusedBitCount > 0) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } Debug.Assert(unusedBitCount == 0); value = ReadOnlyMemory <byte> .Empty; normalizedLastByte = 0; return; } // Build a mask for the bits that are used so the normalized value can be computed // // If 3 bits are "unused" then build a mask for them to check for 0. // -1 << 3 => 0b1111_1111 << 3 => 0b1111_1000 int mask = -1 << unusedBitCount; byte lastByte = sourceSpan[sourceSpan.Length - 1]; byte maskedByte = (byte)(lastByte & mask); if (maskedByte != lastByte) { // T-REC-X.690-201508 sec 11.2.1 if (RuleSet == AsnEncodingRules.DER || RuleSet == AsnEncodingRules.CER) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } } normalizedLastByte = maskedByte; value = source.Slice(1); }
internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory <byte> rebind, out RecipientKeyIdentifier decoded) { decoded = default; AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (sequenceReader.TryReadPrimitiveOctetStringBytes(out tmpSpan)) { decoded.SubjectKeyIdentifier = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else { decoded.SubjectKeyIdentifier = sequenceReader.ReadOctetString(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.GeneralizedTime)) { decoded.Date = sequenceReader.ReadGeneralizedTime(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Sequence)) { System.Security.Cryptography.Pkcs.Asn1.OtherKeyAttributeAsn tmpOther; System.Security.Cryptography.Pkcs.Asn1.OtherKeyAttributeAsn.Decode(ref sequenceReader, rebind, out tmpOther); decoded.Other = tmpOther; } sequenceReader.ThrowIfNotEmpty(); }
static bool crypto_aead_chacha20poly1305_ietf_decrypt(Memory <byte> m, ReadOnlyMemory <byte> c, ReadOnlyMemory <byte> ad, ReadOnlyMemory <byte> npub, ReadOnlyMemory <byte> k) { return(crypto_aead_chacha20poly1305_ietf_decrypt_detached(m, c.Slice(0, c.Length - AeadChaCha20Poly1305MacSize), c.Slice(c.Length - AeadChaCha20Poly1305MacSize, AeadChaCha20Poly1305MacSize), ad, npub, k)); }
internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory <byte> rebind, out DistributionPointNameAsn decoded) { decoded = default; Asn1Tag tag = reader.PeekTag(); AsnValueReader collectionReader; ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { // Decode SEQUENCE OF for FullName { collectionReader = reader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); var tmpList = new List <System.Security.Cryptography.Asn1.GeneralNameAsn>(); System.Security.Cryptography.Asn1.GeneralNameAsn tmpItem; while (collectionReader.HasData) { System.Security.Cryptography.Asn1.GeneralNameAsn.Decode(ref collectionReader, rebind, out tmpItem); tmpList.Add(tmpItem); } decoded.FullName = tmpList.ToArray(); } } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { tmpSpan = reader.ReadEncodedValue(); decoded.NameRelativeToCRLIssuer = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else { throw new CryptographicException(); } }
public static List <PageFragment> ParseTemplatePage(ReadOnlyMemory <char> text) { var to = new List <PageFragment>(); if (text.IsNullOrWhiteSpace()) { return(to); } int pos; var lastPos = 0; while ((pos = text.IndexOf("{{", lastPos)) != -1) { var block = text.Slice(lastPos, pos - lastPos); if (!block.IsNullOrEmpty()) { to.Add(new PageStringFragment(block)); } var varStartPos = pos + 2; var firstChar = text.Span[varStartPos]; if (firstChar == '*') //comment { lastPos = text.IndexOf("*}}", varStartPos) + 3; } else if (firstChar == '#') //block statement { var literal = text.Slice(varStartPos + 1); literal = literal.ParseVarName(out var blockNameSpan); var blockName = blockNameSpan.ToString(); var endExprPos = literal.IndexOf("}}"); if (endExprPos == -1) { throw new SyntaxErrorException($"Unterminated '{blockName}' block expression, near '{literal.DebugLiteral()}'"); } var blockExpr = literal.Slice(0, endExprPos).Trim(); literal = literal.Advance(endExprPos + 2); if (!ScriptConfig.DontEvaluateBlocksNamed.Contains(blockName)) { literal = literal.ParseStatementBody(blockNameSpan, out var body); var elseStatements = new List <PageElseBlock>(); while (literal.StartsWith("{{else")) { literal = literal.ParseElseStatement(blockName, out var elseStatement); elseStatements.Add(elseStatement); } literal = literal.Advance(2 + 1 + blockName.Length + 2); //remove new line after partial block end tag literal = literal.TrimFirstNewLine(); var length = text.Length - pos - literal.Length; var originalText = text.Slice(pos, length); lastPos = pos + length; var statement = new PageBlockFragment(originalText, blockName, blockExpr, body, elseStatements); to.Add(statement); } else { var endBlock = "{{/" + blockName + "}}"; var endBlockPos = literal.IndexOf(endBlock); if (endBlockPos == -1) { throw new SyntaxErrorException($"Unterminated end block '{endBlock}'"); } var endBlockBody = literal.Slice(0, endBlockPos); literal = literal.Advance(endBlockPos + endBlock.Length).TrimFirstNewLine(); var body = new List <PageFragment> { new PageStringFragment(endBlockBody) }; var length = text.Length - pos - literal.Length; var originalText = text.Slice(pos, length); lastPos = pos + length; var statement = new PageBlockFragment(originalText, blockName, blockExpr, body); to.Add(statement); } } else { var literal = text.Slice(varStartPos).Span; literal = literal.ParseJsExpression(out var expr, filterExpression: true); var filters = new List <JsCallExpression>(); if (!literal.StartsWith("}}")) { literal = literal.AdvancePastWhitespace(); if (literal.FirstCharEquals(FilterSep)) { literal = literal.Advance(1); while (true) { literal = literal.ParseJsCallExpression(out var filter, filterExpression: true); filters.Add(filter); literal = literal.AdvancePastWhitespace(); if (literal.IsNullOrEmpty()) { throw new SyntaxErrorException("Unterminated filter expression"); } if (literal.StartsWith("}}")) { literal = literal.Advance(2); break; } if (!literal.FirstCharEquals(FilterSep)) { throw new SyntaxErrorException( $"Expected filter separator '|' but was {literal.DebugFirstChar()}"); } literal = literal.Advance(1); } } else { if (!literal.IsNullOrEmpty()) { literal = literal.Advance(1); } } } else { literal = literal.Advance(2); } var length = text.Length - pos - literal.Length; var originalText = text.Slice(pos, length); lastPos = pos + length; var varFragment = new PageVariableFragment(originalText, expr, filters); to.Add(varFragment); var newLineLen = literal.StartsWith("\n") ? 1 : literal.StartsWith("\r\n") ? 2 : 0; if (newLineLen > 0) { var lastExpr = varFragment.FilterExpressions?.LastOrDefault(); var filterName = lastExpr?.Name ?? varFragment?.InitialExpression?.Name ?? varFragment.Binding; if (filterName != null && ScriptConfig.RemoveNewLineAfterFiltersNamed.Contains(filterName)) { lastPos += newLineLen; } } } } if (lastPos != text.Length) { var lastBlock = lastPos == 0 ? text : text.Slice(lastPos); to.Add(new PageStringFragment(lastBlock)); } return(to); }
private static ReadOnlyMemory <byte> Depad(ReadOnlyMemory <byte> ciphertext, int padSize) { var offset = ciphertext.Length - TwoBlockSizes + padSize; return(ciphertext.Slice(offset, BlockSize)); }
public static ReadOnlyMemory <char> Chop(this ReadOnlyMemory <char> literal, char c) => literal.IsEmpty || literal.Span[literal.Length - 1] != c ? literal : literal.Slice(0, literal.Length - 1);
public TFrameList Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return((TFrameList)(IList <T>)null); } Interlocked.Increment(ref ParallelGatekeeperSingleton.wrapperDepth); try { options.Security.DepthStep(ref reader); try { FrameFormatterSerializationOptions frameOptions = options.GetOptionParams(); if (frameOptions.MthWorkerConfig.MaxConcurrentTasks == 1 || ParallelGatekeeperSingleton.wrapperDepth > 1) { return(DeserializeSynchronous(ref reader, options)); } var readerBackup = reader.CreatePeekReader(); int count = reader.ReadArrayHeader(); if (count == 0) { reader = readerBackup; return(DeserializeSynchronous(ref reader, options)); } var peekreader = reader.CreatePeekReader(); if (FrameItemFormatter <T> .ReadElementHeader(ref peekreader) == Frame <T> .unassigned) { if (frameOptions.ThrowOnUnnasignedFrameDeserialization) { throw new StreamSerializationException($"Unassigned buffer length found during parallel deserialize for {nameof(TFrameList)}"); } reader = readerBackup; return(DeserializeSynchronous(ref reader, options)); } IMessagePackFormatter <T> formatterT = options.Resolver.GetFormatterWithVerify <T>(); ListFrameWrapper valueWrapper = GetTFrameListWrapper(count); Frame <T>[] resItems = valueWrapper.AsFrameArray(); BatchSizeEstimator batchEstimator = new BatchSizeEstimator(frameOptions.BatchSizeEstimatorConfig); void ProcessBatch(BatchWithBufferWritersAndElementOffset batch, CancellationToken token) { try { ReadOnlySpan <int> lengths = batch.buffers.lengths.WrittenSpan; ReadOnlyMemory <byte> bodies = batch.buffers.concatenatedBodies.WrittenMemory; int batchSize = batch.buffers.lengths.WrittenCount; var destSpan = resItems.AsSpan(batch.offset, batchSize); for (int ix = 0, bodyStartIx = 0; ix < batchSize; ix++) { int itemLen = lengths[ix]; ReadOnlyMemory <byte> body = bodies.Slice(bodyStartIx, itemLen); MessagePackReader tmpReader = new MessagePackReader(body) { CancellationToken = token }; destSpan[ix].BufferLength = body.Length; destSpan[ix].Item = formatterT.Deserialize(ref tmpReader, options); bodyStartIx += itemLen; } } finally { objPoolBufferWriterBodies.Return(batch.buffers.concatenatedBodies); objPoolBufferWriterBodyLengths.Return(batch.buffers.lengths); } } using (var mtw = new MultiThreadedWorker <BatchWithBufferWritersAndElementOffset>( frameOptions.MthWorkerConfig, ProcessBatch)) { int i = 0; while (i < count) { int batchSize = Math.Min(count - i, batchEstimator.RecomendedBatchSize); var currentBatch = new BatchWithBufferWritersAndElementOffset() { offset = i, buffers = new BatchWithBufferWriters() { concatenatedBodies = objPoolBufferWriterBodies.Get(), lengths = objPoolBufferWriterBodyLengths.Get() } }; for (int seqIx = 0; seqIx < batchSize; seqIx++) { int itemLength = FrameItemFormatter <T> .ReadElementHeader(ref reader); if (itemLength == Frame <T> .unassigned) { throw new StreamSerializationException($"Unassigned buffer length found during parallel deserialize for {nameof(TFrameList)}"); } currentBatch.buffers.lengths.GetSpan(1)[0] = itemLength; currentBatch.buffers.lengths.Advance(1); ReadOnlySequence <byte> raw = reader.ReadRaw(itemLength); raw.CopyTo(currentBatch.buffers.concatenatedBodies.GetSpan(itemLength)); currentBatch.buffers.concatenatedBodies.Advance(itemLength); batchEstimator.UpdateEstimate(itemLength); } mtw.AddWorkItem(currentBatch, reader.CancellationToken); i += batchSize; } } return(valueWrapper.AsFrameList()); } finally { reader.Depth--; } } finally { Interlocked.Decrement(ref ParallelGatekeeperSingleton.wrapperDepth); } }
public NwkMMPayload(byte ti, bool f, ReadOnlyMemory <byte> data) : base(NwkProtocolDiscriminator.MM, ti, f, data.Slice(1)) { Type = (NwkMMMessageType)data.Span[0]; switch (Type) { case NwkMMMessageType.AuthenticationRequest: case NwkMMMessageType.AuthenticationReply: case NwkMMMessageType.KeyAllocate: case NwkMMMessageType.AuthenticationReject: case NwkMMMessageType.AccessRightsRequest: case NwkMMMessageType.AccessRightsAccept: case NwkMMMessageType.AccessRightsReject: case NwkMMMessageType.AccessRightsTerminateRequest: case NwkMMMessageType.AccessRightsTerminateAccept: case NwkMMMessageType.AccessRightsTerminateReject: case NwkMMMessageType.CipherRequest: case NwkMMMessageType.CipherSuggest: case NwkMMMessageType.CipherReject: case NwkMMMessageType.MMInfoRequest: case NwkMMMessageType.MMInfoAccept: case NwkMMMessageType.MMInfoSuggest: case NwkMMMessageType.MMInfoReject: case NwkMMMessageType.LocateRequest: case NwkMMMessageType.LocateAccept: case NwkMMMessageType.Detach: case NwkMMMessageType.LocateReject: case NwkMMMessageType.IdentityRequest: case NwkMMMessageType.IdentityReply: case NwkMMMessageType.MMIwu: case NwkMMMessageType.TemporaryIdentityAssign: case NwkMMMessageType.TemporaryIdentityAssignAck: case NwkMMMessageType.TemporaryIdentityAssignRej: case NwkMMMessageType.MMNotify: break; default: throw new ArgumentOutOfRangeException(); } }
internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory <byte> rebind, out SignerIdentifierAsn decoded) { decoded = default; Asn1Tag tag = reader.PeekTag(); ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (tag.HasSameClassAndValue(Asn1Tag.Sequence)) { System.Security.Cryptography.Pkcs.Asn1.IssuerAndSerialNumberAsn tmpIssuerAndSerialNumber; System.Security.Cryptography.Pkcs.Asn1.IssuerAndSerialNumberAsn.Decode(ref reader, rebind, out tmpIssuerAndSerialNumber); decoded.IssuerAndSerialNumber = tmpIssuerAndSerialNumber; } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (reader.TryReadPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out tmpSpan)) { decoded.SubjectKeyIdentifier = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else { decoded.SubjectKeyIdentifier = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } else { throw new CryptographicException(); } }
public static HttpRequestHeader ReadHeader(ReadOnlyMemory <byte> buffer, int position, int length) { #if NETSTANDARD2_0 var chars = encoding.GetChars(buffer.Span.Slice(0, position).ToArray()); var charsLength = chars.Length; #else var chars = BufferArrayPool <char> .Rent(encoding.GetMaxCharCount(position)); try { var charsLength = encoding.GetChars(buffer.Span.Slice(0, position), chars.AsSpan()); #endif (var declarations, var headers) = ParseHeaders(chars.AsSpan().Slice(0, charsLength)); if (declarations.Count == 0 || headers.Count == 0) { throw new Exception("Invalid Header"); } var headerInfo = new HttpRequestHeader() { Declarations = declarations, Headers = headers }; headerInfo.IsError = declarations[0].StartsWith(serverErrorResponse); if (headers.TryGetValue(contentTypeHeader, out IList <string> contentTypeHeaderValue)) { if (String.Equals(contentTypeHeaderValue[0], contentTypeBytes, StringComparison.InvariantCultureIgnoreCase)) { headerInfo.ContentType = ContentType.Bytes; } else if (String.Equals(contentTypeHeaderValue[0], contentTypeJson, StringComparison.InvariantCultureIgnoreCase)) { headerInfo.ContentType = ContentType.Json; } else if (String.Equals(contentTypeHeaderValue[0], contentTypeJsonNameless, StringComparison.InvariantCultureIgnoreCase)) { headerInfo.ContentType = ContentType.JsonNameless; } else { throw new Exception("Invalid Header"); } } if (headers.TryGetValue(contentLengthHeader, out IList <string> contentLengthHeaderValue)) { if (int.TryParse(contentLengthHeaderValue[0], out int contentLengthHeaderValueParsed)) { headerInfo.ContentLength = contentLengthHeaderValueParsed; } } if (headers.TryGetValue(transferEncodingHeader, out IList <string> transferEncodingHeaderValue)) { if (transferEncodingHeaderValue[0] == transferEncodingChunked) { headerInfo.Chuncked = true; } } if (headers.TryGetValue(providerTypeHeader, out IList <string> providerTypeHeaderValue)) { headerInfo.ProviderType = providerTypeHeaderValue[0]; } if (headers.TryGetValue(originHeader, out IList <string> originHeaderValue)) { headerInfo.Origin = originHeaderValue[0]; } if (declarations.Count > 0 && declarations[0] == optionsHeader) { headerInfo.Preflight = true; } if (headers.TryGetValue(RelayServiceHeader, out IList <string> relayServiceHeaderValue)) { if (relayServiceHeaderValue[0] == RelayServiceAdd) { headerInfo.RelayServiceAddRemove = true; } else if (relayServiceHeaderValue[0] == RelayServiceRemove) { headerInfo.RelayServiceAddRemove = false; } } if (headers.TryGetValue(RelayKeyHeader, out IList <string> relayKeyHeaderValue)) { headerInfo.RelayKey = relayKeyHeaderValue[0]; } headerInfo.BodyStartBuffer = buffer.Slice(position, length - position); return(headerInfo); #if !NETSTANDARD2_0 } finally { BufferArrayPool <char> .Return(chars); } #endif }
public void Invoke(int index) { uint addressOffset = (uint)index; uint rawInstruction = BinaryPrimitives.ReadUInt32BigEndian(Data.Slice(index * 4, 4).Span); uint virtualAddress = File.VirtualAddress + addressOffset * 4; var instruction = new Instruction(rawInstruction, virtualAddress, addressOffset); if (!Disassembler.IsAddressInDataRegionOrUndefined(virtualAddress)) { // REGIMM if (instruction.OpCode == 1) { // Rely on compiler optimization. ReadOnlySpan <byte> branchCodes = new byte[] { 0, 1, 2, 3, 8, 9, 10, 11, 12, 14, 16, 17, 18, 19 }; if (branchCodes.Contains((byte)instruction.RT)) { // The jump target is a relative amount. This signed offset is stored in IMMSigned. // We need to multiply by 4 (because each segment is 4 bits). We then add our virtual // address to get our final jump offset. The instruction jumps to the instruction following // the branch, not the branch itself, so we need to add 4. // Here we convert it to an absolute jump. uint jumpTarget = instruction.GetAbsoluteJumpAddress(); // If we have a jump target we need to create a label for it. Multiple locations can jump to a single label, // so we only need to add the label to the global list once (using ISet). var label = new RomLabel(jumpTarget); if (Disassembler.Labels.TryGetValue(label.Address, out var existingLabel)) { if (!existingLabel.Name.Equals(label.Name)) { throw new InvalidOperationException($"An attempt was made to declare label {label.Name} at pre-claimed address {label.Address} (existing label = {existingLabel.Name})"); } // The label already exists. Nothing to see here. } else { lock (Disassembler.Labels) { if (Disassembler.Labels.TryGetValue(label.Address, out existingLabel)) { if (!existingLabel.Name.Equals(label.Name)) { throw new InvalidOperationException($"An attempt was made to declare label {label.Name} at pre-claimed address {label.Address} (existing label = {existingLabel.Name})"); } // The label already exists. Nothing to see here. } else { Disassembler.Labels.Add(label.Address, label); } } } } } // J and JAL else if (instruction.OpCode == 2 || instruction.OpCode == 3) { uint functionAddress = instruction.Address * 4 + (virtualAddress & 0xF0000000); var function = new RomFunction(functionAddress, Disassembler.GetFunctionName(functionAddress)); if (Disassembler.Functions.TryGetValue(function.Address, out var existingFunction)) { if (!existingFunction.Name.Equals(function.Name)) { throw new InvalidOperationException($"An attempt was made to declare function {function.Name} at pre-claimed address {function.Address} (existing func = {existingFunction.Name})"); } // The function already exists. Nothing to see here. } else { lock (Disassembler.Functions) { if (Disassembler.Functions.TryGetValue(function.Address, out existingFunction)) { if (!existingFunction.Name.Equals(function.Name)) { throw new InvalidOperationException($"An attempt was made to declare function {function.Name} at pre-claimed address {function.Address} (existing func = {existingFunction.Name})"); } // The function already exists. Nothing to see here. } else { Disassembler.Functions.Add(function.Address, function); } } } } // BEQ, BNE, BLEZ, BGTZ, BEQL, BNEL, BLEZL, BGTZL else if (instruction.OpCode == 4 || instruction.OpCode == 5 || instruction.OpCode == 6 || instruction.OpCode == 7 || instruction.OpCode == 20 || instruction.OpCode == 21 || instruction.OpCode == 22 || instruction.OpCode == 23) { // The jump target is a relative amount. This signed offset is stored in IMMSigned. // We need to multiply by 4 (because each segment is 4 bits). We then add our virtual // address to get our final jump offset. The instruction jumps to the instruction following // the branch, not the branch itself, so we need to add 4. // Here we convert it to an absolute jump. uint jumpTarget = instruction.GetAbsoluteJumpAddress(); // If we have a jump target we need to create a label for it. Multiple locations can jump to a single label, // so we only need to add the label to the global list once (using ISet). var label = new RomLabel(jumpTarget); if (Disassembler.Labels.TryGetValue(label.Address, out var existingLabel)) { if (!existingLabel.Name.Equals(label.Name)) { throw new InvalidOperationException($"An attempt was made to declare label {label.Name} at pre-claimed address {label.Address} (existing label = {existingLabel.Name})"); } // The label already exists. Nothing to see here. } else { lock (Disassembler.Labels) { if (Disassembler.Labels.TryGetValue(label.Address, out existingLabel)) { if (!existingLabel.Name.Equals(label.Name)) { throw new InvalidOperationException($"An attempt was made to declare label {label.Name} at pre-claimed address {label.Address} (existing label = {existingLabel.Name})"); } // The label already exists. Nothing to see here. } else { Disassembler.Labels.Add(label.Address, label); } } } } // LUI else if (instruction.OpCode == 15) { Disassembler.DetermineLoadRef(File, addressOffset); } // COPROCESSOR 1 else if (instruction.OpCode == 17) { // BC1F, BC1T, BC1FL, BC1TL if (instruction.RT == 0 || instruction.RT == 1 || instruction.RT == 2 || instruction.RT == 3) { // The jump target is a relative amount. This signed offset is stored in IMMSigned. // We need to multiply by 4 (because each segment is 4 bits). We then add our virtual // address to get our final jump offset. The instruction jumps to the instruction following // the branch, not the branch itself, so we need to add 4. // Here we convert it to an absolute jump. uint jumpTarget = instruction.GetAbsoluteJumpAddress(); // If we have a jump target we need to create a label for it. Multiple locations can jump to a single label, // so we only need to add the label to the global list once (using ISet). var label = new RomLabel(jumpTarget); if (Disassembler.Labels.TryGetValue(label.Address, out var existingLabel)) { if (!existingLabel.Name.Equals(label.Name)) { throw new InvalidOperationException($"An attempt was made to declare label {label.Name} at pre-claimed address {label.Address} (existing label = {existingLabel.Name})"); } // The label already exists. Nothing to see here. } else { lock (Disassembler.Labels) { if (Disassembler.Labels.TryGetValue(label.Address, out existingLabel)) { if (!existingLabel.Name.Equals(label.Name)) { throw new InvalidOperationException($"An attempt was made to declare label {label.Name} at pre-claimed address {label.Address} (existing label = {existingLabel.Name})"); } // The label already exists. Nothing to see here. } else { Disassembler.Labels.Add(label.Address, label); } } } } } } if (Disassembler.Variables.ContainsKey(virtualAddress)) { string name = Disassembler.GetLoadName(virtualAddress); if (name.StartsWith("__switch")) { int addr = index; uint caseAddr = BinaryPrimitives.ReadUInt32BigEndian(Data.Slice(addr * 4, 4).Span); while (Disassembler.IsAddressInCodeRegion(caseAddr)) { lock (Disassembler.SwitchCases) { Disassembler.SwitchCases.Add(caseAddr); } ++addr; if (addr >= File.Length / 4) { break; } caseAddr = BinaryPrimitives.ReadUInt32BigEndian(Data.Slice(addr * 4, 4).Span); } } } }
public static string GetMimeType(ReadOnlyMemory <byte> file, string fileName) { var mime = "application/octet-stream"; var extension = string.Empty; if (!string.IsNullOrWhiteSpace(fileName)) { string pathExtension = Path.GetExtension(fileName); extension = pathExtension.ToUpper(); } if (file.Slice(0, 3).ToArray().SequenceEqual(MP3)) { mime = "audio/mpeg"; } else if (file.Slice(0, 14).ToArray().SequenceEqual(OGG)) { if (extension == ".OGX") { mime = "application/ogg"; } else if (extension == ".OGA") { mime = "audio/ogg"; } else { mime = "video/ogg"; } } else if (file.Slice(0, 16).ToArray().SequenceEqual(PNG)) { mime = "image/png"; } else if (file.Slice(0, 3).ToArray().SequenceEqual(JPG)) { mime = "image/jpeg"; } else if (file.Slice(0, 2).ToArray().SequenceEqual(BMP)) { mime = "image/bmp"; } else if (file.Slice(0, 8).ToArray().SequenceEqual(DOC)) { mime = "application/msword"; } else if (file.Slice(0, 2).ToArray().SequenceEqual(EXE_DLL)) { mime = "application/x-msdownload"; //both use same mime type } else if (file.Slice(0, 4).ToArray().SequenceEqual(GIF)) { mime = "image/gif"; } else if (file.Slice(0, 4).ToArray().SequenceEqual(ICO)) { mime = "image/x-icon"; } else if (file.Slice(0, 7).ToArray().SequenceEqual(PDF)) { mime = "application/pdf"; } else if (file.Slice(0, 7).ToArray().SequenceEqual(RAR)) { mime = "application/x-rar-compressed"; } else if (file.Slice(0, 3).ToArray().SequenceEqual(SWF)) { mime = "application/x-shockwave-flash"; } else if (file.Slice(0, 4).ToArray().SequenceEqual(TIFF)) { mime = "image/tiff"; } else if (file.Slice(0, 11).ToArray().SequenceEqual(TORRENT)) { mime = "application/x-bittorrent"; } else if (file.Slice(0, 5).ToArray().SequenceEqual(TTF)) { mime = "application/x-font-ttf"; } else if (file.Slice(0, 4).ToArray().SequenceEqual(WAV_AVI)) { mime = extension == ".AVI" ? "video/x-msvideo" : "audio/x-wav"; } else if (file.Slice(0, 16).ToArray().SequenceEqual(WMV_WMA)) { mime = extension == ".WMA" ? "audio/x-ms-wma" : "video/x-ms-wmv"; } else if (file.Slice(0, 4).ToArray().SequenceEqual(ZIP_DOCX)) { mime = extension == ".DOCX" ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/x-zip-compressed"; } return(mime); }
private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory <byte> rebind, out SignedDataAsn decoded) { decoded = default; AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); AsnValueReader collectionReader; ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (!sequenceReader.TryReadInt32(out decoded.Version)) { sequenceReader.ThrowIfNotEmpty(); } // Decode SEQUENCE OF for DigestAlgorithms { collectionReader = sequenceReader.ReadSetOf(); var tmpList = new List <System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn>(); System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn tmpItem; while (collectionReader.HasData) { System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref collectionReader, rebind, out tmpItem); tmpList.Add(tmpItem); } decoded.DigestAlgorithms = tmpList.ToArray(); } System.Security.Cryptography.Pkcs.Asn1.EncapsulatedContentInfoAsn.Decode(ref sequenceReader, rebind, out decoded.EncapContentInfo); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { // Decode SEQUENCE OF for CertificateSet { collectionReader = sequenceReader.ReadSetOf(new Asn1Tag(TagClass.ContextSpecific, 0)); var tmpList = new List <System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn>(); System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn tmpItem; while (collectionReader.HasData) { System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn.Decode(ref collectionReader, rebind, out tmpItem); tmpList.Add(tmpItem); } decoded.CertificateSet = tmpList.ToArray(); } } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { // Decode SEQUENCE OF for Crls { collectionReader = sequenceReader.ReadSetOf(new Asn1Tag(TagClass.ContextSpecific, 1)); var tmpList = new List <ReadOnlyMemory <byte> >(); ReadOnlyMemory <byte> tmpItem; while (collectionReader.HasData) { tmpSpan = collectionReader.ReadEncodedValue(); tmpItem = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); tmpList.Add(tmpItem); } decoded.Crls = tmpList.ToArray(); } } // Decode SEQUENCE OF for SignerInfos { collectionReader = sequenceReader.ReadSetOf(); var tmpList = new List <System.Security.Cryptography.Pkcs.Asn1.SignerInfoAsn>(); System.Security.Cryptography.Pkcs.Asn1.SignerInfoAsn tmpItem; while (collectionReader.HasData) { System.Security.Cryptography.Pkcs.Asn1.SignerInfoAsn.Decode(ref collectionReader, rebind, out tmpItem); tmpList.Add(tmpItem); } decoded.SignerInfos = tmpList.ToArray(); } sequenceReader.ThrowIfNotEmpty(); }
protected static void ProcessNext(HttpMessage message, ReadOnlyMemory <HttpPipelinePolicy> pipeline) { pipeline.Span[0].Process(message, pipeline.Slice(1)); }
public override async ValueTask WriteAsync(ReadOnlyMemory <byte> buffer, CancellationToken cancellationToken = default) { using (TrySNIEventScope.Create(nameof(SslOverTdsStream))) { if (!_encapsulate) { { ValueTask valueTask = _stream.WriteAsync(buffer, cancellationToken); if (!valueTask.IsCompletedSuccessfully) { await valueTask.ConfigureAwait(false); } } Task flushTask = _stream.FlushAsync(); if (flushTask.IsCompletedSuccessfully) { await flushTask.ConfigureAwait(false); } return; } ReadOnlyMemory <byte> remaining = buffer; byte[] packetBuffer = null; try { while (remaining.Length > 0) { int dataLength = Math.Min(PACKET_SIZE_WITHOUT_HEADER, remaining.Length); int packetLength = TdsEnums.HEADER_LEN + dataLength; if (packetBuffer == null) { packetBuffer = ArrayPool <byte> .Shared.Rent(packetLength); } else if (packetBuffer.Length < packetLength) { ArrayPool <byte> .Shared.Return(packetBuffer, clearArray : true); packetBuffer = ArrayPool <byte> .Shared.Rent(packetLength); } SetupPreLoginPacketHeader(packetBuffer, dataLength, remaining.Length - dataLength); remaining.Span.Slice(0, dataLength).CopyTo(packetBuffer.AsSpan(TdsEnums.HEADER_LEN, dataLength)); { ValueTask packetWriteValueTask = _stream.WriteAsync(new ReadOnlyMemory <byte>(packetBuffer, 0, packetLength), cancellationToken); if (!packetWriteValueTask.IsCompletedSuccessfully) { await packetWriteValueTask.ConfigureAwait(false); } } await _stream.FlushAsync().ConfigureAwait(false); remaining = remaining.Slice(dataLength); } } finally { if (packetBuffer != null) { ArrayPool <byte> .Shared.Return(packetBuffer, clearArray : true); } } } }