1 /* The ziplist is a specially encoded dually linked list that is designed
2 * to be very memory efficient. It stores both strings and integer values,
3 * where integers are encoded as actual integers instead of a series of
4 * characters. It allows push and pop operations on either side of the list
5 * in O(1) time. However, because every operation requires a reallocation of
6 * the memory used by the ziplist, the actual complexity is related to the
7 * amount of memory used by the ziplist.
8 *
9 * ----------------------------------------------------------------------------
10 *
11 * ZIPLIST OVERALL LAYOUT
12 * ======================
13 *
14 * The general layout of the ziplist is as follows:
15 *
16 * <zlbytes> <zltail> <zllen> <entry> <entry> ... <entry> <zlend>
17 *
18 * NOTE: all fields are stored in little endian, if not specified otherwise.
19 *
20 * <uint32_t zlbytes> is an unsigned integer to hold the number of bytes that
21 * the ziplist occupies, including the four bytes of the zlbytes field itself.
22 * This value needs to be stored to be able to resize the entire structure
23 * without the need to traverse it first.
24 *
25 * <uint32_t zltail> is the offset to the last entry in the list. This allows
26 * a pop operation on the far side of the list without the need for full
27 * traversal.
28 *
29 * <uint16_t zllen> is the number of entries. When there are more than
30 * 2^16-2 entries, this value is set to 2^16-1 and we need to traverse the
31 * entire list to know how many items it holds.
32 *
33 * <uint8_t zlend> is a special entry representing the end of the ziplist.
34 * Is encoded as a single byte equal to 255. No other normal entry starts
35 * with a byte set to the value of 255.
36 *
37 * ZIPLIST ENTRIES
38 * ===============
39 *
40 * Every entry in the ziplist is prefixed by metadata that contains two pieces
41 * of information. First, the length of the previous entry is stored to be
42 * able to traverse the list from back to front. Second, the entry encoding is
43 * provided. It represents the entry type, integer or string, and in the case
44 * of strings it also represents the length of the string payload.
45 * So a complete entry is stored like this:
46 *
47 * <prevlen> <encoding> <entry-data>
48 *
49 * Sometimes the encoding represents the entry itself, like for small integers
50 * as we'll see later. In such a case the <entry-data> part is missing, and we
51 * could have just:
52 *
53 * <prevlen> <encoding>
54 *
55 * The length of the previous entry, <prevlen>, is encoded in the following way:
56 * If this length is smaller than 254 bytes, it will only consume a single
57 * byte representing the length as an unsinged 8 bit integer. When the length
58 * is greater than or equal to 254, it will consume 5 bytes. The first byte is
59 * set to 254 (FE) to indicate a larger value is following. The remaining 4
60 * bytes take the length of the previous entry as value.
61 *
62 * So practically an entry is encoded in the following way:
63 *
64 * <prevlen from 0 to 253> <encoding> <entry>
65 *
66 * Or alternatively if the previous entry length is greater than 253 bytes
67 * the following encoding is used:
68 *
69 * 0xFE <4 bytes unsigned little endian prevlen> <encoding> <entry>
70 *
71 * The encoding field of the entry depends on the content of the
72 * entry. When the entry is a string, the first 2 bits of the encoding first
73 * byte will hold the type of encoding used to store the length of the string,
74 * followed by the actual length of the string. When the entry is an integer
75 * the first 2 bits are both set to 1. The following 2 bits are used to specify
76 * what kind of integer will be stored after this header. An overview of the
77 * different types and encodings is as follows. The first byte is always enough
78 * to determine the kind of entry.
79 *
80 * |00pppppp| - 1 byte
81 * String value with length less than or equal to 63 bytes (6 bits).
82 * "pppppp" represents the unsigned 6 bit length.
83 * |01pppppp|qqqqqqqq| - 2 bytes
84 * String value with length less than or equal to 16383 bytes (14 bits).
85 * IMPORTANT: The 14 bit number is stored in big endian.
86 * |10000000|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
87 * String value with length greater than or equal to 16384 bytes.
88 * Only the 4 bytes following the first byte represents the length
89 * up to 32^2-1. The 6 lower bits of the first byte are not used and
90 * are set to zero.
91 * IMPORTANT: The 32 bit number is stored in big endian.
92 * |11000000| - 3 bytes
93 * Integer encoded as int16_t (2 bytes).
94 * |11010000| - 5 bytes
95 * Integer encoded as int32_t (4 bytes).
96 * |11100000| - 9 bytes
97 * Integer encoded as int64_t (8 bytes).
98 * |11110000| - 4 bytes
99 * Integer encoded as 24 bit signed (3 bytes).
100 * |11111110| - 2 bytes
101 * Integer encoded as 8 bit signed (1 byte).
102 * |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer.
103 * Unsigned integer from 0 to 12. The encoded value is actually from
104 * 1 to 13 because 0000 and 1111 can not be used, so 1 should be
105 * subtracted from the encoded 4 bit value to obtain the right value.
106 * |11111111| - End of ziplist special entry.
107 *
108 * Like for the ziplist header, all the integers are represented in little
109 * endian byte order, even when this code is compiled in big endian systems.
110 *
111 * EXAMPLES OF ACTUAL ZIPLISTS
112 * ===========================
113 *
114 * The following is a ziplist containing the two elements representing
115 * the strings "2" and "5". It is composed of 15 bytes, that we visually
116 * split into sections:
117 *
118 * [0f 00 00 00] [0c 00 00 00] [02 00] [00 f3] [02 f6] [ff]
119 * | | | | | |
120 * zlbytes zltail entries "2" "5" end
121 *
122 * The first 4 bytes represent the number 15, that is the number of bytes
123 * the whole ziplist is composed of. The second 4 bytes are the offset
124 * at which the last ziplist entry is found, that is 12, in fact the
125 * last entry, that is "5", is at offset 12 inside the ziplist.
126 * The next 16 bit integer represents the number of elements inside the
127 * ziplist, its value is 2 since there are just two elements inside.
128 * Finally "00 f3" is the first entry representing the number 2. It is
129 * composed of the previous entry length, which is zero because this is
130 * our first entry, and the byte F3 which corresponds to the encoding
131 * |1111xxxx| with xxxx between 0001 and 1101. We need to remove the "F"
132 * higher order bits 1111, and subtract 1 from the "3", so the entry value
133 * is "2". The next entry has a prevlen of 02, since the first entry is
134 * composed of exactly two bytes. The entry itself, F6, is encoded exactly
135 * like the first entry, and 6-1 = 5, so the value of the entry is 5.
136 * Finally the special entry FF signals the end of the ziplist.
137 *
138 * Adding another element to the above string with the value "Hello World"
139 * allows us to show how the ziplist encodes small strings. We'll just show
140 * the hex dump of the entry itself. Imagine the bytes as following the
141 * entry that stores "5" in the ziplist above:
142 *
143 * [02] [0b] [48 65 6c 6c 6f 20 57 6f 72 6c 64]
144 *
145 * The first byte, 02, is the length of the previous entry. The next
146 * byte represents the encoding in the pattern |00pppppp| that means
147 * that the entry is a string of length <pppppp>, so 0B means that
148 * an 11 bytes string follows. From the third byte (48) to the last (64)
149 * there are just the ASCII characters for "Hello World".
150 *
151 * ----------------------------------------------------------------------------
152 *
153 * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
154 * Copyright (c) 2009-2017, Salvatore Sanfilippo <antirez at gmail dot com>
155 * All rights reserved.
156 *
157 * Redistribution and use in source and binary forms, with or without
158 * modification, are permitted provided that the following conditions are met:
159 *
160 * * Redistributions of source code must retain the above copyright notice,
161 * this list of conditions and the following disclaimer.
162 * * Redistributions in binary form must reproduce the above copyright
163 * notice, this list of conditions and the following disclaimer in the
164 * documentation and/or other materials provided with the distribution.
165 * * Neither the name of Redis nor the names of its contributors may be used
166 * to endorse or promote products derived from this software without
167 * specific prior written permission.
168 *
169 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
170 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
173 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
174 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
175 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
176 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
177 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
178 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
179 * POSSIBILITY OF SUCH DAMAGE.
180 */
181
182 #include <stdio.h>
183 #include <stdlib.h>
184 #include <string.h>
185 #include <stdint.h>
186 #include <limits.h>
187 #include "zmalloc.h"
188 #include "util.h"
189 #include "ziplist.h"
190 #include "endianconv.h"
191 #include "redisassert.h"
192
193 #define ZIP_END 255 /* Special "end of ziplist" entry. */
194 #define ZIP_BIG_PREVLEN 254 /* Max number of bytes of the previous entry, for
195 the "prevlen" field prefixing each entry, to be
196 represented with just a single byte. Otherwise
197 it is represented as FF AA BB CC DD, where
198 AA BB CC DD are a 4 bytes unsigned integer
199 representing the previous entry len. */
200
201 /* Different encoding/length possibilities */
202 #define ZIP_STR_MASK 0xc0
203 #define ZIP_INT_MASK 0x30
204 #define ZIP_STR_06B (0 << 6)
205 #define ZIP_STR_14B (1 << 6)
206 #define ZIP_STR_32B (2 << 6)
207 #define ZIP_INT_16B (0xc0 | 0<<4)
208 #define ZIP_INT_32B (0xc0 | 1<<4)
209 #define ZIP_INT_64B (0xc0 | 2<<4)
210 #define ZIP_INT_24B (0xc0 | 3<<4)
211 #define ZIP_INT_8B 0xfe
212
213 /* 4 bit integer immediate encoding |1111xxxx| with xxxx between
214 * 0001 and 1101. */
215 #define ZIP_INT_IMM_MASK 0x0f /* Mask to extract the 4 bits value. To add
216 one is needed to reconstruct the value. */
217 #define ZIP_INT_IMM_MIN 0xf1 /* 11110001 */
218 #define ZIP_INT_IMM_MAX 0xfd /* 11111101 */
219
220 #define INT24_MAX 0x7fffff
221 #define INT24_MIN (-INT24_MAX - 1)
222
223 /* Macro to determine if the entry is a string. String entries never start
224 * with "11" as most significant bits of the first byte. */
225 #define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
226
227 /* Utility macros.*/
228
229 /* Return total bytes a ziplist is composed of. */
230 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
231
232 /* Return the offset of the last item inside the ziplist. */
233 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
234
235 /* Return the length of a ziplist, or UINT16_MAX if the length cannot be
236 * determined without scanning the whole ziplist. */
237 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
238
239 /* The size of a ziplist header: two 32 bit integers for the total
240 * bytes count and last item offset. One 16 bit integer for the number
241 * of items field. */
242 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
243
244 /* Size of the "end of ziplist" entry. Just one byte. */
245 #define ZIPLIST_END_SIZE (sizeof(uint8_t))
246
247 /* Return the pointer to the first entry of a ziplist. */
248 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
249
250 /* Return the pointer to the last entry of a ziplist, using the
251 * last entry offset inside the ziplist header. */
252 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
253
254 /* Return the pointer to the last byte of a ziplist, which is, the
255 * end of ziplist FF entry. */
256 #define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
257
258 /* Increment the number of items field in the ziplist header. Note that this
259 * macro should never overflow the unsigned 16 bit integer, since entries are
260 * always pushed one at a time. When UINT16_MAX is reached we want the count
261 * to stay there to signal that a full scan is needed to get the number of
262 * items inside the ziplist. */
263 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
264 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
265 ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
266 }
267
268 /* We use this function to receive information about a ziplist entry.
269 * Note that this is not how the data is actually encoded, is just what we
270 * get filled by a function in order to operate more easily. */
271 typedef struct zlentry {
272 unsigned int prevrawlensize; /* Bytes used to encode the previous entry len*/
273 unsigned int prevrawlen; /* Previous entry len. */
274 unsigned int lensize; /* Bytes used to encode this entry type/len.
275 For example strings have a 1, 2 or 5 bytes
276 header. Integers always use a single byte.*/
277 unsigned int len; /* Bytes used to represent the actual entry.
278 For strings this is just the string length
279 while for integers it is 1, 2, 3, 4, 8 or
280 0 (for 4 bit immediate) depending on the
281 number range. */
282 unsigned int headersize; /* prevrawlensize + lensize. */
283 unsigned char encoding; /* Set to ZIP_STR_* or ZIP_INT_* depending on
284 the entry encoding. However for 4 bits
285 immediate integers this can assume a range
286 of values and must be range-checked. */
287 unsigned char *p; /* Pointer to the very start of the entry, that
288 is, this points to prev-entry-len field. */
289 } zlentry;
290
291 #define ZIPLIST_ENTRY_ZERO(zle) { \
292 (zle)->prevrawlensize = (zle)->prevrawlen = 0; \
293 (zle)->lensize = (zle)->len = (zle)->headersize = 0; \
294 (zle)->encoding = 0; \
295 (zle)->p = NULL; \
296 }
297
298 /* Extract the encoding from the byte pointed by 'ptr' and set it into
299 * 'encoding' field of the zlentry structure. */
300 #define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
301 (encoding) = (ptr[0]); \
302 if ((encoding) < ZIP_STR_MASK) (encoding) &= ZIP_STR_MASK; \
303 } while(0)
304
305 /* Return bytes needed to store integer encoded by 'encoding'. */
zipIntSize(unsigned char encoding)306 unsigned int zipIntSize(unsigned char encoding) {
307 switch(encoding) {
308 case ZIP_INT_8B: return 1;
309 case ZIP_INT_16B: return 2;
310 case ZIP_INT_24B: return 3;
311 case ZIP_INT_32B: return 4;
312 case ZIP_INT_64B: return 8;
313 }
314 if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX)
315 return 0; /* 4 bit immediate */
316 panic("Invalid integer encoding 0x%02X", encoding);
317 return 0;
318 }
319
320 /* Write the encoidng header of the entry in 'p'. If p is NULL it just returns
321 * the amount of bytes required to encode such a length. Arguments:
322 *
323 * 'encoding' is the encoding we are using for the entry. It could be
324 * ZIP_INT_* or ZIP_STR_* or between ZIP_INT_IMM_MIN and ZIP_INT_IMM_MAX
325 * for single-byte small immediate integers.
326 *
327 * 'rawlen' is only used for ZIP_STR_* encodings and is the length of the
328 * srting that this entry represents.
329 *
330 * The function returns the number of bytes used by the encoding/length
331 * header stored in 'p'. */
zipStoreEntryEncoding(unsigned char * p,unsigned char encoding,unsigned int rawlen)332 unsigned int zipStoreEntryEncoding(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
333 unsigned char len = 1, buf[5];
334
335 if (ZIP_IS_STR(encoding)) {
336 /* Although encoding is given it may not be set for strings,
337 * so we determine it here using the raw length. */
338 if (rawlen <= 0x3f) {
339 if (!p) return len;
340 buf[0] = ZIP_STR_06B | rawlen;
341 } else if (rawlen <= 0x3fff) {
342 len += 1;
343 if (!p) return len;
344 buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
345 buf[1] = rawlen & 0xff;
346 } else {
347 len += 4;
348 if (!p) return len;
349 buf[0] = ZIP_STR_32B;
350 buf[1] = (rawlen >> 24) & 0xff;
351 buf[2] = (rawlen >> 16) & 0xff;
352 buf[3] = (rawlen >> 8) & 0xff;
353 buf[4] = rawlen & 0xff;
354 }
355 } else {
356 /* Implies integer encoding, so length is always 1. */
357 if (!p) return len;
358 buf[0] = encoding;
359 }
360
361 /* Store this length at p. */
362 memcpy(p,buf,len);
363 return len;
364 }
365
366 /* Decode the entry encoding type and data length (string length for strings,
367 * number of bytes used for the integer for integer entries) encoded in 'ptr'.
368 * The 'encoding' variable will hold the entry encoding, the 'lensize'
369 * variable will hold the number of bytes required to encode the entry
370 * length, and the 'len' variable will hold the entry length. */
371 #define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
372 ZIP_ENTRY_ENCODING((ptr), (encoding)); \
373 if ((encoding) < ZIP_STR_MASK) { \
374 if ((encoding) == ZIP_STR_06B) { \
375 (lensize) = 1; \
376 (len) = (ptr)[0] & 0x3f; \
377 } else if ((encoding) == ZIP_STR_14B) { \
378 (lensize) = 2; \
379 (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
380 } else if ((encoding) == ZIP_STR_32B) { \
381 (lensize) = 5; \
382 (len) = ((ptr)[1] << 24) | \
383 ((ptr)[2] << 16) | \
384 ((ptr)[3] << 8) | \
385 ((ptr)[4]); \
386 } else { \
387 panic("Invalid string encoding 0x%02X", (encoding)); \
388 } \
389 } else { \
390 (lensize) = 1; \
391 (len) = zipIntSize(encoding); \
392 } \
393 } while(0);
394
395 /* Encode the length of the previous entry and write it to "p". This only
396 * uses the larger encoding (required in __ziplistCascadeUpdate). */
zipStorePrevEntryLengthLarge(unsigned char * p,unsigned int len)397 int zipStorePrevEntryLengthLarge(unsigned char *p, unsigned int len) {
398 if (p != NULL) {
399 p[0] = ZIP_BIG_PREVLEN;
400 memcpy(p+1,&len,sizeof(len));
401 memrev32ifbe(p+1);
402 }
403 return 1+sizeof(len);
404 }
405
406 /* Encode the length of the previous entry and write it to "p". Return the
407 * number of bytes needed to encode this length if "p" is NULL. */
zipStorePrevEntryLength(unsigned char * p,unsigned int len)408 unsigned int zipStorePrevEntryLength(unsigned char *p, unsigned int len) {
409 if (p == NULL) {
410 return (len < ZIP_BIG_PREVLEN) ? 1 : sizeof(len)+1;
411 } else {
412 if (len < ZIP_BIG_PREVLEN) {
413 p[0] = len;
414 return 1;
415 } else {
416 return zipStorePrevEntryLengthLarge(p,len);
417 }
418 }
419 }
420
421 /* Return the number of bytes used to encode the length of the previous
422 * entry. The length is returned by setting the var 'prevlensize'. */
423 #define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
424 if ((ptr)[0] < ZIP_BIG_PREVLEN) { \
425 (prevlensize) = 1; \
426 } else { \
427 (prevlensize) = 5; \
428 } \
429 } while(0);
430
431 /* Return the length of the previous element, and the number of bytes that
432 * are used in order to encode the previous element length.
433 * 'ptr' must point to the prevlen prefix of an entry (that encodes the
434 * length of the previous entry in order to navigate the elements backward).
435 * The length of the previous entry is stored in 'prevlen', the number of
436 * bytes needed to encode the previous entry length are stored in
437 * 'prevlensize'. */
438 #define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
439 ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
440 if ((prevlensize) == 1) { \
441 (prevlen) = (ptr)[0]; \
442 } else if ((prevlensize) == 5) { \
443 assert(sizeof((prevlen)) == 4); \
444 memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
445 memrev32ifbe(&prevlen); \
446 } \
447 } while(0);
448
449 /* Given a pointer 'p' to the prevlen info that prefixes an entry, this
450 * function returns the difference in number of bytes needed to encode
451 * the prevlen if the previous entry changes of size.
452 *
453 * So if A is the number of bytes used right now to encode the 'prevlen'
454 * field.
455 *
456 * And B is the number of bytes that are needed in order to encode the
457 * 'prevlen' if the previous element will be updated to one of size 'len'.
458 *
459 * Then the function returns B - A
460 *
461 * So the function returns a positive number if more space is needed,
462 * a negative number if less space is needed, or zero if the same space
463 * is needed. */
zipPrevLenByteDiff(unsigned char * p,unsigned int len)464 int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
465 unsigned int prevlensize;
466 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
467 return zipStorePrevEntryLength(NULL, len) - prevlensize;
468 }
469
470 /* Return the total number of bytes used by the entry pointed to by 'p'. */
zipRawEntryLength(unsigned char * p)471 unsigned int zipRawEntryLength(unsigned char *p) {
472 unsigned int prevlensize, encoding, lensize, len;
473 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
474 ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
475 return prevlensize + lensize + len;
476 }
477
478 /* Check if string pointed to by 'entry' can be encoded as an integer.
479 * Stores the integer value in 'v' and its encoding in 'encoding'. */
zipTryEncoding(unsigned char * entry,unsigned int entrylen,long long * v,unsigned char * encoding)480 int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
481 long long value;
482
483 if (entrylen >= 32 || entrylen == 0) return 0;
484 if (string2ll((char*)entry,entrylen,&value)) {
485 /* Great, the string can be encoded. Check what's the smallest
486 * of our encoding types that can hold this value. */
487 if (value >= 0 && value <= 12) {
488 *encoding = ZIP_INT_IMM_MIN+value;
489 } else if (value >= INT8_MIN && value <= INT8_MAX) {
490 *encoding = ZIP_INT_8B;
491 } else if (value >= INT16_MIN && value <= INT16_MAX) {
492 *encoding = ZIP_INT_16B;
493 } else if (value >= INT24_MIN && value <= INT24_MAX) {
494 *encoding = ZIP_INT_24B;
495 } else if (value >= INT32_MIN && value <= INT32_MAX) {
496 *encoding = ZIP_INT_32B;
497 } else {
498 *encoding = ZIP_INT_64B;
499 }
500 *v = value;
501 return 1;
502 }
503 return 0;
504 }
505
506 /* Store integer 'value' at 'p', encoded as 'encoding' */
zipSaveInteger(unsigned char * p,int64_t value,unsigned char encoding)507 void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
508 int16_t i16;
509 int32_t i32;
510 int64_t i64;
511 if (encoding == ZIP_INT_8B) {
512 ((int8_t*)p)[0] = (int8_t)value;
513 } else if (encoding == ZIP_INT_16B) {
514 i16 = value;
515 memcpy(p,&i16,sizeof(i16));
516 memrev16ifbe(p);
517 } else if (encoding == ZIP_INT_24B) {
518 i32 = value<<8;
519 memrev32ifbe(&i32);
520 memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));
521 } else if (encoding == ZIP_INT_32B) {
522 i32 = value;
523 memcpy(p,&i32,sizeof(i32));
524 memrev32ifbe(p);
525 } else if (encoding == ZIP_INT_64B) {
526 i64 = value;
527 memcpy(p,&i64,sizeof(i64));
528 memrev64ifbe(p);
529 } else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {
530 /* Nothing to do, the value is stored in the encoding itself. */
531 } else {
532 assert(NULL);
533 }
534 }
535
536 /* Read integer encoded as 'encoding' from 'p' */
zipLoadInteger(unsigned char * p,unsigned char encoding)537 int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
538 int16_t i16;
539 int32_t i32;
540 int64_t i64, ret = 0;
541 if (encoding == ZIP_INT_8B) {
542 ret = ((int8_t*)p)[0];
543 } else if (encoding == ZIP_INT_16B) {
544 memcpy(&i16,p,sizeof(i16));
545 memrev16ifbe(&i16);
546 ret = i16;
547 } else if (encoding == ZIP_INT_32B) {
548 memcpy(&i32,p,sizeof(i32));
549 memrev32ifbe(&i32);
550 ret = i32;
551 } else if (encoding == ZIP_INT_24B) {
552 i32 = 0;
553 memcpy(((uint8_t*)&i32)+1,p,sizeof(i32)-sizeof(uint8_t));
554 memrev32ifbe(&i32);
555 ret = i32>>8;
556 } else if (encoding == ZIP_INT_64B) {
557 memcpy(&i64,p,sizeof(i64));
558 memrev64ifbe(&i64);
559 ret = i64;
560 } else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {
561 ret = (encoding & ZIP_INT_IMM_MASK)-1;
562 } else {
563 assert(NULL);
564 }
565 return ret;
566 }
567
568 /* Return a struct with all information about an entry. */
zipEntry(unsigned char * p,zlentry * e)569 void zipEntry(unsigned char *p, zlentry *e) {
570
571 ZIP_DECODE_PREVLEN(p, e->prevrawlensize, e->prevrawlen);
572 ZIP_DECODE_LENGTH(p + e->prevrawlensize, e->encoding, e->lensize, e->len);
573 e->headersize = e->prevrawlensize + e->lensize;
574 e->p = p;
575 }
576
577 /* Create a new empty ziplist. */
ziplistNew(void)578 unsigned char *ziplistNew(void) {
579 unsigned int bytes = ZIPLIST_HEADER_SIZE+ZIPLIST_END_SIZE;
580 unsigned char *zl = zmalloc(bytes);
581 ZIPLIST_BYTES(zl) = intrev32ifbe(bytes);
582 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE);
583 ZIPLIST_LENGTH(zl) = 0;
584 zl[bytes-1] = ZIP_END;
585 return zl;
586 }
587
588 /* Resize the ziplist. */
ziplistResize(unsigned char * zl,unsigned int len)589 unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
590 zl = zrealloc(zl,len);
591 ZIPLIST_BYTES(zl) = intrev32ifbe(len);
592 zl[len-1] = ZIP_END;
593 return zl;
594 }
595
596 /* When an entry is inserted, we need to set the prevlen field of the next
597 * entry to equal the length of the inserted entry. It can occur that this
598 * length cannot be encoded in 1 byte and the next entry needs to be grow
599 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
600 * because this only happens when an entry is already being inserted (which
601 * causes a realloc and memmove). However, encoding the prevlen may require
602 * that this entry is grown as well. This effect may cascade throughout
603 * the ziplist when there are consecutive entries with a size close to
604 * ZIP_BIG_PREVLEN, so we need to check that the prevlen can be encoded in
605 * every consecutive entry.
606 *
607 * Note that this effect can also happen in reverse, where the bytes required
608 * to encode the prevlen field can shrink. This effect is deliberately ignored,
609 * because it can cause a "flapping" effect where a chain prevlen fields is
610 * first grown and then shrunk again after consecutive inserts. Rather, the
611 * field is allowed to stay larger than necessary, because a large prevlen
612 * field implies the ziplist is holding large entries anyway.
613 *
614 * The pointer "p" points to the first entry that does NOT need to be
615 * updated, i.e. consecutive fields MAY need an update. */
__ziplistCascadeUpdate(unsigned char * zl,unsigned char * p)616 unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
617 size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), rawlen, rawlensize;
618 size_t offset, noffset, extra;
619 unsigned char *np;
620 zlentry cur, next;
621
622 while (p[0] != ZIP_END) {
623 zipEntry(p, &cur);
624 rawlen = cur.headersize + cur.len;
625 rawlensize = zipStorePrevEntryLength(NULL,rawlen);
626
627 /* Abort if there is no next entry. */
628 if (p[rawlen] == ZIP_END) break;
629 zipEntry(p+rawlen, &next);
630
631 /* Abort when "prevlen" has not changed. */
632 if (next.prevrawlen == rawlen) break;
633
634 if (next.prevrawlensize < rawlensize) {
635 /* The "prevlen" field of "next" needs more bytes to hold
636 * the raw length of "cur". */
637 offset = p-zl;
638 extra = rawlensize-next.prevrawlensize;
639 zl = ziplistResize(zl,curlen+extra);
640 p = zl+offset;
641
642 /* Current pointer and offset for next element. */
643 np = p+rawlen;
644 noffset = np-zl;
645
646 /* Update tail offset when next element is not the tail element. */
647 if ((zl+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))) != np) {
648 ZIPLIST_TAIL_OFFSET(zl) =
649 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra);
650 }
651
652 /* Move the tail to the back. */
653 memmove(np+rawlensize,
654 np+next.prevrawlensize,
655 curlen-noffset-next.prevrawlensize-1);
656 zipStorePrevEntryLength(np,rawlen);
657
658 /* Advance the cursor */
659 p += rawlen;
660 curlen += extra;
661 } else {
662 if (next.prevrawlensize > rawlensize) {
663 /* This would result in shrinking, which we want to avoid.
664 * So, set "rawlen" in the available bytes. */
665 zipStorePrevEntryLengthLarge(p+rawlen,rawlen);
666 } else {
667 zipStorePrevEntryLength(p+rawlen,rawlen);
668 }
669
670 /* Stop here, as the raw length of "next" has not changed. */
671 break;
672 }
673 }
674 return zl;
675 }
676
677 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
__ziplistDelete(unsigned char * zl,unsigned char * p,unsigned int num)678 unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
679 unsigned int i, totlen, deleted = 0;
680 size_t offset;
681 int nextdiff = 0;
682 zlentry first, tail;
683
684 zipEntry(p, &first);
685 for (i = 0; p[0] != ZIP_END && i < num; i++) {
686 p += zipRawEntryLength(p);
687 deleted++;
688 }
689
690 totlen = p-first.p; /* Bytes taken by the element(s) to delete. */
691 if (totlen > 0) {
692 if (p[0] != ZIP_END) {
693 /* Storing `prevrawlen` in this entry may increase or decrease the
694 * number of bytes required compare to the current `prevrawlen`.
695 * There always is room to store this, because it was previously
696 * stored by an entry that is now being deleted. */
697 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
698
699 /* Note that there is always space when p jumps backward: if
700 * the new previous entry is large, one of the deleted elements
701 * had a 5 bytes prevlen header, so there is for sure at least
702 * 5 bytes free and we need just 4. */
703 p -= nextdiff;
704 zipStorePrevEntryLength(p,first.prevrawlen);
705
706 /* Update offset for tail */
707 ZIPLIST_TAIL_OFFSET(zl) =
708 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))-totlen);
709
710 /* When the tail contains more than one entry, we need to take
711 * "nextdiff" in account as well. Otherwise, a change in the
712 * size of prevlen doesn't have an effect on the *tail* offset. */
713 zipEntry(p, &tail);
714 if (p[tail.headersize+tail.len] != ZIP_END) {
715 ZIPLIST_TAIL_OFFSET(zl) =
716 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
717 }
718
719 /* Move tail to the front of the ziplist */
720 memmove(first.p,p,
721 intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1);
722 } else {
723 /* The entire tail was deleted. No need to move memory. */
724 ZIPLIST_TAIL_OFFSET(zl) =
725 intrev32ifbe((first.p-zl)-first.prevrawlen);
726 }
727
728 /* Resize and update length */
729 offset = first.p-zl;
730 zl = ziplistResize(zl, intrev32ifbe(ZIPLIST_BYTES(zl))-totlen+nextdiff);
731 ZIPLIST_INCR_LENGTH(zl,-deleted);
732 p = zl+offset;
733
734 /* When nextdiff != 0, the raw length of the next entry has changed, so
735 * we need to cascade the update throughout the ziplist */
736 if (nextdiff != 0)
737 zl = __ziplistCascadeUpdate(zl,p);
738 }
739 return zl;
740 }
741
742 /* Insert item at "p". */
__ziplistInsert(unsigned char * zl,unsigned char * p,unsigned char * s,unsigned int slen)743 unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
744 size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen;
745 unsigned int prevlensize, prevlen = 0;
746 size_t offset;
747 int nextdiff = 0;
748 unsigned char encoding = 0;
749 long long value = 123456789; /* initialized to avoid warning. Using a value
750 that is easy to see if for some reason
751 we use it uninitialized. */
752 zlentry tail;
753
754 /* Find out prevlen for the entry that is inserted. */
755 if (p[0] != ZIP_END) {
756 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
757 } else {
758 unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
759 if (ptail[0] != ZIP_END) {
760 prevlen = zipRawEntryLength(ptail);
761 }
762 }
763
764 /* See if the entry can be encoded */
765 if (zipTryEncoding(s,slen,&value,&encoding)) {
766 /* 'encoding' is set to the appropriate integer encoding */
767 reqlen = zipIntSize(encoding);
768 } else {
769 /* 'encoding' is untouched, however zipStoreEntryEncoding will use the
770 * string length to figure out how to encode it. */
771 reqlen = slen;
772 }
773 /* We need space for both the length of the previous entry and
774 * the length of the payload. */
775 reqlen += zipStorePrevEntryLength(NULL,prevlen);
776 reqlen += zipStoreEntryEncoding(NULL,encoding,slen);
777
778 /* When the insert position is not equal to the tail, we need to
779 * make sure that the next entry can hold this entry's length in
780 * its prevlen field. */
781 int forcelarge = 0;
782 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
783 if (nextdiff == -4 && reqlen < 4) {
784 nextdiff = 0;
785 forcelarge = 1;
786 }
787
788 /* Store offset because a realloc may change the address of zl. */
789 offset = p-zl;
790 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
791 p = zl+offset;
792
793 /* Apply memory move when necessary and update tail offset. */
794 if (p[0] != ZIP_END) {
795 /* Subtract one because of the ZIP_END bytes */
796 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
797
798 /* Encode this entry's raw length in the next entry. */
799 if (forcelarge)
800 zipStorePrevEntryLengthLarge(p+reqlen,reqlen);
801 else
802 zipStorePrevEntryLength(p+reqlen,reqlen);
803
804 /* Update offset for tail */
805 ZIPLIST_TAIL_OFFSET(zl) =
806 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);
807
808 /* When the tail contains more than one entry, we need to take
809 * "nextdiff" in account as well. Otherwise, a change in the
810 * size of prevlen doesn't have an effect on the *tail* offset. */
811 zipEntry(p+reqlen, &tail);
812 if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {
813 ZIPLIST_TAIL_OFFSET(zl) =
814 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
815 }
816 } else {
817 /* This element will be the new tail. */
818 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);
819 }
820
821 /* When nextdiff != 0, the raw length of the next entry has changed, so
822 * we need to cascade the update throughout the ziplist */
823 if (nextdiff != 0) {
824 offset = p-zl;
825 zl = __ziplistCascadeUpdate(zl,p+reqlen);
826 p = zl+offset;
827 }
828
829 /* Write the entry */
830 p += zipStorePrevEntryLength(p,prevlen);
831 p += zipStoreEntryEncoding(p,encoding,slen);
832 if (ZIP_IS_STR(encoding)) {
833 memcpy(p,s,slen);
834 } else {
835 zipSaveInteger(p,value,encoding);
836 }
837 ZIPLIST_INCR_LENGTH(zl,1);
838 return zl;
839 }
840
841 /* Merge ziplists 'first' and 'second' by appending 'second' to 'first'.
842 *
843 * NOTE: The larger ziplist is reallocated to contain the new merged ziplist.
844 * Either 'first' or 'second' can be used for the result. The parameter not
845 * used will be free'd and set to NULL.
846 *
847 * After calling this function, the input parameters are no longer valid since
848 * they are changed and free'd in-place.
849 *
850 * The result ziplist is the contents of 'first' followed by 'second'.
851 *
852 * On failure: returns NULL if the merge is impossible.
853 * On success: returns the merged ziplist (which is expanded version of either
854 * 'first' or 'second', also frees the other unused input ziplist, and sets the
855 * input ziplist argument equal to newly reallocated ziplist return value. */
ziplistMerge(unsigned char ** first,unsigned char ** second)856 unsigned char *ziplistMerge(unsigned char **first, unsigned char **second) {
857 /* If any params are null, we can't merge, so NULL. */
858 if (first == NULL || *first == NULL || second == NULL || *second == NULL)
859 return NULL;
860
861 /* Can't merge same list into itself. */
862 if (*first == *second)
863 return NULL;
864
865 size_t first_bytes = intrev32ifbe(ZIPLIST_BYTES(*first));
866 size_t first_len = intrev16ifbe(ZIPLIST_LENGTH(*first));
867
868 size_t second_bytes = intrev32ifbe(ZIPLIST_BYTES(*second));
869 size_t second_len = intrev16ifbe(ZIPLIST_LENGTH(*second));
870
871 int append;
872 unsigned char *source, *target;
873 size_t target_bytes, source_bytes;
874 /* Pick the largest ziplist so we can resize easily in-place.
875 * We must also track if we are now appending or prepending to
876 * the target ziplist. */
877 if (first_len >= second_len) {
878 /* retain first, append second to first. */
879 target = *first;
880 target_bytes = first_bytes;
881 source = *second;
882 source_bytes = second_bytes;
883 append = 1;
884 } else {
885 /* else, retain second, prepend first to second. */
886 target = *second;
887 target_bytes = second_bytes;
888 source = *first;
889 source_bytes = first_bytes;
890 append = 0;
891 }
892
893 /* Calculate final bytes (subtract one pair of metadata) */
894 size_t zlbytes = first_bytes + second_bytes -
895 ZIPLIST_HEADER_SIZE - ZIPLIST_END_SIZE;
896 size_t zllength = first_len + second_len;
897
898 /* Combined zl length should be limited within UINT16_MAX */
899 zllength = zllength < UINT16_MAX ? zllength : UINT16_MAX;
900
901 /* Save offset positions before we start ripping memory apart. */
902 size_t first_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*first));
903 size_t second_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*second));
904
905 /* Extend target to new zlbytes then append or prepend source. */
906 target = zrealloc(target, zlbytes);
907 if (append) {
908 /* append == appending to target */
909 /* Copy source after target (copying over original [END]):
910 * [TARGET - END, SOURCE - HEADER] */
911 memcpy(target + target_bytes - ZIPLIST_END_SIZE,
912 source + ZIPLIST_HEADER_SIZE,
913 source_bytes - ZIPLIST_HEADER_SIZE);
914 } else {
915 /* !append == prepending to target */
916 /* Move target *contents* exactly size of (source - [END]),
917 * then copy source into vacataed space (source - [END]):
918 * [SOURCE - END, TARGET - HEADER] */
919 memmove(target + source_bytes - ZIPLIST_END_SIZE,
920 target + ZIPLIST_HEADER_SIZE,
921 target_bytes - ZIPLIST_HEADER_SIZE);
922 memcpy(target, source, source_bytes - ZIPLIST_END_SIZE);
923 }
924
925 /* Update header metadata. */
926 ZIPLIST_BYTES(target) = intrev32ifbe(zlbytes);
927 ZIPLIST_LENGTH(target) = intrev16ifbe(zllength);
928 /* New tail offset is:
929 * + N bytes of first ziplist
930 * - 1 byte for [END] of first ziplist
931 * + M bytes for the offset of the original tail of the second ziplist
932 * - J bytes for HEADER because second_offset keeps no header. */
933 ZIPLIST_TAIL_OFFSET(target) = intrev32ifbe(
934 (first_bytes - ZIPLIST_END_SIZE) +
935 (second_offset - ZIPLIST_HEADER_SIZE));
936
937 /* __ziplistCascadeUpdate just fixes the prev length values until it finds a
938 * correct prev length value (then it assumes the rest of the list is okay).
939 * We tell CascadeUpdate to start at the first ziplist's tail element to fix
940 * the merge seam. */
941 target = __ziplistCascadeUpdate(target, target+first_offset);
942
943 /* Now free and NULL out what we didn't realloc */
944 if (append) {
945 zfree(*second);
946 *second = NULL;
947 *first = target;
948 } else {
949 zfree(*first);
950 *first = NULL;
951 *second = target;
952 }
953 return target;
954 }
955
ziplistPush(unsigned char * zl,unsigned char * s,unsigned int slen,int where)956 unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
957 unsigned char *p;
958 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
959 return __ziplistInsert(zl,p,s,slen);
960 }
961
962 /* Returns an offset to use for iterating with ziplistNext. When the given
963 * index is negative, the list is traversed back to front. When the list
964 * doesn't contain an element at the provided index, NULL is returned. */
ziplistIndex(unsigned char * zl,int index)965 unsigned char *ziplistIndex(unsigned char *zl, int index) {
966 unsigned char *p;
967 unsigned int prevlensize, prevlen = 0;
968 if (index < 0) {
969 index = (-index)-1;
970 p = ZIPLIST_ENTRY_TAIL(zl);
971 if (p[0] != ZIP_END) {
972 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
973 while (prevlen > 0 && index--) {
974 p -= prevlen;
975 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
976 }
977 }
978 } else {
979 p = ZIPLIST_ENTRY_HEAD(zl);
980 while (p[0] != ZIP_END && index--) {
981 p += zipRawEntryLength(p);
982 }
983 }
984 return (p[0] == ZIP_END || index > 0) ? NULL : p;
985 }
986
987 /* Return pointer to next entry in ziplist.
988 *
989 * zl is the pointer to the ziplist
990 * p is the pointer to the current element
991 *
992 * The element after 'p' is returned, otherwise NULL if we are at the end. */
ziplistNext(unsigned char * zl,unsigned char * p)993 unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
994 ((void) zl);
995
996 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
997 * and we should return NULL. Otherwise, we should return NULL
998 * when the *next* element is ZIP_END (there is no next entry). */
999 if (p[0] == ZIP_END) {
1000 return NULL;
1001 }
1002
1003 p += zipRawEntryLength(p);
1004 if (p[0] == ZIP_END) {
1005 return NULL;
1006 }
1007
1008 return p;
1009 }
1010
1011 /* Return pointer to previous entry in ziplist. */
ziplistPrev(unsigned char * zl,unsigned char * p)1012 unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
1013 unsigned int prevlensize, prevlen = 0;
1014
1015 /* Iterating backwards from ZIP_END should return the tail. When "p" is
1016 * equal to the first element of the list, we're already at the head,
1017 * and should return NULL. */
1018 if (p[0] == ZIP_END) {
1019 p = ZIPLIST_ENTRY_TAIL(zl);
1020 return (p[0] == ZIP_END) ? NULL : p;
1021 } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
1022 return NULL;
1023 } else {
1024 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
1025 assert(prevlen > 0);
1026 return p-prevlen;
1027 }
1028 }
1029
1030 /* Get entry pointed to by 'p' and store in either '*sstr' or 'sval' depending
1031 * on the encoding of the entry. '*sstr' is always set to NULL to be able
1032 * to find out whether the string pointer or the integer value was set.
1033 * Return 0 if 'p' points to the end of the ziplist, 1 otherwise. */
ziplistGet(unsigned char * p,unsigned char ** sstr,unsigned int * slen,long long * sval)1034 unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
1035 zlentry entry;
1036 if (p == NULL || p[0] == ZIP_END) return 0;
1037 if (sstr) *sstr = NULL;
1038
1039 zipEntry(p, &entry);
1040 if (ZIP_IS_STR(entry.encoding)) {
1041 if (sstr) {
1042 *slen = entry.len;
1043 *sstr = p+entry.headersize;
1044 }
1045 } else {
1046 if (sval) {
1047 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
1048 }
1049 }
1050 return 1;
1051 }
1052
1053 /* Insert an entry at "p". */
ziplistInsert(unsigned char * zl,unsigned char * p,unsigned char * s,unsigned int slen)1054 unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
1055 return __ziplistInsert(zl,p,s,slen);
1056 }
1057
1058 /* Delete a single entry from the ziplist, pointed to by *p.
1059 * Also update *p in place, to be able to iterate over the
1060 * ziplist, while deleting entries. */
ziplistDelete(unsigned char * zl,unsigned char ** p)1061 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
1062 size_t offset = *p-zl;
1063 zl = __ziplistDelete(zl,*p,1);
1064
1065 /* Store pointer to current element in p, because ziplistDelete will
1066 * do a realloc which might result in a different "zl"-pointer.
1067 * When the delete direction is back to front, we might delete the last
1068 * entry and end up with "p" pointing to ZIP_END, so check this. */
1069 *p = zl+offset;
1070 return zl;
1071 }
1072
1073 /* Delete a range of entries from the ziplist. */
ziplistDeleteRange(unsigned char * zl,int index,unsigned int num)1074 unsigned char *ziplistDeleteRange(unsigned char *zl, int index, unsigned int num) {
1075 unsigned char *p = ziplistIndex(zl,index);
1076 return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
1077 }
1078
1079 /* Compare entry pointer to by 'p' with 'sstr' of length 'slen'. */
1080 /* Return 1 if equal. */
ziplistCompare(unsigned char * p,unsigned char * sstr,unsigned int slen)1081 unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
1082 zlentry entry;
1083 unsigned char sencoding;
1084 long long zval, sval;
1085 if (p[0] == ZIP_END) return 0;
1086
1087 zipEntry(p, &entry);
1088 if (ZIP_IS_STR(entry.encoding)) {
1089 /* Raw compare */
1090 if (entry.len == slen) {
1091 return memcmp(p+entry.headersize,sstr,slen) == 0;
1092 } else {
1093 return 0;
1094 }
1095 } else {
1096 /* Try to compare encoded values. Don't compare encoding because
1097 * different implementations may encoded integers differently. */
1098 if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
1099 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
1100 return zval == sval;
1101 }
1102 }
1103 return 0;
1104 }
1105
1106 /* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
1107 * between every comparison. Returns NULL when the field could not be found. */
ziplistFind(unsigned char * p,unsigned char * vstr,unsigned int vlen,unsigned int skip)1108 unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {
1109 int skipcnt = 0;
1110 unsigned char vencoding = 0;
1111 long long vll = 0;
1112
1113 while (p[0] != ZIP_END) {
1114 unsigned int prevlensize, encoding, lensize, len;
1115 unsigned char *q;
1116
1117 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
1118 ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
1119 q = p + prevlensize + lensize;
1120
1121 if (skipcnt == 0) {
1122 /* Compare current entry with specified entry */
1123 if (ZIP_IS_STR(encoding)) {
1124 if (len == vlen && memcmp(q, vstr, vlen) == 0) {
1125 return p;
1126 }
1127 } else {
1128 /* Find out if the searched field can be encoded. Note that
1129 * we do it only the first time, once done vencoding is set
1130 * to non-zero and vll is set to the integer value. */
1131 if (vencoding == 0) {
1132 if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
1133 /* If the entry can't be encoded we set it to
1134 * UCHAR_MAX so that we don't retry again the next
1135 * time. */
1136 vencoding = UCHAR_MAX;
1137 }
1138 /* Must be non-zero by now */
1139 assert(vencoding);
1140 }
1141
1142 /* Compare current entry with specified entry, do it only
1143 * if vencoding != UCHAR_MAX because if there is no encoding
1144 * possible for the field it can't be a valid integer. */
1145 if (vencoding != UCHAR_MAX) {
1146 long long ll = zipLoadInteger(q, encoding);
1147 if (ll == vll) {
1148 return p;
1149 }
1150 }
1151 }
1152
1153 /* Reset skip count */
1154 skipcnt = skip;
1155 } else {
1156 /* Skip entry */
1157 skipcnt--;
1158 }
1159
1160 /* Move to next entry */
1161 p = q + len;
1162 }
1163
1164 return NULL;
1165 }
1166
1167 /* Return length of ziplist. */
ziplistLen(unsigned char * zl)1168 unsigned int ziplistLen(unsigned char *zl) {
1169 unsigned int len = 0;
1170 if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) {
1171 len = intrev16ifbe(ZIPLIST_LENGTH(zl));
1172 } else {
1173 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
1174 while (*p != ZIP_END) {
1175 p += zipRawEntryLength(p);
1176 len++;
1177 }
1178
1179 /* Re-store length if small enough */
1180 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = intrev16ifbe(len);
1181 }
1182 return len;
1183 }
1184
1185 /* Return ziplist blob size in bytes. */
ziplistBlobLen(unsigned char * zl)1186 size_t ziplistBlobLen(unsigned char *zl) {
1187 return intrev32ifbe(ZIPLIST_BYTES(zl));
1188 }
1189
ziplistRepr(unsigned char * zl)1190 void ziplistRepr(unsigned char *zl) {
1191 unsigned char *p;
1192 int index = 0;
1193 zlentry entry;
1194
1195 printf(
1196 "{total bytes %d} "
1197 "{num entries %u}\n"
1198 "{tail offset %u}\n",
1199 intrev32ifbe(ZIPLIST_BYTES(zl)),
1200 intrev16ifbe(ZIPLIST_LENGTH(zl)),
1201 intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)));
1202 p = ZIPLIST_ENTRY_HEAD(zl);
1203 while(*p != ZIP_END) {
1204 zipEntry(p, &entry);
1205 printf(
1206 "{\n"
1207 "\taddr 0x%08lx,\n"
1208 "\tindex %2d,\n"
1209 "\toffset %5ld,\n"
1210 "\thdr+entry len: %5u,\n"
1211 "\thdr len%2u,\n"
1212 "\tprevrawlen: %5u,\n"
1213 "\tprevrawlensize: %2u,\n"
1214 "\tpayload %5u\n",
1215 (long unsigned)p,
1216 index,
1217 (unsigned long) (p-zl),
1218 entry.headersize+entry.len,
1219 entry.headersize,
1220 entry.prevrawlen,
1221 entry.prevrawlensize,
1222 entry.len);
1223 printf("\tbytes: ");
1224 for (unsigned int i = 0; i < entry.headersize+entry.len; i++) {
1225 printf("%02x|",p[i]);
1226 }
1227 printf("\n");
1228 p += entry.headersize;
1229 if (ZIP_IS_STR(entry.encoding)) {
1230 printf("\t[str]");
1231 if (entry.len > 40) {
1232 if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
1233 printf("...");
1234 } else {
1235 if (entry.len &&
1236 fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
1237 }
1238 } else {
1239 printf("\t[int]%lld", (long long) zipLoadInteger(p,entry.encoding));
1240 }
1241 printf("\n}\n");
1242 p += entry.len;
1243 index++;
1244 }
1245 printf("{end}\n\n");
1246 }
1247
1248 #ifdef REDIS_TEST
1249 #include <sys/time.h>
1250 #include "adlist.h"
1251 #include "sds.h"
1252
1253 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
1254
createList()1255 static unsigned char *createList() {
1256 unsigned char *zl = ziplistNew();
1257 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
1258 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
1259 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
1260 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
1261 return zl;
1262 }
1263
createIntList()1264 static unsigned char *createIntList() {
1265 unsigned char *zl = ziplistNew();
1266 char buf[32];
1267
1268 sprintf(buf, "100");
1269 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1270 sprintf(buf, "128000");
1271 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1272 sprintf(buf, "-100");
1273 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
1274 sprintf(buf, "4294967296");
1275 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
1276 sprintf(buf, "non integer");
1277 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1278 sprintf(buf, "much much longer non integer");
1279 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1280 return zl;
1281 }
1282
usec(void)1283 static long long usec(void) {
1284 struct timeval tv;
1285 gettimeofday(&tv,NULL);
1286 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
1287 }
1288
stress(int pos,int num,int maxsize,int dnum)1289 static void stress(int pos, int num, int maxsize, int dnum) {
1290 int i,j,k;
1291 unsigned char *zl;
1292 char posstr[2][5] = { "HEAD", "TAIL" };
1293 long long start;
1294 for (i = 0; i < maxsize; i+=dnum) {
1295 zl = ziplistNew();
1296 for (j = 0; j < i; j++) {
1297 zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
1298 }
1299
1300 /* Do num times a push+pop from pos */
1301 start = usec();
1302 for (k = 0; k < num; k++) {
1303 zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
1304 zl = ziplistDeleteRange(zl,0,1);
1305 }
1306 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
1307 i,intrev32ifbe(ZIPLIST_BYTES(zl)),num,posstr[pos],usec()-start);
1308 zfree(zl);
1309 }
1310 }
1311
pop(unsigned char * zl,int where)1312 static unsigned char *pop(unsigned char *zl, int where) {
1313 unsigned char *p, *vstr;
1314 unsigned int vlen;
1315 long long vlong;
1316
1317 p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
1318 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
1319 if (where == ZIPLIST_HEAD)
1320 printf("Pop head: ");
1321 else
1322 printf("Pop tail: ");
1323
1324 if (vstr) {
1325 if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
1326 }
1327 else {
1328 printf("%lld", vlong);
1329 }
1330
1331 printf("\n");
1332 return ziplistDelete(zl,&p);
1333 } else {
1334 printf("ERROR: Could not pop\n");
1335 exit(1);
1336 }
1337 }
1338
randstring(char * target,unsigned int min,unsigned int max)1339 static int randstring(char *target, unsigned int min, unsigned int max) {
1340 int p = 0;
1341 int len = min+rand()%(max-min+1);
1342 int minval, maxval;
1343 switch(rand() % 3) {
1344 case 0:
1345 minval = 0;
1346 maxval = 255;
1347 break;
1348 case 1:
1349 minval = 48;
1350 maxval = 122;
1351 break;
1352 case 2:
1353 minval = 48;
1354 maxval = 52;
1355 break;
1356 default:
1357 assert(NULL);
1358 }
1359
1360 while(p < len)
1361 target[p++] = minval+rand()%(maxval-minval+1);
1362 return len;
1363 }
1364
verify(unsigned char * zl,zlentry * e)1365 static void verify(unsigned char *zl, zlentry *e) {
1366 int len = ziplistLen(zl);
1367 zlentry _e;
1368
1369 ZIPLIST_ENTRY_ZERO(&_e);
1370
1371 for (int i = 0; i < len; i++) {
1372 memset(&e[i], 0, sizeof(zlentry));
1373 zipEntry(ziplistIndex(zl, i), &e[i]);
1374
1375 memset(&_e, 0, sizeof(zlentry));
1376 zipEntry(ziplistIndex(zl, -len+i), &_e);
1377
1378 assert(memcmp(&e[i], &_e, sizeof(zlentry)) == 0);
1379 }
1380 }
1381
ziplistTest(int argc,char ** argv)1382 int ziplistTest(int argc, char **argv) {
1383 unsigned char *zl, *p;
1384 unsigned char *entry;
1385 unsigned int elen;
1386 long long value;
1387
1388 /* If an argument is given, use it as the random seed. */
1389 if (argc == 2)
1390 srand(atoi(argv[1]));
1391
1392 zl = createIntList();
1393 ziplistRepr(zl);
1394
1395 zfree(zl);
1396
1397 zl = createList();
1398 ziplistRepr(zl);
1399
1400 zl = pop(zl,ZIPLIST_TAIL);
1401 ziplistRepr(zl);
1402
1403 zl = pop(zl,ZIPLIST_HEAD);
1404 ziplistRepr(zl);
1405
1406 zl = pop(zl,ZIPLIST_TAIL);
1407 ziplistRepr(zl);
1408
1409 zl = pop(zl,ZIPLIST_TAIL);
1410 ziplistRepr(zl);
1411
1412 zfree(zl);
1413
1414 printf("Get element at index 3:\n");
1415 {
1416 zl = createList();
1417 p = ziplistIndex(zl, 3);
1418 if (!ziplistGet(p, &entry, &elen, &value)) {
1419 printf("ERROR: Could not access index 3\n");
1420 return 1;
1421 }
1422 if (entry) {
1423 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1424 printf("\n");
1425 } else {
1426 printf("%lld\n", value);
1427 }
1428 printf("\n");
1429 zfree(zl);
1430 }
1431
1432 printf("Get element at index 4 (out of range):\n");
1433 {
1434 zl = createList();
1435 p = ziplistIndex(zl, 4);
1436 if (p == NULL) {
1437 printf("No entry\n");
1438 } else {
1439 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1440 return 1;
1441 }
1442 printf("\n");
1443 zfree(zl);
1444 }
1445
1446 printf("Get element at index -1 (last element):\n");
1447 {
1448 zl = createList();
1449 p = ziplistIndex(zl, -1);
1450 if (!ziplistGet(p, &entry, &elen, &value)) {
1451 printf("ERROR: Could not access index -1\n");
1452 return 1;
1453 }
1454 if (entry) {
1455 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1456 printf("\n");
1457 } else {
1458 printf("%lld\n", value);
1459 }
1460 printf("\n");
1461 zfree(zl);
1462 }
1463
1464 printf("Get element at index -4 (first element):\n");
1465 {
1466 zl = createList();
1467 p = ziplistIndex(zl, -4);
1468 if (!ziplistGet(p, &entry, &elen, &value)) {
1469 printf("ERROR: Could not access index -4\n");
1470 return 1;
1471 }
1472 if (entry) {
1473 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1474 printf("\n");
1475 } else {
1476 printf("%lld\n", value);
1477 }
1478 printf("\n");
1479 zfree(zl);
1480 }
1481
1482 printf("Get element at index -5 (reverse out of range):\n");
1483 {
1484 zl = createList();
1485 p = ziplistIndex(zl, -5);
1486 if (p == NULL) {
1487 printf("No entry\n");
1488 } else {
1489 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1490 return 1;
1491 }
1492 printf("\n");
1493 zfree(zl);
1494 }
1495
1496 printf("Iterate list from 0 to end:\n");
1497 {
1498 zl = createList();
1499 p = ziplistIndex(zl, 0);
1500 while (ziplistGet(p, &entry, &elen, &value)) {
1501 printf("Entry: ");
1502 if (entry) {
1503 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1504 } else {
1505 printf("%lld", value);
1506 }
1507 p = ziplistNext(zl,p);
1508 printf("\n");
1509 }
1510 printf("\n");
1511 zfree(zl);
1512 }
1513
1514 printf("Iterate list from 1 to end:\n");
1515 {
1516 zl = createList();
1517 p = ziplistIndex(zl, 1);
1518 while (ziplistGet(p, &entry, &elen, &value)) {
1519 printf("Entry: ");
1520 if (entry) {
1521 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1522 } else {
1523 printf("%lld", value);
1524 }
1525 p = ziplistNext(zl,p);
1526 printf("\n");
1527 }
1528 printf("\n");
1529 zfree(zl);
1530 }
1531
1532 printf("Iterate list from 2 to end:\n");
1533 {
1534 zl = createList();
1535 p = ziplistIndex(zl, 2);
1536 while (ziplistGet(p, &entry, &elen, &value)) {
1537 printf("Entry: ");
1538 if (entry) {
1539 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1540 } else {
1541 printf("%lld", value);
1542 }
1543 p = ziplistNext(zl,p);
1544 printf("\n");
1545 }
1546 printf("\n");
1547 zfree(zl);
1548 }
1549
1550 printf("Iterate starting out of range:\n");
1551 {
1552 zl = createList();
1553 p = ziplistIndex(zl, 4);
1554 if (!ziplistGet(p, &entry, &elen, &value)) {
1555 printf("No entry\n");
1556 } else {
1557 printf("ERROR\n");
1558 }
1559 printf("\n");
1560 zfree(zl);
1561 }
1562
1563 printf("Iterate from back to front:\n");
1564 {
1565 zl = createList();
1566 p = ziplistIndex(zl, -1);
1567 while (ziplistGet(p, &entry, &elen, &value)) {
1568 printf("Entry: ");
1569 if (entry) {
1570 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1571 } else {
1572 printf("%lld", value);
1573 }
1574 p = ziplistPrev(zl,p);
1575 printf("\n");
1576 }
1577 printf("\n");
1578 zfree(zl);
1579 }
1580
1581 printf("Iterate from back to front, deleting all items:\n");
1582 {
1583 zl = createList();
1584 p = ziplistIndex(zl, -1);
1585 while (ziplistGet(p, &entry, &elen, &value)) {
1586 printf("Entry: ");
1587 if (entry) {
1588 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1589 } else {
1590 printf("%lld", value);
1591 }
1592 zl = ziplistDelete(zl,&p);
1593 p = ziplistPrev(zl,p);
1594 printf("\n");
1595 }
1596 printf("\n");
1597 zfree(zl);
1598 }
1599
1600 printf("Delete inclusive range 0,0:\n");
1601 {
1602 zl = createList();
1603 zl = ziplistDeleteRange(zl, 0, 1);
1604 ziplistRepr(zl);
1605 zfree(zl);
1606 }
1607
1608 printf("Delete inclusive range 0,1:\n");
1609 {
1610 zl = createList();
1611 zl = ziplistDeleteRange(zl, 0, 2);
1612 ziplistRepr(zl);
1613 zfree(zl);
1614 }
1615
1616 printf("Delete inclusive range 1,2:\n");
1617 {
1618 zl = createList();
1619 zl = ziplistDeleteRange(zl, 1, 2);
1620 ziplistRepr(zl);
1621 zfree(zl);
1622 }
1623
1624 printf("Delete with start index out of range:\n");
1625 {
1626 zl = createList();
1627 zl = ziplistDeleteRange(zl, 5, 1);
1628 ziplistRepr(zl);
1629 zfree(zl);
1630 }
1631
1632 printf("Delete with num overflow:\n");
1633 {
1634 zl = createList();
1635 zl = ziplistDeleteRange(zl, 1, 5);
1636 ziplistRepr(zl);
1637 zfree(zl);
1638 }
1639
1640 printf("Delete foo while iterating:\n");
1641 {
1642 zl = createList();
1643 p = ziplistIndex(zl,0);
1644 while (ziplistGet(p,&entry,&elen,&value)) {
1645 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
1646 printf("Delete foo\n");
1647 zl = ziplistDelete(zl,&p);
1648 } else {
1649 printf("Entry: ");
1650 if (entry) {
1651 if (elen && fwrite(entry,elen,1,stdout) == 0)
1652 perror("fwrite");
1653 } else {
1654 printf("%lld",value);
1655 }
1656 p = ziplistNext(zl,p);
1657 printf("\n");
1658 }
1659 }
1660 printf("\n");
1661 ziplistRepr(zl);
1662 zfree(zl);
1663 }
1664
1665 printf("Regression test for >255 byte strings:\n");
1666 {
1667 char v1[257] = {0}, v2[257] = {0};
1668 memset(v1,'x',256);
1669 memset(v2,'y',256);
1670 zl = ziplistNew();
1671 zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
1672 zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
1673
1674 /* Pop values again and compare their value. */
1675 p = ziplistIndex(zl,0);
1676 assert(ziplistGet(p,&entry,&elen,&value));
1677 assert(strncmp(v1,(char*)entry,elen) == 0);
1678 p = ziplistIndex(zl,1);
1679 assert(ziplistGet(p,&entry,&elen,&value));
1680 assert(strncmp(v2,(char*)entry,elen) == 0);
1681 printf("SUCCESS\n\n");
1682 zfree(zl);
1683 }
1684
1685 printf("Regression test deleting next to last entries:\n");
1686 {
1687 char v[3][257] = {{0}};
1688 zlentry e[3] = {{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0,
1689 .len = 0, .headersize = 0, .encoding = 0, .p = NULL}};
1690 size_t i;
1691
1692 for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {
1693 memset(v[i], 'a' + i, sizeof(v[0]));
1694 }
1695
1696 v[0][256] = '\0';
1697 v[1][ 1] = '\0';
1698 v[2][256] = '\0';
1699
1700 zl = ziplistNew();
1701 for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {
1702 zl = ziplistPush(zl, (unsigned char *) v[i], strlen(v[i]), ZIPLIST_TAIL);
1703 }
1704
1705 verify(zl, e);
1706
1707 assert(e[0].prevrawlensize == 1);
1708 assert(e[1].prevrawlensize == 5);
1709 assert(e[2].prevrawlensize == 1);
1710
1711 /* Deleting entry 1 will increase `prevrawlensize` for entry 2 */
1712 unsigned char *p = e[1].p;
1713 zl = ziplistDelete(zl, &p);
1714
1715 verify(zl, e);
1716
1717 assert(e[0].prevrawlensize == 1);
1718 assert(e[1].prevrawlensize == 5);
1719
1720 printf("SUCCESS\n\n");
1721 zfree(zl);
1722 }
1723
1724 printf("Create long list and check indices:\n");
1725 {
1726 zl = ziplistNew();
1727 char buf[32];
1728 int i,len;
1729 for (i = 0; i < 1000; i++) {
1730 len = sprintf(buf,"%d",i);
1731 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
1732 }
1733 for (i = 0; i < 1000; i++) {
1734 p = ziplistIndex(zl,i);
1735 assert(ziplistGet(p,NULL,NULL,&value));
1736 assert(i == value);
1737
1738 p = ziplistIndex(zl,-i-1);
1739 assert(ziplistGet(p,NULL,NULL,&value));
1740 assert(999-i == value);
1741 }
1742 printf("SUCCESS\n\n");
1743 zfree(zl);
1744 }
1745
1746 printf("Compare strings with ziplist entries:\n");
1747 {
1748 zl = createList();
1749 p = ziplistIndex(zl,0);
1750 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1751 printf("ERROR: not \"hello\"\n");
1752 return 1;
1753 }
1754 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1755 printf("ERROR: \"hella\"\n");
1756 return 1;
1757 }
1758
1759 p = ziplistIndex(zl,3);
1760 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1761 printf("ERROR: not \"1024\"\n");
1762 return 1;
1763 }
1764 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1765 printf("ERROR: \"1025\"\n");
1766 return 1;
1767 }
1768 printf("SUCCESS\n\n");
1769 zfree(zl);
1770 }
1771
1772 printf("Merge test:\n");
1773 {
1774 /* create list gives us: [hello, foo, quux, 1024] */
1775 zl = createList();
1776 unsigned char *zl2 = createList();
1777
1778 unsigned char *zl3 = ziplistNew();
1779 unsigned char *zl4 = ziplistNew();
1780
1781 if (ziplistMerge(&zl4, &zl4)) {
1782 printf("ERROR: Allowed merging of one ziplist into itself.\n");
1783 return 1;
1784 }
1785
1786 /* Merge two empty ziplists, get empty result back. */
1787 zl4 = ziplistMerge(&zl3, &zl4);
1788 ziplistRepr(zl4);
1789 if (ziplistLen(zl4)) {
1790 printf("ERROR: Merging two empty ziplists created entries.\n");
1791 return 1;
1792 }
1793 zfree(zl4);
1794
1795 zl2 = ziplistMerge(&zl, &zl2);
1796 /* merge gives us: [hello, foo, quux, 1024, hello, foo, quux, 1024] */
1797 ziplistRepr(zl2);
1798
1799 if (ziplistLen(zl2) != 8) {
1800 printf("ERROR: Merged length not 8, but: %u\n", ziplistLen(zl2));
1801 return 1;
1802 }
1803
1804 p = ziplistIndex(zl2,0);
1805 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1806 printf("ERROR: not \"hello\"\n");
1807 return 1;
1808 }
1809 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1810 printf("ERROR: \"hella\"\n");
1811 return 1;
1812 }
1813
1814 p = ziplistIndex(zl2,3);
1815 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1816 printf("ERROR: not \"1024\"\n");
1817 return 1;
1818 }
1819 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1820 printf("ERROR: \"1025\"\n");
1821 return 1;
1822 }
1823
1824 p = ziplistIndex(zl2,4);
1825 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1826 printf("ERROR: not \"hello\"\n");
1827 return 1;
1828 }
1829 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1830 printf("ERROR: \"hella\"\n");
1831 return 1;
1832 }
1833
1834 p = ziplistIndex(zl2,7);
1835 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1836 printf("ERROR: not \"1024\"\n");
1837 return 1;
1838 }
1839 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1840 printf("ERROR: \"1025\"\n");
1841 return 1;
1842 }
1843 printf("SUCCESS\n\n");
1844 zfree(zl);
1845 }
1846
1847 printf("Stress with random payloads of different encoding:\n");
1848 {
1849 int i,j,len,where;
1850 unsigned char *p;
1851 char buf[1024];
1852 int buflen;
1853 list *ref;
1854 listNode *refnode;
1855
1856 /* Hold temp vars from ziplist */
1857 unsigned char *sstr;
1858 unsigned int slen;
1859 long long sval;
1860
1861 for (i = 0; i < 20000; i++) {
1862 zl = ziplistNew();
1863 ref = listCreate();
1864 listSetFreeMethod(ref,(void (*)(void*))sdsfree);
1865 len = rand() % 256;
1866
1867 /* Create lists */
1868 for (j = 0; j < len; j++) {
1869 where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
1870 if (rand() % 2) {
1871 buflen = randstring(buf,1,sizeof(buf)-1);
1872 } else {
1873 switch(rand() % 3) {
1874 case 0:
1875 buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20);
1876 break;
1877 case 1:
1878 buflen = sprintf(buf,"%lld",(0LL + rand()));
1879 break;
1880 case 2:
1881 buflen = sprintf(buf,"%lld",(0LL + rand()) << 20);
1882 break;
1883 default:
1884 assert(NULL);
1885 }
1886 }
1887
1888 /* Add to ziplist */
1889 zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);
1890
1891 /* Add to reference list */
1892 if (where == ZIPLIST_HEAD) {
1893 listAddNodeHead(ref,sdsnewlen(buf, buflen));
1894 } else if (where == ZIPLIST_TAIL) {
1895 listAddNodeTail(ref,sdsnewlen(buf, buflen));
1896 } else {
1897 assert(NULL);
1898 }
1899 }
1900
1901 assert(listLength(ref) == ziplistLen(zl));
1902 for (j = 0; j < len; j++) {
1903 /* Naive way to get elements, but similar to the stresser
1904 * executed from the Tcl test suite. */
1905 p = ziplistIndex(zl,j);
1906 refnode = listIndex(ref,j);
1907
1908 assert(ziplistGet(p,&sstr,&slen,&sval));
1909 if (sstr == NULL) {
1910 buflen = sprintf(buf,"%lld",sval);
1911 } else {
1912 buflen = slen;
1913 memcpy(buf,sstr,buflen);
1914 buf[buflen] = '\0';
1915 }
1916 assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);
1917 }
1918 zfree(zl);
1919 listRelease(ref);
1920 }
1921 printf("SUCCESS\n\n");
1922 }
1923
1924 printf("Stress with variable ziplist size:\n");
1925 {
1926 stress(ZIPLIST_HEAD,100000,16384,256);
1927 stress(ZIPLIST_TAIL,100000,16384,256);
1928 }
1929
1930 return 0;
1931 }
1932 #endif
1933