Exemple #1
0
size_t Variant::deserialize(const unsigned char *buffer, size_t size)
{
    if (buffer != NULL && size > 0)
    {
        Variant::Type type = static_cast<Variant::Type>(*buffer);
        ++buffer;

        switch (type)
        {
            case UINT8:
            {
                typedef uint8_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint8(static_cast<Type>(*buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT8:
            {
                typedef uint8_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt8(static_cast<Type>(*buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case UINT16:
            {
                typedef uint16_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint16(ntohs(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT16:
            {
                typedef int16_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt16(ntohs(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case UINT32:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint32(ntohl(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT32:
            {
                typedef int32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt32(ntohl(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case UINT64:
            {
                typedef uint64_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint64(ntohll(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT64:
            {
                typedef int64_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt64(ntohll(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case FLOAT:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setFloat(ntohf(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case DOUBLE:
            {
                typedef uint64_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setDouble(ntohd(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }
                break;
            }

            case BOOL:
            {
                typedef bool Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setBool(*reinterpret_cast<const Type *>(buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case CHAR:
            {
                typedef char Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setChar(*reinterpret_cast<const Type *>(buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case STRING:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    Type len = ntohl(*reinterpret_cast<const Type *>(buffer));

                    if (len > 0 && len <= ((size - 1) - sizeof(Type)))
                        setString(reinterpret_cast<const char *>(buffer + sizeof(Type)), len);

                    size = sizeof(Type) + 1 + len;
                }

                break;
            }

            case BINARY:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    Type len = ntohl(*reinterpret_cast<const Type *>(buffer));

                    if (len > 0 && len <= ((size - 1) - sizeof(Type)))
                        setBinary(reinterpret_cast<const unsigned char *>(buffer + sizeof(Type)), len);

                    size = sizeof(Type) + 1 + len;
                }

                break;
            }
        }

        return size;
    }

    return 0;
}
Exemple #2
0
bool CLuaBinary::setStruct(luabridge::LuaRef objVal, luabridge::LuaRef objAttr)
{
    assert(objVal.isTable());
    assert(objAttr.isTable());

    bool bOk = false;
    DataType emType;
    int iSize = Q_INIT_NUMBER;
    std::string strName;

    for (int i = 1; i <= objAttr.length(); i++)
    {
        luabridge::LuaRef objTmp = objAttr[i];
        if (!objTmp.isTable()
            || STATTRTAB_MINLENS > objTmp.length())
        {
            return false;
        }

        luabridge::LuaRef objChildStAttr = getStructAttr(objTmp, strName, emType, iSize);
        luabridge::LuaRef objFieldVal = objVal[strName];

        switch(emType)
        {
        case DTYPE_SINT8:
            {
                bOk = setSint8(getLuaNumber<short>(objFieldVal));
            }
            break;
        case DTYPE_UINT8:
            {
                bOk = setUint8(getLuaNumber<unsigned short>(objFieldVal));
            }
            break;
        case DTYPE_BOOL:
            {
                bOk = setBool(((Q_INIT_NUMBER != getLuaNumber<unsigned short>(objFieldVal)) ? true : false));
            }
            break;
        case DTYPE_SINT16:
            {
                bOk = setSint16(getLuaNumber<short>(objFieldVal));
            }
            break;
        case DTYPE_UINT16:
            {
                bOk = setUint16(getLuaNumber<unsigned short>(objFieldVal));
            }
            break;
        case DTYPE_SINT32:
            {
                bOk = setSint32(getLuaNumber<int>(objFieldVal));
            }
            break;
        case DTYPE_UINT32:
            {
                bOk = setUint32(getLuaNumber<unsigned int>(objFieldVal));
            }
            break;
        case DTYPE_SINT64:
            {
                bOk = setSint64(Q_ToString(getLuaNumber<int64_t>(objFieldVal)).c_str());
            }
            break;
        case DTYPE_UINT64:
            {
                bOk = setUint64(Q_ToString(getLuaNumber<uint64_t>(objFieldVal)).c_str());
            }
            break;
        case DTYPE_FLOAT:
            {
                bOk = setFloat(getLuaNumber<float>(objFieldVal));
            }
            break;
        case DTYPE_DOUBLE:
            {
                bOk = setDouble(getLuaNumber<double>(objFieldVal));
            }
            break;
        case DTYPE_STRING:
            {
                if (!objFieldVal.isString())
                {
                    bOk = false;
                    break;
                }

                const char *pszVal = objFieldVal.cast<const char*>();
                if (objFieldVal.length() >= iSize)
                {
                    bOk = false;
                    Q_Printf("%s", "data len too large.");
                    break;
                }
               
                bOk = setString(pszVal);
                if (bOk)
                {
                    bOk = skipWrite(iSize - (objFieldVal.length() + 1));
                }
            }
            break;
        case DTYPE_BYTE:
            {
                if (!objFieldVal.isString())
                {
                    bOk = false;
                    break;
                }

                const char *pszVal = objFieldVal.cast<const char*>();
                if (objFieldVal.length() > iSize)
                {
                    bOk = false;
                    Q_Printf("%s", "data len too large.");
                    break;
                }

                bOk = setByte(pszVal, objFieldVal.length());
                if (bOk)
                {
                    bOk = skipWrite(iSize - objFieldVal.length());
                }
            }
            break;
        case DTYPE_STRUCT:
            {
                if (!objFieldVal.isTable()
                    || !objChildStAttr.isTable())
                {
                    bOk = false;
                    break;
                }

                bOk = setStruct(objFieldVal, objChildStAttr);
            }
            break;
        case DTYPE_SKIP:
            {
                if (iSize > Q_INIT_NUMBER)
                {
                    bOk = skipWrite(iSize);
                }
            }
            break;

        default:
            bOk = false;
            break;
        }

        if (!bOk)
        {
            return bOk;
        }
    }

    return true;
}