Exemplo n.º 1
0
void MessagePackReader::ParseBin8()
{
    log_trace();
    if (is_.Eof())
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    ParseBin(is_.Get());
}
Exemplo n.º 2
0
void MessagePackReader::ParseInt8()
{
    log_trace();
    if (is_.Eof())
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    handler_->Int( static_cast<signed char>(is_.Get()) );
    ResetToken();
}
Exemplo n.º 3
0
void MessagePackReader::ParseBin16()
{
    log_trace();
    unsigned short length;
    if (is_.Read( reinterpret_cast<char*>(&length), 2 ) != 2)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    length = _msgpack_be16( length );
    ParseBin( length );
}
Exemplo n.º 4
0
void MessagePackReader::ParseMap16()
{
    log_trace();
    unsigned short length;
    if (is_.Read( reinterpret_cast<char*>(&length), 2 ) != 2)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    length = _msgpack_be16( length );  // compiling for mingw had problem when this was combined with the next line
    ParseMap( length );
}
Exemplo n.º 5
0
void MessagePackReader::ParseMap32()
{
    log_trace();
    unsigned length;
    if (is_.Read( reinterpret_cast<char*>(&length), 4 ) != 4)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    length = _msgpack_be32( length );
    ParseMap( length );
}
Exemplo n.º 6
0
void MessagePackReader::ParseKey()
{
    log_trace();
    if (is_.Eof())
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");

    GetToken();
    int length = -1;

    // only allow string keys although the MessagePack spec allows other types
    if ((token_ >= MessagePackFixStr) && (token_ < MessagePackNil))
        length = token_ & 0x1f;
    else if (token_ == MessagePackStr8)
    {
        if (is_.Eof())
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        length = (unsigned char)is_.Get();
    }
    else
        anyrpc_throw(AnyRpcErrorValueInvalid, "Invalid value");

    if (inSitu_)
    {
        // When parsing in place, mark the start position and continue with the decoding
        char* buffer = is_.PutBegin();
        if (length != is_.Skip(length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");

        GetClearToken();  // get the next token and clear the source, i.e. terminate the string
        handler_->Key(buffer,length,copy_);
        is_.PutEnd();
    }
    else if (length > 0)
    {
        // Key strings can only be up to 256 characters
        char str[257];
        if (is_.Read(str,length) != length)
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");

        str[length] = 0;
        handler_->Key(str,length);
        ResetToken();
    }
}
Exemplo n.º 7
0
void MessagePackReader::ParseInt64()
{
    log_trace();
    int64_t value;
    if (is_.Read( reinterpret_cast<char*>(&value), 8 ) != 8)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    value = _msgpack_be64( value );
    handler_->Int64( value );
    ResetToken();
}
Exemplo n.º 8
0
void MessagePackReader::ParseInt16()
{
    log_trace();
    short value;
    if (is_.Read( reinterpret_cast<char*>(&value), 2 ) != 2)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    value = _msgpack_be16( value );
    handler_->Int( static_cast<short>( value ) );
    ResetToken();
}
Exemplo n.º 9
0
void MessagePackReader::ParseUint32()
{
    log_trace();
    unsigned value;
    if (is_.Read( reinterpret_cast<char*>(&value), 4 ) != 4)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    value = _msgpack_be32( value );
    handler_->Uint( value );
    ResetToken();
}
Exemplo n.º 10
0
void JsonReader::ParseTrue()
{
    log_trace();
    if ((is_.Get() != 't') ||
        (is_.Get() != 'r') ||
        (is_.Get() != 'u') ||
        (is_.Get() != 'e'))
        anyrpc_throw(AnyRpcErrorValueInvalid, "Invalid value");

    handler_->BoolTrue();
}
Exemplo n.º 11
0
void JsonReader::ParseNull()
{
    log_trace();
    if ((is_.Get() != 'n') ||
        (is_.Get() != 'u') ||
        (is_.Get() != 'l') ||
        (is_.Get() != 'l'))
        anyrpc_throw(AnyRpcErrorValueInvalid, "Invalid value");

    handler_->Null();
}
Exemplo n.º 12
0
void MessagePackReader::ParseStr(size_t length)
{
    log_debug("MessagePackReader::ParseStr: length=" << length);
    if (inSitu_)
    {
        // When parsing in place, mark the start position and continue with the decoding
        char* buffer = is_.PutBegin();
        if (length != is_.Skip(length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        // Value expects all non-copied strings to be null terminated.
        // If you are already at the end of file, then you can't add the null terminator
        bool copy = copy_ || is_.Eof();
        GetClearToken();  // get the next token and clear the source, i.e. terminate the string
        handler_->String(buffer,length,copy);
        is_.PutEnd();
    }
    else if (length <= 256)
    {
        // Read short strings (probably the majority) onto the stack
        char buffer[257];
        if (length != is_.Read(buffer, length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        buffer[length] = 0;
        handler_->String(buffer,length);
        ResetToken();
    }
    else
    {
        // Long strings need an allocated buffer
        // It would be more efficient if the ownership of this buffer could be transferred to the handler
        char *buffer = static_cast<char*>(malloc(length+1));
        if (!buffer)
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        if (length != is_.Read(buffer, length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        buffer[length] = 0;
        handler_->String(buffer,length);
        ResetToken();
        free(buffer);
    }
}
Exemplo n.º 13
0
void JsonReader::ParseFalse()
{
    log_trace();
    if ((is_.Get() != 'f') ||
        (is_.Get() != 'a') ||
        (is_.Get() != 'l') ||
        (is_.Get() != 's') ||
        (is_.Get() != 'e'))
        anyrpc_throw(AnyRpcErrorValueInvalid, "Invalid value");

    handler_->BoolFalse();
}
Exemplo n.º 14
0
void MessagePackReader::ParseStream()
{
    log_trace();
    if ((token_ == 0) && is_.Eof())
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
    else
    {
        GetToken();
        log_debug("token=" << (int)token_);

        // look up the proper function based on the token and call
        (this->*ParseLookup[token_])();
    }
}
Exemplo n.º 15
0
void MessagePackReader::ParseBin(size_t length)
{
    log_debug("MessagePackReader::ParseBin, length=" << length);
    if (inSitu_)
    {
        // When parsing in place, mark the start position and continue with the decoding
        char* buffer = is_.PutBegin();
        if (length != is_.Skip(length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        handler_->Binary((unsigned char*)buffer,length,copy_);  // binary data is not null terminated so it can be referenced
        ResetToken();
        is_.PutEnd();
    }
    else if (length <= 256)
    {
        // Read short binary data onto the stack
        char buffer[256];
        if (length != is_.Read(buffer, length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        handler_->Binary((unsigned char*)buffer,length);
        ResetToken();
    }
    else
    {
        // Long binary data needs an allocated buffer
        // It would be more efficient if the ownership of this buffer could be transferred to the handler
        char *buffer = static_cast<char*>(malloc(length));
        if (!buffer)
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        if (length != is_.Read(buffer, length))
            anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");
        handler_->Binary((unsigned char*)buffer,length);
        ResetToken();
        free(buffer);
    }
}
Exemplo n.º 16
0
void  MessagePackReader::ParseFloat32()
{
    log_trace();
    union {
        float f;
        int i;
        char str[4];
    } value;

    if (is_.Read( value.str, 4 ) != 4)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");

    value.i = _msgpack_be32(value.i);
    handler_->Float( value.f );
    ResetToken();
}
Exemplo n.º 17
0
void MessagePackReader::ParseFloat64()
{
    log_trace();
    union {
        double d;
        int64_t i64;
        char str[8];
    } value;

    if (is_.Read( value.str, 8 ) != 8)
        anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated");

    value.i64 = _msgpack_be64(value.i64);
    handler_->Double( value.d );
    ResetToken();
}
Exemplo n.º 18
0
// This function is adapted from RapidJason (https://github.com/miloyip/rapidjson)
unsigned JsonReader::ParseHex4()
{
    unsigned codepoint = 0;
    for (int i=0; i<4; i++)
    {
        char c = is_.Get();
        codepoint <<= 4;
        if ((c >= '0') && (c <= '9'))
            codepoint += (c - '0');
        else if ((c >= 'A') && (c <= 'F'))
            codepoint += (c - 'A' + 10);
        else if ((c >= 'a') && (c <= 'f'))
            codepoint += (c - 'a' + 10);
        else
            anyrpc_throw(AnyRpcErrorStringUnicodeEscapeInvalid,
                    "Incorrect digit after escape in string");
    }
    return codepoint;
}
Exemplo n.º 19
0
// This function is adapted from RapidJason (https://github.com/miloyip/rapidjson)
void JsonReader::ParseStringToStream(Stream& os)
{
    log_trace();
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    static const char escape[256] = {
        Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/',
        Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0,
        0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0,
        0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16
    };
#undef Z16

    while (true)
    {
        char c = is_.Peek();
        if (c == '\\') // Escape
        {
            is_.Get();
            unsigned char e = is_.Get();
            if (escape[e])
            {
                os.Put(escape[e]);
            }
            else if (e == 'u')    // Unicode
            {
                unsigned codepoint = ParseHex4();
                if ((codepoint >= 0xD800) && (codepoint <= 0xDBff))
                {
                    // Handle as UTF-16 surrogate pair
                    if ((is_.Get() != '\\') || (is_.Get() != 'u'))
                        anyrpc_throw(AnyRpcErrorStringUnicodeSurrogateInvalid,
                                "The surrogate pair in string is invalid");
                    unsigned codepoint2 = ParseHex4();
                    if ((codepoint2 < 0xDC00) || (codepoint2 > 0xDFFF))
                        anyrpc_throw(AnyRpcErrorStringUnicodeSurrogateInvalid,
                                "The surrogate pair in string is invalid");
                    codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
                }
                EncodeUtf8(os,codepoint);
            }
            else
                anyrpc_throw(AnyRpcErrorStringEscapeInvalid,
                        "Invalid escape character in string");
        }
        else if (c == '"')     // Closing double quote
        {
            is_.Get();
            os.Put('\0');   // null-terminate the string
            return;
        }
        else if (c == '\0')
            anyrpc_throw(AnyRpcErrorStringMissingQuotationMark,
                    "Missing a closing quotation mark in string");
        else if ((unsigned)c < 0x20) // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
            anyrpc_throw(AnyRpcErrorStringEscapeInvalid,
                    "Invalid escape character in string");
        else
            os.Put( is_.Get() );
    }

}