1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE ;;;
5 ;;; Copyright (c) 2012 Google, 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 ;; If you need to debug the (de)serializer, (pushnew :debug-serialization *features*)
21 ;; Otherwise, we try to make (de)serialization as fast as possible,
22 ;; risking life and limb to do so
23 (defparameter $optimize-serialization
24 #+debug-serialization $optimize-default
25 #-debug-serialization $optimize-fast-unsafe)
27 (defconstant $wire-type-varint 0)
28 (defconstant $wire-type-64bit 1)
29 (defconstant $wire-type-string 2)
30 (defconstant $wire-type-start-group 3) ;supposedly deprecated, but no such luck
31 (defconstant $wire-type-end-group 4) ;supposedly deprecated
32 (defconstant $wire-type-32bit 5)
37 (defun make-tag (type index)
38 "Given a wire type or the name of a Protobufs type and a field index,
39 return the tag that encodes both of them."
40 (locally (declare #.$optimize-serialization)
41 (if (typep type 'fixnum)
42 (ilogior type (iash index 3))
43 (let ((type (ecase type
44 ((:int32 :uint32) $wire-type-varint)
45 ((:int64 :uint64) $wire-type-varint)
46 ((:sint32 :sint64) $wire-type-varint)
47 ((:fixed32 :sfixed32) $wire-type-32bit)
48 ((:fixed64 :sfixed64) $wire-type-64bit)
49 ((:string :bytes) $wire-type-string)
50 ((:bool) $wire-type-varint)
51 ((:float) $wire-type-32bit)
52 ((:double) $wire-type-64bit)
53 ;; A few of our homegrown types
54 ((:symbol) $wire-type-string)
55 ((:date :time :datetime :timestamp) $wire-type-64bit))))
56 (ilogior type (iash index 3))))))
58 (define-compiler-macro make-tag (&whole form type index)
59 (setq type (fold-symbol type))
60 (cond ((typep type 'fixnum)
61 `(ilogior ,type (iash ,index 3)))
63 (let ((type (ecase type
64 ((:int32 :uint32) $wire-type-varint)
65 ((:int64 :uint64) $wire-type-varint)
66 ((:sint32 :sint64) $wire-type-varint)
67 ((:fixed32 :sfixed32) $wire-type-32bit)
68 ((:fixed64 :sfixed64) $wire-type-64bit)
69 ((:string :bytes) $wire-type-string)
70 ((:bool) $wire-type-varint)
71 ((:float) $wire-type-32bit)
72 ((:double) $wire-type-64bit)
73 ;; A few of our homegrown types
74 ((:symbol) $wire-type-string)
75 ((:date :time :datetime :timestamp) $wire-type-64bit))))
76 `(ilogior ,type (iash ,index 3))))
79 (defun fold-symbol (x)
80 "Given an expression 'x', constant-fold it until it can be foleded no more."
83 (cond ((eq x last) (return x))
86 (constantp (second x)))
87 (shiftf last x (second x)))
90 (shiftf last x (symbol-value x)))
94 (defun zig-zag-encode32 (val)
95 (declare #.$optimize-serialization)
96 (declare (type (signed-byte 32) val))
97 (logxor (ash val 1) (ash val -31)))
99 (defun zig-zag-encode64 (val)
100 (declare #.$optimize-serialization)
101 (declare (type (signed-byte 64) val))
102 (logxor (ash val 1) (ash val -63)))
104 (define-compiler-macro zig-zag-encode32 (&whole form val)
106 `(locally (declare #.$optimize-serialization
107 (type (signed-byte 32) ,val))
108 (logxor (ash ,val 1) (ash ,val -31)))
111 (define-compiler-macro zig-zag-encode64 (&whole form val)
113 `(locally (declare #.$optimize-serialization
114 (type (signed-byte 64) ,val))
115 (logxor (ash ,val 1) (ash ,val -63)))
118 (defun zig-zag-decode32 (val)
119 (declare #.$optimize-serialization)
120 (declare (type (unsigned-byte 32) val))
121 (logxor (ash val -1) (- (logand val 1))))
123 (defun zig-zag-decode64 (val)
124 (declare #.$optimize-serialization)
125 (declare (type (unsigned-byte 64) val))
126 (logxor (ash val -1) (- (logand val 1))))
128 (define-compiler-macro zig-zag-decode32 (&whole form val)
130 `(locally (declare #.$optimize-serialization
131 (type (unsigned-byte 32) ,val))
132 (logxor (ash ,val -1) (- (logand ,val 1))))
135 (define-compiler-macro zig-zag-decode64 (&whole form val)
137 `(locally (declare #.$optimize-serialization
138 (type (unsigned-byte 64) ,val))
139 (logxor (ash ,val -1) (- (logand ,val 1))))
145 ;; Serialize 'val' of primitive type 'type' into the buffer
146 (defun serialize-prim (val type tag buffer index)
147 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
148 The value is given by 'val', the primitive type by 'type'.
149 Modifies the buffer in place, and returns the new index into the buffer.
150 Watch out, this function turns off most type checking and all array bounds checking."
151 (declare (type (unsigned-byte 32) tag)
153 (locally (declare #.$optimize-serialization)
154 (let ((idx (encode-uint32 tag buffer index)))
155 (declare (type fixnum idx))
158 (encode-uint32 (ldb (byte 32 0) val) buffer idx))
160 (encode-uint64 (ldb (byte 64 0) val) buffer idx))
162 (encode-uint32 (zig-zag-encode32 val) buffer idx))
164 (encode-uint64 (zig-zag-encode64 val) buffer idx))
166 (encode-fixed32 val buffer idx))
168 (encode-sfixed32 val buffer idx))
170 (encode-fixed64 val buffer idx))
172 (encode-sfixed64 val buffer idx))
174 (encode-string val buffer idx))
176 (encode-octets val buffer idx))
178 (encode-uint32 (if val 1 0) buffer idx))
180 (encode-single val buffer idx))
182 (encode-double val buffer idx))
183 ;; A few of our homegrown types
185 (let ((val (if (keywordp val)
187 ;; Non-keyword symbols are consy, avoid them if possible
188 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
189 (encode-string val buffer idx)))
190 ((:date :time :datetime :timestamp)
191 (encode-uint64 (ldb (byte 64 0) val) buffer idx))))))
193 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
194 (setq type (fold-symbol type)
195 tag (fold-symbol tag))
196 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
197 :fixed32 :sfixed32 :fixed64 :sfixed64
198 :string :bytes :bool :float :double))
199 `(locally (declare #.$optimize-serialization
200 (type (simple-array (unsigned-byte 8)) ,buffer)
201 ;; 'tag' is a constant, no need to declare its type
202 (type fixnum ,index))
203 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
204 (declare (type fixnum idx))
207 `(encode-uint32 (ldb (byte 32 0) ,val) ,buffer idx))
209 `(encode-uint64 (ldb (byte 64 0) ,val) ,buffer idx))
211 `(encode-uint32 ,val ,buffer idx))
213 `(encode-uint64 ,val ,buffer idx))
215 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
217 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
219 `(encode-fixed32 ,val ,buffer idx))
221 `(encode-sfixed32 ,val ,buffer idx))
223 `(encode-fixed64 ,val ,buffer idx))
225 `(encode-sfixed64 ,val ,buffer idx))
227 `(encode-string ,val ,buffer idx))
229 `(encode-octets ,val ,buffer idx))
231 `(encode-uint32 (if ,val 1 0) ,buffer idx))
233 `(encode-single ,val ,buffer idx))
235 `(encode-double ,val ,buffer idx)))))
238 (defun serialize-packed (values type tag buffer index)
239 "Serializes a set of packed values into the buffer at the given index.
240 The values are given by 'values', the primitive type by 'type'.
241 Modifies the buffer in place, and returns the new index into the buffer.
242 Watch out, this function turns off most type checking and all array bounds checking."
243 (declare (type (simple-array (unsigned-byte 8)) buffer)
244 (type (unsigned-byte 32) tag)
246 (locally (declare #.$optimize-serialization)
247 (let ((idx (encode-uint32 tag buffer index)))
248 (declare (type fixnum idx))
249 (multiple-value-bind (full-len len)
250 (packed-size values type tag)
251 (declare (type fixnum len) (ignore full-len))
252 (setq idx (encode-uint32 len buffer idx)))
255 (map () #'(lambda (val) (setq idx (encode-uint32 (ldb (byte 32 0) val) buffer idx))) values))
257 (map () #'(lambda (val) (setq idx (encode-uint64 (ldb (byte 64 0) val) buffer idx))) values))
259 (map () #'(lambda (val) (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))) values))
261 (map () #'(lambda (val) (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))) values))
263 (map () #'(lambda (val) (setq idx (encode-fixed32 val buffer idx))) values))
265 (map () #'(lambda (val) (setq idx (encode-sfixed32 val buffer idx))) values))
267 (map () #'(lambda (val) (setq idx (encode-fixed64 val buffer idx))) values))
269 (map () #'(lambda (val) (setq idx (encode-sfixed64 val buffer idx))) values))
271 (map () #'(lambda (val) (setq idx (encode-uint32 (if val 1 0) buffer idx))) values))
273 (map () #'(lambda (val) (setq idx (encode-single val buffer idx))) values))
275 (map () #'(lambda (val) (setq idx (encode-double val buffer idx))) values)))
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-serialization
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 (map () #'(lambda (val)
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))))) ,values)
325 (defun serialize-enum (val enum-values tag buffer index)
326 "Serializes a Protobufs enum value into the buffer at the given index.
327 The value is given by 'val', the enum values are in 'enum-values'.
328 Modifies the buffer in place, and returns the new index into the buffer.
329 Watch out, this function turns off most type checking and all array bounds checking."
330 (declare (type list enum-values)
331 (type (simple-array (unsigned-byte 8)) buffer)
332 (type (unsigned-byte 32) tag)
334 (locally (declare #.$optimize-serialization)
335 (let* ((val (let ((e (find val enum-values :key #'proto-value)))
336 (and e (proto-index e))))
337 (idx (encode-uint32 tag buffer index)))
338 (declare (type (unsigned-byte 32) val)
340 (encode-uint32 (ldb (byte 32 0) val) buffer idx))))
342 (defun serialize-packed-enum (values enum-values tag buffer index)
343 "Serializes Protobufs enum values into the buffer at the given index.
344 The values are given by 'values', the enum values are in 'enum-values'.
345 Modifies the buffer in place, and returns the new index into the buffer.
346 Watch out, this function turns off most type checking and all array bounds checking."
347 (declare (type list enum-values)
348 (type (simple-array (unsigned-byte 8)) buffer)
349 (type (unsigned-byte 32) tag)
351 (locally (declare #.$optimize-serialization)
352 (let ((idx (encode-uint32 tag buffer index)))
353 (declare (type fixnum idx))
354 (multiple-value-bind (full-len len)
355 (packed-enum-size values enum-values tag)
356 (declare (type fixnum len) (ignore full-len))
357 (setq idx (encode-uint32 len buffer idx)))
358 (map () #'(lambda (val)
359 (let ((val (let ((e (find val enum-values :key #'proto-value)))
360 (and e (proto-index e)))))
361 (declare (type (unsigned-byte 32) val))
362 (setq idx (encode-uint32 (ldb (byte 32 0) val) buffer idx)))) values)
368 ;; Deserialize the next object of type 'type'
369 (defun deserialize-prim (type buffer index)
370 "Deserializes the next object of primitive type 'type'.
371 Deserializes from the byte vector 'buffer' starting at 'index'.
372 Returns the value and and the new index into the buffer.
373 Watch out, this function turns off most type checking and all array bounds checking."
374 (declare (type (simple-array (unsigned-byte 8)) buffer)
376 (locally (declare #.$optimize-serialization)
379 (decode-int32 buffer index))
381 (decode-int64 buffer index))
383 (decode-uint32 buffer index))
385 (decode-uint64 buffer index))
387 (multiple-value-bind (val idx)
388 (decode-uint32 buffer index)
389 (values (zig-zag-decode32 val) idx)))
391 (multiple-value-bind (val idx)
392 (decode-uint64 buffer index)
393 (values (zig-zag-decode64 val) idx)))
395 (decode-fixed32 buffer index))
397 (decode-sfixed32 buffer index))
399 (decode-fixed64 buffer index))
401 (decode-sfixed64 buffer index))
403 (decode-string buffer index))
405 (decode-octets buffer index))
407 (multiple-value-bind (val idx)
408 (decode-uint32 buffer index)
409 (values (if (i= val 0) nil t) idx)))
411 (decode-single buffer index))
413 (decode-double buffer index))
414 ;; A few of our homegrown types
416 ;; Note that this is consy, avoid it if possible
417 (multiple-value-bind (val idx)
418 (decode-string buffer index)
419 (values (make-lisp-symbol val) idx)))
420 ((:date :time :datetime :timestamp)
421 (decode-uint64 buffer index)))))
423 (define-compiler-macro deserialize-prim (&whole form type buffer index)
424 (setq type (fold-symbol type))
425 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
426 :fixed32 :sfixed32 :fixed64 :sfixed64
427 :string :bytes :bool :float :double))
428 `(locally (declare #.$optimize-serialization
429 (type (simple-array (unsigned-byte 8)) ,buffer)
430 (type fixnum ,index))
433 `(decode-int32 ,buffer ,index))
435 `(decode-int64 ,buffer ,index))
437 `(decode-uint32 ,buffer ,index))
439 `(decode-uint64 ,buffer ,index))
441 `(multiple-value-bind (val idx)
442 (decode-uint32 ,buffer ,index)
443 (values (zig-zag-decode32 val) idx)))
445 `(multiple-value-bind (val idx)
446 (decode-uint64 ,buffer ,index)
447 (values (zig-zag-decode64 val) idx)))
449 `(decode-fixed32 ,buffer ,index))
451 `(decode-sfixed32 ,buffer ,index))
453 `(decode-fixed64 ,buffer ,index))
455 `(decode-sfixed64 ,buffer ,index))
457 `(decode-string ,buffer ,index))
459 `(decode-octets ,buffer ,index))
461 `(multiple-value-bind (val idx)
462 (decode-uint32 ,buffer ,index)
463 (values (if (i= val 0) nil t) idx)))
465 `(decode-single ,buffer ,index))
467 `(decode-double ,buffer ,index))))
470 (defun deserialize-packed (type buffer index)
471 "Deserializes the next packed values of type 'type'.
472 Deserializes from the byte vector 'buffer' starting at 'index'.
473 Returns the value and and the new index into the buffer.
474 Watch out, this function turns off most type checking and all array bounds checking."
475 (declare (type (simple-array (unsigned-byte 8)) buffer)
477 (locally (declare #.$optimize-serialization)
478 (multiple-value-bind (len idx)
479 (decode-uint32 buffer index)
480 (declare (type (unsigned-byte 32) len)
482 (let ((end (i+ idx len)))
483 (declare (type (unsigned-byte 32) end))
484 (with-collectors ((values collect-value))
487 (return-from deserialize-packed (values values idx)))
488 (multiple-value-bind (val nidx)
491 (decode-int32 buffer idx))
493 (decode-int64 buffer idx))
495 (decode-uint32 buffer idx))
497 (decode-uint64 buffer idx))
499 (multiple-value-bind (val nidx)
500 (decode-uint32 buffer idx)
501 (values (zig-zag-decode32 val) nidx)))
503 (multiple-value-bind (val nidx)
504 (decode-uint64 buffer idx)
505 (values (zig-zag-decode64 val) nidx)))
507 (decode-fixed32 buffer idx))
509 (decode-sfixed32 buffer idx))
511 (decode-fixed64 buffer idx))
513 (decode-sfixed64 buffer idx))
515 (multiple-value-bind (val nidx)
516 (decode-uint32 buffer idx)
517 (values (if (i= val 0) nil t) nidx)))
519 (decode-single buffer idx))
521 (decode-double buffer idx)))
523 (setq idx nidx))))))))
525 (define-compiler-macro deserialize-packed (&whole form type buffer index)
526 (setq type (fold-symbol type))
527 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
528 :fixed32 :sfixed32 :fixed64 :sfixed64
529 :bool :float :double))
530 `(locally (declare #.$optimize-serialization
531 (type (simple-array (unsigned-byte 8)) ,buffer)
532 (type fixnum ,index))
533 (block deserialize-packed
534 (multiple-value-bind (len idx)
535 (decode-uint32 ,buffer ,index)
536 (declare (type (unsigned-byte 32) len)
538 (let ((end (i+ idx len)))
539 (declare (type (unsigned-byte 32) end))
540 (with-collectors ((values collect-value))
543 (return-from deserialize-packed (values values idx)))
544 (multiple-value-bind (val nidx)
547 `(decode-int32 ,buffer idx))
549 `(decode-int64 ,buffer idx))
551 `(decode-uint32 ,buffer idx))
553 `(decode-uint64 ,buffer idx))
555 `(multiple-value-bind (val nidx)
556 (decode-uint32 ,buffer idx)
557 (values (zig-zag-decode32 val) nidx)))
559 `(multiple-value-bind (val nidx)
560 (decode-uint64 ,buffer idx)
561 (values (zig-zag-decode64 val) nidx)))
563 `(decode-fixed32 ,buffer idx))
565 `(decode-sfixed32 ,buffer idx))
567 `(decode-fixed64 ,buffer idx))
569 `(decode-sfixed64 ,buffer idx))
571 `(multiple-value-bind (val nidx)
572 (decode-uint32 ,buffer idx)
573 (values (if (i= val 0) nil t) nidx)))
575 `(decode-single ,buffer idx))
577 `(decode-double ,buffer idx)))
579 (setq idx nidx))))))))
582 (defun deserialize-enum (enum-values buffer index)
583 "Deserializes the next enum value take from 'enum-values'.
584 Deserializes from the byte vector 'buffer' starting at 'index'.
585 Returns the value and and the new index into the buffer.
586 Watch out, this function turns off most type checking and all array bounds checking."
587 (declare (type list enum-values)
588 (type (simple-array (unsigned-byte 8)) buffer)
590 (locally (declare #.$optimize-serialization)
591 (multiple-value-bind (val idx)
592 (decode-int32 buffer index)
593 (let ((val (let ((e (find val enum-values :key #'proto-index)))
594 (and e (proto-value e)))))
597 (defun deserialize-packed-enum (enum-values buffer index)
598 "Deserializes the next packed enum values given in 'enum-values'.
599 Deserializes from the byte vector 'buffer' starting at 'index'.
600 Returns the value and and the new index into the buffer.
601 Watch out, this function turns off most type checking and all array bounds checking."
602 (declare (type list enum-values)
603 (type (simple-array (unsigned-byte 8)) buffer)
605 (locally (declare #.$optimize-serialization)
606 (multiple-value-bind (len idx)
607 (decode-uint32 buffer index)
608 (declare (type (unsigned-byte 32) len)
610 (let ((end (i+ idx len)))
611 (declare (type (unsigned-byte 32) end))
612 (with-collectors ((values collect-value))
615 (return-from deserialize-packed-enum (values values idx)))
616 (multiple-value-bind (val nidx)
617 (decode-int32 buffer idx)
618 (let ((val (let ((e (find val enum-values :key #'proto-index)))
619 (and e (proto-value e)))))
621 (setq idx nidx)))))))))
626 (defun prim-size (val type tag)
627 "Returns the size in bytes that the primitive object will take when serialized.
628 Watch out, this function turns off most type checking."
629 (declare (type (unsigned-byte 32) tag))
630 (locally (declare #.$optimize-serialization)
633 (i+ (length32 tag) (length32 (ldb (byte 32 0) val))))
635 (i+ (length32 tag) (length64 (ldb (byte 64 0) val))))
637 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
639 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
640 ((:fixed32 :sfixed32)
641 (i+ (length32 tag) 4))
642 ((:fixed64 :sfixed64)
643 (i+ (length32 tag) 8))
645 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
646 (i+ (length32 tag) (length32 len) len)))
648 (let ((len (length val)))
649 (i+ (length32 tag) (length32 len) len)))
651 (i+ (length32 tag) 1))
653 (i+ (length32 tag) 4))
655 (i+ (length32 tag) 8))
656 ;; A few of our homegrown types
658 (let ((len (if (keywordp val)
659 (length (symbol-name val))
660 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
661 (i+ (length32 tag) (length32 len) len)))
662 ((:date :time :datetime :timestamp)
663 (i+ (length32 tag) 8)))))
665 (define-compiler-macro prim-size (&whole form val type tag)
666 (setq type (fold-symbol type)
667 tag (fold-symbol tag))
668 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
669 :fixed32 :sfixed32 :fixed64 :sfixed64
670 :string :bytes :bool :float :double))
671 `(locally (declare #.$optimize-serialization)
674 `(i+ (length32 ,tag) (length32 (ldb (byte 32 0) ,val))))
676 `(i+ (length32 ,tag) (length64 (ldb (byte 64 0) ,val))))
678 `(i+ (length32 ,tag) (length32 ,val)))
680 `(i+ (length32 ,tag) (length64 ,val)))
682 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
684 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
685 ((:fixed32 :sfixed32)
686 `(i+ (length32 ,tag) 4))
687 ((:fixed64 :sfixed64)
688 `(i+ (length32 ,tag) 8))
690 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
691 (i+ (length32 ,tag) (length32 len) len)))
693 `(let ((len (length ,val)))
694 (i+ (length32 ,tag) (length32 len) len)))
696 `(i+ (length32 ,tag) 1))
698 `(i+ (length32 ,tag) 4))
700 `(i+ (length32 ,tag) 8))))
703 (defun packed-size (values type tag)
704 "Returns the size in bytes that the packed object will take when serialized.
705 Watch out, this function turns off most type checking."
706 (declare (type (unsigned-byte 32) tag))
707 (locally (declare #.$optimize-serialization)
708 (let ((len (let ((len 0))
709 (declare (type fixnum len))
710 (map () #'(lambda (val)
711 (iincf len (ecase type
712 ((:int32 :uint32) (length32 (ldb (byte 32 0) val)))
713 ((:int64 :uint64) (length64 (ldb (byte 64 0) val)))
714 ((:sint32) (length32 (zig-zag-encode32 val)))
715 ((:sint64) (length64 (zig-zag-encode64 val)))
716 ((:fixed32 :sfixed32) 4)
717 ((:fixed64 :sfixed64) 8)
720 ((:double) 8)))) values)
722 (declare (type (unsigned-byte 32) len))
723 ;; Two value: the full size of the packed object, and the size
724 ;; of just the payload
725 (values (i+ (length32 tag) (length32 len) len) len))))
727 (define-compiler-macro packed-size (&whole form values type tag)
728 (setq type (fold-symbol type)
729 tag (fold-symbol tag))
730 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
731 :fixed32 :sfixed32 :fixed64 :sfixed64
732 :bool :float :double))
733 `(locally (declare #.$optimize-serialization)
734 (let ((len (let ((len 0))
735 (declare (type fixnum len))
736 (map () #'(lambda (val)
737 (iincf len ,(ecase type
738 ((:int32) `(length32 (ldb (byte 32 0) val)))
739 ((:int64) `(length64 (ldb (byte 64 0) val)))
740 ((:uint32) `(length32 val))
741 ((:uint64) `(length64 val))
742 ((:sint32) `(length32 (zig-zag-encode32 val)))
743 ((:sint64) `(length64 (zig-zag-encode64 val)))
744 ((:fixed32 :sfixed32) `4)
745 ((:fixed64 :sfixed64) `8)
748 ((:double) `8)))) ,values)
750 (declare (type (unsigned-byte 32) len))
751 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
754 (defun enum-size (val enum-values tag)
755 "Returns the size in bytes that the enum object will take when serialized."
756 (declare (type list enum-values)
757 (type (unsigned-byte 32) tag))
758 (let ((idx (let ((e (find val enum-values :key #'proto-value)))
759 (and e (proto-index e)))))
760 (assert idx () "There is no enum value for ~S" val)
761 (i+ (length32 tag) (length32 (ldb (byte 32 0) idx)))))
763 (defun packed-enum-size (values enum-values tag)
764 "Returns the size in bytes that the enum values will take when serialized."
765 (declare (type list enum-values)
766 (type (unsigned-byte 32) tag))
767 (let ((len (let ((len 0))
768 (declare (type fixnum len))
769 (map () #'(lambda (val)
770 (let ((idx (let ((e (find val enum-values :key #'proto-value)))
771 (and e (proto-index e)))))
772 (assert idx () "There is no enum value for ~S" val)
773 (iincf len (length32 (ldb (byte 32 0) idx))))) values)
775 (declare (type (unsigned-byte 32) len))
776 ;; Two value: the full size of the packed object, and the size
777 ;; of just the payload
778 (values (i+ (length32 tag) (length32 len) len) len)))
781 ;;; Wire-level encoders
782 ;;; These are called at the lowest level, so arg types are assumed to be correct
784 (defun encode-uint32 (val buffer index)
785 "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
787 Modifies the buffer, and returns the new index into the buffer.
788 Watch out, this function turns off all type checking and array bounds checking."
789 (declare #.$optimize-serialization)
790 (declare (type (unsigned-byte 32) val)
791 (type (simple-array (unsigned-byte 8)) buffer)
793 ;; Seven bits at a time, least significant bits first
794 (loop do (let ((bits (ildb (byte 7 0) val)))
795 (declare (type (unsigned-byte 8) bits))
796 (setq val (iash val -7))
797 (setf (aref buffer index) (ilogior bits (if (i= val 0) 0 128)))
800 (values index buffer)) ;return the buffer to improve 'trace'
802 (defun encode-uint64 (val buffer index)
803 "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
805 Modifies the buffer, and returns the new index into the buffer.
806 Watch out, this function turns off all type checking and array bounds checking."
807 (declare #.$optimize-serialization)
808 (declare (type (unsigned-byte 64) val)
809 (type (simple-array (unsigned-byte 8)) buffer)
811 (loop do (let ((bits (ldb (byte 7 0) val)))
812 (declare (type (unsigned-byte 8) bits))
813 (setq val (ash val -7))
814 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
817 (values index buffer))
819 (defun encode-fixed32 (val buffer index)
820 "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
822 Modifies the buffer, and returns the new index into the buffer.
823 Watch out, this function turns off all type checking and array bounds checking."
824 (declare #.$optimize-serialization)
825 (declare (type (unsigned-byte 32) val)
826 (type (simple-array (unsigned-byte 8)) buffer)
829 (let ((byte (ildb (byte 8 0) val)))
830 (declare (type (unsigned-byte 8) byte))
831 (setq val (iash val -8))
832 (setf (aref buffer index) byte)
834 (values index buffer))
836 (defun encode-fixed64 (val buffer index)
837 "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
839 Modifies the buffer, and returns the new index into the buffer.
840 Watch out, this function turns off all type checking and array bounds checking."
841 (declare #.$optimize-serialization)
842 (declare (type (unsigned-byte 64) val)
843 (type (simple-array (unsigned-byte 8)) buffer)
846 (let ((byte (ldb (byte 8 0) val)))
847 (declare (type (unsigned-byte 8) byte))
848 (setq val (ash val -8))
849 (setf (aref buffer index) byte)
851 (values index buffer))
853 (defun encode-sfixed32 (val buffer index)
854 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
856 Modifies the buffer, and returns the new index into the buffer.
857 Watch out, this function turns off all type checking and array bounds checking."
858 (declare #.$optimize-serialization)
859 (declare (type (signed-byte 32) val)
860 (type (simple-array (unsigned-byte 8)) buffer)
863 (let ((byte (ildb (byte 8 0) val)))
864 (declare (type (unsigned-byte 8) byte))
865 (setq val (iash val -8))
866 (setf (aref buffer index) byte)
868 (values index buffer))
870 (defun encode-sfixed64 (val buffer index)
871 "Encodes the signed 64-bit integer 'val' as a fixed int into the buffer
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-serialization)
876 (declare (type (signed-byte 64) val)
877 (type (simple-array (unsigned-byte 8)) buffer)
880 (let ((byte (ldb (byte 8 0) val)))
881 (declare (type (unsigned-byte 8) byte))
882 (setq val (ash val -8))
883 (setf (aref buffer index) byte)
885 (values index buffer))
887 (defun encode-single (val buffer index)
888 "Encodes the single float 'val' into the buffer at the given index.
889 Modifies the buffer, and returns the new index into the buffer.
890 Watch out, this function turns off all type checking and array bounds checking."
891 (declare #.$optimize-serialization)
892 (declare (type single-float val)
893 (type (simple-array (unsigned-byte 8)) buffer)
895 (let ((bits (single-float-bits val)))
897 (let ((byte (ldb (byte 8 0) bits)))
898 (declare (type (unsigned-byte 8) byte))
899 (setq bits (ash bits -8))
900 (setf (aref buffer index) byte)
902 (values index buffer))
904 (defun encode-double (val buffer index)
905 "Encodes the double float 'val' into the buffer at the given index.
906 Modifies the buffer, and returns the new index into the buffer.
907 Watch out, this function turns off all type checking and array bounds checking."
908 (declare #.$optimize-serialization)
909 (declare (type double-float val)
910 (type (simple-array (unsigned-byte 8)) buffer)
912 (multiple-value-bind (low high)
913 (double-float-bits val)
915 (let ((byte (ldb (byte 8 0) low)))
916 (declare (type (unsigned-byte 8) byte))
917 (setq low (ash low -8))
918 (setf (aref buffer index) byte)
921 (let ((byte (ldb (byte 8 0) high)))
922 (declare (type (unsigned-byte 8) byte))
923 (setq high (ash high -8))
924 (setf (aref buffer index) byte)
926 (values index buffer))
928 (defun encode-string (string buffer index)
929 "Encodes the octets into the buffer at the given index.
930 Modifies the buffer, and returns the new index into the buffer.
931 Watch out, this function turns off all type checking and array bounds checking."
932 (declare #.$optimize-serialization)
933 (declare (type (simple-array (unsigned-byte 8)) buffer)
935 (let* ((octets (babel:string-to-octets string :encoding :utf-8))
936 (len (length octets))
937 (idx (encode-uint32 len buffer index)))
938 (declare (type (array (unsigned-byte 8)) octets)
940 (type (unsigned-byte 32) idx))
941 (replace buffer octets :start1 idx)
942 (values (i+ idx len) buffer)))
944 (defun encode-octets (octets buffer index)
945 "Encodes the octets into the buffer at the given index.
946 Modifies the buffer, and returns the new index into the buffer.
947 Watch out, this function turns off all type checking and array bounds checking."
948 (declare #.$optimize-serialization)
949 (declare (type (array (unsigned-byte 8)) octets)
950 (type (simple-array (unsigned-byte 8)) buffer)
952 (let* ((len (length octets))
953 (idx (encode-uint32 len buffer index)))
954 (declare (type fixnum len)
955 (type (unsigned-byte 32) idx))
956 (replace buffer octets :start1 idx)
957 (values (i+ idx len) buffer)))
960 ;;; Wire-level decoders
961 ;;; These are called at the lowest level, so arg types are assumed to be correct
963 ;; Decode the value from the buffer at the given index,
964 ;; then return the value and new index into the buffer
965 (defun decode-uint32 (buffer index)
966 "Decodes the next 32-bit varint integer in the buffer at the given index.
967 Returns both the decoded value and the new index into the buffer.
968 Watch out, this function turns off all type checking and array bounds checking."
969 (declare #.$optimize-serialization)
970 (declare (type (simple-array (unsigned-byte 8)) buffer)
972 ;; Seven bits at a time, least significant bits first
974 (declare (type (unsigned-byte 32) val))
975 (loop for places fixnum upfrom 0 by 7
976 for byte fixnum = (prog1 (aref buffer index) (iincf index))
977 do (let ((bits (ildb (byte 7 0) byte)))
978 (declare (type (unsigned-byte 8) bits))
979 (setq val (ilogior val (iash bits places))))
982 (assert (< val #.(ash 1 32)) ()
983 "The value ~D is longer than 32 bits" val)
984 (return (values val index))))))
986 (defun decode-uint64 (buffer index)
987 "Decodes the next 64-bit varint integer in the buffer at the given index.
988 Returns both the decoded value and the new index into the buffer.
989 Watch out, this function turns off all type checking and array bounds checking."
990 (declare #.$optimize-serialization)
991 (declare (type (simple-array (unsigned-byte 8)) buffer)
993 ;; Seven bits at a time, least significant bits first
995 (declare (type (unsigned-byte 64) val))
996 (loop for places fixnum upfrom 0 by 7
997 for byte fixnum = (prog1 (aref buffer index) (iincf index))
998 do (let ((bits (ildb (byte 7 0) byte)))
999 (declare (type (unsigned-byte 8) bits))
1000 (setq val (logior val (ash bits places))))
1002 finally (return (values val index)))))
1004 (defun decode-int32 (buffer index)
1005 "Decodes the next 32-bit varint integer in the buffer at the given index.
1006 Returns both the decoded value and the new index into the buffer.
1007 Watch out, this function turns off all type checking and array bounds checking."
1008 (declare #.$optimize-serialization)
1009 (declare (type (simple-array (unsigned-byte 8)) buffer)
1010 (type fixnum index))
1011 (multiple-value-bind (val index)
1012 (decode-uint32 buffer index)
1013 (declare (type fixnum val))
1014 (when (i= (ildb (byte 1 31) val) 1)
1015 (idecf val #.(ash 1 32)))
1016 (values val index)))
1018 (defun decode-int64 (buffer index)
1019 "Decodes the next 64-bit varint integer in the buffer at the given index.
1020 Returns both the decoded value and the new index into the buffer.
1021 Watch out, this function turns off all type checking and array bounds checking."
1022 (declare #.$optimize-serialization)
1023 (declare (type (simple-array (unsigned-byte 8)) buffer)
1024 (type fixnum index))
1025 (multiple-value-bind (val index)
1026 (decode-uint64 buffer index)
1027 (when (i= (ldb (byte 1 63 ) val) 1)
1028 (decf val #.(ash 1 64)))
1029 (values val index)))
1031 (defun decode-fixed32 (buffer index)
1032 "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
1033 Returns both the decoded value and the new index into the buffer.
1034 Watch out, this function turns off all type checking and array bounds checking."
1035 (declare #.$optimize-serialization)
1036 (declare (type (simple-array (unsigned-byte 8)) buffer)
1037 (type fixnum index))
1038 ;; Eight bits at a time, least significant bits first
1040 (declare (type fixnum val))
1042 for places fixnum upfrom 0 by 8
1043 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1044 do (setq val (ilogior val (iash byte places))))
1045 (values val index)))
1047 (defun decode-fixed64 (buffer index)
1048 "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
1049 Returns both the decoded value and the new index into the buffer.
1050 Watch out, this function turns off all type checking and array bounds checking."
1051 (declare #.$optimize-serialization)
1052 (declare (type (simple-array (unsigned-byte 8)) buffer)
1053 (type fixnum index))
1054 ;; Eight bits at a time, least significant bits first
1057 for places fixnum upfrom 0 by 8
1058 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1059 do (setq val (logior val (ash byte places))))
1060 (values val index)))
1062 (defun decode-sfixed32 (buffer index)
1063 "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
1064 Returns both the decoded value and the new index into the buffer.
1065 Watch out, this function turns off all type checking and array bounds checking."
1066 (declare #.$optimize-serialization)
1067 (declare (type (simple-array (unsigned-byte 8)) buffer)
1068 (type fixnum index))
1069 ;; Eight bits at a time, least significant bits first
1071 (declare (type fixnum val))
1073 for places fixnum upfrom 0 by 8
1074 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1075 do (setq val (ilogior val (iash byte places))))
1076 (when (i= (ldb (byte 1 31) val) 1) ;sign bit set, so negative value
1077 (decf val #.(ash 1 32)))
1078 (values val index)))
1080 (defun decode-sfixed64 (buffer index)
1081 "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
1082 Returns both the decoded value and the new index into the buffer.
1083 Watch out, this function turns off all type checking and array bounds checking."
1084 (declare #.$optimize-serialization)
1085 (declare (type (simple-array (unsigned-byte 8)) buffer)
1086 (type fixnum index))
1087 ;; Eight bits at a time, least significant bits first
1090 for places fixnum upfrom 0 by 8
1091 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1092 do (setq val (logior val (ash byte places))))
1093 (when (i= (ldb (byte 1 63) val) 1) ;sign bit set, so negative value
1094 (decf val #.(ash 1 64)))
1095 (values val index)))
1097 (defun decode-single (buffer index)
1098 "Decodes the next single float in the buffer at the given index.
1099 Returns both the decoded value and the new index into the buffer.
1100 Watch out, this function turns off all type checking and array bounds checking."
1101 (declare #.$optimize-serialization)
1102 (declare (type (simple-array (unsigned-byte 8)) buffer)
1103 (type fixnum index))
1104 ;; Eight bits at a time, least significant bits first
1107 for places fixnum upfrom 0 by 8
1108 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1109 do (setq bits (logior bits (ash byte places))))
1110 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
1111 (decf bits #.(ash 1 32)))
1112 (values (make-single-float bits) index)))
1114 (defun decode-double (buffer index)
1115 "Decodes the next double float in the buffer at the given index.
1116 Returns both the decoded value and the new index into the buffer.
1117 Watch out, this function turns off all type checking and array bounds checking."
1118 (declare #.$optimize-serialization)
1119 (declare (type (simple-array (unsigned-byte 8)) buffer)
1120 (type fixnum index))
1121 ;; Eight bits at a time, least significant bits first
1125 for places fixnum upfrom 0 by 8
1126 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1127 do (setq low (logior low (ash byte places))))
1129 for places fixnum upfrom 0 by 8
1130 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1131 do (setq high (logior high (ash byte places))))
1132 ;; High bits are signed, but low bits are unsigned
1133 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
1134 (decf high #.(ash 1 32)))
1135 (values (make-double-float low high) index)))
1137 (defun decode-string (buffer index)
1138 "Decodes the next UTF-8 encoded string in the buffer at the given index.
1139 Returns both the decoded string and the new index into the buffer.
1140 Watch out, this function turns off all type checking and array bounds checking."
1141 (declare #.$optimize-serialization)
1142 (declare (type (simple-array (unsigned-byte 8)) buffer)
1143 (type fixnum index))
1144 (multiple-value-bind (len idx)
1145 (decode-uint32 buffer index)
1146 (declare (type (unsigned-byte 32) len)
1148 (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len))))
1150 (defun decode-octets (buffer index)
1151 "Decodes the next octets in the buffer at the given index.
1152 Returns both the decoded value and the new index into the buffer.
1153 Watch out, this function turns off all type checking and array bounds checking."
1154 (declare #.$optimize-serialization)
1155 (declare (type (simple-array (unsigned-byte 8)) buffer)
1156 (type fixnum index))
1157 (multiple-value-bind (len idx)
1158 (decode-uint32 buffer index)
1159 (declare (type (unsigned-byte 32) len)
1161 (values (subseq buffer idx (i+ idx len)) (i+ idx len))))
1164 ;;; Wire-level lengths
1165 ;;; These are called at the lowest level, so arg types are assumed to be correct
1167 (defun length32 (val)
1168 "Returns the length that 'val' will take when encoded as a 32-bit integer."
1169 (declare #.$optimize-serialization)
1170 (declare (type (unsigned-byte 32) val))
1172 (declare (type fixnum size))
1174 (setq val (iash val -7))
1179 (defun length64 (val)
1180 "Returns the length that 'val' will take when encoded as a 64-bit integer."
1181 (declare #.$optimize-serialization)
1182 (declare (type (unsigned-byte 64) val))
1184 (declare (type fixnum size))
1186 (setq val (ash val -7))
1192 ;;; Skipping elements
1193 ;;; This is called at the lowest level, so arg types are assumed to be correct
1195 (defun skip-element (buffer index tag)
1196 "Skip an element in the buffer at the index of the given wire type.
1197 Returns the new index in the buffer.
1198 Watch out, this function turns off all type checking and all array bounds checking."
1199 (declare #.$optimize-serialization)
1200 (declare (type (simple-array (unsigned-byte 8)) buffer)
1202 (type (unsigned-byte 32) tag))
1203 (case (ilogand tag #x7)
1204 ((#.$wire-type-varint)
1205 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
1206 until (i< byte 128))
1208 ((#.$wire-type-string)
1209 (multiple-value-bind (len idx)
1210 (decode-uint32 buffer index)
1211 (declare (type (unsigned-byte 32) len)
1214 ((#.$wire-type-32bit)
1216 ((#.$wire-type-64bit)
1218 ((#.$wire-type-start-group)
1219 (loop (multiple-value-bind (new-tag idx)
1220 (decode-uint32 buffer index)
1221 (cond ((not (i= (ilogand new-tag #x7) $wire-type-end-group))
1222 ;; If it's not the end of a group, skip the next element
1223 (setq index (skip-element buffer idx new-tag)))
1224 ;; If it's the end of the expected group, we're done
1225 ((i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1228 (assert (i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group)) ()
1229 "Couldn't find a matching end group tag"))))))