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 folded 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 (defmacro gen-zig-zag (bits)
95 "Generate 32- or 64-bit versions of zig-zag encoder/decoder."
96 (assert (and (plusp bits) (zerop (mod bits 8))))
97 (let* ((zig-zag-encode (fintern "~A~A" 'zig-zag-encode bits))
98 (zig-zag-decode (fintern "~A~A" 'zig-zag-decode bits))
99 (zig-zag-shift (1+ (- bits))))
101 (defun ,zig-zag-encode (val)
102 (declare #.$optimize-serialization)
103 (declare (type (signed-byte ,bits) val))
104 (logxor (ash val 1) (ash val ,zig-zag-shift)))
105 (define-compiler-macro ,zig-zag-encode (&whole form val)
107 `(locally (declare #.$optimize-serialization
108 (type (signed-byte ,',bits) ,val))
109 (logxor (ash ,val 1) (ash ,val ,',zig-zag-shift)))
111 (defun ,zig-zag-decode (val)
112 (declare #.$optimize-serialization)
113 (declare (type (unsigned-byte ,bits) val))
114 (logxor (ash val -1) (- (logand val 1))))
115 (define-compiler-macro ,zig-zag-decode (&whole form val)
117 `(locally (declare #.$optimize-serialization
118 (type (unsigned-byte ,',bits) ,val))
119 (logxor (ash ,val -1) (- (logand ,val 1))))
128 ;; Serialize 'val' of primitive type 'type' into the buffer
129 (defun serialize-prim (val type tag buffer index)
130 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
131 The value is given by 'val', the primitive type by 'type'.
132 Modifies the buffer in place, and returns the new index into the buffer.
133 Watch out, this function turns off most type checking and all array bounds checking."
134 (declare (type (unsigned-byte 32) tag)
136 (locally (declare #.$optimize-serialization)
137 (let ((idx (encode-uint32 tag buffer index)))
138 (declare (type fixnum idx))
141 (encode-uint32 (ldb (byte 32 0) val) buffer idx))
143 (encode-uint64 (ldb (byte 64 0) val) buffer idx))
145 (encode-uint32 (zig-zag-encode32 val) buffer idx))
147 (encode-uint64 (zig-zag-encode64 val) buffer idx))
149 (encode-fixed32 val buffer idx))
151 (encode-sfixed32 val buffer idx))
153 (encode-fixed64 val buffer idx))
155 (encode-sfixed64 val buffer idx))
157 (encode-string val buffer idx))
159 (encode-octets val buffer idx))
161 (encode-uint32 (if val 1 0) buffer idx))
163 (encode-single val buffer idx))
165 (encode-double val buffer idx))
166 ;; A few of our homegrown types
168 (let ((val (if (keywordp val)
170 ;; Non-keyword symbols are consy, avoid them if possible
171 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
172 (encode-string val buffer idx)))
173 ((:date :time :datetime :timestamp)
174 (encode-uint64 (ldb (byte 64 0) val) buffer idx))))))
176 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
177 (setq type (fold-symbol type)
178 tag (fold-symbol tag))
179 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
180 :fixed32 :sfixed32 :fixed64 :sfixed64
181 :string :bytes :bool :float :double))
182 `(locally (declare #.$optimize-serialization
183 (type (simple-array (unsigned-byte 8)) ,buffer)
184 ;; 'tag' is a constant, no need to declare its type
185 (type fixnum ,index))
186 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
187 (declare (type fixnum idx))
190 `(encode-uint32 (ldb (byte 32 0) ,val) ,buffer idx))
192 `(encode-uint64 (ldb (byte 64 0) ,val) ,buffer idx))
194 `(encode-uint32 ,val ,buffer idx))
196 `(encode-uint64 ,val ,buffer idx))
198 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
200 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
202 `(encode-fixed32 ,val ,buffer idx))
204 `(encode-sfixed32 ,val ,buffer idx))
206 `(encode-fixed64 ,val ,buffer idx))
208 `(encode-sfixed64 ,val ,buffer idx))
210 `(encode-string ,val ,buffer idx))
212 `(encode-octets ,val ,buffer idx))
214 `(encode-uint32 (if ,val 1 0) ,buffer idx))
216 `(encode-single ,val ,buffer idx))
218 `(encode-double ,val ,buffer idx)))))
221 (defun serialize-packed (values type tag buffer index &optional vectorp)
222 "Serializes a set of packed values into the buffer at the given index.
223 The values are given by 'values', the primitive type by 'type'.
224 Modifies the buffer in place, and returns the new index into the buffer.
225 Watch out, this function turns off most type checking and all array bounds checking."
226 (declare (ignore vectorp)
227 (type (simple-array (unsigned-byte 8)) buffer)
228 (type (unsigned-byte 32) tag)
230 (locally (declare #.$optimize-serialization)
231 (let ((idx (encode-uint32 tag buffer index)))
232 (declare (type fixnum idx))
233 (multiple-value-bind (full-len len)
234 (packed-size values type tag)
235 (declare (type fixnum len) (ignore full-len))
236 (setq idx (encode-uint32 len buffer idx)))
239 (map () #'(lambda (val) (setq idx (encode-uint32 (ldb (byte 32 0) val) buffer idx))) values))
241 (map () #'(lambda (val) (setq idx (encode-uint64 (ldb (byte 64 0) val) buffer idx))) values))
243 (map () #'(lambda (val) (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))) values))
245 (map () #'(lambda (val) (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))) values))
247 (map () #'(lambda (val) (setq idx (encode-fixed32 val buffer idx))) values))
249 (map () #'(lambda (val) (setq idx (encode-sfixed32 val buffer idx))) values))
251 (map () #'(lambda (val) (setq idx (encode-fixed64 val buffer idx))) values))
253 (map () #'(lambda (val) (setq idx (encode-sfixed64 val buffer idx))) values))
255 (map () #'(lambda (val) (setq idx (encode-uint32 (if val 1 0) buffer idx))) values))
257 (map () #'(lambda (val) (setq idx (encode-single val buffer idx))) values))
259 (map () #'(lambda (val) (setq idx (encode-double val buffer idx))) values)))
262 ;; The optimized serializers supply 'vectorp' so we can generate better code
263 (define-compiler-macro serialize-packed (&whole form values type tag buffer index
264 &optional (vectorp nil vectorp-p))
265 (setq type (fold-symbol type)
266 tag (fold-symbol tag))
268 `(member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
269 :fixed32 :sfixed32 :fixed64 :sfixed64
270 :bool :float :double)))
271 `(locally (declare #.$optimize-serialization
272 (type (simple-array (unsigned-byte 8)) ,buffer)
273 ;; 'tag' is a constant, no need to declare its type
274 (type fixnum ,index))
275 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
276 (declare (type fixnum idx))
277 (multiple-value-bind (full-len len)
278 (packed-size ,values ,type ,tag)
279 (declare (type fixnum len) (ignore full-len))
280 (setq idx (encode-uint32 len ,buffer idx)))
281 (,(if vectorp 'dovector 'dolist) (val ,values)
284 `(setq idx (encode-uint32 (ldb (byte 32 0) val) ,buffer idx)))
286 `(setq idx (encode-uint64 (ldb (byte 64 0) val) ,buffer idx)))
288 `(setq idx (encode-uint32 val ,buffer idx)))
290 `(setq idx (encode-uint64 val ,buffer idx)))
292 `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
294 `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
296 `(setq idx (encode-fixed32 val ,buffer idx)))
298 `(setq idx (encode-sfixed32 val ,buffer idx)))
300 `(setq idx (encode-fixed64 val ,buffer idx)))
302 `(setq idx (encode-sfixed64 val ,buffer idx)))
304 `(setq idx (encode-uint32 (if val 1 0) ,buffer idx)))
306 `(setq idx (encode-single val ,buffer idx)))
308 `(setq idx (encode-double val ,buffer idx)))))
312 (defun serialize-enum (val enum-values tag buffer index)
313 "Serializes a Protobufs enum value into the buffer at the given index.
314 The value is given by 'val', the enum values are in 'enum-values'.
315 Modifies the buffer in place, and returns the new index into the buffer.
316 Watch out, this function turns off most type checking and all array bounds checking."
317 (declare (type list enum-values)
318 (type (simple-array (unsigned-byte 8)) buffer)
319 (type (unsigned-byte 32) tag)
321 (locally (declare #.$optimize-serialization)
322 (let* ((val (let ((e (find val enum-values :key #'proto-value)))
323 (and e (proto-index e))))
324 (idx (encode-uint32 tag buffer index)))
325 (declare (type (unsigned-byte 32) val)
327 (encode-uint32 (ldb (byte 32 0) val) buffer idx))))
329 (defun serialize-packed-enum (values enum-values tag buffer index)
330 "Serializes Protobufs enum values into the buffer at the given index.
331 The values are given by 'values', the enum values are in 'enum-values'.
332 Modifies the buffer in place, and returns the new index into the buffer.
333 Watch out, this function turns off most type checking and all array bounds checking."
334 (declare (type list enum-values)
335 (type (simple-array (unsigned-byte 8)) buffer)
336 (type (unsigned-byte 32) tag)
338 (locally (declare #.$optimize-serialization)
339 (let ((idx (encode-uint32 tag buffer index)))
340 (declare (type fixnum idx))
341 (multiple-value-bind (full-len len)
342 (packed-enum-size values enum-values tag)
343 (declare (type fixnum len) (ignore full-len))
344 (setq idx (encode-uint32 len buffer idx)))
345 (map () #'(lambda (val)
346 (let ((val (let ((e (find val enum-values :key #'proto-value)))
347 (and e (proto-index e)))))
348 (declare (type (unsigned-byte 32) val))
349 (setq idx (encode-uint32 (ldb (byte 32 0) val) buffer idx)))) values)
355 ;; Deserialize the next object of type 'type'
356 (defun deserialize-prim (type buffer index)
357 "Deserializes the next object of primitive type 'type'.
358 Deserializes from the byte vector 'buffer' starting at 'index'.
359 Returns the value and and the new index into the buffer.
360 Watch out, this function turns off most type checking and all array bounds checking."
361 (declare (type (simple-array (unsigned-byte 8)) buffer)
363 (locally (declare #.$optimize-serialization)
366 (decode-int32 buffer index))
368 (decode-int64 buffer index))
370 (decode-uint32 buffer index))
372 (decode-uint64 buffer index))
374 (multiple-value-bind (val idx)
375 (decode-uint32 buffer index)
376 (values (zig-zag-decode32 val) idx)))
378 (multiple-value-bind (val idx)
379 (decode-uint64 buffer index)
380 (values (zig-zag-decode64 val) idx)))
382 (decode-fixed32 buffer index))
384 (decode-sfixed32 buffer index))
386 (decode-fixed64 buffer index))
388 (decode-sfixed64 buffer index))
390 (decode-string buffer index))
392 (decode-octets buffer index))
394 (multiple-value-bind (val idx)
395 (decode-uint32 buffer index)
396 (values (if (i= val 0) nil t) idx)))
398 (decode-single buffer index))
400 (decode-double buffer index))
401 ;; A few of our homegrown types
403 ;; Note that this is consy, avoid it if possible
404 (multiple-value-bind (val idx)
405 (decode-string buffer index)
406 (values (make-lisp-symbol val) idx)))
407 ((:date :time :datetime :timestamp)
408 (decode-uint64 buffer index)))))
410 (define-compiler-macro deserialize-prim (&whole form type buffer index)
411 (setq type (fold-symbol type))
412 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
413 :fixed32 :sfixed32 :fixed64 :sfixed64
414 :string :bytes :bool :float :double))
415 `(locally (declare #.$optimize-serialization
416 (type (simple-array (unsigned-byte 8)) ,buffer)
417 (type fixnum ,index))
420 `(decode-int32 ,buffer ,index))
422 `(decode-int64 ,buffer ,index))
424 `(decode-uint32 ,buffer ,index))
426 `(decode-uint64 ,buffer ,index))
428 `(multiple-value-bind (val idx)
429 (decode-uint32 ,buffer ,index)
430 (values (zig-zag-decode32 val) idx)))
432 `(multiple-value-bind (val idx)
433 (decode-uint64 ,buffer ,index)
434 (values (zig-zag-decode64 val) idx)))
436 `(decode-fixed32 ,buffer ,index))
438 `(decode-sfixed32 ,buffer ,index))
440 `(decode-fixed64 ,buffer ,index))
442 `(decode-sfixed64 ,buffer ,index))
444 `(decode-string ,buffer ,index))
446 `(decode-octets ,buffer ,index))
448 `(multiple-value-bind (val idx)
449 (decode-uint32 ,buffer ,index)
450 (values (if (i= val 0) nil t) idx)))
452 `(decode-single ,buffer ,index))
454 `(decode-double ,buffer ,index))))
457 (defun deserialize-packed (type buffer index)
458 "Deserializes the next packed values of type 'type'.
459 Deserializes from the byte vector 'buffer' starting at 'index'.
460 Returns the value and and the new index into the buffer.
461 Watch out, this function turns off most type checking and all array bounds checking."
462 (declare (type (simple-array (unsigned-byte 8)) buffer)
464 (locally (declare #.$optimize-serialization)
465 (multiple-value-bind (len idx)
466 (decode-uint32 buffer index)
467 (declare (type (unsigned-byte 32) len)
469 (let ((end (i+ idx len)))
470 (declare (type (unsigned-byte 32) end))
471 (with-collectors ((values collect-value))
474 (return-from deserialize-packed (values values idx)))
475 (multiple-value-bind (val nidx)
478 (decode-int32 buffer idx))
480 (decode-int64 buffer idx))
482 (decode-uint32 buffer idx))
484 (decode-uint64 buffer idx))
486 (multiple-value-bind (val nidx)
487 (decode-uint32 buffer idx)
488 (values (zig-zag-decode32 val) nidx)))
490 (multiple-value-bind (val nidx)
491 (decode-uint64 buffer idx)
492 (values (zig-zag-decode64 val) nidx)))
494 (decode-fixed32 buffer idx))
496 (decode-sfixed32 buffer idx))
498 (decode-fixed64 buffer idx))
500 (decode-sfixed64 buffer idx))
502 (multiple-value-bind (val nidx)
503 (decode-uint32 buffer idx)
504 (values (if (i= val 0) nil t) nidx)))
506 (decode-single buffer idx))
508 (decode-double buffer idx)))
510 (setq idx nidx))))))))
512 (define-compiler-macro deserialize-packed (&whole form type buffer index)
513 (setq type (fold-symbol type))
514 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
515 :fixed32 :sfixed32 :fixed64 :sfixed64
516 :bool :float :double))
517 `(locally (declare #.$optimize-serialization
518 (type (simple-array (unsigned-byte 8)) ,buffer)
519 (type fixnum ,index))
520 (block deserialize-packed
521 (multiple-value-bind (len idx)
522 (decode-uint32 ,buffer ,index)
523 (declare (type (unsigned-byte 32) len)
525 (let ((end (i+ idx len)))
526 (declare (type (unsigned-byte 32) end))
527 (with-collectors ((values collect-value))
530 (return-from deserialize-packed (values values idx)))
531 (multiple-value-bind (val nidx)
534 `(decode-int32 ,buffer idx))
536 `(decode-int64 ,buffer idx))
538 `(decode-uint32 ,buffer idx))
540 `(decode-uint64 ,buffer idx))
542 `(multiple-value-bind (val nidx)
543 (decode-uint32 ,buffer idx)
544 (values (zig-zag-decode32 val) nidx)))
546 `(multiple-value-bind (val nidx)
547 (decode-uint64 ,buffer idx)
548 (values (zig-zag-decode64 val) nidx)))
550 `(decode-fixed32 ,buffer idx))
552 `(decode-sfixed32 ,buffer idx))
554 `(decode-fixed64 ,buffer idx))
556 `(decode-sfixed64 ,buffer idx))
558 `(multiple-value-bind (val nidx)
559 (decode-uint32 ,buffer idx)
560 (values (if (i= val 0) nil t) nidx)))
562 `(decode-single ,buffer idx))
564 `(decode-double ,buffer idx)))
566 (setq idx nidx))))))))
569 (defun deserialize-enum (enum-values buffer index)
570 "Deserializes the next enum value take from 'enum-values'.
571 Deserializes from the byte vector 'buffer' starting at 'index'.
572 Returns the value and and the new index into the buffer.
573 Watch out, this function turns off most type checking and all array bounds checking."
574 (declare (type list enum-values)
575 (type (simple-array (unsigned-byte 8)) buffer)
577 (locally (declare #.$optimize-serialization)
578 (multiple-value-bind (val idx)
579 (decode-int32 buffer index)
580 (let ((val (let ((e (find val enum-values :key #'proto-index)))
581 (and e (proto-value e)))))
584 (defun deserialize-packed-enum (enum-values buffer index)
585 "Deserializes the next packed enum values given in 'enum-values'.
586 Deserializes from the byte vector 'buffer' starting at 'index'.
587 Returns the value and and the new index into the buffer.
588 Watch out, this function turns off most type checking and all array bounds checking."
589 (declare (type list enum-values)
590 (type (simple-array (unsigned-byte 8)) buffer)
592 (locally (declare #.$optimize-serialization)
593 (multiple-value-bind (len idx)
594 (decode-uint32 buffer index)
595 (declare (type (unsigned-byte 32) len)
597 (let ((end (i+ idx len)))
598 (declare (type (unsigned-byte 32) end))
599 (with-collectors ((values collect-value))
602 (return-from deserialize-packed-enum (values values idx)))
603 (multiple-value-bind (val nidx)
604 (decode-int32 buffer idx)
605 (let ((val (let ((e (find val enum-values :key #'proto-index)))
606 (and e (proto-value e)))))
608 (setq idx nidx)))))))))
613 (defun prim-size (val type tag)
614 "Returns the size in bytes that the primitive object will take when serialized.
615 Watch out, this function turns off most type checking."
616 (declare (type (unsigned-byte 32) tag))
617 (locally (declare #.$optimize-serialization)
620 (i+ (length32 tag) (length32 (ldb (byte 32 0) val))))
622 (i+ (length32 tag) (length64 (ldb (byte 64 0) val))))
624 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
626 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
627 ((:fixed32 :sfixed32)
628 (i+ (length32 tag) 4))
629 ((:fixed64 :sfixed64)
630 (i+ (length32 tag) 8))
632 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
633 (i+ (length32 tag) (length32 len) len)))
635 (let ((len (length val)))
636 (i+ (length32 tag) (length32 len) len)))
638 (i+ (length32 tag) 1))
640 (i+ (length32 tag) 4))
642 (i+ (length32 tag) 8))
643 ;; A few of our homegrown types
645 (let ((len (if (keywordp val)
646 (length (symbol-name val))
647 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
648 (i+ (length32 tag) (length32 len) len)))
649 ((:date :time :datetime :timestamp)
650 (i+ (length32 tag) 8)))))
652 (define-compiler-macro prim-size (&whole form val type tag)
653 (setq type (fold-symbol type)
654 tag (fold-symbol tag))
655 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
656 :fixed32 :sfixed32 :fixed64 :sfixed64
657 :string :bytes :bool :float :double))
658 `(locally (declare #.$optimize-serialization)
661 `(i+ (length32 ,tag) (length32 (ldb (byte 32 0) ,val))))
663 `(i+ (length32 ,tag) (length64 (ldb (byte 64 0) ,val))))
665 `(i+ (length32 ,tag) (length32 ,val)))
667 `(i+ (length32 ,tag) (length64 ,val)))
669 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
671 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
672 ((:fixed32 :sfixed32)
673 `(i+ (length32 ,tag) 4))
674 ((:fixed64 :sfixed64)
675 `(i+ (length32 ,tag) 8))
677 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
678 (i+ (length32 ,tag) (length32 len) len)))
680 `(let ((len (length ,val)))
681 (i+ (length32 ,tag) (length32 len) len)))
683 `(i+ (length32 ,tag) 1))
685 `(i+ (length32 ,tag) 4))
687 `(i+ (length32 ,tag) 8))))
690 (defun packed-size (values type tag &optional vectorp)
691 "Returns the size in bytes that the packed object will take when serialized.
692 Watch out, this function turns off most type checking."
693 (declare (ignore vectorp)
694 (type (unsigned-byte 32) tag))
695 (locally (declare #.$optimize-serialization)
696 (let ((len (let ((len 0))
697 (declare (type fixnum len))
698 (map () #'(lambda (val)
699 (iincf len (ecase type
700 ((:int32 :uint32) (length32 (ldb (byte 32 0) val)))
701 ((:int64 :uint64) (length64 (ldb (byte 64 0) val)))
702 ((:sint32) (length32 (zig-zag-encode32 val)))
703 ((:sint64) (length64 (zig-zag-encode64 val)))
704 ((:fixed32 :sfixed32) 4)
705 ((:fixed64 :sfixed64) 8)
708 ((:double) 8)))) values)
710 (declare (type (unsigned-byte 32) len))
711 ;; Two value: the full size of the packed object, and the size
712 ;; of just the payload
713 (values (i+ (length32 tag) (length32 len) len) len))))
715 ;; The optimized serializers supply 'vectorp' so we can generate better code
716 (define-compiler-macro packed-size (&whole form values type tag
717 &optional (vectorp nil vectorp-p))
718 (setq type (fold-symbol type)
719 tag (fold-symbol tag))
721 (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
722 :fixed32 :sfixed32 :fixed64 :sfixed64
723 :bool :float :double)))
724 `(locally (declare #.$optimize-serialization)
725 (let ((len (let ((len 0))
726 (declare (type fixnum len))
727 (,(if vectorp 'dovector 'dolist) (val ,values)
728 (iincf len ,(ecase type
729 ((:int32) `(length32 (ldb (byte 32 0) val)))
730 ((:int64) `(length64 (ldb (byte 64 0) val)))
731 ((:uint32) `(length32 val))
732 ((:uint64) `(length64 val))
733 ((:sint32) `(length32 (zig-zag-encode32 val)))
734 ((:sint64) `(length64 (zig-zag-encode64 val)))
735 ((:fixed32 :sfixed32) `4)
736 ((:fixed64 :sfixed64) `8)
741 (declare (type (unsigned-byte 32) len))
742 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
745 (defun enum-size (val enum-values tag)
746 "Returns the size in bytes that the enum object will take when serialized."
747 (declare (type list enum-values)
748 (type (unsigned-byte 32) tag))
749 (let ((idx (let ((e (find val enum-values :key #'proto-value)))
750 (and e (proto-index e)))))
752 (serialization-error "There is no enum value for ~S" val))
753 (i+ (length32 tag) (length32 (ldb (byte 32 0) idx)))))
755 (defun packed-enum-size (values enum-values tag)
756 "Returns the size in bytes that the enum values will take when serialized."
757 (declare (type list enum-values)
758 (type (unsigned-byte 32) tag))
759 (let ((len (let ((len 0))
760 (declare (type fixnum len))
761 (map () #'(lambda (val)
762 (let ((idx (let ((e (find val enum-values :key #'proto-value)))
763 (and e (proto-index e)))))
765 (serialization-error "There is no enum value for ~S" val))
766 (iincf len (length32 (ldb (byte 32 0) idx))))) values)
768 (declare (type (unsigned-byte 32) len))
769 ;; Two value: the full size of the packed object, and the size
770 ;; of just the payload
771 (values (i+ (length32 tag) (length32 len) len) len)))
774 ;;; Wire-level encoders
775 ;;; These are called at the lowest level, so arg types are assumed to be correct
777 (defmacro generate-integer-encoders (bits)
778 "Generate 32- or 64-bit versions of integer encoders."
779 (assert (and (plusp bits) (zerop (mod bits 8))))
780 (let* ((encode-uint (fintern "~A~A" 'encode-uint bits))
781 (encode-fixed (fintern "~A~A" 'encode-fixed bits))
782 (encode-sfixed (fintern "~A~A" 'encode-sfixed bits))
784 ;; Given bits, can we use fixnums safely?
785 (fixnump (<= bits (integer-length most-negative-fixnum)))
786 (ldb (if fixnump 'ildb 'ldb))
787 (ash (if fixnump 'iash 'ash))
788 (zerop-val (if fixnump '(i= val 0) '(zerop val))))
790 (defun ,encode-uint (val buffer index)
792 "Encodes the unsigned ~A-bit integer 'val' as a varint into the buffer at the given index.~
793 ~& Modifies the buffer, and returns the new index into the buffer.~
794 ~& Watch out, this function turns off all type checking and array bounds checking." bits)
795 (declare #.$optimize-serialization)
796 (declare (type (unsigned-byte ,bits) val)
797 (type (simple-array (unsigned-byte 8)) buffer)
799 ;; Seven bits at a time, least significant bits first
800 (loop do (let ((bits (,ldb (byte 7 0) val)))
801 (declare (type (unsigned-byte 8) bits))
802 (setq val (,ash val -7))
803 (setf (aref buffer index)
804 (ilogior bits (if ,zerop-val 0 128)))
807 (values index buffer)) ;return the buffer to improve 'trace'
808 (defun ,encode-fixed (val buffer index)
810 "Encodes the unsigned ~A-bit integer 'val' as a fixed int into the buffer at the given index.~
811 ~& Modifies the buffer, and returns the new index into the buffer.~
812 ~& Watch out, this function turns off all type checking and array bounds checking." bits)
813 (declare #.$optimize-serialization)
814 (declare (type (unsigned-byte ,bits) val)
815 (type (simple-array (unsigned-byte 8)) buffer)
817 (loop repeat ,bytes doing
818 (let ((byte (,ldb (byte 8 0) val)))
819 (declare (type (unsigned-byte 8) byte))
820 (setq val (,ash val -8))
821 (setf (aref buffer index) byte)
823 (values index buffer))
824 (defun ,encode-sfixed (val buffer index)
826 "Encodes the signed ~A-bit integer 'val' as a fixed int into the buffer at the given index.~
827 ~& Modifies the buffer, and returns the new index into the buffer.~
828 ~& Watch out, this function turns off all type checking and array bounds checking." bits)
829 (declare #.$optimize-serialization)
830 (declare (type (signed-byte ,bits) val)
831 (type (simple-array (unsigned-byte 8)) buffer)
833 (loop repeat ,bytes doing
834 (let ((byte (,ldb (byte 8 0) val)))
835 (declare (type (unsigned-byte 8) byte))
836 (setq val (,ash val -8))
837 (setf (aref buffer index) byte)
839 (values index buffer)))))
841 (generate-integer-encoders 32)
842 (generate-integer-encoders 64)
844 (defun encode-single (val buffer index)
845 "Encodes the single float 'val' into the buffer at the given index.
846 Modifies the buffer, and returns the new index into the buffer.
847 Watch out, this function turns off all type checking and array bounds checking."
848 (declare #.$optimize-serialization)
849 (declare (type single-float val)
850 (type (simple-array (unsigned-byte 8)) buffer)
852 (let ((bits (single-float-bits val)))
854 (let ((byte (ldb (byte 8 0) bits)))
855 (declare (type (unsigned-byte 8) byte))
856 (setq bits (ash bits -8))
857 (setf (aref buffer index) byte)
859 (values index buffer))
861 (defun encode-double (val buffer index)
862 "Encodes the double float 'val' into the buffer at the given index.
863 Modifies the buffer, and returns the new index into the buffer.
864 Watch out, this function turns off all type checking and array bounds checking."
865 (declare #.$optimize-serialization)
866 (declare (type double-float val)
867 (type (simple-array (unsigned-byte 8)) buffer)
869 (multiple-value-bind (low high)
870 (double-float-bits val)
872 (let ((byte (ldb (byte 8 0) low)))
873 (declare (type (unsigned-byte 8) byte))
874 (setq low (ash low -8))
875 (setf (aref buffer index) byte)
878 (let ((byte (ldb (byte 8 0) high)))
879 (declare (type (unsigned-byte 8) byte))
880 (setq high (ash high -8))
881 (setf (aref buffer index) byte)
883 (values index buffer))
885 (defun encode-string (string buffer index)
886 "Encodes the octets into the buffer at the given index.
887 Modifies the buffer, and returns the new index into the buffer.
888 Watch out, this function turns off all type checking and array bounds checking."
889 (declare #.$optimize-serialization)
890 (declare (type (simple-array (unsigned-byte 8)) buffer)
892 (let* ((octets (babel:string-to-octets string :encoding :utf-8))
893 (len (length octets))
894 (idx (encode-uint32 len buffer index)))
895 (declare (type (array (unsigned-byte 8)) octets)
897 (type (unsigned-byte 32) idx))
898 (replace buffer octets :start1 idx)
899 (values (i+ idx len) buffer)))
901 (defun encode-octets (octets buffer index)
902 "Encodes the octets into the buffer at the given index.
903 Modifies the buffer, and returns the new index into the buffer.
904 Watch out, this function turns off all type checking and array bounds checking."
905 (declare #.$optimize-serialization)
906 (declare (type (array (unsigned-byte 8)) octets)
907 (type (simple-array (unsigned-byte 8)) buffer)
909 (let* ((len (length octets))
910 (idx (encode-uint32 len buffer index)))
911 (declare (type fixnum len)
912 (type (unsigned-byte 32) idx))
913 (replace buffer octets :start1 idx)
914 (values (i+ idx len) buffer)))
917 ;;; Wire-level decoders
918 ;;; These are called at the lowest level, so arg types are assumed to be correct
920 ;; Decode the value from the buffer at the given index,
921 ;; then return the value and new index into the buffer
922 (defmacro generate-integer-decoders (bits)
923 "Generate 32- or 64-bit versions of integer decoders."
924 (assert (and (plusp bits) (zerop (mod bits 8))))
925 (let* ((decode-uint (fintern "~A~A" 'decode-uint bits))
926 (decode-int (fintern "~A~A" 'decode-int bits))
927 (decode-fixed (fintern "~A~A" 'decode-fixed bits))
928 (decode-sfixed (fintern "~A~A" 'decode-sfixed bits))
930 ;; Given bits, can we use fixnums safely?
931 (fixnump (<= bits (integer-length most-negative-fixnum)))
932 (ldb (if fixnump 'ildb 'ldb))
933 (ash (if fixnump 'iash 'ash))
934 (decf (if fixnump 'idecf 'decf))
935 (logior (if fixnump 'ilogior 'logior)))
937 (defun ,decode-uint (buffer index)
939 "Decodes the next ~A-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." bits)
942 (declare #.$optimize-serialization)
943 (declare (type (simple-array (unsigned-byte 8)) buffer)
945 ;; Seven bits at a time, least significant bits first
947 (declare (type (unsigned-byte ,bits) val))
948 (loop for places fixnum upfrom 0 by 7
949 for byte fixnum = (prog1 (aref buffer index) (iincf index))
950 do (let ((bits (ildb (byte 7 0) byte)))
951 (declare (type (unsigned-byte 8) bits))
952 (setq val (,logior val (,ash bits places))))
955 (unless (< val ,(ash 1 bits))
956 (serialization-error "The value ~D is longer than ~A bits" val ,bits))
957 (return (values val index))))))
958 (defun ,decode-int (buffer index)
960 "Decodes the next ~A-bit varint integer in the buffer at the given index.~
961 ~& Returns both the decoded value and the new index into the buffer.~
962 ~& Watch out, this function turns off all type checking and array bounds checking." bits)
963 (declare #.$optimize-serialization)
964 (declare (type (simple-array (unsigned-byte 8)) buffer)
966 (multiple-value-bind (val index)
967 (,decode-uint buffer index)
968 ,@(when fixnump `((declare (type fixnum val))))
969 (when (i= (,ldb (byte 1 ,(1- bits)) val) 1)
970 (,decf val ,(ash 1 bits)))
972 (defun ,decode-fixed (buffer index)
974 "Decodes the next ~A-bit unsigned fixed integer in the buffer at the given index.~
975 ~& Returns both the decoded value and the new index into the buffer.~
976 ~& Watch out, this function turns off all type checking and array bounds checking." bits)
977 (declare #.$optimize-serialization)
978 (declare (type (simple-array (unsigned-byte 8)) buffer)
980 ;; Eight bits at a time, least significant bits first
982 ,@(when fixnump `((declare (type fixnum val))))
984 for places fixnum upfrom 0 by 8
985 for byte fixnum = (prog1 (aref buffer index) (iincf index))
986 do (setq val (,logior val (,ash byte places))))
988 (defun ,decode-sfixed (buffer index)
990 "Decodes the next ~A-bit signed fixed integer in the buffer at the given index.~
991 ~& Returns both the decoded value and the new index into the buffer.~
992 ~& Watch out, this function turns off all type checking and array bounds checking." bits)
993 (declare #.$optimize-serialization)
994 (declare (type (simple-array (unsigned-byte 8)) buffer)
996 ;; Eight bits at a time, least significant bits first
998 ,@(when fixnump `((declare (type fixnum val))))
1000 for places fixnum upfrom 0 by 8
1001 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1002 do (setq val (,logior val (,ash byte places))))
1003 (when (i= (,ldb (byte 1 ,(1- bits)) val) 1) ;sign bit set, so negative value
1004 (,decf val ,(ash 1 bits)))
1005 (values val index))))))
1007 (generate-integer-decoders 32)
1008 (generate-integer-decoders 64)
1010 (defun decode-single (buffer index)
1011 "Decodes the next single float in the buffer at the given index.
1012 Returns both the decoded value and the new index into the buffer.
1013 Watch out, this function turns off all type checking and array bounds checking."
1014 (declare #.$optimize-serialization)
1015 (declare (type (simple-array (unsigned-byte 8)) buffer)
1016 (type fixnum index))
1017 ;; Eight bits at a time, least significant bits first
1020 for places fixnum upfrom 0 by 8
1021 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1022 do (setq bits (logior bits (ash byte places))))
1023 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
1024 (decf bits #.(ash 1 32)))
1025 (values (make-single-float bits) index)))
1027 (defun decode-double (buffer index)
1028 "Decodes the next double float in the buffer at the given index.
1029 Returns both the decoded value and the new index into the buffer.
1030 Watch out, this function turns off all type checking and array bounds checking."
1031 (declare #.$optimize-serialization)
1032 (declare (type (simple-array (unsigned-byte 8)) buffer)
1033 (type fixnum index))
1034 ;; Eight bits at a time, least significant bits first
1038 for places fixnum upfrom 0 by 8
1039 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1040 do (setq low (logior low (ash byte places))))
1042 for places fixnum upfrom 0 by 8
1043 for byte fixnum = (prog1 (aref buffer index) (iincf index))
1044 do (setq high (logior high (ash byte places))))
1045 ;; High bits are signed, but low bits are unsigned
1046 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
1047 (decf high #.(ash 1 32)))
1048 (values (make-double-float low high) index)))
1050 (defun decode-string (buffer index)
1051 "Decodes the next UTF-8 encoded string in the buffer at the given index.
1052 Returns both the decoded string and the new index into the buffer.
1053 Watch out, this function turns off all type checking and array bounds checking."
1054 (declare #.$optimize-serialization)
1055 (declare (type (simple-array (unsigned-byte 8)) buffer)
1056 (type fixnum index))
1057 (multiple-value-bind (len idx)
1058 (decode-uint32 buffer index)
1059 (declare (type (unsigned-byte 32) len)
1061 (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len))))
1063 (defun decode-octets (buffer index)
1064 "Decodes the next octets in the buffer at the given index.
1065 Returns both the decoded value and the new index into the buffer.
1066 Watch out, this function turns off all type checking and array bounds checking."
1067 (declare #.$optimize-serialization)
1068 (declare (type (simple-array (unsigned-byte 8)) buffer)
1069 (type fixnum index))
1070 (multiple-value-bind (len idx)
1071 (decode-uint32 buffer index)
1072 (declare (type (unsigned-byte 32) len)
1074 (values (subseq buffer idx (i+ idx len)) (i+ idx len))))
1077 ;;; Wire-level lengths
1078 ;;; These are called at the lowest level, so arg types are assumed to be correct
1080 (defmacro gen-length (bits)
1081 "Generate 32- or 64-bit versions of integer length functions."
1082 (assert (and (plusp bits) (zerop (mod bits 8))))
1083 (let* (;; Given bits, can we use fixnums safely?
1084 (fixnump (<= bits (integer-length most-negative-fixnum)))
1085 (ash (if fixnump 'iash 'ash))
1086 (zerop-val (if fixnump '(i= val 0) '(zerop val))))
1087 `(defun ,(fintern "~A~A" 'length bits) (val)
1088 ,(format nil "Returns the length that 'val' will take when encoded as a ~A-bit integer." bits)
1089 (declare #.$optimize-serialization)
1090 (declare (type (unsigned-byte ,bits) val))
1092 (declare (type fixnum size))
1094 (setq val (,ash val -7))
1103 ;;; Skipping elements
1104 ;;; This is called at the lowest level, so arg types are assumed to be correct
1106 (defun skip-element (buffer index tag)
1107 "Skip an element in the buffer at the index of the given wire type.
1108 Returns the new index in the buffer.
1109 Watch out, this function turns off all type checking and all array bounds checking."
1110 (declare #.$optimize-serialization)
1111 (declare (type (simple-array (unsigned-byte 8)) buffer)
1113 (type (unsigned-byte 32) tag))
1114 (case (ilogand tag #x7)
1115 ((#.$wire-type-varint)
1116 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
1117 until (i< byte 128))
1119 ((#.$wire-type-string)
1120 (multiple-value-bind (len idx)
1121 (decode-uint32 buffer index)
1122 (declare (type (unsigned-byte 32) len)
1125 ((#.$wire-type-32bit)
1127 ((#.$wire-type-64bit)
1129 ((#.$wire-type-start-group)
1130 (loop (multiple-value-bind (new-tag idx)
1131 (decode-uint32 buffer index)
1132 (cond ((not (i= (ilogand new-tag #x7) $wire-type-end-group))
1133 ;; If it's not the end of a group, skip the next element
1134 (setq index (skip-element buffer idx new-tag)))
1135 ;; If it's the end of the expected group, we're done
1136 ((i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1139 (unless (i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1140 (serialization-error "Couldn't find a matching end group tag")))))))