Beispiel #1
0
        private int PropertiesValidation(BlittableJsonToken rootTokenTypen, int mainPropOffsetSize, int mainPropIdSize,
                                         int objStartOffset, int numberOfPropsNames)
        {
            byte offset;
            var  numberOfProperties = ReadVariableSizeInt(_mem + objStartOffset, 0, out offset);
            var  current            = objStartOffset + offset;

            if (numberOfProperties < 0)
            {
                ThrowInvalidNumbeOfProperties();
            }

            for (var i = 1; i <= numberOfProperties; i++)
            {
                var propOffset = ReadNumber(_mem + current, mainPropOffsetSize);
                if ((propOffset > objStartOffset) || (propOffset < 0))
                {
                    ThrowInvalidPropertiesOffest();
                }
                current += mainPropOffsetSize;

                if (rootTokenTypen == BlittableJsonToken.StartObject)
                {
                    var id = ReadNumber(_mem + current, mainPropIdSize);
                    if ((id > numberOfPropsNames) || (id < 0))
                    {
                        ThrowInvalidPropertiesId();
                    }
                    current += mainPropIdSize;
                }

                int propOffsetSize;
                int propIdSize;
                var tokenType = TokenValidation(*(_mem + current), out propOffsetSize, out propIdSize);
                current++;

                var propValueOffset = objStartOffset - propOffset;

                switch (tokenType)
                {
                case BlittableJsonToken.StartObject:
                    PropertiesValidation(tokenType, propOffsetSize, propIdSize, propValueOffset, numberOfPropsNames);
                    break;

                case BlittableJsonToken.StartArray:
                    PropertiesValidation(tokenType, propOffsetSize, propIdSize, propValueOffset, numberOfPropsNames);
                    break;

                case BlittableJsonToken.Integer:
                    ReadVariableSizeLong(propValueOffset);
                    break;

                case BlittableJsonToken.LazyNumber:
                    var numberLength = ReadVariableSizeInt(propValueOffset, out byte lengthOffset);
                    var escCount     = ReadVariableSizeInt(propValueOffset + lengthOffset + numberLength, out byte escOffset);

                    // if number has any non-ascii symbols, we rull it out immediately
                    if (escCount > 0)
                    {
                        ThrowInvalidNumber(propValueOffset);
                    }

                    var numberCharsStart = _mem + objStartOffset - propOffset + lengthOffset;

                    // try and validate number using double's validation
                    if (_context.TryParseDouble(numberCharsStart, numberLength, out _) == false)
                    {
                        ThrowInvalidNumber(propValueOffset);
                    }
                    break;

                case BlittableJsonToken.String:
                    StringValidation(propValueOffset);
                    break;

                case BlittableJsonToken.CompressedString:
                    var stringLength           = ReadVariableSizeInt(propValueOffset, out offset);
                    var compressedStringLength = ReadVariableSizeInt(propValueOffset + offset, out offset);
                    if ((compressedStringLength > stringLength) ||
                        (compressedStringLength < 0) ||
                        (stringLength < 0))
                    {
                        ThrowInvalidCompressedString();
                    }
                    break;

                case BlittableJsonToken.Boolean:
                    var boolProp = ReadNumber(_mem + propValueOffset, 1);
                    if ((boolProp != 0) && (boolProp != 1))
                    {
                        ThrowInvalidBool();
                    }
                    break;

                case BlittableJsonToken.Null:
                    if (ReadNumber(_mem + propValueOffset, 1) != 0)
                    {
                        ThrowInvalidNull();
                    }
                    break;

                case BlittableJsonToken.EmbeddedBlittable:
                    byte offsetLen;
                    stringLength = ReadVariableSizeInt(propValueOffset, out offsetLen);
                    var blittableJsonReaderObject = new BlittableJsonReaderObject(_mem + propValueOffset + offsetLen, stringLength, _context);
                    blittableJsonReaderObject.BlittableValidation();
                    break;

                default:
                    ThrowInvalidTokenType();
                    break;
                }
            }
            return(current);
        }
Beispiel #2
0
        private int PropertiesValidation(BlittableJsonToken rootTokenTypen, int mainPropOffsetSize, int mainPropIdSize,
                                         int objStartOffset, int numberOfPropsNames)
        {
            byte offset;
            var  numberOfProperties = ReadVariableSizeInt(_mem + objStartOffset, 0, out offset);
            var  current            = objStartOffset + offset;

            if (numberOfProperties < 0)
            {
                ThrowInvalidNumbeOfProperties();
            }

            for (var i = 1; i <= numberOfProperties; i++)
            {
                var propOffset = ReadNumber(_mem + current, mainPropOffsetSize);
                if ((propOffset > objStartOffset) || (propOffset < 0))
                {
                    ThrowInvalidPropertiesOffest();
                }
                current += mainPropOffsetSize;

                if (rootTokenTypen == BlittableJsonToken.StartObject)
                {
                    var id = ReadNumber(_mem + current, mainPropIdSize);
                    if ((id > numberOfPropsNames) || (id < 0))
                    {
                        ThrowInvalidPropertiesId();
                    }
                    current += mainPropIdSize;
                }

                int propOffsetSize;
                int propIdSize;
                var tokenType = TokenValidation(*(_mem + current), out propOffsetSize, out propIdSize);
                current++;

                var propValueOffset = objStartOffset - propOffset;

                switch (tokenType)
                {
                case BlittableJsonToken.StartObject:
                    PropertiesValidation(tokenType, propOffsetSize, propIdSize, propValueOffset, numberOfPropsNames);
                    break;

                case BlittableJsonToken.StartArray:
                    PropertiesValidation(tokenType, propOffsetSize, propIdSize, propValueOffset, numberOfPropsNames);
                    break;

                case BlittableJsonToken.Integer:
                    ReadVariableSizeLong(propValueOffset);
                    break;

                case BlittableJsonToken.LazyNumber:
                    var floatLen          = ReadNumber(_mem + objStartOffset - propOffset, 1);
                    var floatStringBuffer = new string(' ', floatLen);
                    fixed(char *pChars = floatStringBuffer)
                    {
                        for (int j = 0; j < floatLen; j++)
                        {
                            pChars[j] = (char)ReadNumber((_mem + objStartOffset - propOffset + 1 + j), sizeof(byte));
                        }
                    }

                    double _double;
                    var    result = double.TryParse(floatStringBuffer, NumberStyles.Float, CultureInfo.InvariantCulture, out _double);
                    if (!(result))
                    {
                        ThrowInvalidDouble(floatStringBuffer);
                    }
                    break;

                case BlittableJsonToken.String:
                    StringValidation(propValueOffset);
                    break;

                case BlittableJsonToken.CompressedString:
                    var stringLength           = ReadVariableSizeInt(propValueOffset, out offset);
                    var compressedStringLength = ReadVariableSizeInt(propValueOffset + offset, out offset);
                    if ((compressedStringLength > stringLength) ||
                        (compressedStringLength < 0) ||
                        (stringLength < 0))
                    {
                        ThrowInvalidCompressedString();
                    }
                    break;

                case BlittableJsonToken.Boolean:
                    var boolProp = ReadNumber(_mem + propValueOffset, 1);
                    if ((boolProp != 0) && (boolProp != 1))
                    {
                        ThrowInvalidBool();
                    }
                    break;

                case BlittableJsonToken.Null:
                    if (ReadNumber(_mem + propValueOffset, 1) != 0)
                    {
                        ThrowInvalidNull();
                    }
                    break;

                case BlittableJsonToken.EmbeddedBlittable:
                    byte offsetLen;
                    stringLength = ReadVariableSizeInt(propValueOffset, out offsetLen);
                    var blittableJsonReaderObject = new BlittableJsonReaderObject(_mem + propValueOffset + offsetLen, stringLength, _context);
                    blittableJsonReaderObject.BlittableValidation();
                    break;

                default:
                    ThrowInvalidTokenType();
                    break;
                }
            }
            return(current);
        }