summaryrefslogtreecommitdiffstats
path: root/pb_decode.c
blob: 884cf18e4addf092174c3efee9d0a5d7d705f1c4 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* pb_decode.c -- decode a protobuf using callback functions
 *
 * 2011 Petteri Aimonen <jpa@kapsi.fi>
 */

#include "pb_decode.h"

const pb_decoder_t PB_DECODERS[PB_LAST_ACT] = {
    NULL,
    &pb_dec_uint32,
    &pb_dec_sint32,
    &pb_dec_uint32, // Cast to int32
    &pb_dec_fixed32,
    &pb_dec_fixed32, // Cast to int32
    &pb_dec_uint64,
    &pb_dec_sint64,
    &pb_dec_uint64, // Cast to int64
    &pb_dec_fixed64,
    &pb_dec_fixed64, // Cast to int64
    &pb_dec_bool,
    &pb_dec_float,
    &pb_dec_double,
    &pb_dec_bytes,
    &pb_dec_string,
    &pb_dec_submessage
};

enum wire_type {
    WT_VARINT = 0,
    WT_64BIT  = 1,
    WT_STRING = 2,
    WT_32BIT  = 5
};

// Note: pb_decode_varint32 is a bit un-orthodox:
// it will refuse to decode values that exceed uint32 range.
// The Google implementation would simply cast to 32 bits.
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
{
    char byte;
    int bitpos = 0;
    *dest = 0;
    
    while (bitpos < 32 && pb_read(stream, &byte, 1))
    {
        *dest |= (byte & 0x7F) << bitpos;
        bitpos += 7;
        
        if (!(byte & 0x80))
            return true;
    }
    
    return false;
}

bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest)
{
    char byte;
    int bitpos = 0;
    *dest = 0;
    
    while (bitpos < 64 && pb_read(stream, &byte, 1))
    {
        *dest |= (byte & 0x7F) << bitpos;
        bitpos += 7;
        
        if (!(byte & 0x80))
            return true;
    }
    
    return false;
}

bool pb_skip_varint(pb_istream_t *stream)
{
    char byte;
    do
    {
        if (!pb_read(stream, &byte, 1))
            return false;
    } while (byte & 0x80);
    return true;
}

bool pb_skip_string(pb_istream_t *stream)
{
    uint32_t length;
    if (!pb_decode_varint32(stream, &length))
        return false;
    
    return pb_read(stream, NULL, length);
}

bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest)
{
    while (stream->bytes_left)
    {
        uint32_t temp;
        if (!pb_decode_varint32(stream, &temp))
            return false;
        
        int field_number = temp >> 3;
        int wire_type = temp & 7;
        
        const pb_field_t *field = fields;
        while (field->field_number != 0)
        {
            if (field->field_number != field_number)
            {
                field++;
                continue;
            }
            
            void *destfield = dest + field->offset; 
            
            if (field->action == PB_ACT_HAS)
            {
                *(bool*)destfield = true;
                field++;
                continue;
            }
            
            pb_decoder_t func = PB_DECODERS[field->action];
            if (!func(stream, field, destfield))
                return false;
            
            break;
        }
        
        if (field->field_number == 0) // No match found, skip data
        {
            bool status = false;
            switch (wire_type)
            {
                case WT_VARINT:
                    status = pb_skip_varint(stream);
                    break;
                
                case WT_64BIT:
                    status = pb_read(stream, NULL, 8);
                    break;
                
                case WT_STRING:
                    status = pb_skip_string(stream);
                    break;
                
                case WT_32BIT:
                    status = pb_read(stream, NULL, 4);
                    break;
            }
            
            if (!status)
                return false;
        }
    }
    
    return true;
}

bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    return pb_decode_varint32(stream, (uint32_t*)dest);
}

bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    uint32_t *x = (uint32_t*)dest;
    bool status = pb_decode_varint32(stream, x);
    *x = (*x >> 1) ^ -(int32_t)(*x & 1);
    return status;
}

bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    char bytes[4] = {0};
    bool status = pb_read(stream, bytes, 4);
    *(uint32_t*)dest = 
        bytes[0] | ((uint32_t)bytes[1] << 8) |
        ((uint32_t)bytes[2] << 16) | ((uint32_t)bytes[3] << 24);
    return status;
}

bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    return pb_decode_varint64(stream, (uint64_t*)dest);
}

bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    uint64_t *x = (uint64_t*)dest;
    bool status = pb_decode_varint64(stream, x);
    *x = (*x >> 1) ^ -(int64_t)(*x & 1);
    return status;
}

bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    char bytes[8] = {0};
    bool status = pb_read(stream, bytes, 8);
    *(uint64_t*)dest =
        (uint64_t)bytes[0] | ((uint64_t)bytes[1] << 8) |
        ((uint64_t)bytes[2] << 16) | ((uint64_t)bytes[3] << 24) |
        ((uint64_t)bytes[4] << 32) | ((uint64_t)bytes[5] << 40) |
        ((uint64_t)bytes[6] << 48) | ((uint64_t)bytes[7] << 56);
    return status;
}

bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    uint32_t temp = 0;
    bool status = pb_decode_varint32(stream, &temp);
    *(bool*)dest = !!temp;
    return status;
}

bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    return pb_read(stream, (char*)dest, sizeof(float));
}

bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    return pb_read(stream, (char*)dest, sizeof(double));
}

bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    pb_bytearray_t *x = (pb_bytearray_t*)dest;
    uint32_t temp;
    if (!pb_decode_varint32(stream, &temp))
        return false;
    x->size = temp;
    
    if (x->size > field->fieldsize)
        return false;
    
    return pb_read(stream, x->bytes, x->size);
}

bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    uint32_t size;
    if (!pb_decode_varint32(stream, &size))
        return false;
    
    if (size > field->fieldsize - 1)
        return false;
    
    bool status = pb_read(stream, (char*)dest, size);
    *((char*)dest + size) = 0;
    return status;
}

bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
    pb_callback_t *x = (pb_callback_t*)dest;
    
    if (x->funcs.decode == NULL)
        return pb_skip_string(stream);
    
    uint32_t size;
    if (!pb_decode_varint32(stream, &size))
        return false;
    
    if (stream->bytes_left < size)
        return false;
    
    // Make a limited-length istream for decoding submessage
    pb_istream_t shortstream = *stream;
    shortstream.bytes_left = size;
    bool status = x->funcs.decode(&shortstream, field, x->arg);
    stream->bytes_left -= size - shortstream.bytes_left;
    return status;
}