1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc. ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; Protocol buffers wire format
18 (defconstant $wire-type-varint 0)
19 (defconstant $wire-type-64bit 1)
20 (defconstant $wire-type-string 2)
21 (defconstant $wire-type-32bit 5)
23 (defun make-tag (type index)
24 "Given a wire type or the name of a Protobufs type and a field index,
25 return the tag that encodes both of them."
26 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
27 (if (typep type 'fixnum)
29 (let ((type (ecase type
30 ((:int32 :uint32) $wire-type-varint)
31 ((:int64 :uint64) $wire-type-varint)
32 ((:sint32 :sint64) $wire-type-varint)
33 ((:fixed32 :sfixed32) $wire-type-32bit)
34 ((:fixed64 :sfixed64) $wire-type-64bit)
35 ((:string :bytes) $wire-type-string)
36 ((:bool) $wire-type-varint)
37 ((:float) $wire-type-32bit)
38 ((:double) $wire-type-64bit)
39 ;; A few of our homegrown types
40 ((:symbol) $wire-type-string)
41 ((:date :time :datetime :timestamp) $wire-type-64bit))))
42 (ilogior type (iash index 3))))))
44 (define-compiler-macro make-tag (&whole form type index)
45 (cond ((typep type 'fixnum)
46 `(ilogior ,type (iash ,index 3)))
48 (let ((type (ecase type
49 ((:int32 :uint32) $wire-type-varint)
50 ((:int64 :uint64) $wire-type-varint)
51 ((:sint32 :sint64) $wire-type-varint)
52 ((:fixed32 :sfixed32) $wire-type-32bit)
53 ((:fixed64 :sfixed64) $wire-type-64bit)
54 ((:string :bytes) $wire-type-string)
55 ((:bool) $wire-type-varint)
56 ((:float) $wire-type-32bit)
57 ((:double) $wire-type-64bit)
58 ;; A few of our homegrown types
59 ((:symbol) $wire-type-string)
60 ((:date :time :datetime :timestamp) $wire-type-64bit))))
61 `(ilogior ,type (iash ,index 3))))
65 (defun zig-zag-encode32 (val)
66 (declare (optimize (speed 3) (safety 0) (debug 0)))
67 (declare (type (signed-byte 32) val))
68 (logxor (ash val 1) (ash val -31)))
70 (defun zig-zag-encode64 (val)
71 (declare (optimize (speed 3) (safety 0) (debug 0)))
72 (declare (type (signed-byte 64) val))
73 (logxor (ash val 1) (ash val -63)))
75 (define-compiler-macro zig-zag-encode32 (&whole form val)
77 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
78 (type (signed-byte 32) ,val))
79 (logxor (ash ,val 1) (ash ,val -31)))
82 (define-compiler-macro zig-zag-encode64 (&whole form val)
84 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
85 (type (signed-byte 64) ,val))
86 (logxor (ash ,val 1) (ash ,val -63)))
89 (defun zig-zag-decode32 (val)
90 (declare (optimize (speed 3) (safety 0) (debug 0)))
91 (logxor (ash val -1) (- (logand val 1))))
93 (defun zig-zag-decode64 (val)
94 (declare (optimize (speed 3) (safety 0) (debug 0)))
95 (logxor (ash val -1) (- (logand val 1))))
97 (define-compiler-macro zig-zag-decode32 (&whole form val)
99 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
100 (logxor (ash ,val -1) (- (logand ,val 1))))
103 (define-compiler-macro zig-zag-decode64 (&whole form val)
105 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
106 (logxor (ash ,val -1) (- (logand ,val 1))))
112 ;; Serialize 'val' of primitive type 'type' into the buffer
113 (defun serialize-prim (val type tag buffer index)
114 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
115 The value is given by 'val', the primitive type by 'type'.
116 Modifies the buffer in place, and returns the new index into the buffer.
117 Watch out, this function turns off most type checking and all array bounds checking."
118 (declare (type (simple-array (unsigned-byte 8)) buffer)
119 (type (unsigned-byte 32) tag)
121 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
122 (let ((idx (encode-uint32 tag buffer index)))
123 (declare (type fixnum idx))
126 (encode-uint32 val buffer idx))
128 (encode-uint64 val buffer idx))
130 (encode-uint32 (zig-zag-encode32 val) buffer idx))
132 (encode-uint64 (zig-zag-encode64 val) buffer idx))
134 (encode-fixed32 val buffer idx))
136 (encode-sfixed32 val buffer idx))
138 (encode-fixed64 val buffer idx))
140 (encode-sfixed64 val buffer idx))
142 (encode-octets (babel:string-to-octets val :encoding :utf-8) buffer idx))
144 (encode-octets val buffer idx))
146 (encode-uint32 (if val 1 0) buffer idx))
148 (encode-single val buffer idx))
150 (encode-double val buffer idx))
151 ;; A few of our homegrown types
153 (let ((val (if (keywordp val)
155 ;; Non-keyword symbols are consy, avoid them if possible
156 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
157 (encode-octets (babel:string-to-octets val :encoding :utf-8) buffer idx)))
158 ((:date :time :datetime :timestamp)
159 (encode-uint64 val buffer idx))))))
161 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
162 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
163 :fixed32 :sfixed32 :fixed64 :sfixed64
164 :string :bytes :bool :float :double))
165 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
166 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
167 (declare (type fixnum idx))
170 `(encode-uint32 ,val ,buffer idx))
172 `(encode-uint64 ,val ,buffer idx))
174 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
176 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
178 `(encode-fixed32 ,val ,buffer idx))
180 `(encode-sfixed32 ,val ,buffer idx))
182 `(encode-fixed64 ,val ,buffer idx))
184 `(encode-sfixed64 ,val ,buffer idx))
186 `(encode-octets (babel:string-to-octets ,val :encoding :utf-8) ,buffer idx))
188 `(encode-octets ,val ,buffer idx))
190 `(encode-uint32 (if ,val 1 0) ,buffer idx))
192 `(encode-single ,val ,buffer idx))
194 `(encode-double ,val ,buffer idx)))))
197 (defun serialize-packed (values type tag buffer index)
198 "Serializes a set of packed values into the buffer at the given index.
199 The values are given by 'values', the primitive type by 'type'.
200 Modifies the buffer in place, and returns the new index into the buffer.
201 Watch out, this function turns off most type checking and all array bounds checking."
202 (declare (type (simple-array (unsigned-byte 8)) buffer)
203 (type (unsigned-byte 32) tag)
205 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
206 (let ((idx (encode-uint32 tag buffer index)))
207 (declare (type fixnum idx))
208 (multiple-value-bind (full-len len)
209 (packed-size values type tag)
210 (declare (type fixnum len) (ignore full-len))
211 (setq idx (encode-uint32 len buffer idx)))
214 (dolist (val values idx)
215 (setq idx (encode-uint32 val buffer idx))))
217 (dolist (val values idx)
218 (setq idx (encode-uint64 val buffer idx))))
220 (dolist (val values idx)
221 (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))))
223 (dolist (val values idx)
224 (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
226 (dolist (val values idx)
227 (setq idx (encode-fixed32 val buffer idx))))
229 (dolist (val values idx)
230 (setq idx (encode-sfixed32 val buffer idx))))
232 (dolist (val values idx)
233 (setq idx (encode-fixed64 val buffer idx))))
235 (dolist (val values idx)
236 (setq idx (encode-sfixed64 val buffer idx))))
238 (dolist (val values idx)
239 (setq idx (encode-single val buffer idx))))
241 (dolist (val values idx)
242 (setq idx (encode-double val buffer idx))))))))
244 (define-compiler-macro serialize-packed (&whole form values type tag buffer index)
245 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
246 :fixed32 :sfixed32 :fixed64 :sfixed64
248 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
249 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
250 (declare (type fixnum idx))
251 (multiple-value-bind (full-len len)
252 (packed-size ,values ,type ,tag)
253 (declare (type fixnum len) (ignore full-len))
254 (setq idx (encode-uint32 len ,buffer idx)))
255 (dolist (val ,values idx)
258 `(setq idx (encode-uint32 val ,buffer idx)))
260 `(setq idx (encode-uint64 val ,buffer idx)))
262 `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
264 `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
266 `(setq idx (encode-fixed32 val ,buffer idx)))
268 `(setq idx (encode-sfixed32 val ,buffer idx)))
270 `(setq idx (encode-fixed64 val ,buffer idx)))
272 `(setq idx (encode-sfixed64 val ,buffer idx)))
274 `(setq idx (encode-single val ,buffer idx)))
276 `(setq idx (encode-double val ,buffer idx)))))))
279 (defun serialize-enum (val values tag buffer index)
280 "Serializes a Protobufs enum value into the buffer at the given index.
281 The value is given by 'val', the enum values are in 'values'.
282 Modifies the buffer in place, and returns the new index into the buffer.
283 Watch out, this function turns off most type checking and all array bounds checking."
284 (declare (type (simple-array (unsigned-byte 8)) buffer)
285 (type (unsigned-byte 32) tag)
287 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
288 (let* ((val (let ((e (find val values :key #'proto-value)))
289 (and e (proto-index e))))
290 (idx (encode-uint32 tag buffer index)))
291 (declare (type (unsigned-byte 32) val)
293 (encode-uint32 val buffer idx))))
298 ;; Deserialize the next object of type 'type'
299 (defun deserialize-prim (type buffer index)
300 "Deserializes the next object of primitive type 'type'.
301 Deserializes from the byte vector 'buffer' starting at 'index'.
302 Returns the value and and the new index into the buffer.
303 Watch out, this function turns off most type checking and all array bounds checking."
304 (declare (type (simple-array (unsigned-byte 8)) buffer)
306 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
309 (decode-uint32 buffer index))
311 (decode-uint64 buffer index))
313 (multiple-value-bind (val idx)
314 (decode-uint32 buffer index)
315 (values (zig-zag-decode32 val) idx)))
317 (multiple-value-bind (val idx)
318 (decode-uint64 buffer index)
319 (values (zig-zag-decode64 val) idx)))
321 (decode-fixed32 buffer index))
323 (decode-sfixed32 buffer index))
325 (decode-fixed64 buffer index))
327 (decode-sfixed64 buffer index))
329 (multiple-value-bind (val idx)
330 (decode-octets buffer index)
331 (values (babel:octets-to-string val :encoding :utf-8) idx)))
333 (decode-octets buffer index))
335 (multiple-value-bind (val idx)
336 (decode-uint32 buffer index)
337 (values (if (zerop val) nil t) idx)))
339 (decode-single buffer index))
341 (decode-double buffer index))
342 ;; A few of our homegrown types
344 ;; Note that this is consy, avoid it if possible
345 (multiple-value-bind (val idx)
346 (decode-octets buffer index)
347 (let ((val (babel:octets-to-string val :encoding :utf-8)))
348 (values (make-lisp-symbol val) idx))))
349 ((:date :time :datetime :timestamp)
350 (decode-uint64 buffer index)))))
352 (define-compiler-macro deserialize-prim (&whole form type buffer index)
353 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
354 :fixed32 :sfixed32 :fixed64 :sfixed64
355 :string :bytes :bool :float :double))
356 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
359 `(decode-uint32 ,buffer ,index))
361 `(decode-uint64 ,buffer ,index))
363 `(multiple-value-bind (val idx)
364 (decode-uint32 ,buffer ,index)
365 (values (zig-zag-decode32 val) idx)))
367 `(multiple-value-bind (val idx)
368 (decode-uint64 ,buffer ,index)
369 (values (zig-zag-decode64 val) idx)))
371 `(decode-fixed32 ,buffer ,index))
373 `(decode-sfixed32 ,buffer ,index))
375 `(decode-fixed64 ,buffer ,index))
377 `(decode-sfixed64 ,buffer ,index))
379 `(multiple-value-bind (val idx)
380 (decode-octets ,buffer ,index)
381 (values (babel:octets-to-string val :encoding :utf-8) idx)))
383 `(decode-octets ,buffer ,index))
385 `(multiple-value-bind (val idx)
386 (decode-uint32 ,buffer ,index)
387 (values (if (zerop val) nil t) idx)))
389 `(decode-single ,buffer ,index))
391 `(decode-double ,buffer ,index))))
394 (defun deserialize-packed (type buffer index)
395 "Deserializes the next packed values of type 'type'.
396 Deserializes from the byte vector 'buffer' starting at 'index'.
397 Returns the value and and the new index into the buffer.
398 Watch out, this function turns off most type checking and all array bounds checking."
399 (declare (type (simple-array (unsigned-byte 8)) buffer)
401 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
402 (multiple-value-bind (len idx)
403 (decode-uint32 buffer index)
404 (declare (type (unsigned-byte 32) len)
406 (let ((end (i+ idx len)))
407 (declare (type (unsigned-byte 32) end))
408 (with-collectors ((values collect-value))
411 (return-from deserialize-packed (values values idx)))
412 (multiple-value-bind (val nidx)
415 (decode-uint32 buffer idx))
417 (decode-uint64 buffer idx))
419 (multiple-value-bind (val idx)
420 (decode-uint32 buffer idx)
421 (values (zig-zag-decode32 val) idx)))
423 (multiple-value-bind (val idx)
424 (decode-uint64 buffer idx)
425 (values (zig-zag-decode64 val) idx)))
427 (decode-fixed32 buffer idx))
429 (decode-sfixed32 buffer idx))
431 (decode-fixed64 buffer idx))
433 (decode-sfixed64 buffer idx))
435 (decode-single buffer idx))
437 (decode-double buffer idx)))
439 (setq idx nidx))))))))
441 (defun deserialize-enum (values buffer index)
442 "Deserializes the next enum value take from 'values'.
443 Deserializes from the byte vector 'buffer' starting at 'index'.
444 Returns the value and and the new index into the buffer.
445 Watch out, this function turns off most type checking and all array bounds checking."
446 (declare (type (simple-array (unsigned-byte 8)) buffer)
448 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
449 (multiple-value-bind (val idx)
450 (decode-uint32 buffer index)
451 (let ((val (let ((e (find val values :key #'proto-index)))
452 (and e (proto-value e)))))
458 (defun prim-size (val type tag)
459 "Returns the size in bytes that the primitive object will take when serialized.
460 Watch out, this function turns off most type checking."
461 (declare (type (unsigned-byte 32) tag))
462 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
465 (i+ (length32 tag) (length32 val)))
467 (i+ (length32 tag) (length64 val)))
469 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
471 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
472 ((:fixed32 :sfixed32)
473 (i+ (length32 tag) 4))
474 ((:fixed64 :sfixed64)
475 (i+ (length32 tag) 8))
477 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
478 (i+ (length32 tag) (length32 len) len)))
480 (let ((len (length val)))
481 (i+ (length32 tag) (length32 len) len)))
483 (i+ (length32 tag) 1))
485 (i+ (length32 tag) 4))
487 (i+ (length32 tag) 8))
488 ;; A few of our homegrown types
490 (let ((len (if (keywordp val)
491 (length (symbol-name val))
492 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
493 (i+ (length32 tag) (length32 len) len)))
494 ((:date :time :datetime :timestamp)
495 (i+ (length32 tag) 8)))))
497 (define-compiler-macro prim-size (&whole form val type tag)
498 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
499 :fixed32 :sfixed32 :fixed64 :sfixed64
500 :string :bytes :bool :float :double))
501 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
504 `(i+ (length32 ,tag) (length32 ,val)))
506 `(i+ (length32 ,tag) (length64 ,val)))
508 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
510 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
511 ((:fixed32 :sfixed32)
512 `(i+ (length32 ,tag) 4))
513 ((:fixed64 :sfixed64)
514 `(i+ (length32 ,tag) 8))
516 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
517 (i+ (length32 ,tag) (length32 len) len)))
519 `(let ((len (length ,val)))
520 (i+ (length32 ,tag) (length32 len) len)))
522 `(i+ (length32 ,tag) 1))
524 `(i+ (length32 ,tag) 4))
526 `(i+ (length32 ,tag) 8))))
529 (defun packed-size (values type tag)
530 "Returns the size in bytes that the packed object will take when serialized.
531 Watch out, this function turns off most type checking."
532 (declare (type (unsigned-byte 32) tag))
533 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
534 (let ((len (let ((len 0))
535 (declare (type fixnum len))
536 (dolist (val values len)
537 (iincf len (ecase type
538 ((:int32 :uint32) (length32 val))
539 ((:int64 :uint64) (length64 val))
540 ((:sint32) (length32 (zig-zag-encode32 val)))
541 ((:sint64) (length64 (zig-zag-encode64 val)))
542 ((:fixed32 :sfixed32) 4)
543 ((:fixed64 :sfixed64) 8)
546 (declare (type (unsigned-byte 32) len))
547 ;; Two value: the full size of the packed object, and the size
548 ;; of just the payload
549 (values (i+ (length32 tag) (length32 len) len) len))))
551 (define-compiler-macro packed-size (&whole form values type tag)
552 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
553 :fixed32 :sfixed32 :fixed64 :sfixed64
555 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
556 (let ((len (let ((len 0))
557 (declare (type fixnum len))
558 (dolist (val ,values len)
559 (iincf len ,(ecase type
560 ((:int32 :uint32) `(length32 val))
561 ((:int64 :uint64) `(length64 val))
562 ((:sint32) `(length32 (zig-zag-encode32 val)))
563 ((:sint64) `(length64 (zig-zag-encode64 val)))
564 ((:fixed32 :sfixed32) `4)
565 ((:fixed64 :sfixed64) `8)
568 (declare (type (unsigned-byte 32) len))
569 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
572 (defun enum-size (val values tag)
573 "Returns the size in bytes that the enum object will take when serialized."
574 (declare (type (unsigned-byte 32) tag))
575 (let ((idx (let ((e (find val values :key #'proto-value)))
576 (and e (proto-index e)))))
577 (assert idx () "There is no enum value for ~S" val)
578 (i+ (length32 tag) (length32 idx))))
583 (defun encode-uint32 (val buffer index)
584 "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
586 Modifies the buffer, and returns the new index into the buffer.
587 Watch out, this function turns off all type checking and array bounds checking."
588 (declare (optimize (speed 3) (safety 0) (debug 0)))
589 (declare (type (unsigned-byte 32) val)
590 (type (simple-array (unsigned-byte 8)) buffer)
592 ;; Seven bits at a time, least significant bits first
593 (loop do (let ((bits (ldb (byte 7 0) val)))
594 (declare (type (unsigned-byte 8) bits))
595 (setq val (ash val -7))
596 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
599 (values index buffer)) ;return the buffer to improve 'trace'
601 (defun encode-uint64 (val buffer index)
602 "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
604 Modifies the buffer, and returns the new index into the buffer.
605 Watch out, this function turns off all type checking and array bounds checking."
606 (declare (optimize (speed 3) (safety 0) (debug 0)))
607 (declare (type (unsigned-byte 64) val)
608 (type (simple-array (unsigned-byte 8)) buffer)
610 (loop do (let ((bits (ldb (byte 7 0) val)))
611 (declare (type (unsigned-byte 8) bits))
612 (setq val (ash val -7))
613 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
616 (values index buffer))
618 (defun encode-fixed32 (val buffer index)
619 "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
621 Modifies the buffer, and returns the new index into the buffer.
622 Watch out, this function turns off most type checking and all array bounds checking."
623 (declare (type (unsigned-byte 32) val)
624 (type (simple-array (unsigned-byte 8)) buffer)
626 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
628 (let ((byte (ldb (byte 8 0) val)))
629 (declare (type (unsigned-byte 8) byte))
630 (setq val (ash val -8))
631 (setf (aref buffer index) byte)
633 (values index buffer))
635 (defun encode-fixed64 (val buffer index)
636 "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
638 Modifies the buffer, and returns the new index into the buffer.
639 Watch out, this function turns off most type checking and all array bounds checking."
640 (declare (type (unsigned-byte 64) val)
641 (type (simple-array (unsigned-byte 8)) buffer)
643 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
645 (let ((byte (ldb (byte 8 0) val)))
646 (declare (type (unsigned-byte 8) byte))
647 (setq val (ash val -8))
648 (setf (aref buffer index) byte)
650 (values index buffer))
652 (defun encode-sfixed32 (val buffer index)
653 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
655 Modifies the buffer, and returns the new index into the buffer.
656 Watch out, this function turns off most type checking and all array bounds checking."
657 (declare (type (signed-byte 32) val)
658 (type (simple-array (unsigned-byte 8)) buffer)
660 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
662 (let ((byte (ldb (byte 8 0) val)))
663 (declare (type (unsigned-byte 8) byte))
664 (setq val (ash val -8))
665 (setf (aref buffer index) byte)
667 (values index buffer))
669 (defun encode-sfixed64 (val buffer index)
670 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
672 Modifies the buffer, and returns the new index into the buffer.
673 Watch out, this function turns off most type checking and all array bounds checking."
674 (declare (type (signed-byte 64) val)
675 (type (simple-array (unsigned-byte 8)) buffer)
677 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
679 (let ((byte (ldb (byte 8 0) val)))
680 (declare (type (unsigned-byte 8) byte))
681 (setq val (ash val -8))
682 (setf (aref buffer index) byte)
684 (values index buffer))
686 (defun encode-single (val buffer index)
687 "Encodes the single float 'val' into the buffer at the given index.
688 Modifies the buffer, and returns the new index into the buffer.
689 Watch out, this function turns off most type checking and all array bounds checking."
690 (declare (type single-float val)
691 (type (simple-array (unsigned-byte 8)) buffer)
693 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
694 (let ((bits (single-float-bits val)))
696 (let ((byte (ldb (byte 8 0) bits)))
697 (declare (type (unsigned-byte 8) byte))
698 (setq bits (ash bits -8))
699 (setf (aref buffer index) byte)
701 (values index buffer))
703 (defun encode-double (val buffer index)
704 "Encodes the double float 'val' into the buffer at the given index.
705 Modifies the buffer, and returns the new index into the buffer.
706 Watch out, this function turns off most type checking and all array bounds checking."
707 (declare (type double-float val)
708 (type (simple-array (unsigned-byte 8)) buffer)
710 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
711 (multiple-value-bind (low high)
712 (double-float-bits val)
714 (let ((byte (ldb (byte 8 0) low)))
715 (declare (type (unsigned-byte 8) byte))
716 (setq low (ash low -8))
717 (setf (aref buffer index) byte)
720 (let ((byte (ldb (byte 8 0) high)))
721 (declare (type (unsigned-byte 8) byte))
722 (setq high (ash high -8))
723 (setf (aref buffer index) byte)
725 (values index buffer))
727 (defun encode-octets (octets buffer index)
728 "Encodes the octets into the buffer at the given index.
729 Modifies the buffer, and returns the new index into the buffer.
730 Watch out, this function turns off most type checking and all array bounds checking."
731 (declare (type (simple-array (unsigned-byte 8)) buffer)
733 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
734 (let* ((len (length octets))
735 (idx (encode-uint32 len buffer index)))
736 (declare (type fixnum len)
737 (type (unsigned-byte 32) idx))
738 (replace buffer octets :start1 idx)
739 (values (i+ idx len) buffer))))
744 ;; Decode the value from the buffer at the given index,
745 ;; then return the value and new index into the buffer
746 (defun decode-uint32 (buffer index)
747 "Decodes the next 32-bit varint integer in the buffer at the given index.
748 Returns both the decoded value and the new index into the buffer.
749 Watch out, this function turns off most type checking and all array bounds checking."
750 (declare (optimize (speed 3) (safety 0) (debug 0)))
751 (declare (type (simple-array (unsigned-byte 8)) buffer)
753 ;; Seven bits at a time, least significant bits first
755 for places fixnum upfrom 0 by 7
756 for byte fixnum = (prog1 (aref buffer index) (iincf index))
757 do (setq val (logior val (ash (ldb (byte 7 0) byte) places)))
760 (assert (< val #.(ash 1 32)) ()
761 "The value ~D is longer than 32 bits" val)
762 (return (values val index)))))
764 (defun decode-uint64 (buffer index)
765 "Decodes the next 64-bit varint integer in the buffer at the given index.
766 Returns both the decoded value and the new index into the buffer.
767 Watch out, this function turns off most type checking and all array bounds checking."
768 (declare (optimize (speed 3) (safety 0) (debug 0)))
769 (declare (type (simple-array (unsigned-byte 8)) buffer)
771 ;; Seven bits at a time, least significant bits first
773 for places fixnum upfrom 0 by 7
774 for byte fixnum = (prog1 (aref buffer index) (iincf index))
775 do (setq val (logior val (ash (ldb (byte 7 0) byte) places)))
777 finally (return (values val index))))
779 (defun decode-fixed32 (buffer index)
780 "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
781 Returns both the decoded value and the new index into the buffer.
782 Watch out, this function turns off most type checking and all array bounds checking."
783 (declare (type (simple-array (unsigned-byte 8)) buffer)
785 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
786 ;; Eight bits at a time, least significant bits first
789 for places fixnum upfrom 0 by 8
790 for byte fixnum = (prog1 (aref buffer index) (iincf index))
791 do (setq val (logior val (ash byte places))))
792 (values val index))))
794 (defun decode-sfixed32 (buffer index)
795 "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
796 Returns both the decoded value and the new index into the buffer.
797 Watch out, this function turns off most type checking and all array bounds checking."
798 (declare (type (simple-array (unsigned-byte 8)) buffer)
800 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
801 ;; Eight bits at a time, least significant bits first
804 for places fixnum upfrom 0 by 8
805 for byte fixnum = (prog1 (aref buffer index) (iincf index))
806 do (setq val (logior val (ash byte places))))
807 (when (i= (ldb (byte 1 31) val) 1) ;sign bit set, so negative value
808 (decf val #.(ash 1 32)))
809 (values val index))))
811 (defun decode-fixed64 (buffer index)
812 "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
813 Returns both the decoded value and the new index into the buffer.
814 Watch out, this function turns off most type checking and all array bounds checking."
815 (declare (type (simple-array (unsigned-byte 8)) buffer)
817 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
818 ;; Eight bits at a time, least significant bits first
821 for places fixnum upfrom 0 by 8
822 for byte fixnum = (prog1 (aref buffer index) (iincf index))
823 do (setq val (logior val (ash byte places))))
824 (values val index))))
826 (defun decode-sfixed64 (buffer index)
827 "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
828 Returns both the decoded value and the new index into the buffer.
829 Watch out, this function turns off most type checking and all array bounds checking."
830 (declare (type (simple-array (unsigned-byte 8)) buffer)
832 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
833 ;; Eight bits at a time, least significant bits first
836 for places fixnum upfrom 0 by 8
837 for byte fixnum = (prog1 (aref buffer index) (iincf index))
838 do (setq val (logior val (ash byte places))))
839 (when (i= (ldb (byte 1 63) val) 1) ;sign bit set, so negative value
840 (decf val #.(ash 1 64)))
841 (values val index))))
843 (defun decode-single (buffer index)
844 "Decodes the next single float in the buffer at the given index.
845 Returns both the decoded value and the new index into the buffer.
846 Watch out, this function turns off most type checking and all array bounds checking."
847 (declare (type (simple-array (unsigned-byte 8)) buffer)
849 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
850 ;; Eight bits at a time, least significant bits first
853 for places fixnum upfrom 0 by 8
854 for byte fixnum = (prog1 (aref buffer index) (iincf index))
855 do (setq bits (logior bits (ash byte places))))
856 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
857 (decf bits #.(ash 1 32)))
858 (values (make-single-float bits) index))))
860 (defun decode-double (buffer index)
861 "Decodes the next double float in the buffer at the given index.
862 Returns both the decoded value and the new index into the buffer.
863 Watch out, this function turns off most type checking and all array bounds checking."
864 (declare (type (simple-array (unsigned-byte 8)) buffer)
866 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
867 ;; Eight bits at a time, least significant bits first
871 for places fixnum upfrom 0 by 8
872 for byte fixnum = (prog1 (aref buffer index) (iincf index))
873 do (setq low (logior low (ash byte places))))
875 for places fixnum upfrom 0 by 8
876 for byte fixnum = (prog1 (aref buffer index) (iincf index))
877 do (setq high (logior high (ash byte places))))
878 ;; High bits are signed, but low bits are unsigned
879 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
880 (decf high #.(ash 1 32)))
881 (values (make-double-float low high) index))))
883 (defun decode-octets (buffer index)
884 "Decodes the next octets in the buffer at the given index.
885 Returns both the decoded value and the new index into the buffer.
886 Watch out, this function turns off most type checking and all array bounds checking."
887 (declare (type (simple-array (unsigned-byte 8)) buffer)
889 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
890 (multiple-value-bind (len idx)
891 (decode-uint32 buffer index)
892 (declare (type (unsigned-byte 32) len)
894 (values (subseq buffer idx (i+ idx len)) (i+ idx len)))))
899 (defun length32 (val)
900 "Returns the length that 'val' will take when encoded as a 32-bit integer."
901 (declare (optimize (speed 3) (safety 0) (debug 0)))
903 (declare (type fixnum size))
905 (setq val (ash val -7))
910 (defun length64 (val)
911 "Returns the length that 'val' will take when encoded as a 64-bit integer."
912 (declare (optimize (speed 3) (safety 0) (debug 0)))
914 (declare (type fixnum size))
916 (setq val (ash val -7))
922 ;;; Skipping elements
924 (defun skip-element (buffer index wire-type)
925 "Skip an element in the buffer at the index of the given wire type.
926 Returns the new index in the buffer.
927 Watch out, this function turns off all type checking and all array bounds checking."
928 (declare (optimize (speed 3) (safety 0) (debug 0)))
929 (declare (type (simple-array (unsigned-byte 8)) buffer)
931 (type (unsigned-byte 32) wire-type))
934 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
938 (multiple-value-bind (len idx)
939 (decode-uint32 buffer index)
940 (declare (type (unsigned-byte 32) len)