MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / lpEncodeBacklen

Function lpEncodeBacklen

src/listpack.c:328–363  ·  view source on GitHub ↗

Store a reverse-encoded variable length field, representing the length * of the previous element of size 'l', in the target buffer 'buf'. * The function returns the number of bytes used to encode it, from * 1 to 5. If 'buf' is NULL the function just returns the number of bytes * needed in order to encode the backlen. */

Source from the content-addressed store, hash-verified

326 * 1 to 5. If 'buf' is NULL the function just returns the number of bytes
327 * needed in order to encode the backlen. */
328unsigned long lpEncodeBacklen(unsigned char *buf, uint64_t l) {
329 if (l <= 127) {
330 if (buf) buf[0] = l;
331 return 1;
332 } else if (l < 16383) {
333 if (buf) {
334 buf[0] = l>>7;
335 buf[1] = (l&127)|128;
336 }
337 return 2;
338 } else if (l < 2097151) {
339 if (buf) {
340 buf[0] = l>>14;
341 buf[1] = ((l>>7)&127)|128;
342 buf[2] = (l&127)|128;
343 }
344 return 3;
345 } else if (l < 268435455) {
346 if (buf) {
347 buf[0] = l>>21;
348 buf[1] = ((l>>14)&127)|128;
349 buf[2] = ((l>>7)&127)|128;
350 buf[3] = (l&127)|128;
351 }
352 return 4;
353 } else {
354 if (buf) {
355 buf[0] = l>>28;
356 buf[1] = ((l>>21)&127)|128;
357 buf[2] = ((l>>14)&127)|128;
358 buf[3] = ((l>>7)&127)|128;
359 buf[4] = (l&127)|128;
360 }
361 return 5;
362 }
363}
364
365/* Decode the backlen and returns it. If the encoding looks invalid (more than
366 * 5 bytes are used), UINT64_MAX is returned to report the problem. */

Callers 4

lpSkipFunction · 0.85
lpPrevFunction · 0.85
lpInsertFunction · 0.85
lpValidateNextFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected