Example #1
0
        public void ToHexVal_Test()
        {
            // input/output
            var chars             = "0123456789abcdef";
            var expectedHexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

            var sets = chars.Select((ch, i) => new Tuple <char, byte>(ch, expectedHexValues[i])).ToList();

            foreach (var t in sets)
            {
                var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1);
                Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}");
            }
        }
Example #2
0
 public void ToHexVal_WrongInputChar_Test()
 {
     TJSONProtocolHelper.ToHexVal((byte)'s');
 }
Example #3
0
        /// <summary>
        ///     Read in a JSON string, unescaping as appropriate.. Skip Reading from the
        ///     context if skipContext is true.
        /// </summary>
        private async ValueTask <byte[]> ReadJsonStringAsync(bool skipContext, CancellationToken cancellationToken)
        {
            using (var buffer = new MemoryStream())
            {
                var codeunits = new List <char>();


                if (!skipContext)
                {
                    await Context.ReadConditionalDelimiterAsync(cancellationToken);
                }

                await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Quote, cancellationToken);

                while (true)
                {
                    var ch = await Reader.ReadAsync(cancellationToken);

                    if (ch == TJSONProtocolConstants.Quote[0])
                    {
                        break;
                    }

                    // escaped?
                    if (ch != TJSONProtocolConstants.EscSequences[0])
                    {
                        await buffer.WriteAsync(new[] { ch }, 0, 1, cancellationToken);

                        continue;
                    }

                    // distinguish between \uXXXX and \?
                    ch = await Reader.ReadAsync(cancellationToken);

                    if (ch != TJSONProtocolConstants.EscSequences[1]) // control chars like \n
                    {
                        var off = Array.IndexOf(TJSONProtocolConstants.EscapeChars, (char)ch);
                        if (off == -1)
                        {
                            throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected control char");
                        }
                        ch = TJSONProtocolConstants.EscapeCharValues[off];
                        await buffer.WriteAsync(new[] { ch }, 0, 1, cancellationToken);

                        continue;
                    }

                    // it's \uXXXX
                    await Trans.ReadAllAsync(_tempBuffer, 0, 4, cancellationToken);

                    var wch = (short)((TJSONProtocolHelper.ToHexVal(_tempBuffer[0]) << 12) +
                                      (TJSONProtocolHelper.ToHexVal(_tempBuffer[1]) << 8) +
                                      (TJSONProtocolHelper.ToHexVal(_tempBuffer[2]) << 4) +
                                      TJSONProtocolHelper.ToHexVal(_tempBuffer[3]));

                    if (char.IsHighSurrogate((char)wch))
                    {
                        if (codeunits.Count > 0)
                        {
                            throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected low surrogate char");
                        }
                        codeunits.Add((char)wch);
                    }
                    else if (char.IsLowSurrogate((char)wch))
                    {
                        if (codeunits.Count == 0)
                        {
                            throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected high surrogate char");
                        }

                        codeunits.Add((char)wch);
                        var tmp = Utf8Encoding.GetBytes(codeunits.ToArray());
                        await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken);

                        codeunits.Clear();
                    }
                    else
                    {
                        var tmp = Utf8Encoding.GetBytes(new[] { (char)wch });
                        await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken);
                    }
                }

                if (codeunits.Count > 0)
                {
                    throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected low surrogate char");
                }

                return(buffer.ToArray());
            }
        }