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 (eval-when (:compile-toplevel :load-toplevel :execute)
20 (defconstant $wire-type-varint 0)
21 (defconstant $wire-type-64bit 1)
22 (defconstant $wire-type-string 2)
23 (defconstant $wire-type-start-group 3) ;supposedly obsolete
24 (defconstant $wire-type-end-group 4) ;supposedly obsolete
25 (defconstant $wire-type-32bit 5)
30 (defun make-tag (type index)
31 "Given a wire type or the name of a Protobufs type and a field index,
32 return the tag that encodes both of them."
33 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
34 (if (typep type 'fixnum)
35 (ilogior type (iash index 3))
36 (let ((type (ecase type
37 ((:int32 :uint32) $wire-type-varint)
38 ((:int64 :uint64) $wire-type-varint)
39 ((:sint32 :sint64) $wire-type-varint)
40 ((:fixed32 :sfixed32) $wire-type-32bit)
41 ((:fixed64 :sfixed64) $wire-type-64bit)
42 ((:string :bytes) $wire-type-string)
43 ((:bool) $wire-type-varint)
44 ((:float) $wire-type-32bit)
45 ((:double) $wire-type-64bit)
46 ;; A few of our homegrown types
47 ((:symbol) $wire-type-string)
48 ((:date :time :datetime :timestamp) $wire-type-64bit))))
49 (ilogior type (iash index 3))))))
51 (define-compiler-macro make-tag (&whole form type index)
52 (setq type (fold-symbol type))
53 (cond ((typep type 'fixnum)
54 `(ilogior ,type (iash ,index 3)))
56 (let ((type (ecase type
57 ((:int32 :uint32) $wire-type-varint)
58 ((:int64 :uint64) $wire-type-varint)
59 ((:sint32 :sint64) $wire-type-varint)
60 ((:fixed32 :sfixed32) $wire-type-32bit)
61 ((:fixed64 :sfixed64) $wire-type-64bit)
62 ((:string :bytes) $wire-type-string)
63 ((:bool) $wire-type-varint)
64 ((:float) $wire-type-32bit)
65 ((:double) $wire-type-64bit)
66 ;; A few of our homegrown types
67 ((:symbol) $wire-type-string)
68 ((:date :time :datetime :timestamp) $wire-type-64bit))))
69 `(ilogior ,type (iash ,index 3))))
72 (defun fold-symbol (x)
73 "Given an expression 'x', constant-fold it until it can be foleded no more."
76 (cond ((eq x last) (return x))
79 (constantp (second x)))
80 (shiftf last x (second x)))
83 (shiftf last x (symbol-value x)))
87 (defun zig-zag-encode32 (val)
88 (declare (optimize (speed 3) (safety 0) (debug 0)))
89 (declare (type (signed-byte 32) val))
90 (logxor (ash val 1) (ash val -31)))
92 (defun zig-zag-encode64 (val)
93 (declare (optimize (speed 3) (safety 0) (debug 0)))
94 (declare (type (signed-byte 64) val))
95 (logxor (ash val 1) (ash val -63)))
97 (define-compiler-macro zig-zag-encode32 (&whole form val)
99 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
100 (type (signed-byte 32) ,val))
101 (logxor (ash ,val 1) (ash ,val -31)))
104 (define-compiler-macro zig-zag-encode64 (&whole form val)
106 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
107 (type (signed-byte 64) ,val))
108 (logxor (ash ,val 1) (ash ,val -63)))
111 (defun zig-zag-decode32 (val)
112 (declare (optimize (speed 3) (safety 0) (debug 0)))
113 (logxor (ash val -1) (- (logand val 1))))
115 (defun zig-zag-decode64 (val)
116 (declare (optimize (speed 3) (safety 0) (debug 0)))
117 (logxor (ash val -1) (- (logand val 1))))
119 (define-compiler-macro zig-zag-decode32 (&whole form val)
121 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
122 (logxor (ash ,val -1) (- (logand ,val 1))))
125 (define-compiler-macro zig-zag-decode64 (&whole form val)
127 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
128 (logxor (ash ,val -1) (- (logand ,val 1))))
134 ;; Serialize 'val' of primitive type 'type' into the buffer
135 (defun serialize-prim (val type tag buffer index)
136 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
137 The value is given by 'val', the primitive type by 'type'.
138 Modifies the buffer in place, and returns the new index into the buffer.
139 Watch out, this function turns off most type checking and all array bounds checking."
141 (type (unsigned-byte 32) tag)
143 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
144 (let ((idx (encode-uint32 tag buffer index)))
145 (declare (type fixnum idx))
148 (encode-uint32 (ldb (byte 32 0) val) buffer idx))
150 (encode-uint64 (ldb (byte 64 0) val) buffer idx))
152 (encode-uint32 (zig-zag-encode32 val) buffer idx))
154 (encode-uint64 (zig-zag-encode64 val) buffer idx))
156 (encode-fixed32 val buffer idx))
158 (encode-sfixed32 val buffer idx))
160 (encode-fixed64 val buffer idx))
162 (encode-sfixed64 val buffer idx))
164 (encode-string val buffer idx))
166 (encode-octets val buffer idx))
168 (encode-uint32 (if val 1 0) buffer idx))
170 (encode-single val buffer idx))
172 (encode-double val buffer idx))
173 ;; A few of our homegrown types
175 (let ((val (if (keywordp val)
177 ;; Non-keyword symbols are consy, avoid them if possible
178 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
179 (encode-string val buffer idx)))
180 ((:date :time :datetime :timestamp)
181 (encode-uint64 (ldb (byte 64 0) val) buffer idx))))))
183 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
184 (setq type (fold-symbol type)
185 tag (fold-symbol tag))
186 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
187 :fixed32 :sfixed32 :fixed64 :sfixed64
188 :string :bytes :bool :float :double))
189 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
190 (type (simple-array (unsigned-byte 8)) ,buffer)
191 ;; 'tag' is a constant, no need to declare its type
192 (type fixnum ,index))
193 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
194 (declare (type fixnum idx))
197 `(encode-uint32 (ldb (byte 32 0) ,val) ,buffer idx))
199 `(encode-uint64 (ldb (byte 64 0) ,val) ,buffer idx))
201 `(encode-uint32 ,val ,buffer idx))
203 `(encode-uint64 ,val ,buffer idx))
205 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
207 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
209 `(encode-fixed32 ,val ,buffer idx))
211 `(encode-sfixed32 ,val ,buffer idx))
213 `(encode-fixed64 ,val ,buffer idx))
215 `(encode-sfixed64 ,val ,buffer idx))
217 `(encode-string ,val ,buffer idx))
219 `(encode-octets ,val ,buffer idx))
221 `(encode-uint32 (if ,val 1 0) ,buffer idx))
223 `(encode-single ,val ,buffer idx))
225 `(encode-double ,val ,buffer idx)))))
228 (defun serialize-packed (values type tag buffer index)
229 "Serializes a set of packed values into the buffer at the given index.
230 The values are given by 'values', the primitive type by 'type'.
231 Modifies the buffer in place, and returns the new index into the buffer.
232 Watch out, this function turns off most type checking and all array bounds checking."
233 (declare (type (simple-array (unsigned-byte 8)) buffer)
234 (type (unsigned-byte 32) tag)
236 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
237 (let ((idx (encode-uint32 tag buffer index)))
238 (declare (type fixnum idx))
239 (multiple-value-bind (full-len len)
240 (packed-size values type tag)
241 (declare (type fixnum len) (ignore full-len))
242 (setq idx (encode-uint32 len buffer idx)))
245 (dolist (val values idx)
246 (setq idx (encode-uint32 (ldb (byte 32 0) val) buffer idx))))
248 (dolist (val values idx)
249 (setq idx (encode-uint64 (ldb (byte 64 0) val) buffer idx))))
251 (dolist (val values idx)
252 (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))))
254 (dolist (val values idx)
255 (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
257 (dolist (val values idx)
258 (setq idx (encode-fixed32 val buffer idx))))
260 (dolist (val values idx)
261 (setq idx (encode-sfixed32 val buffer idx))))
263 (dolist (val values idx)
264 (setq idx (encode-fixed64 val buffer idx))))
266 (dolist (val values idx)
267 (setq idx (encode-sfixed64 val buffer idx))))
269 (dolist (val values idx)
270 (setq idx (encode-uint32 (if val 1 0) buffer idx))))
272 (dolist (val values idx)
273 (setq idx (encode-single val buffer idx))))
275 (dolist (val values idx)
276 (setq idx (encode-double val buffer idx))))))))
278 (define-compiler-macro serialize-packed (&whole form values type tag buffer index)
279 (setq type (fold-symbol type)
280 tag (fold-symbol tag))
281 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
282 :fixed32 :sfixed32 :fixed64 :sfixed64
283 :bool :float :double))
284 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
285 (type (simple-array (unsigned-byte 8)) ,buffer)
286 ;; 'tag' is a constant, no need to declare its type
287 (type fixnum ,index))
288 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
289 (declare (type fixnum idx))
290 (multiple-value-bind (full-len len)
291 (packed-size ,values ,type ,tag)
292 (declare (type fixnum len) (ignore full-len))
293 (setq idx (encode-uint32 len ,buffer idx)))
294 (dolist (val ,values idx)
297 `(setq idx (encode-uint32 (ldb (byte 32 0) val) ,buffer idx)))
299 `(setq idx (encode-uint64 (ldb (byte 64 0) val) ,buffer idx)))
301 `(setq idx (encode-uint32 val ,buffer idx)))
303 `(setq idx (encode-uint64 val ,buffer idx)))
305 `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
307 `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
309 `(setq idx (encode-fixed32 val ,buffer idx)))
311 `(setq idx (encode-sfixed32 val ,buffer idx)))
313 `(setq idx (encode-fixed64 val ,buffer idx)))
315 `(setq idx (encode-sfixed64 val ,buffer idx)))
317 `(setq idx (encode-uint32 (if val 1 0) ,buffer idx)))
319 `(setq idx (encode-single val ,buffer idx)))
321 `(setq idx (encode-double val ,buffer idx)))))))
324 (defun serialize-enum (val values tag buffer index)
325 "Serializes a Protobufs enum value into the buffer at the given index.
326 The value is given by 'val', the enum values are in 'values'.
327 Modifies the buffer in place, and returns the new index into the buffer.
328 Watch out, this function turns off most type checking and all array bounds checking."
329 (declare (type (simple-array (unsigned-byte 8)) buffer)
330 (type (unsigned-byte 32) tag)
332 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
333 (let* ((val (let ((e (find val values :key #'proto-value)))
334 (and e (proto-index e))))
335 (idx (encode-uint32 tag buffer index)))
336 (declare (type (unsigned-byte 32) val)
338 (encode-uint32 (ldb (byte 32 0) val) buffer idx))))
343 ;; Deserialize the next object of type 'type'
344 (defun deserialize-prim (type buffer index)
345 "Deserializes the next object of primitive type 'type'.
346 Deserializes from the byte vector 'buffer' starting at 'index'.
347 Returns the value and and the new index into the buffer.
348 Watch out, this function turns off most type checking and all array bounds checking."
349 (declare (type (simple-array (unsigned-byte 8)) buffer)
351 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
354 (decode-int32 buffer index))
356 (decode-int64 buffer index))
358 (decode-uint32 buffer index))
360 (decode-uint64 buffer index))
362 (multiple-value-bind (val idx)
363 (decode-uint32 buffer index)
364 (values (zig-zag-decode32 val) idx)))
366 (multiple-value-bind (val idx)
367 (decode-uint64 buffer index)
368 (values (zig-zag-decode64 val) idx)))
370 (decode-fixed32 buffer index))
372 (decode-sfixed32 buffer index))
374 (decode-fixed64 buffer index))
376 (decode-sfixed64 buffer index))
378 (decode-string buffer index))
380 (decode-octets buffer index))
382 (multiple-value-bind (val idx)
383 (decode-uint32 buffer index)
384 (values (if (i= val 0) nil t) idx)))
386 (decode-single buffer index))
388 (decode-double buffer index))
389 ;; A few of our homegrown types
391 ;; Note that this is consy, avoid it if possible
392 (multiple-value-bind (val idx)
393 (decode-string buffer index)
394 (values (make-lisp-symbol val) idx)))
395 ((:date :time :datetime :timestamp)
396 (decode-uint64 buffer index)))))
398 (define-compiler-macro deserialize-prim (&whole form type buffer index)
399 (setq type (fold-symbol type))
400 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
401 :fixed32 :sfixed32 :fixed64 :sfixed64
402 :string :bytes :bool :float :double))
403 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
404 (type (simple-array (unsigned-byte 8)) ,buffer)
405 (type fixnum ,index))
408 `(decode-int32 ,buffer ,index))
410 `(decode-int64 ,buffer ,index))
412 `(decode-uint32 ,buffer ,index))
414 `(decode-uint64 ,buffer ,index))
416 `(multiple-value-bind (val idx)
417 (decode-uint32 ,buffer ,index)
418 (values (zig-zag-decode32 val) idx)))
420 `(multiple-value-bind (val idx)
421 (decode-uint64 ,buffer ,index)
422 (values (zig-zag-decode64 val) idx)))
424 `(decode-fixed32 ,buffer ,index))
426 `(decode-sfixed32 ,buffer ,index))
428 `(decode-fixed64 ,buffer ,index))
430 `(decode-sfixed64 ,buffer ,index))
432 `(decode-string ,buffer ,index))
434 `(decode-octets ,buffer ,index))
436 `(multiple-value-bind (val idx)
437 (decode-uint32 ,buffer ,index)
438 (values (if (i= val 0) nil t) idx)))
440 `(decode-single ,buffer ,index))
442 `(decode-double ,buffer ,index))))
445 (defun deserialize-packed (type buffer index)
446 "Deserializes the next packed values of type 'type'.
447 Deserializes from the byte vector 'buffer' starting at 'index'.
448 Returns the value and and the new index into the buffer.
449 Watch out, this function turns off most type checking and all array bounds checking."
450 (declare (type (simple-array (unsigned-byte 8)) buffer)
452 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
453 (multiple-value-bind (len idx)
454 (decode-uint32 buffer index)
455 (declare (type (unsigned-byte 32) len)
457 (let ((end (i+ idx len)))
458 (declare (type (unsigned-byte 32) end))
459 (with-collectors ((values collect-value))
462 (return-from deserialize-packed (values values idx)))
463 (multiple-value-bind (val nidx)
466 (decode-int32 buffer idx))
468 (decode-int64 buffer idx))
470 (decode-uint32 buffer idx))
472 (decode-uint64 buffer idx))
474 (multiple-value-bind (val nidx)
475 (decode-uint32 buffer idx)
476 (values (zig-zag-decode32 val) nidx)))
478 (multiple-value-bind (val nidx)
479 (decode-uint64 buffer idx)
480 (values (zig-zag-decode64 val) nidx)))
482 (decode-fixed32 buffer idx))
484 (decode-sfixed32 buffer idx))
486 (decode-fixed64 buffer idx))
488 (decode-sfixed64 buffer idx))
490 (multiple-value-bind (val nidx)
491 (decode-uint32 buffer idx)
492 (values (if (i= val 0) nil t) nidx)))
494 (decode-single buffer idx))
496 (decode-double buffer idx)))
498 (setq idx nidx))))))))
500 (define-compiler-macro deserialize-packed (&whole form type buffer index)
501 (setq type (fold-symbol type))
502 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
503 :fixed32 :sfixed32 :fixed64 :sfixed64
504 :bool :float :double))
505 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
506 (type (simple-array (unsigned-byte 8)) ,buffer)
507 (type fixnum ,index))
508 (block deserialize-packed
509 (multiple-value-bind (len idx)
510 (decode-uint32 ,buffer ,index)
511 (declare (type (unsigned-byte 32) len)
513 (let ((end (i+ idx len)))
514 (declare (type (unsigned-byte 32) end))
515 (with-collectors ((values collect-value))
518 (return-from deserialize-packed (values values idx)))
519 (multiple-value-bind (val nidx)
522 `(decode-int32 ,buffer idx))
524 `(decode-int64 ,buffer idx))
526 `(decode-uint32 ,buffer idx))
528 `(decode-uint64 ,buffer idx))
530 `(multiple-value-bind (val nidx)
531 (decode-uint32 ,buffer idx)
532 (values (zig-zag-decode32 val) nidx)))
534 `(multiple-value-bind (val nidx)
535 (decode-uint64 ,buffer idx)
536 (values (zig-zag-decode64 val) nidx)))
538 `(decode-fixed32 ,buffer idx))
540 `(decode-sfixed32 ,buffer idx))
542 `(decode-fixed64 ,buffer idx))
544 `(decode-sfixed64 ,buffer idx))
546 `(multiple-value-bind (val nidx)
547 (decode-uint32 ,buffer idx)
548 (values (if (i= val 0) nil t) nidx)))
550 `(decode-single ,buffer idx))
552 `(decode-double ,buffer idx)))
554 (setq idx nidx))))))))
557 (defun deserialize-enum (values buffer index)
558 "Deserializes the next enum value take from 'values'.
559 Deserializes from the byte vector 'buffer' starting at 'index'.
560 Returns the value and and the new index into the buffer.
561 Watch out, this function turns off most type checking and all array bounds checking."
562 (declare (type (simple-array (unsigned-byte 8)) buffer)
564 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
565 (multiple-value-bind (val idx)
566 (decode-int32 buffer index)
567 (let ((val (let ((e (find val values :key #'proto-index)))
568 (and e (proto-value e)))))
574 (defun prim-size (val type tag)
575 "Returns the size in bytes that the primitive object will take when serialized.
576 Watch out, this function turns off most type checking."
577 (declare (type (unsigned-byte 32) tag))
578 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
581 (i+ (length32 tag) (length32 (ldb (byte 32 0) val))))
583 (i+ (length32 tag) (length64 (ldb (byte 64 0) val))))
585 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
587 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
588 ((:fixed32 :sfixed32)
589 (i+ (length32 tag) 4))
590 ((:fixed64 :sfixed64)
591 (i+ (length32 tag) 8))
593 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
594 (i+ (length32 tag) (length32 len) len)))
596 (let ((len (length val)))
597 (i+ (length32 tag) (length32 len) len)))
599 (i+ (length32 tag) 1))
601 (i+ (length32 tag) 4))
603 (i+ (length32 tag) 8))
604 ;; A few of our homegrown types
606 (let ((len (if (keywordp val)
607 (length (symbol-name val))
608 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
609 (i+ (length32 tag) (length32 len) len)))
610 ((:date :time :datetime :timestamp)
611 (i+ (length32 tag) 8)))))
613 (define-compiler-macro prim-size (&whole form val type tag)
614 (setq type (fold-symbol type)
615 tag (fold-symbol tag))
616 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
617 :fixed32 :sfixed32 :fixed64 :sfixed64
618 :string :bytes :bool :float :double))
619 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
622 `(i+ (length32 ,tag) (length32 (ldb (byte 32 0) ,val))))
624 `(i+ (length32 ,tag) (length64 (ldb (byte 64 0) ,val))))
626 `(i+ (length32 ,tag) (length32 ,val)))
628 `(i+ (length32 ,tag) (length64 ,val)))
630 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
632 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
633 ((:fixed32 :sfixed32)
634 `(i+ (length32 ,tag) 4))
635 ((:fixed64 :sfixed64)
636 `(i+ (length32 ,tag) 8))
638 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
639 (i+ (length32 ,tag) (length32 len) len)))
641 `(let ((len (length ,val)))
642 (i+ (length32 ,tag) (length32 len) len)))
644 `(i+ (length32 ,tag) 1))
646 `(i+ (length32 ,tag) 4))
648 `(i+ (length32 ,tag) 8))))
651 (defun packed-size (values type tag)
652 "Returns the size in bytes that the packed object will take when serialized.
653 Watch out, this function turns off most type checking."
654 (declare (type (unsigned-byte 32) tag))
655 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
656 (let ((len (let ((len 0))
657 (declare (type fixnum len))
658 (dolist (val values len)
659 (iincf len (ecase type
660 ((:int32 :uint32) (length32 (ldb (byte 32 0) val)))
661 ((:int64 :uint64) (length64 (ldb (byte 64 0) val)))
662 ((:sint32) (length32 (zig-zag-encode32 val)))
663 ((:sint64) (length64 (zig-zag-encode64 val)))
664 ((:fixed32 :sfixed32) 4)
665 ((:fixed64 :sfixed64) 8)
669 (declare (type (unsigned-byte 32) len))
670 ;; Two value: the full size of the packed object, and the size
671 ;; of just the payload
672 (values (i+ (length32 tag) (length32 len) len) len))))
674 (define-compiler-macro packed-size (&whole form values type tag)
675 (setq type (fold-symbol type)
676 tag (fold-symbol tag))
677 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
678 :fixed32 :sfixed32 :fixed64 :sfixed64
679 :bool :float :double))
680 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
681 (let ((len (let ((len 0))
682 (declare (type fixnum len))
683 (dolist (val ,values len)
684 (iincf len ,(ecase type
685 ((:int32) `(length32 (ldb (byte 32 0) val)))
686 ((:int64) `(length64 (ldb (byte 64 0) val)))
687 ((:uint32) `(length32 val))
688 ((:uint64) `(length64 val))
689 ((:sint32) `(length32 (zig-zag-encode32 val)))
690 ((:sint64) `(length64 (zig-zag-encode64 val)))
691 ((:fixed32 :sfixed32) `4)
692 ((:fixed64 :sfixed64) `8)
696 (declare (type (unsigned-byte 32) len))
697 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
700 (defun enum-size (val values tag)
701 "Returns the size in bytes that the enum object will take when serialized."
702 (declare (type (unsigned-byte 32) tag))
703 (let ((idx (let ((e (find val values :key #'proto-value)))
704 (and e (proto-index e)))))
705 (assert idx () "There is no enum value for ~S" val)
706 (i+ (length32 tag) (length32 (ldb (byte 32 0) idx)))))
709 ;;; Wire-level encoders
710 ;;; These are called at the lowest level, so arg types are assumed to be correct
712 (defun encode-uint32 (val buffer index)
713 "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
715 Modifies the buffer, and returns the new index into the buffer.
716 Watch out, this function turns off all type checking and array bounds checking."
717 (declare (optimize (speed 3) (safety 0) (debug 0)))
718 (declare (type (unsigned-byte 32) val)
719 (type (simple-array (unsigned-byte 8)) buffer)
721 ;; Seven bits at a time, least significant bits first
722 (loop do (let ((bits (ildb (byte 7 0) val)))
723 (declare (type (unsigned-byte 8) bits))
724 (setq val (iash val -7))
725 (setf (aref buffer index) (ilogior bits (if (i= val 0) 0 128)))
728 (values index buffer)) ;return the buffer to improve 'trace'
730 (defun encode-uint64 (val buffer index)
731 "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
733 Modifies the buffer, and returns the new index into the buffer.
734 Watch out, this function turns off all type checking and array bounds checking."
735 (declare (optimize (speed 3) (safety 0) (debug 0)))
736 (declare (type (unsigned-byte 64) val)
737 (type (simple-array (unsigned-byte 8)) buffer)
739 (loop do (let ((bits (ldb (byte 7 0) val)))
740 (declare (type (unsigned-byte 8) bits))
741 (setq val (ash val -7))
742 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
745 (values index buffer))
747 (defun encode-fixed32 (val buffer index)
748 "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
750 Modifies the buffer, and returns the new index into the buffer.
751 Watch out, this function turns off all type checking and array bounds checking."
752 (declare (optimize (speed 3) (safety 0) (debug 0)))
753 (declare (type (unsigned-byte 32) val)
754 (type (simple-array (unsigned-byte 8)) buffer)
757 (let ((byte (ildb (byte 8 0) val)))
758 (declare (type (unsigned-byte 8) byte))
759 (setq val (iash val -8))
760 (setf (aref buffer index) byte)
762 (values index buffer))
764 (defun encode-fixed64 (val buffer index)
765 "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
767 Modifies the buffer, and returns the new index into the buffer.
768 Watch out, this function turns off all type checking and array bounds checking."
769 (declare (optimize (speed 3) (safety 0) (debug 0)))
770 (declare (type (unsigned-byte 64) val)
771 (type (simple-array (unsigned-byte 8)) buffer)
774 (let ((byte (ldb (byte 8 0) val)))
775 (declare (type (unsigned-byte 8) byte))
776 (setq val (ash val -8))
777 (setf (aref buffer index) byte)
779 (values index buffer))
781 (defun encode-sfixed32 (val buffer index)
782 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
784 Modifies the buffer, and returns the new index into the buffer.
785 Watch out, this function turns off all type checking and array bounds checking."
786 (declare (optimize (speed 3) (safety 0) (debug 0)))
787 (declare (type (signed-byte 32) val)
788 (type (simple-array (unsigned-byte 8)) buffer)
791 (let ((byte (ildb (byte 8 0) val)))
792 (declare (type (unsigned-byte 8) byte))
793 (setq val (iash val -8))
794 (setf (aref buffer index) byte)
796 (values index buffer))
798 (defun encode-sfixed64 (val buffer index)
799 "Encodes the signed 64-bit integer 'val' as a fixed int into the buffer
801 Modifies the buffer, and returns the new index into the buffer.
802 Watch out, this function turns off all type checking and array bounds checking."
803 (declare (optimize (speed 3) (safety 0) (debug 0)))
804 (declare (type (signed-byte 64) val)
805 (type (simple-array (unsigned-byte 8)) buffer)
808 (let ((byte (ldb (byte 8 0) val)))
809 (declare (type (unsigned-byte 8) byte))
810 (setq val (ash val -8))
811 (setf (aref buffer index) byte)
813 (values index buffer))
815 (defun encode-single (val buffer index)
816 "Encodes the single float 'val' into the buffer at the given index.
817 Modifies the buffer, and returns the new index into the buffer.
818 Watch out, this function turns off all type checking and array bounds checking."
819 (declare (optimize (speed 3) (safety 0) (debug 0)))
820 (declare (type single-float val)
821 (type (simple-array (unsigned-byte 8)) buffer)
823 (let ((bits (single-float-bits val)))
825 (let ((byte (ldb (byte 8 0) bits)))
826 (declare (type (unsigned-byte 8) byte))
827 (setq bits (ash bits -8))
828 (setf (aref buffer index) byte)
830 (values index buffer))
832 (defun encode-double (val buffer index)
833 "Encodes the double float 'val' into the buffer at the given index.
834 Modifies the buffer, and returns the new index into the buffer.
835 Watch out, this function turns off all type checking and array bounds checking."
836 (declare (optimize (speed 3) (safety 0) (debug 0)))
837 (declare (type double-float val)
838 (type (simple-array (unsigned-byte 8)) buffer)
840 (multiple-value-bind (low high)
841 (double-float-bits val)
843 (let ((byte (ldb (byte 8 0) low)))
844 (declare (type (unsigned-byte 8) byte))
845 (setq low (ash low -8))
846 (setf (aref buffer index) byte)
849 (let ((byte (ldb (byte 8 0) high)))
850 (declare (type (unsigned-byte 8) byte))
851 (setq high (ash high -8))
852 (setf (aref buffer index) byte)
854 (values index buffer))
856 (defun encode-string (string buffer index)
857 "Encodes the octets into the buffer at the given index.
858 Modifies the buffer, and returns the new index into the buffer.
859 Watch out, this function turns off all type checking and array bounds checking."
860 (declare (optimize (speed 3) (safety 0) (debug 0)))
861 (declare (type (simple-array (unsigned-byte 8)) buffer)
863 (let* ((octets (babel:string-to-octets string :encoding :utf-8))
864 (len (length octets))
865 (idx (encode-uint32 len buffer index)))
866 (declare (type fixnum len)
867 (type (unsigned-byte 32) idx))
868 (replace buffer octets :start1 idx)
869 (values (i+ idx len) buffer)))
871 (defun encode-octets (octets buffer index)
872 "Encodes the octets into the buffer at the given index.
873 Modifies the buffer, and returns the new index into the buffer.
874 Watch out, this function turns off all type checking and array bounds checking."
875 (declare (optimize (speed 3) (safety 0) (debug 0)))
876 (declare (type (simple-array (unsigned-byte 8)) buffer)
878 (let* ((len (length octets))
879 (idx (encode-uint32 len buffer index)))
880 (declare (type fixnum len)
881 (type (unsigned-byte 32) idx))
882 (replace buffer octets :start1 idx)
883 (values (i+ idx len) buffer)))
886 ;;; Wire-level decoders
887 ;;; These are called at the lowest level, so arg types are assumed to be correct
889 ;; Decode the value from the buffer at the given index,
890 ;; then return the value and new index into the buffer
891 (defun decode-uint32 (buffer index)
892 "Decodes the next 32-bit varint integer in the buffer at the given index.
893 Returns both the decoded value and the new index into the buffer.
894 Watch out, this function turns off all type checking and array bounds checking."
895 (declare (optimize (speed 3) (safety 0) (debug 0)))
896 (declare (type (simple-array (unsigned-byte 8)) buffer)
898 ;; Seven bits at a time, least significant bits first
899 (loop with val fixnum = 0
900 for places fixnum upfrom 0 by 7
901 for byte fixnum = (prog1 (aref buffer index) (iincf index))
902 do (setq val (ilogior val (iash (ildb (byte 7 0) byte) places)))
905 (assert (< val #.(ash 1 32)) ()
906 "The value ~D is longer than 32 bits" val)
907 (return (values val index)))))
909 (defun decode-uint64 (buffer index)
910 "Decodes the next 64-bit varint integer in the buffer at the given index.
911 Returns both the decoded value and the new index into the buffer.
912 Watch out, this function turns off all type checking and array bounds checking."
913 (declare (optimize (speed 3) (safety 0) (debug 0)))
914 (declare (type (simple-array (unsigned-byte 8)) buffer)
916 ;; Seven bits at a time, least significant bits first
918 for places fixnum upfrom 0 by 7
919 for byte fixnum = (prog1 (aref buffer index) (iincf index))
920 do (setq val (logior val (ash (ildb (byte 7 0) byte) places)))
922 finally (return (values val index))))
924 (defun decode-int32 (buffer index)
925 "Decodes the next 32-bit varint integer in the buffer at the given index.
926 Returns both the decoded value and the new index into the buffer.
927 Watch out, this function turns off all type checking and array bounds checking."
928 (declare (optimize (speed 3) (safety 0) (debug 0)))
929 (declare (type (simple-array (unsigned-byte 8)) buffer)
931 (multiple-value-bind (val index)
932 (decode-uint32 buffer index)
933 (declare (type fixnum val))
934 (when (i= (ildb (byte 1 31) val) 1)
935 (idecf val #.(ash 1 32)))
938 (defun decode-int64 (buffer index)
939 "Decodes the next 64-bit varint integer in the buffer at the given index.
940 Returns both the decoded value and the new index into the buffer.
941 Watch out, this function turns off all type checking and array bounds checking."
942 (declare (optimize (speed 3) (safety 0) (debug 0)))
943 (declare (type (simple-array (unsigned-byte 8)) buffer)
945 (multiple-value-bind (val index)
946 (decode-uint64 buffer index)
947 (when (i= (ldb (byte 1 63 ) val) 1)
948 (decf val #.(ash 1 64)))
951 (defun decode-fixed32 (buffer index)
952 "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
953 Returns both the decoded value and the new index into the buffer.
954 Watch out, this function turns off all type checking and array bounds checking."
955 (declare (optimize (speed 3) (safety 0) (debug 0)))
956 (declare (type (simple-array (unsigned-byte 8)) buffer)
958 ;; Eight bits at a time, least significant bits first
960 (declare (type fixnum val))
962 for places fixnum upfrom 0 by 8
963 for byte fixnum = (prog1 (aref buffer index) (iincf index))
964 do (setq val (ilogior val (iash byte places))))
967 (defun decode-sfixed32 (buffer index)
968 "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
969 Returns both the decoded value and the new index into the buffer.
970 Watch out, this function turns off all type checking and array bounds checking."
971 (declare (optimize (speed 3) (safety 0) (debug 0)))
972 (declare (type (simple-array (unsigned-byte 8)) buffer)
974 ;; Eight bits at a time, least significant bits first
976 (declare (type fixnum val))
978 for places fixnum upfrom 0 by 8
979 for byte fixnum = (prog1 (aref buffer index) (iincf index))
980 do (setq val (ilogior val (iash byte places))))
981 (when (i= (ldb (byte 1 31) val) 1) ;sign bit set, so negative value
982 (decf val #.(ash 1 32)))
985 (defun decode-fixed64 (buffer index)
986 "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
987 Returns both the decoded value and the new index into the buffer.
988 Watch out, this function turns off all type checking and array bounds checking."
989 (declare (optimize (speed 3) (safety 0) (debug 0)))
990 (declare (type (simple-array (unsigned-byte 8)) buffer)
992 ;; Eight bits at a time, least significant bits first
995 for places fixnum upfrom 0 by 8
996 for byte fixnum = (prog1 (aref buffer index) (iincf index))
997 do (setq val (logior val (ash byte places))))
1000 (defun decode-sfixed64 (buffer index)
1001 "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
1002 Returns both the decoded value and the new index into the buffer.
1003 Watch out, this function turns off all type checking and array bounds checking."
1004 (declare (optimize (speed 3) (safety 0) (debug 0)))
1005 (declare (type (simple-array (unsigned-byte 8)) buffer)
1006 (type fixnum index))
1007 ;; Eight bits at a time, least significant bits first
1010 for places fixnum upfrom 0 by 8
1011 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1012 do (setq val (logior val (ash byte places))))
1013 (when (i= (ldb (byte 1 63) val) 1) ;sign bit set, so negative value
1014 (decf val #.(ash 1 64)))
1015 (values val index)))
1017 (defun decode-single (buffer index)
1018 "Decodes the next single float in the buffer at the given index.
1019 Returns both the decoded value and the new index into the buffer.
1020 Watch out, this function turns off all type checking and array bounds checking."
1021 (declare (optimize (speed 3) (safety 0) (debug 0)))
1022 (declare (type (simple-array (unsigned-byte 8)) buffer)
1023 (type fixnum index))
1024 ;; Eight bits at a time, least significant bits first
1027 for places fixnum upfrom 0 by 8
1028 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1029 do (setq bits (logior bits (ash byte places))))
1030 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
1031 (decf bits #.(ash 1 32)))
1032 (values (make-single-float bits) index)))
1034 (defun decode-double (buffer index)
1035 "Decodes the next double float in the buffer at the given index.
1036 Returns both the decoded value and the new index into the buffer.
1037 Watch out, this function turns off all type checking and array bounds checking."
1038 (declare (optimize (speed 3) (safety 0) (debug 0)))
1039 (declare (type (simple-array (unsigned-byte 8)) buffer)
1040 (type fixnum index))
1041 ;; Eight bits at a time, least significant bits first
1045 for places fixnum upfrom 0 by 8
1046 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1047 do (setq low (logior low (ash byte places))))
1049 for places fixnum upfrom 0 by 8
1050 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1051 do (setq high (logior high (ash byte places))))
1052 ;; High bits are signed, but low bits are unsigned
1053 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
1054 (decf high #.(ash 1 32)))
1055 (values (make-double-float low high) index)))
1057 (defun decode-string (buffer index)
1058 "Decodes the next UTF-8 encoded string in the buffer at the given index.
1059 Returns both the decoded string and the new index into the buffer.
1060 Watch out, this function turns off all type checking and array bounds checking."
1061 (declare (optimize (speed 3) (safety 0) (debug 0)))
1062 (declare (type (simple-array (unsigned-byte 8)) buffer)
1063 (type fixnum index))
1064 (multiple-value-bind (len idx)
1065 (decode-uint32 buffer index)
1066 (declare (type (unsigned-byte 32) len)
1068 (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len))))
1070 (defun decode-octets (buffer index)
1071 "Decodes the next octets in the buffer at the given index.
1072 Returns both the decoded value and the new index into the buffer.
1073 Watch out, this function turns off all type checking and array bounds checking."
1074 (declare (optimize (speed 3) (safety 0) (debug 0)))
1075 (declare (type (simple-array (unsigned-byte 8)) buffer)
1076 (type fixnum index))
1077 (multiple-value-bind (len idx)
1078 (decode-uint32 buffer index)
1079 (declare (type (unsigned-byte 32) len)
1081 (values (subseq buffer idx (i+ idx len)) (i+ idx len))))
1084 ;;; Wire-level lengths
1085 ;;; These are called at the lowest level, so arg types are assumed to be correct
1087 (defun length32 (val)
1088 "Returns the length that 'val' will take when encoded as a 32-bit integer."
1089 (declare (optimize (speed 3) (safety 0) (debug 0)))
1090 (declare (type (unsigned-byte 32) val))
1092 (declare (type fixnum size))
1094 (setq val (iash val -7))
1099 (defun length64 (val)
1100 "Returns the length that 'val' will take when encoded as a 64-bit integer."
1101 (declare (optimize (speed 3) (safety 0) (debug 0)))
1102 (declare (type (unsigned-byte 64) val))
1104 (declare (type fixnum size))
1106 (setq val (ash val -7))
1112 ;;; Skipping elements
1113 ;;; This is called at the lowest level, so arg types are assumed to be correct
1115 (defun skip-element (buffer index tag)
1116 "Skip an element in the buffer at the index of the given wire type.
1117 Returns the new index in the buffer.
1118 Watch out, this function turns off all type checking and all array bounds checking."
1119 (declare (optimize (speed 3) (safety 0) (debug 0)))
1120 (declare (type (simple-array (unsigned-byte 8)) buffer)
1122 (type (unsigned-byte 32) tag))
1123 (case (ilogand tag #x7)
1124 ((#.$wire-type-varint)
1125 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
1126 until (i< byte 128))
1128 ((#.$wire-type-string)
1129 (multiple-value-bind (len idx)
1130 (decode-uint32 buffer index)
1131 (declare (type (unsigned-byte 32) len)
1134 ((#.$wire-type-32bit)
1136 ((#.$wire-type-64bit)
1138 ((#.$wire-type-start-group)
1139 (loop (multiple-value-bind (new-tag idx)
1140 (decode-uint32 buffer index)
1141 (cond ((not (i= (ilogand new-tag #x7) $wire-type-end-group))
1142 ;; If it's not the end of a group, skip the next element
1143 (setq index (skip-element buffer idx new-tag)))
1144 ;; If it's the end of the expected group, we're done
1145 ((i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1148 (assert (i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group)) ()
1149 "Couldn't find a matching end group tag"))))))