]> git.saurik.com Git - redis.git/blob - zip.c
code to iterate over a ziplist
[redis.git] / zip.c
1 #define ZIP_BIGLEN 254
2 #define ZIP_END 255
3
4 /* The following macro returns the number of bytes needed to encode the length
5 * for the integer value _l, that is, 1 byte for lengths < ZIP_BIGLEN and
6 * 5 bytes for all the other lengths. */
7 #define ZIP_LEN_BYTES(_l) (((_l) < ZIP_BIGLEN) ? 1 : sizeof(unsigned int)+1)
8
9 /* Decode the encoded length pointed by 'p' */
10 static unsigned int zipDecodeLength(unsigned char *p) {
11 unsigned int len = *p;
12
13 if (len < ZIP_BIGLEN) return len;
14 memcpy(&len,p+1,sizeof(unsigned int));
15 return len;
16 }
17
18 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
19 * the amount of bytes required to encode such a length. */
20 static unsigned int zipEncodeLength(unsigned char *p, unsigned int len) {
21 if (p == NULL) {
22 return ZIP_LEN_BYTES(len);
23 } else {
24 if (len < ZIP_BIGLEN) {
25 p[0] = len;
26 return 1;
27 } else {
28 p[0] = ZIP_BIGLEN;
29 memcpy(p+1,&len,sizeof(len));
30 return 1+sizeof(len);
31 }
32 }
33 }
34
35 /* Return the total amount used by an entry (encoded length + payload). */
36 static unsigned int zipRawEntryLength(unsigned char *p) {
37 unsigned int l = zipDecodeLength(p);
38 return zipEncodeLength(NULL,l) + l;
39 }
40
41 /* Resize the zip* structure. */
42 static unsigned char *zipResize(unsigned char *z, unsigned int len) {
43 z = zrealloc(z,len);
44 z[len-1] = ZIP_END;
45 return z;
46 }