]> git.saurik.com Git - redis.git/blob - deps/hiredis/sds.c
ziplist.c fixes for bigendian
[redis.git] / deps / hiredis / sds.c
1 /* SDSLib, A C dynamic strings library
2 *
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #define SDS_ABORT_ON_OOM
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include "sds.h"
38
39 static void sdsOomAbort(void) {
40 fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
41 abort();
42 }
43
44 sds sdsnewlen(const void *init, size_t initlen) {
45 struct sdshdr *sh;
46
47 sh = malloc(sizeof(struct sdshdr)+initlen+1);
48 #ifdef SDS_ABORT_ON_OOM
49 if (sh == NULL) sdsOomAbort();
50 #else
51 if (sh == NULL) return NULL;
52 #endif
53 sh->len = initlen;
54 sh->free = 0;
55 if (initlen) {
56 if (init) memcpy(sh->buf, init, initlen);
57 else memset(sh->buf,0,initlen);
58 }
59 sh->buf[initlen] = '\0';
60 return (char*)sh->buf;
61 }
62
63 sds sdsempty(void) {
64 return sdsnewlen("",0);
65 }
66
67 sds sdsnew(const char *init) {
68 size_t initlen = (init == NULL) ? 0 : strlen(init);
69 return sdsnewlen(init, initlen);
70 }
71
72 sds sdsdup(const sds s) {
73 return sdsnewlen(s, sdslen(s));
74 }
75
76 void sdsfree(sds s) {
77 if (s == NULL) return;
78 free(s-sizeof(struct sdshdr));
79 }
80
81 void sdsupdatelen(sds s) {
82 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
83 int reallen = strlen(s);
84 sh->free += (sh->len-reallen);
85 sh->len = reallen;
86 }
87
88 static sds sdsMakeRoomFor(sds s, size_t addlen) {
89 struct sdshdr *sh, *newsh;
90 size_t free = sdsavail(s);
91 size_t len, newlen;
92
93 if (free >= addlen) return s;
94 len = sdslen(s);
95 sh = (void*) (s-(sizeof(struct sdshdr)));
96 newlen = (len+addlen)*2;
97 newsh = realloc(sh, sizeof(struct sdshdr)+newlen+1);
98 #ifdef SDS_ABORT_ON_OOM
99 if (newsh == NULL) sdsOomAbort();
100 #else
101 if (newsh == NULL) return NULL;
102 #endif
103
104 newsh->free = newlen - len;
105 return newsh->buf;
106 }
107
108 /* Grow the sds to have the specified length. Bytes that were not part of
109 * the original length of the sds will be set to zero. */
110 sds sdsgrowzero(sds s, size_t len) {
111 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
112 size_t totlen, curlen = sh->len;
113
114 if (len <= curlen) return s;
115 s = sdsMakeRoomFor(s,len-curlen);
116 if (s == NULL) return NULL;
117
118 /* Make sure added region doesn't contain garbage */
119 sh = (void*)(s-(sizeof(struct sdshdr)));
120 memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
121 totlen = sh->len+sh->free;
122 sh->len = len;
123 sh->free = totlen-sh->len;
124 return s;
125 }
126
127 sds sdscatlen(sds s, const void *t, size_t len) {
128 struct sdshdr *sh;
129 size_t curlen = sdslen(s);
130
131 s = sdsMakeRoomFor(s,len);
132 if (s == NULL) return NULL;
133 sh = (void*) (s-(sizeof(struct sdshdr)));
134 memcpy(s+curlen, t, len);
135 sh->len = curlen+len;
136 sh->free = sh->free-len;
137 s[curlen+len] = '\0';
138 return s;
139 }
140
141 sds sdscat(sds s, const char *t) {
142 return sdscatlen(s, t, strlen(t));
143 }
144
145 sds sdscpylen(sds s, char *t, size_t len) {
146 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
147 size_t totlen = sh->free+sh->len;
148
149 if (totlen < len) {
150 s = sdsMakeRoomFor(s,len-sh->len);
151 if (s == NULL) return NULL;
152 sh = (void*) (s-(sizeof(struct sdshdr)));
153 totlen = sh->free+sh->len;
154 }
155 memcpy(s, t, len);
156 s[len] = '\0';
157 sh->len = len;
158 sh->free = totlen-len;
159 return s;
160 }
161
162 sds sdscpy(sds s, char *t) {
163 return sdscpylen(s, t, strlen(t));
164 }
165
166 sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
167 va_list cpy;
168 char *buf, *t;
169 size_t buflen = 16;
170
171 while(1) {
172 buf = malloc(buflen);
173 #ifdef SDS_ABORT_ON_OOM
174 if (buf == NULL) sdsOomAbort();
175 #else
176 if (buf == NULL) return NULL;
177 #endif
178 buf[buflen-2] = '\0';
179 va_copy(cpy,ap);
180 vsnprintf(buf, buflen, fmt, cpy);
181 if (buf[buflen-2] != '\0') {
182 free(buf);
183 buflen *= 2;
184 continue;
185 }
186 break;
187 }
188 t = sdscat(s, buf);
189 free(buf);
190 return t;
191 }
192
193 sds sdscatprintf(sds s, const char *fmt, ...) {
194 va_list ap;
195 char *t;
196 va_start(ap, fmt);
197 t = sdscatvprintf(s,fmt,ap);
198 va_end(ap);
199 return t;
200 }
201
202 sds sdstrim(sds s, const char *cset) {
203 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
204 char *start, *end, *sp, *ep;
205 size_t len;
206
207 sp = start = s;
208 ep = end = s+sdslen(s)-1;
209 while(sp <= end && strchr(cset, *sp)) sp++;
210 while(ep > start && strchr(cset, *ep)) ep--;
211 len = (sp > ep) ? 0 : ((ep-sp)+1);
212 if (sh->buf != sp) memmove(sh->buf, sp, len);
213 sh->buf[len] = '\0';
214 sh->free = sh->free+(sh->len-len);
215 sh->len = len;
216 return s;
217 }
218
219 sds sdsrange(sds s, int start, int end) {
220 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
221 size_t newlen, len = sdslen(s);
222
223 if (len == 0) return s;
224 if (start < 0) {
225 start = len+start;
226 if (start < 0) start = 0;
227 }
228 if (end < 0) {
229 end = len+end;
230 if (end < 0) end = 0;
231 }
232 newlen = (start > end) ? 0 : (end-start)+1;
233 if (newlen != 0) {
234 if (start >= (signed)len) {
235 newlen = 0;
236 } else if (end >= (signed)len) {
237 end = len-1;
238 newlen = (start > end) ? 0 : (end-start)+1;
239 }
240 } else {
241 start = 0;
242 }
243 if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
244 sh->buf[newlen] = 0;
245 sh->free = sh->free+(sh->len-newlen);
246 sh->len = newlen;
247 return s;
248 }
249
250 void sdstolower(sds s) {
251 int len = sdslen(s), j;
252
253 for (j = 0; j < len; j++) s[j] = tolower(s[j]);
254 }
255
256 void sdstoupper(sds s) {
257 int len = sdslen(s), j;
258
259 for (j = 0; j < len; j++) s[j] = toupper(s[j]);
260 }
261
262 int sdscmp(sds s1, sds s2) {
263 size_t l1, l2, minlen;
264 int cmp;
265
266 l1 = sdslen(s1);
267 l2 = sdslen(s2);
268 minlen = (l1 < l2) ? l1 : l2;
269 cmp = memcmp(s1,s2,minlen);
270 if (cmp == 0) return l1-l2;
271 return cmp;
272 }
273
274 /* Split 's' with separator in 'sep'. An array
275 * of sds strings is returned. *count will be set
276 * by reference to the number of tokens returned.
277 *
278 * On out of memory, zero length string, zero length
279 * separator, NULL is returned.
280 *
281 * Note that 'sep' is able to split a string using
282 * a multi-character separator. For example
283 * sdssplit("foo_-_bar","_-_"); will return two
284 * elements "foo" and "bar".
285 *
286 * This version of the function is binary-safe but
287 * requires length arguments. sdssplit() is just the
288 * same function but for zero-terminated strings.
289 */
290 sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
291 int elements = 0, slots = 5, start = 0, j;
292
293 sds *tokens = malloc(sizeof(sds)*slots);
294 #ifdef SDS_ABORT_ON_OOM
295 if (tokens == NULL) sdsOomAbort();
296 #endif
297 if (seplen < 1 || len < 0 || tokens == NULL) return NULL;
298 if (len == 0) {
299 *count = 0;
300 return tokens;
301 }
302 for (j = 0; j < (len-(seplen-1)); j++) {
303 /* make sure there is room for the next element and the final one */
304 if (slots < elements+2) {
305 sds *newtokens;
306
307 slots *= 2;
308 newtokens = realloc(tokens,sizeof(sds)*slots);
309 if (newtokens == NULL) {
310 #ifdef SDS_ABORT_ON_OOM
311 sdsOomAbort();
312 #else
313 goto cleanup;
314 #endif
315 }
316 tokens = newtokens;
317 }
318 /* search the separator */
319 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
320 tokens[elements] = sdsnewlen(s+start,j-start);
321 if (tokens[elements] == NULL) {
322 #ifdef SDS_ABORT_ON_OOM
323 sdsOomAbort();
324 #else
325 goto cleanup;
326 #endif
327 }
328 elements++;
329 start = j+seplen;
330 j = j+seplen-1; /* skip the separator */
331 }
332 }
333 /* Add the final element. We are sure there is room in the tokens array. */
334 tokens[elements] = sdsnewlen(s+start,len-start);
335 if (tokens[elements] == NULL) {
336 #ifdef SDS_ABORT_ON_OOM
337 sdsOomAbort();
338 #else
339 goto cleanup;
340 #endif
341 }
342 elements++;
343 *count = elements;
344 return tokens;
345
346 #ifndef SDS_ABORT_ON_OOM
347 cleanup:
348 {
349 int i;
350 for (i = 0; i < elements; i++) sdsfree(tokens[i]);
351 free(tokens);
352 return NULL;
353 }
354 #endif
355 }
356
357 void sdsfreesplitres(sds *tokens, int count) {
358 if (!tokens) return;
359 while(count--)
360 sdsfree(tokens[count]);
361 free(tokens);
362 }
363
364 sds sdsfromlonglong(long long value) {
365 char buf[32], *p;
366 unsigned long long v;
367
368 v = (value < 0) ? -value : value;
369 p = buf+31; /* point to the last character */
370 do {
371 *p-- = '0'+(v%10);
372 v /= 10;
373 } while(v);
374 if (value < 0) *p-- = '-';
375 p++;
376 return sdsnewlen(p,32-(p-buf));
377 }
378
379 sds sdscatrepr(sds s, char *p, size_t len) {
380 s = sdscatlen(s,"\"",1);
381 while(len--) {
382 switch(*p) {
383 case '\\':
384 case '"':
385 s = sdscatprintf(s,"\\%c",*p);
386 break;
387 case '\n': s = sdscatlen(s,"\\n",1); break;
388 case '\r': s = sdscatlen(s,"\\r",1); break;
389 case '\t': s = sdscatlen(s,"\\t",1); break;
390 case '\a': s = sdscatlen(s,"\\a",1); break;
391 case '\b': s = sdscatlen(s,"\\b",1); break;
392 default:
393 if (isprint(*p))
394 s = sdscatprintf(s,"%c",*p);
395 else
396 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
397 break;
398 }
399 p++;
400 }
401 return sdscatlen(s,"\"",1);
402 }
403
404 /* Split a line into arguments, where every argument can be in the
405 * following programming-language REPL-alike form:
406 *
407 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
408 *
409 * The number of arguments is stored into *argc, and an array
410 * of sds is returned. The caller should sdsfree() all the returned
411 * strings and finally free() the array itself.
412 *
413 * Note that sdscatrepr() is able to convert back a string into
414 * a quoted string in the same format sdssplitargs() is able to parse.
415 */
416 sds *sdssplitargs(char *line, int *argc) {
417 char *p = line;
418 char *current = NULL;
419 char **vector = NULL;
420
421 *argc = 0;
422 while(1) {
423 /* skip blanks */
424 while(*p && isspace(*p)) p++;
425 if (*p) {
426 /* get a token */
427 int inq=0; /* set to 1 if we are in "quotes" */
428 int done=0;
429
430 if (current == NULL) current = sdsempty();
431 while(!done) {
432 if (inq) {
433 if (*p == '\\' && *(p+1)) {
434 char c;
435
436 p++;
437 switch(*p) {
438 case 'n': c = '\n'; break;
439 case 'r': c = '\r'; break;
440 case 't': c = '\t'; break;
441 case 'b': c = '\b'; break;
442 case 'a': c = '\a'; break;
443 default: c = *p; break;
444 }
445 current = sdscatlen(current,&c,1);
446 } else if (*p == '"') {
447 /* closing quote must be followed by a space */
448 if (*(p+1) && !isspace(*(p+1))) goto err;
449 done=1;
450 } else if (!*p) {
451 /* unterminated quotes */
452 goto err;
453 } else {
454 current = sdscatlen(current,p,1);
455 }
456 } else {
457 switch(*p) {
458 case ' ':
459 case '\n':
460 case '\r':
461 case '\t':
462 case '\0':
463 done=1;
464 break;
465 case '"':
466 inq=1;
467 break;
468 default:
469 current = sdscatlen(current,p,1);
470 break;
471 }
472 }
473 if (*p) p++;
474 }
475 /* add the token to the vector */
476 vector = realloc(vector,((*argc)+1)*sizeof(char*));
477 vector[*argc] = current;
478 (*argc)++;
479 current = NULL;
480 } else {
481 return vector;
482 }
483 }
484
485 err:
486 while((*argc)--)
487 sdsfree(vector[*argc]);
488 free(vector);
489 if (current) sdsfree(current);
490 return NULL;
491 }
492
493 #ifdef SDS_TEST_MAIN
494 #include <stdio.h>
495
496 int __failed_tests = 0;
497 int __test_num = 0;
498 #define test_cond(descr,_c) do { \
499 __test_num++; printf("%d - %s: ", __test_num, descr); \
500 if(_c) printf("PASSED\n"); else {printf("FAILED\n"); __failed_tests++;} \
501 } while(0);
502 #define test_report() do { \
503 printf("%d tests, %d passed, %d failed\n", __test_num, \
504 __test_num-__failed_tests, __failed_tests); \
505 if (__failed_tests) { \
506 printf("=== WARNING === We have failed tests here...\n"); \
507 } \
508 } while(0);
509
510 int main(void) {
511 {
512 sds x = sdsnew("foo"), y;
513
514 test_cond("Create a string and obtain the length",
515 sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
516
517 sdsfree(x);
518 x = sdsnewlen("foo",2);
519 test_cond("Create a string with specified length",
520 sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
521
522 x = sdscat(x,"bar");
523 test_cond("Strings concatenation",
524 sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
525
526 x = sdscpy(x,"a");
527 test_cond("sdscpy() against an originally longer string",
528 sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
529
530 x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
531 test_cond("sdscpy() against an originally shorter string",
532 sdslen(x) == 33 &&
533 memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
534
535 sdsfree(x);
536 x = sdscatprintf(sdsempty(),"%d",123);
537 test_cond("sdscatprintf() seems working in the base case",
538 sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)
539
540 sdsfree(x);
541 x = sdstrim(sdsnew("xxciaoyyy"),"xy");
542 test_cond("sdstrim() correctly trims characters",
543 sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
544
545 y = sdsrange(sdsdup(x),1,1);
546 test_cond("sdsrange(...,1,1)",
547 sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
548
549 sdsfree(y);
550 y = sdsrange(sdsdup(x),1,-1);
551 test_cond("sdsrange(...,1,-1)",
552 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
553
554 sdsfree(y);
555 y = sdsrange(sdsdup(x),-2,-1);
556 test_cond("sdsrange(...,-2,-1)",
557 sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
558
559 sdsfree(y);
560 y = sdsrange(sdsdup(x),2,1);
561 test_cond("sdsrange(...,2,1)",
562 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
563
564 sdsfree(y);
565 y = sdsrange(sdsdup(x),1,100);
566 test_cond("sdsrange(...,1,100)",
567 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
568
569 sdsfree(y);
570 y = sdsrange(sdsdup(x),100,100);
571 test_cond("sdsrange(...,100,100)",
572 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
573
574 sdsfree(y);
575 sdsfree(x);
576 x = sdsnew("foo");
577 y = sdsnew("foa");
578 test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
579
580 sdsfree(y);
581 sdsfree(x);
582 x = sdsnew("bar");
583 y = sdsnew("bar");
584 test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
585
586 sdsfree(y);
587 sdsfree(x);
588 x = sdsnew("aar");
589 y = sdsnew("bar");
590 test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
591 }
592 test_report()
593 }
594 #endif