-
Notifications
You must be signed in to change notification settings - Fork 2
/
resp.c
409 lines (335 loc) · 8.79 KB
/
resp.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2015 José Carlos Nieto, https://menteslibres.net/xiam
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* */
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <limits.h>
#include <ctype.h>
#include "resp.h"
// createRespObject allocates a new object of the given type.
respObject *createRespObject(int type) {
respObject *r;
r = (respObject *)malloc(sizeof(respObject));
if (r == NULL) {
return NULL;
}
r->type = type;
r->elements = 0;
r->len = 0;
r->str = NULL;
r->element = NULL;
return r;
}
// freeRespObject frees the allocated memory of the given object.
void freeRespObject(respObject *r) {
unsigned int i;
switch(r->type) {
case RESP_OBJECT_INTEGER:
break;
case RESP_OBJECT_ARRAY:
if (r->element != NULL) {
for (i = 0; i < r->elements; i++) {
if (r->element[i] != NULL) {
freeRespObject(r->element[i]);
}
}
free(r->element);
}
break;
case RESP_OBJECT_BINARY:
case RESP_OBJECT_ERROR:
case RESP_OBJECT_STATUS:
if (r->str != NULL) {
free(r->str);
}
break;
}
free(r);
}
// createRespArray allocates a RESP object of type array with the given number
// of elements.
respObject *createRespArray(unsigned int elements) {
respObject *r;
int i;
if (elements > RESP_ARRAY_MAX_LEN) {
return NULL;
}
r = createRespObject(RESP_OBJECT_ARRAY);
r->elements = elements;
r->element = (respObject **)malloc(sizeof(respObject *)*elements);
if (r->element == NULL) {
freeRespObject(r);
return NULL;
}
for (i = 0; i < r->elements; i++) {
r->element[i] = NULL;
}
return r;
}
// createRespInteger allocates a RESP object of type integer with the given
// value.
respObject *createRespInteger(int integer) {
respObject *r;
r = createRespObject(RESP_OBJECT_INTEGER);
r->integer = integer;
return r;
}
// createRespNil allocates a RESP object of type nil.
respObject *createRespNil() {
respObject *r;
r = createRespObject(RESP_OBJECT_NIL);
return r;
}
// createRespString allocates a RESP object of type string with the given
// value.
respObject *createRespString(int type, unsigned char *str) {
respObject *r;
int len;
for (len = 0; *(str + ++len) != '\0';);
if (len > RESP_STRING_MAX_LEN) {
return NULL;
}
if (type == RESP_OBJECT_ERROR || type == RESP_OBJECT_STATUS) {
r = createRespObject(type);
if (r == NULL) {
return NULL;
}
r->str = (unsigned char *)malloc(sizeof(unsigned char)*len);
if (r->str == NULL) {
freeRespObject(r);
return NULL;
}
memcpy(r->str, str, len);
r->str[len] = '\0';
r->len = len;
return r;
}
return NULL;
}
// createRespBulk allocates a RESP object that is binary safe.
respObject *createRespBulk(unsigned char *str, int len) {
respObject *r;
if (len > RESP_STRING_MAX_LEN) {
return NULL;
}
r = createRespObject(RESP_OBJECT_BINARY);
if (r == NULL) {
return NULL;
}
r->str = (unsigned char *)malloc(sizeof(unsigned char)*len);
if (r->str == NULL) {
freeRespObject(r);
return NULL;
}
memcpy(r->str, str, len);
r->str[len] = '\0';
r->len = len;
return r;
}
// respNextLine is an internal function used to read the next line to decode.
int respNextLine(unsigned char *src) {
int i;
for (i = 1; i < RESP_LINE_MAX_LEN; i++) {
if (src[i] == '\n' && src[i - 1] == '\r') {
return i + 1;
}
}
return -1;
}
// respDecode reads a RESP encoded message from src and dumps it into r.
int respDecode(respObject **r, unsigned char *src) {
int offset;
int buflen;
int i, t;
offset = respNextLine(src);
if (offset < 0) {
return RESP_ERROR_INCOMPLETE_MESSAGE;
}
*r = NULL;
switch (src[0]) {
case ':':
*r = createRespInteger((int)respAtoi(src+1));
if (*r == NULL) {
return RESP_ERROR_CANNOT_ALLOCATE;
}
break;
case '+':
src[offset-2] = '\0';
*r = createRespString(RESP_OBJECT_STATUS, (unsigned char *)src+1);
if (*r == NULL) {
return RESP_ERROR_CANNOT_ALLOCATE;
}
break;
case '-':
src[offset-2] = '\0';
*r = createRespString(RESP_OBJECT_ERROR, (unsigned char *)src+1);
if (*r == NULL) {
return RESP_ERROR_CANNOT_ALLOCATE;
}
break;
case '$':
/* Get length of the buffer */
buflen = respAtoi(src+1);
if (buflen < 0 || buflen > RESP_STRING_MAX_LEN) {
return RESP_ERROR_WRONG_FORMAT;
}
/* Making sure we have a message. */
if (src[offset+buflen] != '\r' || src[offset+buflen+1] != '\n') {
return RESP_ERROR_INCOMPLETE_MESSAGE;
}
/* Reading length of the buffer. */
*r = createRespBulk(src+offset, buflen);
if (*r == NULL) {
return RESP_ERROR_CANNOT_ALLOCATE;
}
/* Length of the buffer plus \r\n */
offset += buflen + 2;
break;
case '*':
/* Get length of the buffer */
buflen = respAtoi(src+1);
if (buflen < 0 || buflen > RESP_ARRAY_MAX_LEN) {
return RESP_ERROR_WRONG_FORMAT;
}
*r = createRespArray(buflen);
if (*r == NULL) {
return RESP_ERROR_CANNOT_ALLOCATE;
}
for (i = 0; i < buflen; i++) {
t = respDecode(&((*r)->element[i]), src+offset);
if (t < 0) {
freeRespObject(*r);
return RESP_ERROR_INCOMPLETE_MESSAGE;
}
offset += t;
}
break;
}
if (*r == NULL) {
return RESP_ERROR_UNKNOWN_MESSAGE;
}
return offset;
}
// respEncode marshals a RESP object into dest.
int respEncode(respObject *r, unsigned char *dest) {
int l, j;
unsigned char sizebuf[RESP_MAX_ITOA_LEN];
switch (r->type) {
case RESP_OBJECT_INTEGER:
dest[0] = ':';
l = respItoa(sizebuf, r->integer);
memcpy(dest+1, sizebuf, l);
l = l + 1;
break;
case RESP_OBJECT_BINARY:
dest[0] = '$';
l = respItoa(sizebuf, (int)r->len);
memcpy(dest+1, sizebuf, l);
dest[l+1] = '\r';
dest[l+2] = '\n';
memcpy(dest+l+3, r->str, r->len);
l = l + 3 + r->len;
break;
case RESP_OBJECT_STATUS:
dest[0] = '+';
memcpy(dest + 1, r->str, r->len);
l = r->len + 1;
break;
case RESP_OBJECT_ERROR:
dest[0] = '-';
memcpy(dest+1, r->str, r->len);
l = r->len + 1;
break;
case RESP_OBJECT_ARRAY:
dest[0] = '*';
l = respItoa(sizebuf, (int)r->elements);
memcpy(dest+1, sizebuf, l);
dest[l+1] = '\r';
dest[l+2] = '\n';
l = l + 3;
for (j = 0; j < r->elements; j++) {
l = l + respEncode(r->element[j], dest + l);
}
dest[l+1] = '\0';
return l;
break;
case RESP_OBJECT_NIL:
memcpy(dest, "$-1", 3);
l = 3;
break;
}
dest[l] = '\r';
dest[l+1] = '\n';
dest[l+2] = '\0';
return l + 2;
}
/* Stolen from https://github.com/redis/hiredis/blob/ec229678c22e673e8d48070593f7b028a514882a/sds.c#L299 */
int respItoa(unsigned char *s, int value) {
unsigned char *p, aux;
unsigned int v;
unsigned int l;
/* Generate the string representation, this method produces
* an reversed string. */
v = (value < 0) ? -value : value;
p = s;
do {
*p++ = '0'+(v%10);
v /= 10;
} while(v);
if (value < 0) *p++ = '-';
/* Compute length and add null term. */
l = p-s;
*p = '\0';
/* Reverse the string. */
p--;
while(s < p) {
aux = *s;
*s = *p;
*p = aux;
s++;
p--;
}
return l;
}
/* Stolen from
* http://tinodidriksen.com/2010/02/16/cpp-convert-string-to-int-speed/ */
int respAtoi(const unsigned char *p) {
int x = 0;
int neg = 0;
if (*p == '-') {
neg = 1;
++p;
}
while (*p >= '0' && *p <= '9') {
x = (x*10) + (*p - '0');
if (x > INT_MAX) {
return -1;
}
++p;
}
if (neg) {
x = -x;
}
return x;
}