1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc. ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; Protocol buffers wire format
18 (defconstant $wire-type-varint 0)
19 (defconstant $wire-type-64bit 1)
20 (defconstant $wire-type-string 2)
21 (defconstant $wire-type-start-group 3) ;supposedly obsolete
22 (defconstant $wire-type-end-group 4) ;supposedly obsolete
23 (defconstant $wire-type-32bit 5)
25 (defun make-tag (type index)
26 "Given a wire type or the name of a Protobufs type and a field index,
27 return the tag that encodes both of them."
28 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
29 (if (typep type 'fixnum)
31 (let ((type (ecase type
32 ((:int32 :uint32) $wire-type-varint)
33 ((:int64 :uint64) $wire-type-varint)
34 ((:sint32 :sint64) $wire-type-varint)
35 ((:fixed32 :sfixed32) $wire-type-32bit)
36 ((:fixed64 :sfixed64) $wire-type-64bit)
37 ((:string :bytes) $wire-type-string)
38 ((:bool) $wire-type-varint)
39 ((:float) $wire-type-32bit)
40 ((:double) $wire-type-64bit)
41 ;; A few of our homegrown types
42 ((:symbol) $wire-type-string)
43 ((:date :time :datetime :timestamp) $wire-type-64bit))))
44 (ilogior type (iash index 3))))))
46 (define-compiler-macro make-tag (&whole form type index)
47 (cond ((typep type 'fixnum)
48 `(ilogior ,type (iash ,index 3)))
50 (let ((type (ecase type
51 ((:int32 :uint32) $wire-type-varint)
52 ((:int64 :uint64) $wire-type-varint)
53 ((:sint32 :sint64) $wire-type-varint)
54 ((:fixed32 :sfixed32) $wire-type-32bit)
55 ((:fixed64 :sfixed64) $wire-type-64bit)
56 ((:string :bytes) $wire-type-string)
57 ((:bool) $wire-type-varint)
58 ((:float) $wire-type-32bit)
59 ((:double) $wire-type-64bit)
60 ;; A few of our homegrown types
61 ((:symbol) $wire-type-string)
62 ((:date :time :datetime :timestamp) $wire-type-64bit))))
63 `(ilogior ,type (iash ,index 3))))
67 (defun zig-zag-encode32 (val)
68 (declare (optimize (speed 3) (safety 0) (debug 0)))
69 (declare (type (signed-byte 32) val))
70 (logxor (ash val 1) (ash val -31)))
72 (defun zig-zag-encode64 (val)
73 (declare (optimize (speed 3) (safety 0) (debug 0)))
74 (declare (type (signed-byte 64) val))
75 (logxor (ash val 1) (ash val -63)))
77 (define-compiler-macro zig-zag-encode32 (&whole form val)
79 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
80 (type (signed-byte 32) ,val))
81 (logxor (ash ,val 1) (ash ,val -31)))
84 (define-compiler-macro zig-zag-encode64 (&whole form val)
86 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
87 (type (signed-byte 64) ,val))
88 (logxor (ash ,val 1) (ash ,val -63)))
91 (defun zig-zag-decode32 (val)
92 (declare (optimize (speed 3) (safety 0) (debug 0)))
93 (logxor (ash val -1) (- (logand val 1))))
95 (defun zig-zag-decode64 (val)
96 (declare (optimize (speed 3) (safety 0) (debug 0)))
97 (logxor (ash val -1) (- (logand val 1))))
99 (define-compiler-macro zig-zag-decode32 (&whole form val)
101 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
102 (logxor (ash ,val -1) (- (logand ,val 1))))
105 (define-compiler-macro zig-zag-decode64 (&whole form val)
107 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
108 (logxor (ash ,val -1) (- (logand ,val 1))))
114 ;; Serialize 'val' of primitive type 'type' into the buffer
115 (defun serialize-prim (val type tag buffer index)
116 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
117 The value is given by 'val', the primitive type by 'type'.
118 Modifies the buffer in place, and returns the new index into the buffer.
119 Watch out, this function turns off most type checking and all array bounds checking."
120 (declare (type (simple-array (unsigned-byte 8)) buffer)
121 (type (unsigned-byte 32) tag)
123 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
124 (let ((idx (encode-uint32 tag buffer index)))
125 (declare (type fixnum idx))
128 (encode-uint32 val buffer idx))
130 (encode-uint64 val buffer idx))
132 (encode-uint32 (zig-zag-encode32 val) buffer idx))
134 (encode-uint64 (zig-zag-encode64 val) buffer idx))
136 (encode-fixed32 val buffer idx))
138 (encode-sfixed32 val buffer idx))
140 (encode-fixed64 val buffer idx))
142 (encode-sfixed64 val buffer idx))
144 (encode-string val buffer idx))
146 (encode-octets val buffer idx))
148 (encode-uint32 (if val 1 0) buffer idx))
150 (encode-single val buffer idx))
152 (encode-double val buffer idx))
153 ;; A few of our homegrown types
155 (let ((val (if (keywordp val)
157 ;; Non-keyword symbols are consy, avoid them if possible
158 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
159 (encode-string val buffer idx)))
160 ((:date :time :datetime :timestamp)
161 (encode-uint64 val buffer idx))))))
163 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
164 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
165 :fixed32 :sfixed32 :fixed64 :sfixed64
166 :string :bytes :bool :float :double))
167 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
168 (type (simple-array (unsigned-byte 8)) ,buffer)
169 ;; 'tag' is a constant, no need to declare its type
170 (type fixnum ,index))
171 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
172 (declare (type fixnum idx))
175 `(encode-uint32 ,val ,buffer idx))
177 `(encode-uint64 ,val ,buffer idx))
179 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
181 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
183 `(encode-fixed32 ,val ,buffer idx))
185 `(encode-sfixed32 ,val ,buffer idx))
187 `(encode-fixed64 ,val ,buffer idx))
189 `(encode-sfixed64 ,val ,buffer idx))
191 `(encode-string ,val ,buffer idx))
193 `(encode-octets ,val ,buffer idx))
195 `(encode-uint32 (if ,val 1 0) ,buffer idx))
197 `(encode-single ,val ,buffer idx))
199 `(encode-double ,val ,buffer idx)))))
202 (defun serialize-packed (values type tag buffer index)
203 "Serializes a set of packed values into the buffer at the given index.
204 The values are given by 'values', the primitive type by 'type'.
205 Modifies the buffer in place, and returns the new index into the buffer.
206 Watch out, this function turns off most type checking and all array bounds checking."
207 (declare (type (simple-array (unsigned-byte 8)) buffer)
208 (type (unsigned-byte 32) tag)
210 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
211 (let ((idx (encode-uint32 tag buffer index)))
212 (declare (type fixnum idx))
213 (multiple-value-bind (full-len len)
214 (packed-size values type tag)
215 (declare (type fixnum len) (ignore full-len))
216 (setq idx (encode-uint32 len buffer idx)))
219 (dolist (val values idx)
220 (setq idx (encode-uint32 val buffer idx))))
222 (dolist (val values idx)
223 (setq idx (encode-uint64 val buffer idx))))
225 (dolist (val values idx)
226 (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))))
228 (dolist (val values idx)
229 (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
231 (dolist (val values idx)
232 (setq idx (encode-fixed32 val buffer idx))))
234 (dolist (val values idx)
235 (setq idx (encode-sfixed32 val buffer idx))))
237 (dolist (val values idx)
238 (setq idx (encode-fixed64 val buffer idx))))
240 (dolist (val values idx)
241 (setq idx (encode-sfixed64 val buffer idx))))
243 (dolist (val values idx)
244 (setq idx (encode-single val buffer idx))))
246 (dolist (val values idx)
247 (setq idx (encode-double val buffer idx))))))))
249 (define-compiler-macro serialize-packed (&whole form values type tag buffer index)
250 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
251 :fixed32 :sfixed32 :fixed64 :sfixed64
253 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
254 (type (simple-array (unsigned-byte 8)) ,buffer)
255 (type fixnum ,index))
256 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
257 (declare (type fixnum idx))
258 (multiple-value-bind (full-len len)
259 (packed-size ,values ,type ,tag)
260 (declare (type fixnum len) (ignore full-len))
261 (setq idx (encode-uint32 len ,buffer idx)))
262 (dolist (val ,values idx)
265 `(setq idx (encode-uint32 val ,buffer idx)))
267 `(setq idx (encode-uint64 val ,buffer idx)))
269 `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
271 `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
273 `(setq idx (encode-fixed32 val ,buffer idx)))
275 `(setq idx (encode-sfixed32 val ,buffer idx)))
277 `(setq idx (encode-fixed64 val ,buffer idx)))
279 `(setq idx (encode-sfixed64 val ,buffer idx)))
281 `(setq idx (encode-single val ,buffer idx)))
283 `(setq idx (encode-double val ,buffer idx)))))))
286 (defun serialize-enum (val values tag buffer index)
287 "Serializes a Protobufs enum value into the buffer at the given index.
288 The value is given by 'val', the enum values are in 'values'.
289 Modifies the buffer in place, and returns the new index into the buffer.
290 Watch out, this function turns off most type checking and all array bounds checking."
291 (declare (type (simple-array (unsigned-byte 8)) buffer)
292 (type (unsigned-byte 32) tag)
294 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
295 (let* ((val (let ((e (find val values :key #'proto-value)))
296 (and e (proto-index e))))
297 (idx (encode-uint32 tag buffer index)))
298 (declare (type (unsigned-byte 32) val)
300 (encode-uint32 val buffer idx))))
305 ;; Deserialize the next object of type 'type'
306 (defun deserialize-prim (type buffer index)
307 "Deserializes the next object of primitive type 'type'.
308 Deserializes from the byte vector 'buffer' starting at 'index'.
309 Returns the value and and the new index into the buffer.
310 Watch out, this function turns off most type checking and all array bounds checking."
311 (declare (type (simple-array (unsigned-byte 8)) buffer)
313 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
316 (decode-uint32 buffer index))
318 (decode-uint64 buffer index))
320 (multiple-value-bind (val idx)
321 (decode-uint32 buffer index)
322 (values (zig-zag-decode32 val) idx)))
324 (multiple-value-bind (val idx)
325 (decode-uint64 buffer index)
326 (values (zig-zag-decode64 val) idx)))
328 (decode-fixed32 buffer index))
330 (decode-sfixed32 buffer index))
332 (decode-fixed64 buffer index))
334 (decode-sfixed64 buffer index))
336 (decode-string buffer index))
338 (decode-octets buffer index))
340 (multiple-value-bind (val idx)
341 (decode-uint32 buffer index)
342 (values (if (zerop val) nil t) idx)))
344 (decode-single buffer index))
346 (decode-double buffer index))
347 ;; A few of our homegrown types
349 ;; Note that this is consy, avoid it if possible
350 (multiple-value-bind (val idx)
351 (decode-string buffer index)
352 (values (make-lisp-symbol val) idx)))
353 ((:date :time :datetime :timestamp)
354 (decode-uint64 buffer index)))))
356 (define-compiler-macro deserialize-prim (&whole form type buffer index)
357 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
358 :fixed32 :sfixed32 :fixed64 :sfixed64
359 :string :bytes :bool :float :double))
360 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
361 (type (simple-array (unsigned-byte 8)) ,buffer)
362 (type fixnum ,index))
365 `(decode-uint32 ,buffer ,index))
367 `(decode-uint64 ,buffer ,index))
369 `(multiple-value-bind (val idx)
370 (decode-uint32 ,buffer ,index)
371 (values (zig-zag-decode32 val) idx)))
373 `(multiple-value-bind (val idx)
374 (decode-uint64 ,buffer ,index)
375 (values (zig-zag-decode64 val) idx)))
377 `(decode-fixed32 ,buffer ,index))
379 `(decode-sfixed32 ,buffer ,index))
381 `(decode-fixed64 ,buffer ,index))
383 `(decode-sfixed64 ,buffer ,index))
385 `(decode-string ,buffer ,index))
387 `(decode-octets ,buffer ,index))
389 `(multiple-value-bind (val idx)
390 (decode-uint32 ,buffer ,index)
391 (values (if (zerop val) nil t) idx)))
393 `(decode-single ,buffer ,index))
395 `(decode-double ,buffer ,index))))
398 (defun deserialize-packed (type buffer index)
399 "Deserializes the next packed values of type 'type'.
400 Deserializes from the byte vector 'buffer' starting at 'index'.
401 Returns the value and and the new index into the buffer.
402 Watch out, this function turns off most type checking and all array bounds checking."
403 (declare (type (simple-array (unsigned-byte 8)) buffer)
405 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
406 (multiple-value-bind (len idx)
407 (decode-uint32 buffer index)
408 (declare (type (unsigned-byte 32) len)
410 (let ((end (i+ idx len)))
411 (declare (type (unsigned-byte 32) end))
412 (with-collectors ((values collect-value))
415 (return-from deserialize-packed (values values idx)))
416 (multiple-value-bind (val nidx)
419 (decode-uint32 buffer idx))
421 (decode-uint64 buffer idx))
423 (multiple-value-bind (val idx)
424 (decode-uint32 buffer idx)
425 (values (zig-zag-decode32 val) idx)))
427 (multiple-value-bind (val idx)
428 (decode-uint64 buffer idx)
429 (values (zig-zag-decode64 val) idx)))
431 (decode-fixed32 buffer idx))
433 (decode-sfixed32 buffer idx))
435 (decode-fixed64 buffer idx))
437 (decode-sfixed64 buffer idx))
439 (decode-single buffer idx))
441 (decode-double buffer idx)))
443 (setq idx nidx))))))))
445 (define-compiler-macro deserialize-packed (&whole form type buffer index)
446 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
447 :fixed32 :sfixed32 :fixed64 :sfixed64
449 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
450 (type (simple-array (unsigned-byte 8)) ,buffer)
451 (type fixnum ,index))
452 (multiple-value-bind (len idx)
453 (decode-uint32 .buffer ,index)
454 (declare (type (unsigned-byte 32) len)
456 (let ((end (i+ idx len)))
457 (declare (type (unsigned-byte 32) end))
458 (with-collectors ((values collect-value))
461 (return-from deserialize-packed (values values idx)))
462 (multiple-value-bind (val nidx)
465 `(decode-uint32 ,buffer idx))
467 `(decode-uint64 ,buffer idx))
469 `(multiple-value-bind (val idx)
470 (decode-uint32 ,buffer idx)
471 (values (zig-zag-decode32 val) idx)))
473 `(multiple-value-bind (val idx)
474 (decode-uint64 ,buffer idx)
475 (values (zig-zag-decode64 val) idx)))
477 `(decode-fixed32 ,buffer idx))
479 `(decode-sfixed32 ,buffer idx))
481 `(decode-fixed64 ,buffer idx))
483 `(decode-sfixed64 ,buffer idx))
485 `(decode-single ,buffer idx))
487 `(decode-double ,buffer idx)))
489 (setq idx nidx)))))))
492 (defun deserialize-enum (values buffer index)
493 "Deserializes the next enum value take from 'values'.
494 Deserializes from the byte vector 'buffer' starting at 'index'.
495 Returns the value and and the new index into the buffer.
496 Watch out, this function turns off most type checking and all array bounds checking."
497 (declare (type (simple-array (unsigned-byte 8)) buffer)
499 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
500 (multiple-value-bind (val idx)
501 (decode-uint32 buffer index)
502 (let ((val (let ((e (find val values :key #'proto-index)))
503 (and e (proto-value e)))))
509 (defun prim-size (val type tag)
510 "Returns the size in bytes that the primitive object will take when serialized.
511 Watch out, this function turns off most type checking."
512 (declare (type (unsigned-byte 32) tag))
513 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
516 (i+ (length32 tag) (length32 val)))
518 (i+ (length32 tag) (length64 val)))
520 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
522 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
523 ((:fixed32 :sfixed32)
524 (i+ (length32 tag) 4))
525 ((:fixed64 :sfixed64)
526 (i+ (length32 tag) 8))
528 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
529 (i+ (length32 tag) (length32 len) len)))
531 (let ((len (length val)))
532 (i+ (length32 tag) (length32 len) len)))
534 (i+ (length32 tag) 1))
536 (i+ (length32 tag) 4))
538 (i+ (length32 tag) 8))
539 ;; A few of our homegrown types
541 (let ((len (if (keywordp val)
542 (length (symbol-name val))
543 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
544 (i+ (length32 tag) (length32 len) len)))
545 ((:date :time :datetime :timestamp)
546 (i+ (length32 tag) 8)))))
548 (define-compiler-macro prim-size (&whole form val type tag)
549 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
550 :fixed32 :sfixed32 :fixed64 :sfixed64
551 :string :bytes :bool :float :double))
552 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
555 `(i+ (length32 ,tag) (length32 ,val)))
557 `(i+ (length32 ,tag) (length64 ,val)))
559 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
561 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
562 ((:fixed32 :sfixed32)
563 `(i+ (length32 ,tag) 4))
564 ((:fixed64 :sfixed64)
565 `(i+ (length32 ,tag) 8))
567 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
568 (i+ (length32 ,tag) (length32 len) len)))
570 `(let ((len (length ,val)))
571 (i+ (length32 ,tag) (length32 len) len)))
573 `(i+ (length32 ,tag) 1))
575 `(i+ (length32 ,tag) 4))
577 `(i+ (length32 ,tag) 8))))
580 (defun packed-size (values type tag)
581 "Returns the size in bytes that the packed object will take when serialized.
582 Watch out, this function turns off most type checking."
583 (declare (type (unsigned-byte 32) tag))
584 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
585 (let ((len (let ((len 0))
586 (declare (type fixnum len))
587 (dolist (val values len)
588 (iincf len (ecase type
589 ((:int32 :uint32) (length32 val))
590 ((:int64 :uint64) (length64 val))
591 ((:sint32) (length32 (zig-zag-encode32 val)))
592 ((:sint64) (length64 (zig-zag-encode64 val)))
593 ((:fixed32 :sfixed32) 4)
594 ((:fixed64 :sfixed64) 8)
597 (declare (type (unsigned-byte 32) len))
598 ;; Two value: the full size of the packed object, and the size
599 ;; of just the payload
600 (values (i+ (length32 tag) (length32 len) len) len))))
602 (define-compiler-macro packed-size (&whole form values type tag)
603 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
604 :fixed32 :sfixed32 :fixed64 :sfixed64
606 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
607 (let ((len (let ((len 0))
608 (declare (type fixnum len))
609 (dolist (val ,values len)
610 (iincf len ,(ecase type
611 ((:int32 :uint32) `(length32 val))
612 ((:int64 :uint64) `(length64 val))
613 ((:sint32) `(length32 (zig-zag-encode32 val)))
614 ((:sint64) `(length64 (zig-zag-encode64 val)))
615 ((:fixed32 :sfixed32) `4)
616 ((:fixed64 :sfixed64) `8)
619 (declare (type (unsigned-byte 32) len))
620 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
623 (defun enum-size (val values tag)
624 "Returns the size in bytes that the enum object will take when serialized."
625 (declare (type (unsigned-byte 32) tag))
626 (let ((idx (let ((e (find val values :key #'proto-value)))
627 (and e (proto-index e)))))
628 (assert idx () "There is no enum value for ~S" val)
629 (i+ (length32 tag) (length32 idx))))
634 (defun encode-uint32 (val buffer index)
635 "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
637 Modifies the buffer, and returns the new index into the buffer.
638 Watch out, this function turns off all type checking and array bounds checking."
639 (declare (optimize (speed 3) (safety 0) (debug 0)))
640 (declare (type (unsigned-byte 32) val)
641 (type (simple-array (unsigned-byte 8)) buffer)
643 ;; Seven bits at a time, least significant bits first
644 (loop do (let ((bits (ldb (byte 7 0) val)))
645 (declare (type (unsigned-byte 8) bits))
646 (setq val (ash val -7))
647 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
650 (values index buffer)) ;return the buffer to improve 'trace'
652 (defun encode-uint64 (val buffer index)
653 "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
655 Modifies the buffer, and returns the new index into the buffer.
656 Watch out, this function turns off all type checking and array bounds checking."
657 (declare (optimize (speed 3) (safety 0) (debug 0)))
658 (declare (type (unsigned-byte 64) val)
659 (type (simple-array (unsigned-byte 8)) buffer)
661 (loop do (let ((bits (ldb (byte 7 0) val)))
662 (declare (type (unsigned-byte 8) bits))
663 (setq val (ash val -7))
664 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
667 (values index buffer))
669 (defun encode-fixed32 (val buffer index)
670 "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
672 Modifies the buffer, and returns the new index into the buffer.
673 Watch out, this function turns off most type checking and all array bounds checking."
674 (declare (type (unsigned-byte 32) val)
675 (type (simple-array (unsigned-byte 8)) buffer)
677 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
679 (let ((byte (ldb (byte 8 0) val)))
680 (declare (type (unsigned-byte 8) byte))
681 (setq val (ash val -8))
682 (setf (aref buffer index) byte)
684 (values index buffer))
686 (defun encode-fixed64 (val buffer index)
687 "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
689 Modifies the buffer, and returns the new index into the buffer.
690 Watch out, this function turns off most type checking and all array bounds checking."
691 (declare (type (unsigned-byte 64) val)
692 (type (simple-array (unsigned-byte 8)) buffer)
694 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
696 (let ((byte (ldb (byte 8 0) val)))
697 (declare (type (unsigned-byte 8) byte))
698 (setq val (ash val -8))
699 (setf (aref buffer index) byte)
701 (values index buffer))
703 (defun encode-sfixed32 (val buffer index)
704 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
706 Modifies the buffer, and returns the new index into the buffer.
707 Watch out, this function turns off most type checking and all array bounds checking."
708 (declare (type (signed-byte 32) val)
709 (type (simple-array (unsigned-byte 8)) buffer)
711 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
713 (let ((byte (ldb (byte 8 0) val)))
714 (declare (type (unsigned-byte 8) byte))
715 (setq val (ash val -8))
716 (setf (aref buffer index) byte)
718 (values index buffer))
720 (defun encode-sfixed64 (val buffer index)
721 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
723 Modifies the buffer, and returns the new index into the buffer.
724 Watch out, this function turns off most type checking and all array bounds checking."
725 (declare (type (signed-byte 64) val)
726 (type (simple-array (unsigned-byte 8)) buffer)
728 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
730 (let ((byte (ldb (byte 8 0) val)))
731 (declare (type (unsigned-byte 8) byte))
732 (setq val (ash val -8))
733 (setf (aref buffer index) byte)
735 (values index buffer))
737 (defun encode-single (val buffer index)
738 "Encodes the single float 'val' into the buffer at the given index.
739 Modifies the buffer, and returns the new index into the buffer.
740 Watch out, this function turns off most type checking and all array bounds checking."
741 (declare (type single-float val)
742 (type (simple-array (unsigned-byte 8)) buffer)
744 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
745 (let ((bits (single-float-bits val)))
747 (let ((byte (ldb (byte 8 0) bits)))
748 (declare (type (unsigned-byte 8) byte))
749 (setq bits (ash bits -8))
750 (setf (aref buffer index) byte)
752 (values index buffer))
754 (defun encode-double (val buffer index)
755 "Encodes the double float 'val' into the buffer at the given index.
756 Modifies the buffer, and returns the new index into the buffer.
757 Watch out, this function turns off most type checking and all array bounds checking."
758 (declare (type double-float val)
759 (type (simple-array (unsigned-byte 8)) buffer)
761 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
762 (multiple-value-bind (low high)
763 (double-float-bits val)
765 (let ((byte (ldb (byte 8 0) low)))
766 (declare (type (unsigned-byte 8) byte))
767 (setq low (ash low -8))
768 (setf (aref buffer index) byte)
771 (let ((byte (ldb (byte 8 0) high)))
772 (declare (type (unsigned-byte 8) byte))
773 (setq high (ash high -8))
774 (setf (aref buffer index) byte)
776 (values index buffer))
778 (defun encode-string (string buffer index)
779 "Encodes the octets into the buffer at the given index.
780 Modifies the buffer, and returns the new index into the buffer.
781 Watch out, this function turns off most type checking and all array bounds checking."
782 (declare (type (simple-array (unsigned-byte 8)) buffer)
784 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
785 (let* ((octets (babel:string-to-octets string :encoding :utf-8))
786 (len (length octets))
787 (idx (encode-uint32 len buffer index)))
788 (declare (type fixnum len)
789 (type (unsigned-byte 32) idx))
790 (replace buffer octets :start1 idx)
791 (values (i+ idx len) buffer))))
793 (defun encode-octets (octets buffer index)
794 "Encodes the octets into the buffer at the given index.
795 Modifies the buffer, and returns the new index into the buffer.
796 Watch out, this function turns off most type checking and all array bounds checking."
797 (declare (type (simple-array (unsigned-byte 8)) buffer)
799 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
800 (let* ((len (length octets))
801 (idx (encode-uint32 len buffer index)))
802 (declare (type fixnum len)
803 (type (unsigned-byte 32) idx))
804 (replace buffer octets :start1 idx)
805 (values (i+ idx len) buffer))))
810 ;; Decode the value from the buffer at the given index,
811 ;; then return the value and new index into the buffer
812 (defun decode-uint32 (buffer index)
813 "Decodes the next 32-bit varint integer in the buffer at the given index.
814 Returns both the decoded value and the new index into the buffer.
815 Watch out, this function turns off most type checking and all array bounds checking."
816 (declare (optimize (speed 3) (safety 0) (debug 0)))
817 (declare (type (simple-array (unsigned-byte 8)) buffer)
819 ;; Seven bits at a time, least significant bits first
821 for places fixnum upfrom 0 by 7
822 for byte fixnum = (prog1 (aref buffer index) (iincf index))
823 do (setq val (logior val (ash (ldb (byte 7 0) byte) places)))
826 (assert (< val #.(ash 1 32)) ()
827 "The value ~D is longer than 32 bits" val)
828 (return (values val index)))))
830 (defun decode-uint64 (buffer index)
831 "Decodes the next 64-bit varint integer in the buffer at the given index.
832 Returns both the decoded value and the new index into the buffer.
833 Watch out, this function turns off most type checking and all array bounds checking."
834 (declare (optimize (speed 3) (safety 0) (debug 0)))
835 (declare (type (simple-array (unsigned-byte 8)) buffer)
837 ;; Seven bits at a time, least significant bits first
839 for places fixnum upfrom 0 by 7
840 for byte fixnum = (prog1 (aref buffer index) (iincf index))
841 do (setq val (logior val (ash (ldb (byte 7 0) byte) places)))
843 finally (return (values val index))))
845 (defun decode-fixed32 (buffer index)
846 "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
847 Returns both the decoded value and the new index into the buffer.
848 Watch out, this function turns off most type checking and all array bounds checking."
849 (declare (type (simple-array (unsigned-byte 8)) buffer)
851 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
852 ;; Eight bits at a time, least significant bits first
855 for places fixnum upfrom 0 by 8
856 for byte fixnum = (prog1 (aref buffer index) (iincf index))
857 do (setq val (logior val (ash byte places))))
858 (values val index))))
860 (defun decode-sfixed32 (buffer index)
861 "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
862 Returns both the decoded value and the new index into the buffer.
863 Watch out, this function turns off most type checking and all array bounds checking."
864 (declare (type (simple-array (unsigned-byte 8)) buffer)
866 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
867 ;; Eight bits at a time, least significant bits first
870 for places fixnum upfrom 0 by 8
871 for byte fixnum = (prog1 (aref buffer index) (iincf index))
872 do (setq val (logior val (ash byte places))))
873 (when (i= (ldb (byte 1 31) val) 1) ;sign bit set, so negative value
874 (decf val #.(ash 1 32)))
875 (values val index))))
877 (defun decode-fixed64 (buffer index)
878 "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
879 Returns both the decoded value and the new index into the buffer.
880 Watch out, this function turns off most type checking and all array bounds checking."
881 (declare (type (simple-array (unsigned-byte 8)) buffer)
883 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
884 ;; Eight bits at a time, least significant bits first
887 for places fixnum upfrom 0 by 8
888 for byte fixnum = (prog1 (aref buffer index) (iincf index))
889 do (setq val (logior val (ash byte places))))
890 (values val index))))
892 (defun decode-sfixed64 (buffer index)
893 "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
894 Returns both the decoded value and the new index into the buffer.
895 Watch out, this function turns off most type checking and all array bounds checking."
896 (declare (type (simple-array (unsigned-byte 8)) buffer)
898 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
899 ;; Eight bits at a time, least significant bits first
902 for places fixnum upfrom 0 by 8
903 for byte fixnum = (prog1 (aref buffer index) (iincf index))
904 do (setq val (logior val (ash byte places))))
905 (when (i= (ldb (byte 1 63) val) 1) ;sign bit set, so negative value
906 (decf val #.(ash 1 64)))
907 (values val index))))
909 (defun decode-single (buffer index)
910 "Decodes the next single float 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 most type checking and all array bounds checking."
913 (declare (type (simple-array (unsigned-byte 8)) buffer)
915 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
916 ;; Eight bits at a time, least significant bits first
919 for places fixnum upfrom 0 by 8
920 for byte fixnum = (prog1 (aref buffer index) (iincf index))
921 do (setq bits (logior bits (ash byte places))))
922 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
923 (decf bits #.(ash 1 32)))
924 (values (make-single-float bits) index))))
926 (defun decode-double (buffer index)
927 "Decodes the next double float in the buffer at the given index.
928 Returns both the decoded value and the new index into the buffer.
929 Watch out, this function turns off most type checking and all array bounds checking."
930 (declare (type (simple-array (unsigned-byte 8)) buffer)
932 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
933 ;; Eight bits at a time, least significant bits first
937 for places fixnum upfrom 0 by 8
938 for byte fixnum = (prog1 (aref buffer index) (iincf index))
939 do (setq low (logior low (ash byte places))))
941 for places fixnum upfrom 0 by 8
942 for byte fixnum = (prog1 (aref buffer index) (iincf index))
943 do (setq high (logior high (ash byte places))))
944 ;; High bits are signed, but low bits are unsigned
945 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
946 (decf high #.(ash 1 32)))
947 (values (make-double-float low high) index))))
949 (defun decode-string (buffer index)
950 "Decodes the next UTF-8 encoded string in the buffer at the given index.
951 Returns both the decoded string and the new index into the buffer.
952 Watch out, this function turns off most type checking and all array bounds checking."
953 (declare (type (simple-array (unsigned-byte 8)) buffer)
955 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
956 (multiple-value-bind (len idx)
957 (decode-uint32 buffer index)
958 (declare (type (unsigned-byte 32) len)
960 (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len)))))
962 (defun decode-octets (buffer index)
963 "Decodes the next octets in the buffer at the given index.
964 Returns both the decoded value and the new index into the buffer.
965 Watch out, this function turns off most type checking and all array bounds checking."
966 (declare (type (simple-array (unsigned-byte 8)) buffer)
968 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
969 (multiple-value-bind (len idx)
970 (decode-uint32 buffer index)
971 (declare (type (unsigned-byte 32) len)
973 (values (subseq buffer idx (i+ idx len)) (i+ idx len)))))
978 (defun length32 (val)
979 "Returns the length that 'val' will take when encoded as a 32-bit integer."
980 (declare (optimize (speed 3) (safety 0) (debug 0)))
982 (declare (type fixnum size))
984 (setq val (ash val -7))
989 (defun length64 (val)
990 "Returns the length that 'val' will take when encoded as a 64-bit integer."
991 (declare (optimize (speed 3) (safety 0) (debug 0)))
993 (declare (type fixnum size))
995 (setq val (ash val -7))
1001 ;;; Skipping elements
1003 (defun skip-element (buffer index tag)
1004 "Skip an element in the buffer at the index of the given wire type.
1005 Returns the new index in the buffer.
1006 Watch out, this function turns off all type checking and all array bounds checking."
1007 (declare (optimize (speed 3) (safety 0) (debug 0)))
1008 (declare (type (simple-array (unsigned-byte 8)) buffer)
1010 (type (unsigned-byte 32) tag))
1011 (case (ilogand tag #x7)
1012 (($wire-type-varint)
1013 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
1014 until (i< byte 128))
1016 (($wire-type-string)
1017 (multiple-value-bind (len idx)
1018 (decode-uint32 buffer index)
1019 (declare (type (unsigned-byte 32) len)
1026 (($wire-type-start-group)
1027 (loop (multiple-value-bind (new-tag idx)
1028 (decode-uint32 buffer index)
1029 (cond ((not (i= (ilogand new-tag #x7) $wire-type-start-group))
1030 (setq index (skip-element buffer idx new-tag)))
1031 ;; Clever test for matching end group number
1032 ((i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1035 (assert (i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group)) ()
1036 "Couldn't find a matching end group tag"))))))