You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

read.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of Redis nor the names of its contributors may be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "fmacros.h"
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #ifndef _MSC_VER
  35. #include <unistd.h>
  36. #endif
  37. #include <assert.h>
  38. #include <errno.h>
  39. #include <ctype.h>
  40. #include "read.h"
  41. #include "sds.h"
  42. static void __redisReaderSetError(redisReader *r, int type, const char *str) {
  43. size_t len;
  44. if (r->reply != NULL && r->fn && r->fn->freeObject) {
  45. r->fn->freeObject(r->reply);
  46. r->reply = NULL;
  47. }
  48. /* Clear input buffer on errors. */
  49. if (r->buf != NULL) {
  50. sdsfree(r->buf);
  51. r->buf = NULL;
  52. r->pos = r->len = 0;
  53. }
  54. /* Reset task stack. */
  55. r->ridx = -1;
  56. /* Set error. */
  57. r->err = type;
  58. len = strlen(str);
  59. len = len < (sizeof(r->errstr)-1) ? len : (sizeof(r->errstr)-1);
  60. memcpy(r->errstr,str,len);
  61. r->errstr[len] = '\0';
  62. }
  63. static size_t chrtos(char *buf, size_t size, char byte) {
  64. size_t len = 0;
  65. switch(byte) {
  66. case '\\':
  67. case '"':
  68. len = snprintf(buf,size,"\"\\%c\"",byte);
  69. break;
  70. case '\n': len = snprintf(buf,size,"\"\\n\""); break;
  71. case '\r': len = snprintf(buf,size,"\"\\r\""); break;
  72. case '\t': len = snprintf(buf,size,"\"\\t\""); break;
  73. case '\a': len = snprintf(buf,size,"\"\\a\""); break;
  74. case '\b': len = snprintf(buf,size,"\"\\b\""); break;
  75. default:
  76. if (isprint(byte))
  77. len = snprintf(buf,size,"\"%c\"",byte);
  78. else
  79. len = snprintf(buf,size,"\"\\x%02x\"",(unsigned char)byte);
  80. break;
  81. }
  82. return len;
  83. }
  84. static void __redisReaderSetErrorProtocolByte(redisReader *r, char byte) {
  85. char cbuf[8], sbuf[128];
  86. chrtos(cbuf,sizeof(cbuf),byte);
  87. snprintf(sbuf,sizeof(sbuf),
  88. "Protocol error, got %s as reply type byte", cbuf);
  89. __redisReaderSetError(r,REDIS_ERR_PROTOCOL,sbuf);
  90. }
  91. static void __redisReaderSetErrorOOM(redisReader *r) {
  92. __redisReaderSetError(r,REDIS_ERR_OOM,"Out of memory");
  93. }
  94. static char *readBytes(redisReader *r, unsigned int bytes) {
  95. char *p;
  96. if (r->len-r->pos >= bytes) {
  97. p = r->buf+r->pos;
  98. r->pos += bytes;
  99. return p;
  100. }
  101. return NULL;
  102. }
  103. /* Find pointer to \r\n. */
  104. static char *seekNewline(char *s, size_t len) {
  105. int pos = 0;
  106. int _len = len-1;
  107. /* Position should be < len-1 because the character at "pos" should be
  108. * followed by a \n. Note that strchr cannot be used because it doesn't
  109. * allow to search a limited length and the buffer that is being searched
  110. * might not have a trailing NULL character. */
  111. while (pos < _len) {
  112. while(pos < _len && s[pos] != '\r') pos++;
  113. if (s[pos] != '\r') {
  114. /* Not found. */
  115. return NULL;
  116. } else {
  117. if (s[pos+1] == '\n') {
  118. /* Found. */
  119. return s+pos;
  120. } else {
  121. /* Continue searching. */
  122. pos++;
  123. }
  124. }
  125. }
  126. return NULL;
  127. }
  128. /* Read a long long value starting at *s, under the assumption that it will be
  129. * terminated by \r\n. Ambiguously returns -1 for unexpected input. */
  130. static long long readLongLong(char *s) {
  131. long long v = 0;
  132. int dec, mult = 1;
  133. char c;
  134. if (*s == '-') {
  135. mult = -1;
  136. s++;
  137. } else if (*s == '+') {
  138. mult = 1;
  139. s++;
  140. }
  141. while ((c = *(s++)) != '\r') {
  142. dec = c - '0';
  143. if (dec >= 0 && dec < 10) {
  144. v *= 10;
  145. v += dec;
  146. } else {
  147. /* Should not happen... */
  148. return -1;
  149. }
  150. }
  151. return mult*v;
  152. }
  153. static char *readLine(redisReader *r, int *_len) {
  154. char *p, *s;
  155. int len;
  156. p = r->buf+r->pos;
  157. s = seekNewline(p,(r->len-r->pos));
  158. if (s != NULL) {
  159. len = s-(r->buf+r->pos);
  160. r->pos += len+2; /* skip \r\n */
  161. if (_len) *_len = len;
  162. return p;
  163. }
  164. return NULL;
  165. }
  166. static void moveToNextTask(redisReader *r) {
  167. redisReadTask *cur, *prv;
  168. while (r->ridx >= 0) {
  169. /* Return a.s.a.p. when the stack is now empty. */
  170. if (r->ridx == 0) {
  171. r->ridx--;
  172. return;
  173. }
  174. cur = &(r->rstack[r->ridx]);
  175. prv = &(r->rstack[r->ridx-1]);
  176. assert(prv->type == REDIS_REPLY_ARRAY);
  177. if (cur->idx == prv->elements-1) {
  178. r->ridx--;
  179. } else {
  180. /* Reset the type because the next item can be anything */
  181. assert(cur->idx < prv->elements);
  182. cur->type = -1;
  183. cur->elements = -1;
  184. cur->idx++;
  185. return;
  186. }
  187. }
  188. }
  189. static int processLineItem(redisReader *r) {
  190. redisReadTask *cur = &(r->rstack[r->ridx]);
  191. void *obj;
  192. char *p;
  193. int len;
  194. if ((p = readLine(r,&len)) != NULL) {
  195. if (cur->type == REDIS_REPLY_INTEGER) {
  196. if (r->fn && r->fn->createInteger)
  197. obj = r->fn->createInteger(cur,readLongLong(p));
  198. else
  199. obj = (void*)REDIS_REPLY_INTEGER;
  200. } else {
  201. /* Type will be error or status. */
  202. if (r->fn && r->fn->createString)
  203. obj = r->fn->createString(cur,p,len);
  204. else
  205. obj = (void*)(size_t)(cur->type);
  206. }
  207. if (obj == NULL) {
  208. __redisReaderSetErrorOOM(r);
  209. return REDIS_ERR;
  210. }
  211. /* Set reply if this is the root object. */
  212. if (r->ridx == 0) r->reply = obj;
  213. moveToNextTask(r);
  214. return REDIS_OK;
  215. }
  216. return REDIS_ERR;
  217. }
  218. static int processBulkItem(redisReader *r) {
  219. redisReadTask *cur = &(r->rstack[r->ridx]);
  220. void *obj = NULL;
  221. char *p, *s;
  222. long len;
  223. unsigned long bytelen;
  224. int success = 0;
  225. p = r->buf+r->pos;
  226. s = seekNewline(p,r->len-r->pos);
  227. if (s != NULL) {
  228. p = r->buf+r->pos;
  229. bytelen = s-(r->buf+r->pos)+2; /* include \r\n */
  230. len = readLongLong(p);
  231. if (len < 0) {
  232. /* The nil object can always be created. */
  233. if (r->fn && r->fn->createNil)
  234. obj = r->fn->createNil(cur);
  235. else
  236. obj = (void*)REDIS_REPLY_NIL;
  237. success = 1;
  238. } else {
  239. /* Only continue when the buffer contains the entire bulk item. */
  240. bytelen += len+2; /* include \r\n */
  241. if (r->pos+bytelen <= r->len) {
  242. if (r->fn && r->fn->createString)
  243. obj = r->fn->createString(cur,s+2,len);
  244. else
  245. obj = (void*)REDIS_REPLY_STRING;
  246. success = 1;
  247. }
  248. }
  249. /* Proceed when obj was created. */
  250. if (success) {
  251. if (obj == NULL) {
  252. __redisReaderSetErrorOOM(r);
  253. return REDIS_ERR;
  254. }
  255. r->pos += bytelen;
  256. /* Set reply if this is the root object. */
  257. if (r->ridx == 0) r->reply = obj;
  258. moveToNextTask(r);
  259. return REDIS_OK;
  260. }
  261. }
  262. return REDIS_ERR;
  263. }
  264. static int processMultiBulkItem(redisReader *r) {
  265. redisReadTask *cur = &(r->rstack[r->ridx]);
  266. void *obj;
  267. char *p;
  268. long elements;
  269. int root = 0;
  270. /* Set error for nested multi bulks with depth > 7 */
  271. if (r->ridx == 8) {
  272. __redisReaderSetError(r,REDIS_ERR_PROTOCOL,
  273. "No support for nested multi bulk replies with depth > 7");
  274. return REDIS_ERR;
  275. }
  276. if ((p = readLine(r,NULL)) != NULL) {
  277. elements = readLongLong(p);
  278. root = (r->ridx == 0);
  279. if (elements == -1) {
  280. if (r->fn && r->fn->createNil)
  281. obj = r->fn->createNil(cur);
  282. else
  283. obj = (void*)REDIS_REPLY_NIL;
  284. if (obj == NULL) {
  285. __redisReaderSetErrorOOM(r);
  286. return REDIS_ERR;
  287. }
  288. moveToNextTask(r);
  289. } else {
  290. if (r->fn && r->fn->createArray)
  291. obj = r->fn->createArray(cur,elements);
  292. else
  293. obj = (void*)REDIS_REPLY_ARRAY;
  294. if (obj == NULL) {
  295. __redisReaderSetErrorOOM(r);
  296. return REDIS_ERR;
  297. }
  298. /* Modify task stack when there are more than 0 elements. */
  299. if (elements > 0) {
  300. cur->elements = elements;
  301. cur->obj = obj;
  302. r->ridx++;
  303. r->rstack[r->ridx].type = -1;
  304. r->rstack[r->ridx].elements = -1;
  305. r->rstack[r->ridx].idx = 0;
  306. r->rstack[r->ridx].obj = NULL;
  307. r->rstack[r->ridx].parent = cur;
  308. r->rstack[r->ridx].privdata = r->privdata;
  309. } else {
  310. moveToNextTask(r);
  311. }
  312. }
  313. /* Set reply if this is the root object. */
  314. if (root) r->reply = obj;
  315. return REDIS_OK;
  316. }
  317. return REDIS_ERR;
  318. }
  319. static int processItem(redisReader *r) {
  320. redisReadTask *cur = &(r->rstack[r->ridx]);
  321. char *p;
  322. /* check if we need to read type */
  323. if (cur->type < 0) {
  324. if ((p = readBytes(r,1)) != NULL) {
  325. switch (p[0]) {
  326. case '-':
  327. cur->type = REDIS_REPLY_ERROR;
  328. break;
  329. case '+':
  330. cur->type = REDIS_REPLY_STATUS;
  331. break;
  332. case ':':
  333. cur->type = REDIS_REPLY_INTEGER;
  334. break;
  335. case '$':
  336. cur->type = REDIS_REPLY_STRING;
  337. break;
  338. case '*':
  339. cur->type = REDIS_REPLY_ARRAY;
  340. break;
  341. default:
  342. __redisReaderSetErrorProtocolByte(r,*p);
  343. return REDIS_ERR;
  344. }
  345. } else {
  346. /* could not consume 1 byte */
  347. return REDIS_ERR;
  348. }
  349. }
  350. /* process typed item */
  351. switch(cur->type) {
  352. case REDIS_REPLY_ERROR:
  353. case REDIS_REPLY_STATUS:
  354. case REDIS_REPLY_INTEGER:
  355. return processLineItem(r);
  356. case REDIS_REPLY_STRING:
  357. return processBulkItem(r);
  358. case REDIS_REPLY_ARRAY:
  359. return processMultiBulkItem(r);
  360. default:
  361. assert(NULL);
  362. return REDIS_ERR; /* Avoid warning. */
  363. }
  364. }
  365. redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
  366. redisReader *r;
  367. r = calloc(sizeof(redisReader),1);
  368. if (r == NULL)
  369. return NULL;
  370. r->err = 0;
  371. r->errstr[0] = '\0';
  372. r->fn = fn;
  373. r->buf = sdsempty();
  374. r->maxbuf = REDIS_READER_MAX_BUF;
  375. if (r->buf == NULL) {
  376. free(r);
  377. return NULL;
  378. }
  379. r->ridx = -1;
  380. return r;
  381. }
  382. void redisReaderFree(redisReader *r) {
  383. if (r->reply != NULL && r->fn && r->fn->freeObject)
  384. r->fn->freeObject(r->reply);
  385. if (r->buf != NULL)
  386. sdsfree(r->buf);
  387. free(r);
  388. }
  389. int redisReaderFeed(redisReader *r, const char *buf, size_t len) {
  390. sds newbuf;
  391. /* Return early when this reader is in an erroneous state. */
  392. if (r->err)
  393. return REDIS_ERR;
  394. /* Copy the provided buffer. */
  395. if (buf != NULL && len >= 1) {
  396. /* Destroy internal buffer when it is empty and is quite large. */
  397. if (r->len == 0 && r->maxbuf != 0 && sdsavail(r->buf) > r->maxbuf) {
  398. sdsfree(r->buf);
  399. r->buf = sdsempty();
  400. r->pos = 0;
  401. /* r->buf should not be NULL since we just free'd a larger one. */
  402. assert(r->buf != NULL);
  403. }
  404. newbuf = sdscatlen(r->buf,buf,len);
  405. if (newbuf == NULL) {
  406. __redisReaderSetErrorOOM(r);
  407. return REDIS_ERR;
  408. }
  409. r->buf = newbuf;
  410. r->len = sdslen(r->buf);
  411. }
  412. return REDIS_OK;
  413. }
  414. int redisReaderGetReply(redisReader *r, void **reply) {
  415. /* Default target pointer to NULL. */
  416. if (reply != NULL)
  417. *reply = NULL;
  418. /* Return early when this reader is in an erroneous state. */
  419. if (r->err)
  420. return REDIS_ERR;
  421. /* When the buffer is empty, there will never be a reply. */
  422. if (r->len == 0)
  423. return REDIS_OK;
  424. /* Set first item to process when the stack is empty. */
  425. if (r->ridx == -1) {
  426. r->rstack[0].type = -1;
  427. r->rstack[0].elements = -1;
  428. r->rstack[0].idx = -1;
  429. r->rstack[0].obj = NULL;
  430. r->rstack[0].parent = NULL;
  431. r->rstack[0].privdata = r->privdata;
  432. r->ridx = 0;
  433. }
  434. /* Process items in reply. */
  435. while (r->ridx >= 0)
  436. if (processItem(r) != REDIS_OK)
  437. break;
  438. /* Return ASAP when an error occurred. */
  439. if (r->err)
  440. return REDIS_ERR;
  441. /* Discard part of the buffer when we've consumed at least 1k, to avoid
  442. * doing unnecessary calls to memmove() in sds.c. */
  443. if (r->pos >= 1024) {
  444. sdsrange(r->buf,r->pos,-1);
  445. r->pos = 0;
  446. r->len = sdslen(r->buf);
  447. }
  448. /* Emit a reply when there is one. */
  449. if (r->ridx == -1) {
  450. if (reply != NULL)
  451. *reply = r->reply;
  452. r->reply = NULL;
  453. }
  454. return REDIS_OK;
  455. }