Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 764 lines (683 sloc) 30.304 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
;;;; Copyright 2011 Google Inc. All Rights Reserved

;;;; Redistribution and use in source and binary forms, with or without
;;;; modification, are permitted provided that the following conditions are
;;;; met:

;;;; * Redistributions of source code must retain the above copyright
;;;; notice, this list of conditions and the following disclaimer.
;;;; * Redistributions in binary form must reproduce the above
;;;; copyright notice, this list of conditions and the following disclaimer
;;;; in the documentation and/or other materials provided with the
;;;; distribution.
;;;; * Neither the name of Google Inc. nor the names of its
;;;; contributors may be used to endorse or promote products derived from
;;;; this software without specific prior written permission.

;;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;;;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;;; Author: brown@google.com (Robert Brown)

;;;; Variable length encoding for integers and floating point numbers.

(in-package #:varint)
(declaim #.*optimize-fast-unsafe*)

(defconst +max-octets-32+ 5 "Maximum number of octets needed to encode a 32-bit integer.")
(defconst +max-octets-64+ 10 "Maximum number of octets needed to encode a 64-bit integer.")

;;; XXXX: Do we really need this many conditions? Maybe eliminate
;;; varint-error, encoding-error, and/or parsing-error.

(define-condition varint-error (error)
  ()
  (:documentation "Superclass of all VARINT conditions."))

(define-condition encoding-error (varint-error)
  ()
  (:documentation "Superclass of all VARINT encoding conditions."))

(define-condition buffer-overflow (encoding-error)
  ()
  (:documentation "Buffer space exhausted while encoding a value."))

(define-condition parsing-error (varint-error)
  ()
  (:documentation "Superclass of all VARINT decoding conditions."))

(define-condition data-exhausted (parsing-error)
  ()
  (:documentation "Decoding a value requires more data than is available."))

(define-condition value-out-of-range (parsing-error)
  ()
  (:documentation "Value decoded is outside the range of the return type."))

(define-condition alignment (parsing-error)
  ()
  (:documentation "Data buffer does not contain the type of value we have
been asked to skip over or parse backwards."))

(declaim (ftype (function (octet-vector vector-index uint32) (values vector-index &optional))
                encode-uint32))

(defun encode-uint32 (buffer index v)
  "Encode V, an unsigned 32-bit integer, into BUFFER at INDEX."
  (declare (type octet-vector buffer)
           (type vector-index index)
           (type uint32 v))
  (cond ((< v (ash 1 7))
         (setf (aref buffer index) v)
         (incf index))
        ((< v (ash 1 14))
         (setf (nibbles:ub16ref/le buffer index)
               (logior #x80
                       (ldb (byte 7 0) v)
                       (ash (ldb (byte 7 7) v) 8)))
         (incf index 2))
        ((< v (ash 1 21))
         (setf (nibbles:ub16ref/le buffer index)
               (logior #x8080
                       (ldb (byte 7 0) v)
                       (ash (ldb (byte 7 7) v) 8)))
         (incf index 2)
         (setf (aref buffer index) (ldb (byte 7 14) v))
         (incf index))
        ((< v (ash 1 28))
         (setf (nibbles:ub32ref/le buffer index)
               (logior #x808080
                       (ldb (byte 7 0) v)
                       (ash (ldb (byte 7 7) v) 8)
                       (ash (ldb (byte 7 14) v) 16)
                       (ash (ldb (byte 7 21) v) 24)))
         (incf index 4))
        (t (setf (nibbles:ub32ref/le buffer index)
                 (logior #x80808080
                         (ldb (byte 7 0) v)
                         (ash (ldb (byte 7 7) v) 8)
                         (ash (ldb (byte 7 14) v) 16)
                         (ash (ldb (byte 7 21) v) 24)))
           (incf index 4)
           (setf (aref buffer index) (ldb (byte 4 28) v))
           (incf index)))
  index)

(declaim (ftype (function (octet-vector vector-index vector-index uint32)
                          (values vector-index &optional))
                encode-uint32-carefully))

(defun encode-uint32-carefully (buffer index limit v)
  "Encode V, an unsigned 32-bit integer, into BUFFER at INDEX, taking care
to never write past position LIMIT. If writing past LIMIT is required to
encode V, then raise ENCODE-OVERFLOW."
  (declare (type octet-vector buffer)
           (type vector-index index limit)
           (type uint32 v))
  (cond ((< v (ash 1 7))
         (when (>= index limit) (error 'buffer-overflow))
         (setf (aref buffer index) v)
         (incf index))
        ((< v (ash 1 14))
         (when (>= (1+ index) limit) (error 'buffer-overflow))
         (setf (nibbles:ub16ref/le buffer index)
               (logior #x80
                       (ldb (byte 7 0) v)
                       (ash (ldb (byte 7 7) v) 8)))
         (incf index 2))
        ((< v (ash 1 21))
         (when (>= (+ index 2) limit) (error 'buffer-overflow))
         (setf (nibbles:ub16ref/le buffer index)
               (logior #x8080
                       (ldb (byte 7 0) v)
                       (ash (ldb (byte 7 7) v) 8)))
         (incf index 2)
         (setf (aref buffer index) (ldb (byte 7 14) v))
         (incf index))
        ((< v (ash 1 28))
         (when (>= (+ index 3) limit) (error 'buffer-overflow))
         (setf (nibbles:ub32ref/le buffer index)
               (logior #x808080
                       (ldb (byte 7 0) v)
                       (ash (ldb (byte 7 7) v) 8)
                       (ash (ldb (byte 7 14) v) 16)
                       (ash (ldb (byte 7 21) v) 24)))
         (incf index 4))
        (t (when (>= (+ index 4) limit) (error 'buffer-overflow))
           (setf (nibbles:ub32ref/le buffer index)
                 (logior #x80808080
                         (ldb (byte 7 0) v)
                         (ash (ldb (byte 7 7) v) 8)
                         (ash (ldb (byte 7 14) v) 16)
                         (ash (ldb (byte 7 21) v) 24)))
           (incf index 4)
           (setf (aref buffer index) (ldb (byte 4 28) v))
           (incf index)))
  index)

(declaim (ftype (function (octet-vector vector-index uint64) (values vector-index &optional))
                encode-uint64))

(defun encode-uint64 (buffer index v)
  "Encode V, an unsigned 64-bit integer, into BUFFER at INDEX."
  (declare (type octet-vector buffer)
           (type vector-index index)
           (type uint64 v))
  (loop do (let ((bits (ldb (byte 8 0) v)))
             (setf v (ash v -7))
             (setf (aref buffer index)
                   (logior bits (if (not (zerop v)) 128 0)))
             (incf index))
        while (not (zerop v)))
  index)

(declaim (ftype (function (octet-vector vector-index vector-index uint64)
                          (values vector-index &optional))
                encode-uint64-carefully))

(defun encode-uint64-carefully (buffer index limit v)
  "Encode V, an unsigned 64-bit integer, into BUFFER at INDEX, taking care
to never write past position LIMIT. If writing past LIMIT is required to
encode V, then raise ENCODE-OVERFLOW."
  (declare (type octet-vector buffer)
           (type vector-index index limit)
           (type uint64 v))
  (loop do (let ((bits (ldb (byte 8 0) v)))
             (setf v (ash v -7))
             (when (>= index limit)
               (error 'buffer-overflow))
             (setf (aref buffer index)
                   (logior bits (if (not (zerop v)) 128 0)))
             (incf index))
        while (not (zerop v)))
  index)

(declaim (ftype (function (octet-vector vector-index) (values uint32 vector-index &optional))
                parse-uint32))

(defun parse-uint32 (buffer index)
  (declare (type octet-vector buffer)
           (type vector-index index))
  (prog* ((byte (prog1 (aref buffer index) (incf index)))
          (result (ldb (byte 7 0) byte)))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 7) result) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 14) result) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 21) result) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 4 28) result) (ldb (byte 4 0) byte))
     (when (< byte 128) (go done))
     (error 'value-out-of-range)
   DONE
     (return (values result index))))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values uint32 vector-index &optional))
                parse-uint32-carefully))

(defun parse-uint32-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (if (<= (+ index +max-octets-32+) limit)
      (parse-uint32 buffer index)
      (progn
        (when (>= index limit)
          (error 'data-exhausted))
        (prog* ((byte (aref buffer index))
                (result (ldb (byte 7 0) byte)))
           (incf index)
           (when (< byte 128) (go done))
           (when (>= index limit) (go bad))
           (setf byte (prog1 (aref buffer index) (incf index)))
           (setf (ldb (byte 7 7) result) (ldb (byte 7 0) byte))
           (when (< byte 128) (go done))
           (when (>= index limit) (go bad))
           (setf byte (prog1 (aref buffer index) (incf index)))
           (setf (ldb (byte 7 14) result) (ldb (byte 7 0) byte))
           (when (< byte 128) (go done))
           (when (>= index limit) (go bad))
           (setf byte (prog1 (aref buffer index) (incf index)))
           (setf (ldb (byte 7 21) result) (ldb (byte 7 0) byte))
           (when (< byte 128) (go done))
           (when (>= index limit) (go bad))
           (setf byte (prog1 (aref buffer index) (incf index)))
           (setf (ldb (byte 4 28) result) (ldb (byte 4 0) byte))
           (when (< byte 128) (go done))
           (error 'value-out-of-range)
         BAD
           (error 'data-exhausted)
         DONE
           (return (values result index))))))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values (unsigned-byte 31) vector-index &optional))
                parse-uint31-carefully))

(defun parse-uint31-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (multiple-value-bind (result new-index)
      (parse-uint32-carefully buffer index limit)
    (when (= (ldb (byte 1 31) result) 1) ; sign bit set, so value is negative
      (error 'value-out-of-range))
    (values result new-index)))

(declaim (ftype (function (octet-vector vector-index) (values uint64 vector-index &optional))
                parse-uint64))

(defun parse-uint64 (buffer index)
  (declare (type octet-vector buffer)
           (type vector-index index))
  (prog* ((byte (prog1 (aref buffer index) (incf index)))
          (result1 (ldb (byte 7 0) byte))
          (result2 0)
          (result3 0))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 7) result1) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 14) result1) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 21) result1) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))

     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf result2 (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 7) result2) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 14) result2) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 21) result2) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))

     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf result3 (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 1 7) result3) (ldb (byte 1 0) byte))
     (when (< byte 128) (go done))

     (error 'value-out-of-range)
   DONE
     (return (values (logior result1 (ash result2 28) (ash result3 56))
                     index))))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values uint64 vector-index &optional))
                parse-uint64-carefully))

(defun parse-uint64-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (when (>= index limit)
    (error 'data-exhausted))
  (prog* ((byte (prog1 (aref buffer index) (incf index)))
          (result1 (ldb (byte 7 0) byte))
          (result2 0)
          (result3 0))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 7) result1) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 14) result1) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 21) result1) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))

     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf result2 (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 7) result2) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 14) result2) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 7 21) result2) (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))

     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf result3 (ldb (byte 7 0) byte))
     (when (< byte 128) (go done))
     (when (>= index limit) (go bad))
     (setf byte (prog1 (aref buffer index) (incf index)))
     (setf (ldb (byte 1 7) result3) (ldb (byte 1 0) byte))
     (when (< byte 128) (go done))
     (error 'value-out-of-range)

   BAD
     (error 'data-exhausted)
   DONE
     (return (values (logior result1 (ash result2 28) (ash result3 56))
                     index))))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values int64 vector-index &optional))
                parse-int64-carefully))

(defun parse-int64-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (multiple-value-bind (result new-index)
      (parse-uint64-carefully buffer index limit)
    (when (= (ldb (byte 1 63) result) 1) ; sign bit set, so value is negative
      (decf result (ash 1 64)))
    (values result new-index)))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values int32 vector-index &optional))
                parse-int32-carefully))

(defun parse-int32-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (multiple-value-bind (result new-index)
      (parse-int64-carefully buffer index limit)
    (when (or (>= result (ash 1 31)) (< result (- (ash 1 31))))
      (error 'value-out-of-range))
    (values result new-index)))

(declaim (ftype (function (octet-vector vector-index vector-index) (values vector-index &optional))
                skip32-carefully))

(defun skip32-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (prog ()
     (when (> (+ index +max-octets-32+) limit) (go carefully))

     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
   value-out-of-range
     (error 'value-out-of-range)

   carefully
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (go value-out-of-range)

   data-exhausted
     (error 'data-exhausted)
   done
     (return (1+ index))))

(declaim (ftype (function (octet-vector vector-index vector-index) (values vector-index &optional))
                skip64-carefully))

(defun skip64-carefully (buffer index limit)
  (declare (type octet-vector buffer)
           (type vector-index index limit))
  (prog ()
     (when (> (+ index +max-octets-64+) limit) (go carefully))

     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (< (aref buffer index) 128) (go done))
   value-out-of-range
     (error 'value-out-of-range)

   carefully
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (incf index)
     (when (>= index limit) (go data-exhausted))
     (when (< (aref buffer index) 128) (go done))
     (go value-out-of-range)

   data-exhausted
     (error 'data-exhausted)
   done
     (return (1+ index))))

(declaim (ftype (function (octet-vector vector-index vector-index) (values vector-index &optional))
                skip32-backward-slow))

(defun skip32-backward-slow (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (assert (>= index base))
  (when (or (= index base)
            (> (aref buffer (decf index)) 127))
    (error 'alignment))
  (dotimes (i +max-octets-32+)
    (when (= index base)
      (return-from skip32-backward-slow index))
    (when (< (aref buffer (decf index)) 128)
      (return-from skip32-backward-slow (1+ index))))
  (error 'alignment))

(declaim (ftype (function (octet-vector vector-index vector-index) (values vector-index &optional))
                skip64-backward-slow))

(defun skip64-backward-slow (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (assert (>= index base))
  (when (or (= index base)
            (> (aref buffer (decf index)) 127))
    (error 'alignment))
  (dotimes (i +max-octets-64+)
    (when (= index base)
      (return-from skip64-backward-slow index))
    (when (< (aref buffer (decf index)) 128)
      (return-from skip64-backward-slow (1+ index))))
  (error 'alignment))

(declaim (ftype (function (octet-vector vector-index vector-index) (values vector-index &optional))
                skip32-backward))

(defun skip32-backward (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (if (<= index (+ base +max-octets-32+))
      (skip32-backward-slow buffer index base)
      (prog ()
         (when (> (aref buffer (decf index)) 127) (go bad))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
       BAD
         (error 'value-out-of-range)
       DONE
         (return (1+ index)))))

(declaim (ftype (function (octet-vector vector-index vector-index) (values vector-index &optional))
                skip64-backward))

(defun skip64-backward (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (if (<= index (+ base +max-octets-64+))
      (skip64-backward-slow buffer index base)
      (prog ()
         (when (> (aref buffer (decf index)) 127) (go bad))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
         (when (< (aref buffer (decf index)) 128) (go done))
       BAD
         (error 'value-out-of-range)
       DONE
         (return (1+ index)))))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values uint32 vector-index &optional))
                parse32-backward-slow))

(defun parse32-backward-slow (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (let ((prev (skip32-backward-slow buffer index base)))
    (values (parse-uint32 buffer prev) prev)))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values uint64 vector-index &optional))
                parse64-backward-slow))

(defun parse64-backward-slow (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (let ((prev (skip64-backward-slow buffer index base)))
    (values (parse-uint64 buffer prev) prev)))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values uint32 vector-index &optional))
                parse32-backward))

(defun parse32-backward (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (if (<= index (+ base +max-octets-32+))
      (parse32-backward-slow buffer index base)
      (let ((byte (aref buffer (decf index))))
        (when (logbitp 7 byte) (error 'alignment))
        (prog ((result (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))

           (when (>= result (ash 1 25)) (error 'value-out-of-range))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (error 'value-out-of-range)

         DONE
           (return (values result (1+ index)))))))

(declaim (ftype (function (octet-vector vector-index vector-index)
                          (values uint64 vector-index &optional))
                parse64-backward))

(defun parse64-backward (buffer index base)
  (declare (type octet-vector buffer)
           (type vector-index index base))
  (if (<= index (+ base +max-octets-64+))
      (parse64-backward-slow buffer index base)
      (let ((byte (aref buffer (decf index))))
        (when (> byte 127) (error 'alignment))
        (prog ((result (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))

           (when (>= result (ash 1 57)) (error 'value-out-of-range))
           (setf result (logior (ash result 7) (ldb (byte 7 0) byte)))
           (setf byte (aref buffer (decf index)))
           (when (not (logbitp 7 byte)) (go done))
           (error 'value-out-of-range)

         DONE
           (return (values result (1+ index)))))))

(declaim (ftype (function (uint32) (values (integer 1 5) &optional)) length32))

(defun length32 (v)
  (declare (type uint32 v))
  (setf v (ash v -7))
  (when (zerop v) (return-from length32 1))
  (setf v (ash v -7))
  (when (zerop v) (return-from length32 2))
  (setf v (ash v -7))
  (when (zerop v) (return-from length32 3))
  (setf v (ash v -7))
  (when (zerop v) (return-from length32 4))
  5)

;; This version is more compact. Seems slower for small numbers, same
;; or faster for big numbers.

;; (declaim (ftype (function (uint32) (integer 1 5)) length32-x)
;; #+opt (inline length32-x))

;; (defun length32-x (v)
;; (declare (type uint32 v))
;; (prog ((result 1))
;; (setf v (ash v -7))
;; (when (zerop v) (go done))
;; (incf result)
;; (setf v (ash v -7))
;; (when (zerop v) (go done))
;; (incf result)
;; (setf v (ash v -7))
;; (when (zerop v) (go done))
;; (incf result)
;; (setf v (ash v -7))
;; (when (zerop v) (go done))
;; (incf result)
;; done
;; (return result)))

(declaim (ftype (function (uint64) (values (integer 1 10) &optional)) length64))

(defun length64 (v)
  (declare (type uint64 v))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 1))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 2))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 3))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 4))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 5))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 6))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 7))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 8))
  (setf v (ash v -7))
  (when (zerop v) (return-from length64 9))
  10)
Something went wrong with that request. Please try again.