Encode the string element pointed by 's' of size 'len' in the target * buffer 's'. The function should be called with 'buf' having always enough * space for encoding the string. This is done by calling lpEncodeGetType() * before calling this function. */
| 382 | * space for encoding the string. This is done by calling lpEncodeGetType() |
| 383 | * before calling this function. */ |
| 384 | void lpEncodeString(unsigned char *buf, unsigned char *s, uint32_t len) { |
| 385 | if (len < 64) { |
| 386 | buf[0] = len | LP_ENCODING_6BIT_STR; |
| 387 | memcpy(buf+1,s,len); |
| 388 | } else if (len < 4096) { |
| 389 | buf[0] = (len >> 8) | LP_ENCODING_12BIT_STR; |
| 390 | buf[1] = len & 0xff; |
| 391 | memcpy(buf+2,s,len); |
| 392 | } else { |
| 393 | buf[0] = LP_ENCODING_32BIT_STR; |
| 394 | buf[1] = len & 0xff; |
| 395 | buf[2] = (len >> 8) & 0xff; |
| 396 | buf[3] = (len >> 16) & 0xff; |
| 397 | buf[4] = (len >> 24) & 0xff; |
| 398 | memcpy(buf+5,s,len); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /* Return the encoded length of the listpack element pointed by 'p'. |
| 403 | * This includes the encoding byte, length bytes, and the element data itself. |