1 /* Bit operations. 2 * 3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * * Neither the name of Redis nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without 16 * specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "redis.h" 32 33 /* ----------------------------------------------------------------------------- 34 * Helpers and low level bit functions. 35 * -------------------------------------------------------------------------- */ 36 37 /* This helper function used by GETBIT / SETBIT parses the bit offset argument 38 * making sure an error is returned if it is negative or if it overflows 39 * Redis 512 MB limit for the string value. */ 40 static int getBitOffsetFromArgument(redisClient *c, robj *o, size_t *offset) { 41 long long loffset; 42 char *err = "bit offset is not an integer or out of range"; 43 44 if (getLongLongFromObjectOrReply(c,o,&loffset,err) != REDIS_OK) 45 return REDIS_ERR; 46 47 /* Limit offset to 512MB in bytes */ 48 if ((loffset < 0) || ((unsigned long long)loffset >> 3) >= (512*1024*1024)) 49 { 50 addReplyError(c,err); 51 return REDIS_ERR; 52 } 53 54 *offset = (size_t)loffset; 55 return REDIS_OK; 56 } 57 58 /* Count number of bits set in the binary array pointed by 's' and long 59 * 'count' bytes. The implementation of this function is required to 60 * work with a input string length up to 512 MB. */ 61 size_t redisPopcount(void *s, long count) { 62 size_t bits = 0; 63 unsigned char *p = s; 64 uint32_t *p4; 65 static const unsigned char bitsinbyte[256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8}; 66 67 /* Count initial bytes not aligned to 32 bit. */ 68 while((unsigned long)p & 3 && count) { 69 bits += bitsinbyte[*p++]; 70 count--; 71 } 72 73 /* Count bits 16 bytes at a time */ 74 p4 = (uint32_t*)p; 75 while(count>=16) { 76 uint32_t aux1, aux2, aux3, aux4; 77 78 aux1 = *p4++; 79 aux2 = *p4++; 80 aux3 = *p4++; 81 aux4 = *p4++; 82 count -= 16; 83 84 aux1 = aux1 - ((aux1 >> 1) & 0x55555555); 85 aux1 = (aux1 & 0x33333333) + ((aux1 >> 2) & 0x33333333); 86 aux2 = aux2 - ((aux2 >> 1) & 0x55555555); 87 aux2 = (aux2 & 0x33333333) + ((aux2 >> 2) & 0x33333333); 88 aux3 = aux3 - ((aux3 >> 1) & 0x55555555); 89 aux3 = (aux3 & 0x33333333) + ((aux3 >> 2) & 0x33333333); 90 aux4 = aux4 - ((aux4 >> 1) & 0x55555555); 91 aux4 = (aux4 & 0x33333333) + ((aux4 >> 2) & 0x33333333); 92 bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) + 93 ((((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) + 94 ((((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) + 95 ((((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24); 96 } 97 /* Count the remaining bytes. */ 98 p = (unsigned char*)p4; 99 while(count--) bits += bitsinbyte[*p++]; 100 return bits; 101 } 102 103 /* Return the position of the first bit set to one (if 'bit' is 1) or 104 * zero (if 'bit' is 0) in the bitmap starting at 's' and long 'count' bytes. 105 * 106 * The function is guaranteed to return a value >= 0 if 'bit' is 0 since if 107 * no zero bit is found, it returns count*8 assuming the string is zero 108 * padded on the right. However if 'bit' is 1 it is possible that there is 109 * not a single set bit in the bitmap. In this special case -1 is returned. */ 110 long redisBitpos(void *s, long count, int bit) { 111 unsigned long *l; 112 unsigned char *c; 113 unsigned long skipval, word = 0, one; 114 long pos = 0; /* Position of bit, to return to the caller. */ 115 int j; 116 117 /* Process whole words first, seeking for first word that is not 118 * all ones or all zeros respectively if we are lookig for zeros 119 * or ones. This is much faster with large strings having contiguous 120 * blocks of 1 or 0 bits compared to the vanilla bit per bit processing. 121 * 122 * Note that if we start from an address that is not aligned 123 * to sizeof(unsigned long) we consume it byte by byte until it is 124 * aligned. */ 125 126 /* Skip initial bits not aligned to sizeof(unsigned long) byte by byte. */ 127 skipval = bit ? 0 : UCHAR_MAX; 128 c = (unsigned char*) s; 129 while((unsigned long)c & (sizeof(*l)-1) && count) { 130 if (*c != skipval) break; 131 c++; 132 count--; 133 pos += 8; 134 } 135 136 /* Skip bits with full word step. */ 137 skipval = bit ? 0 : ULONG_MAX; 138 l = (unsigned long*) c; 139 while (count >= sizeof(*l)) { 140 if (*l != skipval) break; 141 l++; 142 count -= sizeof(*l); 143 pos += sizeof(*l)*8; 144 } 145 146 /* Load bytes into "word" considering the first byte as the most significant 147 * (we basically consider it as written in big endian, since we consider the 148 * string as a set of bits from left to right, with the first bit at position 149 * zero. 150 * 151 * Note that the loading is designed to work even when the bytes left 152 * (count) are less than a full word. We pad it with zero on the right. */ 153 c = (unsigned char*)l; 154 for (j = 0; j < sizeof(*l); j++) { 155 word <<= 8; 156 if (count) { 157 word |= *c; 158 c++; 159 count--; 160 } 161 } 162 163 /* Special case: 164 * If bits in the string are all zero and we are looking for one, 165 * return -1 to signal that there is not a single "1" in the whole 166 * string. This can't happen when we are looking for "0" as we assume 167 * that the right of the string is zero padded. */ 168 if (bit == 1 && word == 0) return -1; 169 170 /* Last word left, scan bit by bit. The first thing we need is to 171 * have a single "1" set in the most significant position in an 172 * unsigned long. We don't know the size of the long so we use a 173 * simple trick. */ 174 one = ULONG_MAX; /* All bits set to 1.*/ 175 one >>= 1; /* All bits set to 1 but the MSB. */ 176 one = ~one; /* All bits set to 0 but the MSB. */ 177 178 while(one) { 179 if (((one & word) != 0) == bit) return pos; 180 pos++; 181 one >>= 1; 182 } 183 184 /* If we reached this point, there is a bug in the algorithm, since 185 * the case of no match is handled as a special case before. */ 186 redisPanic("End of redisBitpos() reached."); 187 return 0; /* Just to avoid warnings. */ 188 } 189 190 /* ----------------------------------------------------------------------------- 191 * Bits related string commands: GETBIT, SETBIT, BITCOUNT, BITOP. 192 * -------------------------------------------------------------------------- */ 193 194 #define BITOP_AND 0 195 #define BITOP_OR 1 196 #define BITOP_XOR 2 197 #define BITOP_NOT 3 198 199 /* SETBIT key offset bitvalue */ 200 void setbitCommand(redisClient *c) { 201 robj *o; 202 char *err = "bit is not an integer or out of range"; 203 size_t bitoffset; 204 int byte, bit; 205 int byteval, bitval; 206 long on; 207 208 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK) 209 return; 210 211 if (getLongFromObjectOrReply(c,c->argv[3],&on,err) != REDIS_OK) 212 return; 213 214 /* Bits can only be set or cleared... */ 215 if (on & ~1) { 216 addReplyError(c,err); 217 return; 218 } 219 220 o = lookupKeyWrite(c->db,c->argv[1]); 221 if (o == NULL) { 222 o = createObject(REDIS_STRING,sdsempty()); 223 dbAdd(c->db,c->argv[1],o); 224 } else { 225 if (checkType(c,o,REDIS_STRING)) return; 226 o = dbUnshareStringValue(c->db,c->argv[1],o); 227 } 228 229 /* Grow sds value to the right length if necessary */ 230 byte = bitoffset >> 3; 231 o->ptr = sdsgrowzero(o->ptr,byte+1); 232 233 /* Get current values */ 234 byteval = ((uint8_t*)o->ptr)[byte]; 235 bit = 7 - (bitoffset & 0x7); 236 bitval = byteval & (1 << bit); 237 238 /* Update byte with new bit value and return original value */ 239 byteval &= ~(1 << bit); 240 byteval |= ((on & 0x1) << bit); 241 ((uint8_t*)o->ptr)[byte] = byteval; 242 signalModifiedKey(c->db,c->argv[1]); 243 notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"setbit",c->argv[1],c->db->id); 244 server.dirty++; 245 addReply(c, bitval ? shared.cone : shared.czero); 246 } 247 248 /* GETBIT key offset */ 249 void getbitCommand(redisClient *c) { 250 robj *o; 251 char llbuf[32]; 252 size_t bitoffset; 253 size_t byte, bit; 254 size_t bitval = 0; 255 256 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK) 257 return; 258 259 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || 260 checkType(c,o,REDIS_STRING)) return; 261 262 byte = bitoffset >> 3; 263 bit = 7 - (bitoffset & 0x7); 264 if (sdsEncodedObject(o)) { 265 if (byte < sdslen(o->ptr)) 266 bitval = ((uint8_t*)o->ptr)[byte] & (1 << bit); 267 } else { 268 if (byte < (size_t)ll2string(llbuf,sizeof(llbuf),(long)o->ptr)) 269 bitval = llbuf[byte] & (1 << bit); 270 } 271 272 addReply(c, bitval ? shared.cone : shared.czero); 273 } 274 275 /* BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */ 276 void bitopCommand(redisClient *c) { 277 char *opname = c->argv[1]->ptr; 278 robj *o, *targetkey = c->argv[2]; 279 long op, j, numkeys; 280 robj **objects; /* Array of source objects. */ 281 unsigned char **src; /* Array of source strings pointers. */ 282 long *len, maxlen = 0; /* Array of length of src strings, and max len. */ 283 long minlen = 0; /* Min len among the input keys. */ 284 unsigned char *res = NULL; /* Resulting string. */ 285 286 /* Parse the operation name. */ 287 if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,"and")) 288 op = BITOP_AND; 289 else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,"or")) 290 op = BITOP_OR; 291 else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,"xor")) 292 op = BITOP_XOR; 293 else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,"not")) 294 op = BITOP_NOT; 295 else { 296 addReply(c,shared.syntaxerr); 297 return; 298 } 299 300 /* Sanity check: NOT accepts only a single key argument. */ 301 if (op == BITOP_NOT && c->argc != 4) { 302 addReplyError(c,"BITOP NOT must be called with a single source key."); 303 return; 304 } 305 306 /* Lookup keys, and store pointers to the string objects into an array. */ 307 numkeys = c->argc - 3; 308 src = zmalloc(sizeof(unsigned char*) * numkeys); 309 len = zmalloc(sizeof(long) * numkeys); 310 objects = zmalloc(sizeof(robj*) * numkeys); 311 for (j = 0; j < numkeys; j++) { 312 o = lookupKeyRead(c->db,c->argv[j+3]); 313 /* Handle non-existing keys as empty strings. */ 314 if (o == NULL) { 315 objects[j] = NULL; 316 src[j] = NULL; 317 len[j] = 0; 318 minlen = 0; 319 continue; 320 } 321 /* Return an error if one of the keys is not a string. */ 322 if (checkType(c,o,REDIS_STRING)) { 323 for (j = j-1; j >= 0; j--) { 324 if (objects[j]) 325 decrRefCount(objects[j]); 326 } 327 zfree(src); 328 zfree(len); 329 zfree(objects); 330 return; 331 } 332 objects[j] = getDecodedObject(o); 333 src[j] = objects[j]->ptr; 334 len[j] = sdslen(objects[j]->ptr); 335 if (len[j] > maxlen) maxlen = len[j]; 336 if (j == 0 || len[j] < minlen) minlen = len[j]; 337 } 338 339 /* Compute the bit operation, if at least one string is not empty. */ 340 if (maxlen) { 341 res = (unsigned char*) sdsnewlen(NULL,maxlen); 342 unsigned char output, byte; 343 long i; 344 345 /* Fast path: as far as we have data for all the input bitmaps we 346 * can take a fast path that performs much better than the 347 * vanilla algorithm. */ 348 j = 0; 349 if (minlen && numkeys <= 16) { 350 unsigned long *lp[16]; 351 unsigned long *lres = (unsigned long*) res; 352 353 /* Note: sds pointer is always aligned to 8 byte boundary. */ 354 memcpy(lp,src,sizeof(unsigned long*)*numkeys); 355 memcpy(res,src[0],minlen); 356 357 /* Different branches per different operations for speed (sorry). */ 358 if (op == BITOP_AND) { 359 while(minlen >= sizeof(unsigned long)*4) { 360 for (i = 1; i < numkeys; i++) { 361 lres[0] &= lp[i][0]; 362 lres[1] &= lp[i][1]; 363 lres[2] &= lp[i][2]; 364 lres[3] &= lp[i][3]; 365 lp[i]+=4; 366 } 367 lres+=4; 368 j += sizeof(unsigned long)*4; 369 minlen -= sizeof(unsigned long)*4; 370 } 371 } else if (op == BITOP_OR) { 372 while(minlen >= sizeof(unsigned long)*4) { 373 for (i = 1; i < numkeys; i++) { 374 lres[0] |= lp[i][0]; 375 lres[1] |= lp[i][1]; 376 lres[2] |= lp[i][2]; 377 lres[3] |= lp[i][3]; 378 lp[i]+=4; 379 } 380 lres+=4; 381 j += sizeof(unsigned long)*4; 382 minlen -= sizeof(unsigned long)*4; 383 } 384 } else if (op == BITOP_XOR) { 385 while(minlen >= sizeof(unsigned long)*4) { 386 for (i = 1; i < numkeys; i++) { 387 lres[0] ^= lp[i][0]; 388 lres[1] ^= lp[i][1]; 389 lres[2] ^= lp[i][2]; 390 lres[3] ^= lp[i][3]; 391 lp[i]+=4; 392 } 393 lres+=4; 394 j += sizeof(unsigned long)*4; 395 minlen -= sizeof(unsigned long)*4; 396 } 397 } else if (op == BITOP_NOT) { 398 while(minlen >= sizeof(unsigned long)*4) { 399 lres[0] = ~lres[0]; 400 lres[1] = ~lres[1]; 401 lres[2] = ~lres[2]; 402 lres[3] = ~lres[3]; 403 lres+=4; 404 j += sizeof(unsigned long)*4; 405 minlen -= sizeof(unsigned long)*4; 406 } 407 } 408 } 409 410 /* j is set to the next byte to process by the previous loop. */ 411 for (; j < maxlen; j++) { 412 output = (len[0] <= j) ? 0 : src[0][j]; 413 if (op == BITOP_NOT) output = ~output; 414 for (i = 1; i < numkeys; i++) { 415 byte = (len[i] <= j) ? 0 : src[i][j]; 416 switch(op) { 417 case BITOP_AND: output &= byte; break; 418 case BITOP_OR: output |= byte; break; 419 case BITOP_XOR: output ^= byte; break; 420 } 421 } 422 res[j] = output; 423 } 424 } 425 for (j = 0; j < numkeys; j++) { 426 if (objects[j]) 427 decrRefCount(objects[j]); 428 } 429 zfree(src); 430 zfree(len); 431 zfree(objects); 432 433 /* Store the computed value into the target key */ 434 if (maxlen) { 435 o = createObject(REDIS_STRING,res); 436 setKey(c->db,targetkey,o); 437 notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"set",targetkey,c->db->id); 438 decrRefCount(o); 439 } else if (dbDelete(c->db,targetkey)) { 440 signalModifiedKey(c->db,targetkey); 441 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",targetkey,c->db->id); 442 } 443 server.dirty++; 444 addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */ 445 } 446 447 /* BITCOUNT key [start end] */ 448 void bitcountCommand(redisClient *c) { 449 robj *o; 450 long start, end, strlen; 451 unsigned char *p; 452 char llbuf[32]; 453 454 /* Lookup, check for type, and return 0 for non existing keys. */ 455 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || 456 checkType(c,o,REDIS_STRING)) return; 457 458 /* Set the 'p' pointer to the string, that can be just a stack allocated 459 * array if our string was integer encoded. */ 460 if (o->encoding == REDIS_ENCODING_INT) { 461 p = (unsigned char*) llbuf; 462 strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr); 463 } else { 464 p = (unsigned char*) o->ptr; 465 strlen = sdslen(o->ptr); 466 } 467 468 /* Parse start/end range if any. */ 469 if (c->argc == 4) { 470 if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK) 471 return; 472 if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK) 473 return; 474 /* Convert negative indexes */ 475 if (start < 0) start = strlen+start; 476 if (end < 0) end = strlen+end; 477 if (start < 0) start = 0; 478 if (end < 0) end = 0; 479 if (end >= strlen) end = strlen-1; 480 } else if (c->argc == 2) { 481 /* The whole string. */ 482 start = 0; 483 end = strlen-1; 484 } else { 485 /* Syntax error. */ 486 addReply(c,shared.syntaxerr); 487 return; 488 } 489 490 /* Precondition: end >= 0 && end < strlen, so the only condition where 491 * zero can be returned is: start > end. */ 492 if (start > end) { 493 addReply(c,shared.czero); 494 } else { 495 long bytes = end-start+1; 496 497 addReplyLongLong(c,redisPopcount(p+start,bytes)); 498 } 499 } 500 501 /* BITPOS key bit [start [end]] */ 502 void bitposCommand(redisClient *c) { 503 robj *o; 504 long bit, start, end, strlen; 505 unsigned char *p; 506 char llbuf[32]; 507 int end_given = 0; 508 509 /* Parse the bit argument to understand what we are looking for, set 510 * or clear bits. */ 511 if (getLongFromObjectOrReply(c,c->argv[2],&bit,NULL) != REDIS_OK) 512 return; 513 if (bit != 0 && bit != 1) { 514 addReplyError(c, "The bit argument must be 1 or 0."); 515 return; 516 } 517 518 /* If the key does not exist, from our point of view it is an infinite 519 * array of 0 bits. If the user is looking for the fist clear bit return 0, 520 * If the user is looking for the first set bit, return -1. */ 521 if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) { 522 addReplyLongLong(c, bit ? -1 : 0); 523 return; 524 } 525 if (checkType(c,o,REDIS_STRING)) return; 526 527 /* Set the 'p' pointer to the string, that can be just a stack allocated 528 * array if our string was integer encoded. */ 529 if (o->encoding == REDIS_ENCODING_INT) { 530 p = (unsigned char*) llbuf; 531 strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr); 532 } else { 533 p = (unsigned char*) o->ptr; 534 strlen = sdslen(o->ptr); 535 } 536 537 /* Parse start/end range if any. */ 538 if (c->argc == 4 || c->argc == 5) { 539 if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != REDIS_OK) 540 return; 541 if (c->argc == 5) { 542 if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != REDIS_OK) 543 return; 544 end_given = 1; 545 } else { 546 end = strlen-1; 547 } 548 /* Convert negative indexes */ 549 if (start < 0) start = strlen+start; 550 if (end < 0) end = strlen+end; 551 if (start < 0) start = 0; 552 if (end < 0) end = 0; 553 if (end >= strlen) end = strlen-1; 554 } else if (c->argc == 3) { 555 /* The whole string. */ 556 start = 0; 557 end = strlen-1; 558 } else { 559 /* Syntax error. */ 560 addReply(c,shared.syntaxerr); 561 return; 562 } 563 564 /* For empty ranges (start > end) we return -1 as an empty range does 565 * not contain a 0 nor a 1. */ 566 if (start > end) { 567 addReplyLongLong(c, -1); 568 } else { 569 long bytes = end-start+1; 570 long pos = redisBitpos(p+start,bytes,bit); 571 572 /* If we are looking for clear bits, and the user specified an exact 573 * range with start-end, we can't consider the right of the range as 574 * zero padded (as we do when no explicit end is given). 575 * 576 * So if redisBitpos() returns the first bit outside the range, 577 * we return -1 to the caller, to mean, in the specified range there 578 * is not a single "0" bit. */ 579 if (end_given && bit == 0 && pos == bytes*8) { 580 addReplyLongLong(c,-1); 581 return; 582 } 583 if (pos != -1) pos += start*8; /* Adjust for the bytes we skipped. */ 584 addReplyLongLong(c,pos); 585 } 586 } 587