1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc. ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; Protocol buffers wire format
18 (eval-when (:compile-toplevel :load-toplevel :execute)
20 (defconstant $wire-type-varint 0)
21 (defconstant $wire-type-64bit 1)
22 (defconstant $wire-type-string 2)
23 (defconstant $wire-type-start-group 3) ;supposedly obsolete
24 (defconstant $wire-type-end-group 4) ;supposedly obsolete
25 (defconstant $wire-type-32bit 5)
30 (defun make-tag (type index)
31 "Given a wire type or the name of a Protobufs type and a field index,
32 return the tag that encodes both of them."
33 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
34 (if (typep type 'fixnum)
36 (let ((type (ecase type
37 ((:int32 :uint32) $wire-type-varint)
38 ((:int64 :uint64) $wire-type-varint)
39 ((:sint32 :sint64) $wire-type-varint)
40 ((:fixed32 :sfixed32) $wire-type-32bit)
41 ((:fixed64 :sfixed64) $wire-type-64bit)
42 ((:string :bytes) $wire-type-string)
43 ((:bool) $wire-type-varint)
44 ((:float) $wire-type-32bit)
45 ((:double) $wire-type-64bit)
46 ;; A few of our homegrown types
47 ((:symbol) $wire-type-string)
48 ((:date :time :datetime :timestamp) $wire-type-64bit))))
49 (ilogior type (iash index 3))))))
51 (define-compiler-macro make-tag (&whole form type index)
52 (cond ((typep type 'fixnum)
53 `(ilogior ,type (iash ,index 3)))
55 (let ((type (ecase type
56 ((:int32 :uint32) $wire-type-varint)
57 ((:int64 :uint64) $wire-type-varint)
58 ((:sint32 :sint64) $wire-type-varint)
59 ((:fixed32 :sfixed32) $wire-type-32bit)
60 ((:fixed64 :sfixed64) $wire-type-64bit)
61 ((:string :bytes) $wire-type-string)
62 ((:bool) $wire-type-varint)
63 ((:float) $wire-type-32bit)
64 ((:double) $wire-type-64bit)
65 ;; A few of our homegrown types
66 ((:symbol) $wire-type-string)
67 ((:date :time :datetime :timestamp) $wire-type-64bit))))
68 `(ilogior ,type (iash ,index 3))))
72 (defun zig-zag-encode32 (val)
73 (declare (optimize (speed 3) (safety 0) (debug 0)))
74 (declare (type (signed-byte 32) val))
75 (logxor (ash val 1) (ash val -31)))
77 (defun zig-zag-encode64 (val)
78 (declare (optimize (speed 3) (safety 0) (debug 0)))
79 (declare (type (signed-byte 64) val))
80 (logxor (ash val 1) (ash val -63)))
82 (define-compiler-macro zig-zag-encode32 (&whole form val)
84 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
85 (type (signed-byte 32) ,val))
86 (logxor (ash ,val 1) (ash ,val -31)))
89 (define-compiler-macro zig-zag-encode64 (&whole form val)
91 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
92 (type (signed-byte 64) ,val))
93 (logxor (ash ,val 1) (ash ,val -63)))
96 (defun zig-zag-decode32 (val)
97 (declare (optimize (speed 3) (safety 0) (debug 0)))
98 (logxor (ash val -1) (- (logand val 1))))
100 (defun zig-zag-decode64 (val)
101 (declare (optimize (speed 3) (safety 0) (debug 0)))
102 (logxor (ash val -1) (- (logand val 1))))
104 (define-compiler-macro zig-zag-decode32 (&whole form val)
106 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
107 (logxor (ash ,val -1) (- (logand ,val 1))))
110 (define-compiler-macro zig-zag-decode64 (&whole form val)
112 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
113 (logxor (ash ,val -1) (- (logand ,val 1))))
119 ;; Serialize 'val' of primitive type 'type' into the buffer
120 (defun serialize-prim (val type tag buffer index)
121 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
122 The value is given by 'val', the primitive type by 'type'.
123 Modifies the buffer in place, and returns the new index into the buffer.
124 Watch out, this function turns off most type checking and all array bounds checking."
125 (declare (type (simple-array (unsigned-byte 8)) buffer)
126 (type (unsigned-byte 32) tag)
128 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
129 (let ((idx (encode-uint32 tag buffer index)))
130 (declare (type fixnum idx))
133 (encode-uint32 val buffer idx))
135 (encode-uint64 val buffer idx))
137 (encode-uint32 (zig-zag-encode32 val) buffer idx))
139 (encode-uint64 (zig-zag-encode64 val) buffer idx))
141 (encode-fixed32 val buffer idx))
143 (encode-sfixed32 val buffer idx))
145 (encode-fixed64 val buffer idx))
147 (encode-sfixed64 val buffer idx))
149 (encode-string val buffer idx))
151 (encode-octets val buffer idx))
153 (encode-uint32 (if val 1 0) buffer idx))
155 (encode-single val buffer idx))
157 (encode-double val buffer idx))
158 ;; A few of our homegrown types
160 (let ((val (if (keywordp val)
162 ;; Non-keyword symbols are consy, avoid them if possible
163 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
164 (encode-string val buffer idx)))
165 ((:date :time :datetime :timestamp)
166 (encode-uint64 val buffer idx))))))
168 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
169 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
170 :fixed32 :sfixed32 :fixed64 :sfixed64
171 :string :bytes :bool :float :double))
172 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
173 (type (simple-array (unsigned-byte 8)) ,buffer)
174 ;; 'tag' is a constant, no need to declare its type
175 (type fixnum ,index))
176 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
177 (declare (type fixnum idx))
180 `(encode-uint32 ,val ,buffer idx))
182 `(encode-uint64 ,val ,buffer idx))
184 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
186 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
188 `(encode-fixed32 ,val ,buffer idx))
190 `(encode-sfixed32 ,val ,buffer idx))
192 `(encode-fixed64 ,val ,buffer idx))
194 `(encode-sfixed64 ,val ,buffer idx))
196 `(encode-string ,val ,buffer idx))
198 `(encode-octets ,val ,buffer idx))
200 `(encode-uint32 (if ,val 1 0) ,buffer idx))
202 `(encode-single ,val ,buffer idx))
204 `(encode-double ,val ,buffer idx)))))
207 (defun serialize-packed (values type tag buffer index)
208 "Serializes a set of packed values into the buffer at the given index.
209 The values are given by 'values', the primitive type by 'type'.
210 Modifies the buffer in place, and returns the new index into the buffer.
211 Watch out, this function turns off most type checking and all array bounds checking."
212 (declare (type (simple-array (unsigned-byte 8)) buffer)
213 (type (unsigned-byte 32) tag)
215 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
216 (let ((idx (encode-uint32 tag buffer index)))
217 (declare (type fixnum idx))
218 (multiple-value-bind (full-len len)
219 (packed-size values type tag)
220 (declare (type fixnum len) (ignore full-len))
221 (setq idx (encode-uint32 len buffer idx)))
224 (dolist (val values idx)
225 (setq idx (encode-uint32 val buffer idx))))
227 (dolist (val values idx)
228 (setq idx (encode-uint64 val buffer idx))))
230 (dolist (val values idx)
231 (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))))
233 (dolist (val values idx)
234 (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
236 (dolist (val values idx)
237 (setq idx (encode-fixed32 val buffer idx))))
239 (dolist (val values idx)
240 (setq idx (encode-sfixed32 val buffer idx))))
242 (dolist (val values idx)
243 (setq idx (encode-fixed64 val buffer idx))))
245 (dolist (val values idx)
246 (setq idx (encode-sfixed64 val buffer idx))))
248 (dolist (val values idx)
249 (setq idx (encode-single val buffer idx))))
251 (dolist (val values idx)
252 (setq idx (encode-double val buffer idx))))))))
254 (define-compiler-macro serialize-packed (&whole form values type tag buffer index)
255 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
256 :fixed32 :sfixed32 :fixed64 :sfixed64
258 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
259 (type (simple-array (unsigned-byte 8)) ,buffer)
260 (type fixnum ,index))
261 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
262 (declare (type fixnum idx))
263 (multiple-value-bind (full-len len)
264 (packed-size ,values ,type ,tag)
265 (declare (type fixnum len) (ignore full-len))
266 (setq idx (encode-uint32 len ,buffer idx)))
267 (dolist (val ,values idx)
270 `(setq idx (encode-uint32 val ,buffer idx)))
272 `(setq idx (encode-uint64 val ,buffer idx)))
274 `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
276 `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
278 `(setq idx (encode-fixed32 val ,buffer idx)))
280 `(setq idx (encode-sfixed32 val ,buffer idx)))
282 `(setq idx (encode-fixed64 val ,buffer idx)))
284 `(setq idx (encode-sfixed64 val ,buffer idx)))
286 `(setq idx (encode-single val ,buffer idx)))
288 `(setq idx (encode-double val ,buffer idx)))))))
291 (defun serialize-enum (val values tag buffer index)
292 "Serializes a Protobufs enum value into the buffer at the given index.
293 The value is given by 'val', the enum values are in 'values'.
294 Modifies the buffer in place, and returns the new index into the buffer.
295 Watch out, this function turns off most type checking and all array bounds checking."
296 (declare (type (simple-array (unsigned-byte 8)) buffer)
297 (type (unsigned-byte 32) tag)
299 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
300 (let* ((val (let ((e (find val values :key #'proto-value)))
301 (and e (proto-index e))))
302 (idx (encode-uint32 tag buffer index)))
303 (declare (type (unsigned-byte 32) val)
305 (encode-uint32 val buffer idx))))
310 ;; Deserialize the next object of type 'type'
311 (defun deserialize-prim (type buffer index)
312 "Deserializes the next object of primitive type 'type'.
313 Deserializes from the byte vector 'buffer' starting at 'index'.
314 Returns the value and and the new index into the buffer.
315 Watch out, this function turns off most type checking and all array bounds checking."
316 (declare (type (simple-array (unsigned-byte 8)) buffer)
318 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
321 (decode-uint32 buffer index))
323 (decode-uint64 buffer index))
325 (multiple-value-bind (val idx)
326 (decode-uint32 buffer index)
327 (values (zig-zag-decode32 val) idx)))
329 (multiple-value-bind (val idx)
330 (decode-uint64 buffer index)
331 (values (zig-zag-decode64 val) idx)))
333 (decode-fixed32 buffer index))
335 (decode-sfixed32 buffer index))
337 (decode-fixed64 buffer index))
339 (decode-sfixed64 buffer index))
341 (decode-string buffer index))
343 (decode-octets buffer index))
345 (multiple-value-bind (val idx)
346 (decode-uint32 buffer index)
347 (values (if (i= val 0) nil t) idx)))
349 (decode-single buffer index))
351 (decode-double buffer index))
352 ;; A few of our homegrown types
354 ;; Note that this is consy, avoid it if possible
355 (multiple-value-bind (val idx)
356 (decode-string buffer index)
357 (values (make-lisp-symbol val) idx)))
358 ((:date :time :datetime :timestamp)
359 (decode-uint64 buffer index)))))
361 (define-compiler-macro deserialize-prim (&whole form type buffer index)
362 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
363 :fixed32 :sfixed32 :fixed64 :sfixed64
364 :string :bytes :bool :float :double))
365 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
366 (type (simple-array (unsigned-byte 8)) ,buffer)
367 (type fixnum ,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))))
403 (defun deserialize-packed (type buffer index)
404 "Deserializes the next packed values of type 'type'.
405 Deserializes from the byte vector 'buffer' starting at 'index'.
406 Returns the value and and the new index into the buffer.
407 Watch out, this function turns off most type checking and all array bounds checking."
408 (declare (type (simple-array (unsigned-byte 8)) buffer)
410 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
411 (multiple-value-bind (len idx)
412 (decode-uint32 buffer index)
413 (declare (type (unsigned-byte 32) len)
415 (let ((end (i+ idx len)))
416 (declare (type (unsigned-byte 32) end))
417 (with-collectors ((values collect-value))
420 (return-from deserialize-packed (values values idx)))
421 (multiple-value-bind (val nidx)
424 (decode-uint32 buffer idx))
426 (decode-uint64 buffer idx))
428 (multiple-value-bind (val idx)
429 (decode-uint32 buffer idx)
430 (values (zig-zag-decode32 val) idx)))
432 (multiple-value-bind (val idx)
433 (decode-uint64 buffer idx)
434 (values (zig-zag-decode64 val) idx)))
436 (decode-fixed32 buffer idx))
438 (decode-sfixed32 buffer idx))
440 (decode-fixed64 buffer idx))
442 (decode-sfixed64 buffer idx))
444 (decode-single buffer idx))
446 (decode-double buffer idx)))
448 (setq idx nidx))))))))
450 (define-compiler-macro deserialize-packed (&whole form type buffer index)
451 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
452 :fixed32 :sfixed32 :fixed64 :sfixed64
454 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
455 (type (simple-array (unsigned-byte 8)) ,buffer)
456 (type fixnum ,index))
457 (block deserialize-packed
458 (multiple-value-bind (len idx)
459 (decode-uint32 ,buffer ,index)
460 (declare (type (unsigned-byte 32) len)
462 (let ((end (i+ idx len)))
463 (declare (type (unsigned-byte 32) end))
464 (with-collectors ((values collect-value))
467 (return-from deserialize-packed (values values idx)))
468 (multiple-value-bind (val nidx)
471 `(decode-uint32 ,buffer idx))
473 `(decode-uint64 ,buffer idx))
475 `(multiple-value-bind (val idx)
476 (decode-uint32 ,buffer idx)
477 (values (zig-zag-decode32 val) idx)))
479 `(multiple-value-bind (val idx)
480 (decode-uint64 ,buffer idx)
481 (values (zig-zag-decode64 val) idx)))
483 `(decode-fixed32 ,buffer idx))
485 `(decode-sfixed32 ,buffer idx))
487 `(decode-fixed64 ,buffer idx))
489 `(decode-sfixed64 ,buffer idx))
491 `(decode-single ,buffer idx))
493 `(decode-double ,buffer idx)))
495 (setq idx nidx))))))))
498 (defun deserialize-enum (values buffer index)
499 "Deserializes the next enum value take from 'values'.
500 Deserializes from the byte vector 'buffer' starting at 'index'.
501 Returns the value and and the new index into the buffer.
502 Watch out, this function turns off most type checking and all array bounds checking."
503 (declare (type (simple-array (unsigned-byte 8)) buffer)
505 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
506 (multiple-value-bind (val idx)
507 (decode-uint32 buffer index)
508 (let ((val (let ((e (find val values :key #'proto-index)))
509 (and e (proto-value e)))))
515 (defun prim-size (val type tag)
516 "Returns the size in bytes that the primitive object will take when serialized.
517 Watch out, this function turns off most type checking."
518 (declare (type (unsigned-byte 32) tag))
519 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
522 (i+ (length32 tag) (length32 val)))
524 (i+ (length32 tag) (length64 val)))
526 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
528 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
529 ((:fixed32 :sfixed32)
530 (i+ (length32 tag) 4))
531 ((:fixed64 :sfixed64)
532 (i+ (length32 tag) 8))
534 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
535 (i+ (length32 tag) (length32 len) len)))
537 (let ((len (length val)))
538 (i+ (length32 tag) (length32 len) len)))
540 (i+ (length32 tag) 1))
542 (i+ (length32 tag) 4))
544 (i+ (length32 tag) 8))
545 ;; A few of our homegrown types
547 (let ((len (if (keywordp val)
548 (length (symbol-name val))
549 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
550 (i+ (length32 tag) (length32 len) len)))
551 ((:date :time :datetime :timestamp)
552 (i+ (length32 tag) 8)))))
554 (define-compiler-macro prim-size (&whole form val type tag)
555 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
556 :fixed32 :sfixed32 :fixed64 :sfixed64
557 :string :bytes :bool :float :double))
558 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
561 `(i+ (length32 ,tag) (length32 ,val)))
563 `(i+ (length32 ,tag) (length64 ,val)))
565 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
567 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
568 ((:fixed32 :sfixed32)
569 `(i+ (length32 ,tag) 4))
570 ((:fixed64 :sfixed64)
571 `(i+ (length32 ,tag) 8))
573 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
574 (i+ (length32 ,tag) (length32 len) len)))
576 `(let ((len (length ,val)))
577 (i+ (length32 ,tag) (length32 len) len)))
579 `(i+ (length32 ,tag) 1))
581 `(i+ (length32 ,tag) 4))
583 `(i+ (length32 ,tag) 8))))
586 (defun packed-size (values type tag)
587 "Returns the size in bytes that the packed object will take when serialized.
588 Watch out, this function turns off most type checking."
589 (declare (type (unsigned-byte 32) tag))
590 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
591 (let ((len (let ((len 0))
592 (declare (type fixnum len))
593 (dolist (val values len)
594 (iincf len (ecase type
595 ((:int32 :uint32) (length32 val))
596 ((:int64 :uint64) (length64 val))
597 ((:sint32) (length32 (zig-zag-encode32 val)))
598 ((:sint64) (length64 (zig-zag-encode64 val)))
599 ((:fixed32 :sfixed32) 4)
600 ((:fixed64 :sfixed64) 8)
603 (declare (type (unsigned-byte 32) len))
604 ;; Two value: the full size of the packed object, and the size
605 ;; of just the payload
606 (values (i+ (length32 tag) (length32 len) len) len))))
608 (define-compiler-macro packed-size (&whole form values type tag)
609 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
610 :fixed32 :sfixed32 :fixed64 :sfixed64
612 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
613 (let ((len (let ((len 0))
614 (declare (type fixnum len))
615 (dolist (val ,values len)
616 (iincf len ,(ecase type
617 ((:int32 :uint32) `(length32 val))
618 ((:int64 :uint64) `(length64 val))
619 ((:sint32) `(length32 (zig-zag-encode32 val)))
620 ((:sint64) `(length64 (zig-zag-encode64 val)))
621 ((:fixed32 :sfixed32) `4)
622 ((:fixed64 :sfixed64) `8)
625 (declare (type (unsigned-byte 32) len))
626 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
629 (defun enum-size (val values tag)
630 "Returns the size in bytes that the enum object will take when serialized."
631 (declare (type (unsigned-byte 32) tag))
632 (let ((idx (let ((e (find val values :key #'proto-value)))
633 (and e (proto-index e)))))
634 (assert idx () "There is no enum value for ~S" val)
635 (i+ (length32 tag) (length32 idx))))
638 ;;; Wire-level encoders
639 ;;; These are called at the lowest level, so arg types are assumed to be correct
641 (defun encode-uint32 (val buffer index)
642 "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
644 Modifies the buffer, and returns the new index into the buffer.
645 Watch out, this function turns off all type checking and array bounds checking."
646 (declare (optimize (speed 3) (safety 0) (debug 0)))
647 (declare (type (unsigned-byte 32) val)
648 (type (simple-array (unsigned-byte 8)) buffer)
650 ;; Seven bits at a time, least significant bits first
651 (loop do (let ((bits (ildb (byte 7 0) val)))
652 (declare (type (unsigned-byte 8) bits))
653 (setq val (iash val -7))
654 (setf (aref buffer index) (ilogior bits (if (i= val 0) 0 128)))
657 (values index buffer)) ;return the buffer to improve 'trace'
659 (defun encode-uint64 (val buffer index)
660 "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
662 Modifies the buffer, and returns the new index into the buffer.
663 Watch out, this function turns off all type checking and array bounds checking."
664 (declare (optimize (speed 3) (safety 0) (debug 0)))
665 (declare (type (unsigned-byte 64) val)
666 (type (simple-array (unsigned-byte 8)) buffer)
668 (loop do (let ((bits (ldb (byte 7 0) val)))
669 (declare (type (unsigned-byte 8) bits))
670 (setq val (ash val -7))
671 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
674 (values index buffer))
676 (defun encode-fixed32 (val buffer index)
677 "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
679 Modifies the buffer, and returns the new index into the buffer.
680 Watch out, this function turns off all type checking and array bounds checking."
681 (declare (optimize (speed 3) (safety 0) (debug 0)))
682 (declare (type (unsigned-byte 32) val)
683 (type (simple-array (unsigned-byte 8)) buffer)
686 (let ((byte (ildb (byte 8 0) val)))
687 (declare (type (unsigned-byte 8) byte))
688 (setq val (iash val -8))
689 (setf (aref buffer index) byte)
691 (values index buffer))
693 (defun encode-fixed64 (val buffer index)
694 "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
696 Modifies the buffer, and returns the new index into the buffer.
697 Watch out, this function turns off all type checking and array bounds checking."
698 (declare (optimize (speed 3) (safety 0) (debug 0)))
699 (declare (type (unsigned-byte 64) val)
700 (type (simple-array (unsigned-byte 8)) buffer)
703 (let ((byte (ldb (byte 8 0) val)))
704 (declare (type (unsigned-byte 8) byte))
705 (setq val (ash val -8))
706 (setf (aref buffer index) byte)
708 (values index buffer))
710 (defun encode-sfixed32 (val buffer index)
711 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
713 Modifies the buffer, and returns the new index into the buffer.
714 Watch out, this function turns off all type checking and array bounds checking."
715 (declare (optimize (speed 3) (safety 0) (debug 0)))
716 (declare (type (signed-byte 32) val)
717 (type (simple-array (unsigned-byte 8)) buffer)
720 (let ((byte (ildb (byte 8 0) val)))
721 (declare (type (unsigned-byte 8) byte))
722 (setq val (iash val -8))
723 (setf (aref buffer index) byte)
725 (values index buffer))
727 (defun encode-sfixed64 (val buffer index)
728 "Encodes the signed 64-bit integer 'val' as a fixed int into the buffer
730 Modifies the buffer, and returns the new index into the buffer.
731 Watch out, this function turns off all type checking and array bounds checking."
732 (declare (optimize (speed 3) (safety 0) (debug 0)))
733 (declare (type (signed-byte 64) val)
734 (type (simple-array (unsigned-byte 8)) buffer)
737 (let ((byte (ldb (byte 8 0) val)))
738 (declare (type (unsigned-byte 8) byte))
739 (setq val (ash val -8))
740 (setf (aref buffer index) byte)
742 (values index buffer))
744 (defun encode-single (val buffer index)
745 "Encodes the single float 'val' into the buffer at the given index.
746 Modifies the buffer, and returns the new index into the buffer.
747 Watch out, this function turns off all type checking and array bounds checking."
748 (declare (optimize (speed 3) (safety 0) (debug 0)))
749 (declare (type single-float val)
750 (type (simple-array (unsigned-byte 8)) buffer)
752 (let ((bits (single-float-bits val)))
754 (let ((byte (ldb (byte 8 0) bits)))
755 (declare (type (unsigned-byte 8) byte))
756 (setq bits (ash bits -8))
757 (setf (aref buffer index) byte)
759 (values index buffer))
761 (defun encode-double (val buffer index)
762 "Encodes the double float 'val' into the buffer at the given index.
763 Modifies the buffer, and returns the new index into the buffer.
764 Watch out, this function turns off all type checking and array bounds checking."
765 (declare (optimize (speed 3) (safety 0) (debug 0)))
766 (declare (type double-float val)
767 (type (simple-array (unsigned-byte 8)) buffer)
769 (multiple-value-bind (low high)
770 (double-float-bits val)
772 (let ((byte (ldb (byte 8 0) low)))
773 (declare (type (unsigned-byte 8) byte))
774 (setq low (ash low -8))
775 (setf (aref buffer index) byte)
778 (let ((byte (ldb (byte 8 0) high)))
779 (declare (type (unsigned-byte 8) byte))
780 (setq high (ash high -8))
781 (setf (aref buffer index) byte)
783 (values index buffer))
785 (defun encode-string (string buffer index)
786 "Encodes the octets into the buffer at the given index.
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 (speed 3) (safety 0) (debug 0)))
790 (declare (type (simple-array (unsigned-byte 8)) buffer)
792 (let* ((octets (babel:string-to-octets string :encoding :utf-8))
793 (len (length octets))
794 (idx (encode-uint32 len buffer index)))
795 (declare (type fixnum len)
796 (type (unsigned-byte 32) idx))
797 (replace buffer octets :start1 idx)
798 (values (i+ idx len) buffer)))
800 (defun encode-octets (octets buffer index)
801 "Encodes the octets into the buffer at the given index.
802 Modifies the buffer, and returns the new index into the buffer.
803 Watch out, this function turns off all type checking and array bounds checking."
804 (declare (optimize (speed 3) (safety 0) (debug 0)))
805 (declare (type (simple-array (unsigned-byte 8)) buffer)
807 (let* ((len (length octets))
808 (idx (encode-uint32 len buffer index)))
809 (declare (type fixnum len)
810 (type (unsigned-byte 32) idx))
811 (replace buffer octets :start1 idx)
812 (values (i+ idx len) buffer)))
815 ;;; Wire-level decoders
816 ;;; These are called at the lowest level, so arg types are assumed to be correct
818 ;; Decode the value from the buffer at the given index,
819 ;; then return the value and new index into the buffer
820 (defun decode-uint32 (buffer index)
821 "Decodes the next 32-bit varint integer in the buffer at the given index.
822 Returns both the decoded value and the new index into the buffer.
823 Watch out, this function turns off all type checking and array bounds checking."
824 (declare (optimize (speed 3) (safety 0) (debug 0)))
825 (declare (type (simple-array (unsigned-byte 8)) buffer)
827 ;; Seven bits at a time, least significant bits first
828 (loop with val fixnum = 0
829 for places fixnum upfrom 0 by 7
830 for byte fixnum = (prog1 (aref buffer index) (iincf index))
831 do (setq val (ilogior val (iash (ildb (byte 7 0) byte) places)))
834 (assert (< val #.(ash 1 32)) ()
835 "The value ~D is longer than 32 bits" val)
836 (return (values val index)))))
838 (defun decode-uint64 (buffer index)
839 "Decodes the next 64-bit varint integer in the buffer at the given index.
840 Returns both the decoded value and the new index into the buffer.
841 Watch out, this function turns off all type checking and array bounds checking."
842 (declare (optimize (speed 3) (safety 0) (debug 0)))
843 (declare (type (simple-array (unsigned-byte 8)) buffer)
845 ;; Seven bits at a time, least significant bits first
847 for places fixnum upfrom 0 by 7
848 for byte fixnum = (prog1 (aref buffer index) (iincf index))
849 do (setq val (logior val (ash (ildb (byte 7 0) byte) places)))
851 finally (return (values val index))))
853 (defun decode-fixed32 (buffer index)
854 "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
855 Returns both the decoded value and the new index into the buffer.
856 Watch out, this function turns off all type checking and array bounds checking."
857 (declare (optimize (speed 3) (safety 0) (debug 0)))
858 (declare (type (simple-array (unsigned-byte 8)) buffer)
860 ;; Eight bits at a time, least significant bits first
862 (declare (type fixnum val))
864 for places fixnum upfrom 0 by 8
865 for byte fixnum = (prog1 (aref buffer index) (iincf index))
866 do (setq val (ilogior val (iash byte places))))
869 (defun decode-sfixed32 (buffer index)
870 "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
871 Returns both the decoded value and the new index into the buffer.
872 Watch out, this function turns off all type checking and array bounds checking."
873 (declare (optimize (speed 3) (safety 0) (debug 0)))
874 (declare (type (simple-array (unsigned-byte 8)) buffer)
876 ;; Eight bits at a time, least significant bits first
878 (declare (type fixnum val))
880 for places fixnum upfrom 0 by 8
881 for byte fixnum = (prog1 (aref buffer index) (iincf index))
882 do (setq val (ilogior val (iash byte places))))
883 (when (i= (ldb (byte 1 31) val) 1) ;sign bit set, so negative value
884 (decf val #.(ash 1 32)))
887 (defun decode-fixed64 (buffer index)
888 "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
889 Returns both the decoded value and the new index into the buffer.
890 Watch out, this function turns off all type checking and array bounds checking."
891 (declare (optimize (speed 3) (safety 0) (debug 0)))
892 (declare (type (simple-array (unsigned-byte 8)) buffer)
894 ;; Eight bits at a time, least significant bits first
897 for places fixnum upfrom 0 by 8
898 for byte fixnum = (prog1 (aref buffer index) (iincf index))
899 do (setq val (logior val (ash byte places))))
902 (defun decode-sfixed64 (buffer index)
903 "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
904 Returns both the decoded value and the new index into the buffer.
905 Watch out, this function turns off all type checking and array bounds checking."
906 (declare (optimize (speed 3) (safety 0) (debug 0)))
907 (declare (type (simple-array (unsigned-byte 8)) buffer)
909 ;; Eight bits at a time, least significant bits first
912 for places fixnum upfrom 0 by 8
913 for byte fixnum = (prog1 (aref buffer index) (iincf index))
914 do (setq val (logior val (ash byte places))))
915 (when (i= (ldb (byte 1 63) val) 1) ;sign bit set, so negative value
916 (decf val #.(ash 1 64)))
919 (defun decode-single (buffer index)
920 "Decodes the next single float in the buffer at the given index.
921 Returns both the decoded value and the new index into the buffer.
922 Watch out, this function turns off all type checking and array bounds checking."
923 (declare (optimize (speed 3) (safety 0) (debug 0)))
924 (declare (type (simple-array (unsigned-byte 8)) buffer)
926 ;; Eight bits at a time, least significant bits first
929 for places fixnum upfrom 0 by 8
930 for byte fixnum = (prog1 (aref buffer index) (iincf index))
931 do (setq bits (logior bits (ash byte places))))
932 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
933 (decf bits #.(ash 1 32)))
934 (values (make-single-float bits) index)))
936 (defun decode-double (buffer index)
937 "Decodes the next double float in the buffer at the given index.
938 Returns both the decoded value and the new index into the buffer.
939 Watch out, this function turns off all type checking and array bounds checking."
940 (declare (optimize (speed 3) (safety 0) (debug 0)))
941 (declare (type (simple-array (unsigned-byte 8)) buffer)
943 ;; Eight bits at a time, least significant bits first
947 for places fixnum upfrom 0 by 8
948 for byte fixnum = (prog1 (aref buffer index) (iincf index))
949 do (setq low (logior low (ash byte places))))
951 for places fixnum upfrom 0 by 8
952 for byte fixnum = (prog1 (aref buffer index) (iincf index))
953 do (setq high (logior high (ash byte places))))
954 ;; High bits are signed, but low bits are unsigned
955 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
956 (decf high #.(ash 1 32)))
957 (values (make-double-float low high) index)))
959 (defun decode-string (buffer index)
960 "Decodes the next UTF-8 encoded string in the buffer at the given index.
961 Returns both the decoded string and the new index into the buffer.
962 Watch out, this function turns off all type checking and array bounds checking."
963 (declare (optimize (speed 3) (safety 0) (debug 0)))
964 (declare (type (simple-array (unsigned-byte 8)) buffer)
966 (multiple-value-bind (len idx)
967 (decode-uint32 buffer index)
968 (declare (type (unsigned-byte 32) len)
970 (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len))))
972 (defun decode-octets (buffer index)
973 "Decodes the next octets in the buffer at the given index.
974 Returns both the decoded value and the new index into the buffer.
975 Watch out, this function turns off all type checking and array bounds checking."
976 (declare (optimize (speed 3) (safety 0) (debug 0)))
977 (declare (type (simple-array (unsigned-byte 8)) buffer)
979 (multiple-value-bind (len idx)
980 (decode-uint32 buffer index)
981 (declare (type (unsigned-byte 32) len)
983 (values (subseq buffer idx (i+ idx len)) (i+ idx len))))
986 ;;; Wire-level lengths
987 ;;; These are called at the lowest level, so arg types are assumed to be correct
989 (defun length32 (val)
990 "Returns the length that 'val' will take when encoded as a 32-bit integer."
991 (declare (optimize (speed 3) (safety 0) (debug 0)))
992 (declare (type (unsigned-byte 32) val))
994 (declare (type fixnum size))
996 (setq val (iash val -7))
1001 (defun length64 (val)
1002 "Returns the length that 'val' will take when encoded as a 64-bit integer."
1003 (declare (optimize (speed 3) (safety 0) (debug 0)))
1004 (declare (type (unsigned-byte 64) val))
1006 (declare (type fixnum size))
1008 (setq val (ash val -7))
1014 ;;; Skipping elements
1015 ;;; This is called at the lowest level, so arg types are assumed to be correct
1017 (defun skip-element (buffer index tag)
1018 "Skip an element in the buffer at the index of the given wire type.
1019 Returns the new index in the buffer.
1020 Watch out, this function turns off all type checking and all array bounds checking."
1021 (declare (optimize (speed 3) (safety 0) (debug 0)))
1022 (declare (type (simple-array (unsigned-byte 8)) buffer)
1024 (type (unsigned-byte 32) tag))
1025 (case (ilogand tag #x7)
1026 ((#.$wire-type-varint)
1027 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
1028 until (i< byte 128))
1030 ((#.$wire-type-string)
1031 (multiple-value-bind (len idx)
1032 (decode-uint32 buffer index)
1033 (declare (type (unsigned-byte 32) len)
1036 ((#.$wire-type-32bit)
1038 ((#.$wire-type-64bit)
1040 ((#.$wire-type-start-group)
1041 (loop (multiple-value-bind (new-tag idx)
1042 (decode-uint32 buffer index)
1043 (cond ((not (i= (ilogand new-tag #x7) $wire-type-end-group))
1044 ;; If it's not the end of a group, skip the next element
1045 (setq index (skip-element buffer idx new-tag)))
1046 ;; If it's the end of the expected group, we're done
1047 ((i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1050 (assert (i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group)) ()
1051 "Couldn't find a matching end group tag"))))))