summaryrefslogtreecommitdiffstats
path: root/src/packet/FieldParser.cpp
blob: 500a97339560e36c53fd95a4c8ba3d316efe248e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "FieldParser.hpp"

Field FieldParser::Parse(FieldType type, byte *data, size_t len) {
    switch (type) {
        case VarInt:
            return ParseVarInt(data, len);
        case Boolean:
            return ParseBool(data, len);
        case String:
            return ParseString(data, len);
        case Long:
            return ParseLong(data, len);
        case Int:
            return ParseInt(data, len);
        case UnsignedByte:
            return ParseUByte(data, len);
        case Byte8_t:
            return ParseByte(data, len);
        case Float:
            return ParseFloat(data, len);
        case Position:
            return ParsePosition(data, len);
        case Double:
            return ParseDouble(data, len);
        case ByteArray:
            return ParseByteArray(data, len);
        default:
            throw 105;
    }
}

Field FieldParser::ParseString(byte *data, size_t len) {
    Field fLen = ParseVarInt(data, 0);
    Field f;
    f.SetRaw(data, fLen.GetLength() + fLen.GetVarInt(), String);
    return f;
}

Field FieldParser::ParseBool(byte *data, size_t len) {
    Field f;
    f.SetRaw(data,1,Boolean);
    return f;
}

Field FieldParser::ParseVarInt(byte *data, size_t len) {
    if (len != 0) {
        Field f;
        f.SetRaw(data, len, VarInt);
        return f;
    }
    int val = VarIntRead(data, len);
    Field f;
    f.SetVarInt(val);
    return f;
}

Field FieldParser::ParseLong(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 8, Long);
    return f;
}

Field FieldParser::ParseInt(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 4, Int);
    return f;
}

Field FieldParser::ParseUByte(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 1, UnsignedByte);
    return f;
}

Field FieldParser::ParseByte(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 1, Byte8_t);
    return f;
}

Field FieldParser::ParseFloat(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 4, Float);
    return f;
}

Field FieldParser::ParsePosition(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 8, Position);
    return f;
}

Field FieldParser::ParseDouble(byte *data, size_t len) {
    Field f;
    f.SetRaw(data, 8, Double);
    return f;
}

Field FieldParser::ParseByteArray(byte *data, size_t len) {
    if (len == 0)
        throw 119;
    Field f;
    f.SetRaw(data, len, Byte8_t);
    //f.SetRaw(data, len, ByteArray);
    return f;
}