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-32bit 5)
23 (defun make-tag (type index)
24 "Given a wire type or the name of a Protobufs type and a field index,
25 return the tag that encodes both of them."
26 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
27 (if (typep type 'fixnum)
29 (let ((type (ecase type
30 ((:int32 :uint32) $wire-type-varint)
31 ((:int64 :uint64) $wire-type-varint)
32 ((:sint32 :sint64) $wire-type-varint)
33 ((:fixed32 :sfixed32) $wire-type-32bit)
34 ((:fixed64 :sfixed64) $wire-type-64bit)
35 ((:string :bytes) $wire-type-string)
36 ((:bool) $wire-type-varint)
37 ((:float) $wire-type-32bit)
38 ((:double) $wire-type-64bit)
39 ;; A few of our homegrown types
40 ((:symbol) $wire-type-string)
41 ((:date :time :datetime :timestamp) $wire-type-64bit))))
42 (ilogior type (iash index 3))))))
44 (define-compiler-macro make-tag (&whole form type index)
45 (cond ((typep type 'fixnum)
46 `(ilogior ,type (iash ,index 3)))
48 (let ((type (ecase type
49 ((:int32 :uint32) $wire-type-varint)
50 ((:int64 :uint64) $wire-type-varint)
51 ((:sint32 :sint64) $wire-type-varint)
52 ((:fixed32 :sfixed32) $wire-type-32bit)
53 ((:fixed64 :sfixed64) $wire-type-64bit)
54 ((:string :bytes) $wire-type-string)
55 ((:bool) $wire-type-varint)
56 ((:float) $wire-type-32bit)
57 ((:double) $wire-type-64bit)
58 ;; A few of our homegrown types
59 ((:symbol) $wire-type-string)
60 ((:date :time :datetime :timestamp) $wire-type-64bit))))
61 `(ilogior ,type (iash ,index 3))))
65 (defun zig-zag-encode32 (val)
66 (declare (optimize (speed 3) (safety 0) (debug 0)))
67 (declare (type (signed-byte 32) val))
68 (logxor (ash val 1) (ash val -31)))
70 (defun zig-zag-encode64 (val)
71 (declare (optimize (speed 3) (safety 0) (debug 0)))
72 (declare (type (signed-byte 64) val))
73 (logxor (ash val 1) (ash val -63)))
75 (define-compiler-macro zig-zag-encode32 (&whole form val)
77 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
78 (type (signed-byte 32) ,val))
79 (logxor (ash ,val 1) (ash ,val -31)))
82 (define-compiler-macro zig-zag-encode64 (&whole form val)
84 `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
85 (type (signed-byte 64) ,val))
86 (logxor (ash ,val 1) (ash ,val -63)))
89 (defun zig-zag-decode32 (val)
90 (declare (optimize (speed 3) (safety 0) (debug 0)))
91 (logxor (ash val -1) (- (logand val 1))))
93 (defun zig-zag-decode64 (val)
94 (declare (optimize (speed 3) (safety 0) (debug 0)))
95 (logxor (ash val -1) (- (logand val 1))))
97 (define-compiler-macro zig-zag-decode32 (&whole form val)
99 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
100 (logxor (ash ,val -1) (- (logand ,val 1))))
103 (define-compiler-macro zig-zag-decode64 (&whole form val)
105 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
106 (logxor (ash ,val -1) (- (logand ,val 1))))
112 ;; Serialize 'val' of primitive type 'type' into the buffer
113 (defun serialize-prim (val type tag buffer index)
114 "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
115 The value is given by 'val', the primitive type by 'type'.
116 Modifies the buffer in place, and returns the new index into the buffer.
117 Watch out, this function turns off most type checking and all array bounds checking."
118 (declare (type (simple-array (unsigned-byte 8)) buffer)
119 (type (unsigned-byte 32) tag)
121 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
122 (let ((idx (encode-uint32 tag buffer index)))
123 (declare (type fixnum idx))
126 (encode-uint32 val buffer idx))
128 (encode-uint64 val buffer idx))
130 (encode-uint32 (zig-zag-encode32 val) buffer idx))
132 (encode-uint64 (zig-zag-encode64 val) buffer idx))
134 (encode-fixed32 val buffer idx))
136 (encode-sfixed32 val buffer idx))
138 (encode-fixed64 val buffer idx))
140 (encode-sfixed64 val buffer idx))
142 (encode-string val buffer idx))
144 (encode-octets val buffer idx))
146 (encode-uint32 (if val 1 0) buffer idx))
148 (encode-single val buffer idx))
150 (encode-double val buffer idx))
151 ;; A few of our homegrown types
153 (let ((val (if (keywordp val)
155 ;; Non-keyword symbols are consy, avoid them if possible
156 (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
157 (encode-string val buffer idx)))
158 ((:date :time :datetime :timestamp)
159 (encode-uint64 val buffer idx))))))
161 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
162 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
163 :fixed32 :sfixed32 :fixed64 :sfixed64
164 :string :bytes :bool :float :double))
165 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
166 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
167 (declare (type fixnum idx))
170 `(encode-uint32 ,val ,buffer idx))
172 `(encode-uint64 ,val ,buffer idx))
174 `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
176 `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
178 `(encode-fixed32 ,val ,buffer idx))
180 `(encode-sfixed32 ,val ,buffer idx))
182 `(encode-fixed64 ,val ,buffer idx))
184 `(encode-sfixed64 ,val ,buffer idx))
186 `(encode-string ,val ,buffer idx))
188 `(encode-octets ,val ,buffer idx))
190 `(encode-uint32 (if ,val 1 0) ,buffer idx))
192 `(encode-single ,val ,buffer idx))
194 `(encode-double ,val ,buffer idx)))))
197 (defun serialize-packed (values type tag buffer index)
198 "Serializes a set of packed values into the buffer at the given index.
199 The values are given by 'values', the primitive type by 'type'.
200 Modifies the buffer in place, and returns the new index into the buffer.
201 Watch out, this function turns off most type checking and all array bounds checking."
202 (declare (type (simple-array (unsigned-byte 8)) buffer)
203 (type (unsigned-byte 32) tag)
205 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
206 (let ((idx (encode-uint32 tag buffer index)))
207 (declare (type fixnum idx))
208 (multiple-value-bind (full-len len)
209 (packed-size values type tag)
210 (declare (type fixnum len) (ignore full-len))
211 (setq idx (encode-uint32 len buffer idx)))
214 (dolist (val values idx)
215 (setq idx (encode-uint32 val buffer idx))))
217 (dolist (val values idx)
218 (setq idx (encode-uint64 val buffer idx))))
220 (dolist (val values idx)
221 (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))))
223 (dolist (val values idx)
224 (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
226 (dolist (val values idx)
227 (setq idx (encode-fixed32 val buffer idx))))
229 (dolist (val values idx)
230 (setq idx (encode-sfixed32 val buffer idx))))
232 (dolist (val values idx)
233 (setq idx (encode-fixed64 val buffer idx))))
235 (dolist (val values idx)
236 (setq idx (encode-sfixed64 val buffer idx))))
238 (dolist (val values idx)
239 (setq idx (encode-single val buffer idx))))
241 (dolist (val values idx)
242 (setq idx (encode-double val buffer idx))))))))
244 (define-compiler-macro serialize-packed (&whole form values type tag buffer index)
245 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
246 :fixed32 :sfixed32 :fixed64 :sfixed64
248 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
249 (let ((idx (encode-uint32 ,tag ,buffer ,index)))
250 (declare (type fixnum idx))
251 (multiple-value-bind (full-len len)
252 (packed-size ,values ,type ,tag)
253 (declare (type fixnum len) (ignore full-len))
254 (setq idx (encode-uint32 len ,buffer idx)))
255 (dolist (val ,values idx)
258 `(setq idx (encode-uint32 val ,buffer idx)))
260 `(setq idx (encode-uint64 val ,buffer idx)))
262 `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
264 `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
266 `(setq idx (encode-fixed32 val ,buffer idx)))
268 `(setq idx (encode-sfixed32 val ,buffer idx)))
270 `(setq idx (encode-fixed64 val ,buffer idx)))
272 `(setq idx (encode-sfixed64 val ,buffer idx)))
274 `(setq idx (encode-single val ,buffer idx)))
276 `(setq idx (encode-double val ,buffer idx)))))))
279 (defun serialize-enum (val values tag buffer index)
280 "Serializes a Protobufs enum value into the buffer at the given index.
281 The value is given by 'val', the enum values are in 'values'.
282 Modifies the buffer in place, and returns the new index into the buffer.
283 Watch out, this function turns off most type checking and all array bounds checking."
284 (declare (type (simple-array (unsigned-byte 8)) buffer)
285 (type (unsigned-byte 32) tag)
287 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
288 (let* ((val (let ((e (find val values :key #'proto-value)))
289 (and e (proto-index e))))
290 (idx (encode-uint32 tag buffer index)))
291 (declare (type (unsigned-byte 32) val)
293 (encode-uint32 val buffer idx))))
298 ;; Deserialize the next object of type 'type'
299 (defun deserialize-prim (type buffer index)
300 "Deserializes the next object of primitive type 'type'.
301 Deserializes from the byte vector 'buffer' starting at 'index'.
302 Returns the value and and the new index into the buffer.
303 Watch out, this function turns off most type checking and all array bounds checking."
304 (declare (type (simple-array (unsigned-byte 8)) buffer)
306 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
309 (decode-uint32 buffer index))
311 (decode-uint64 buffer index))
313 (multiple-value-bind (val idx)
314 (decode-uint32 buffer index)
315 (values (zig-zag-decode32 val) idx)))
317 (multiple-value-bind (val idx)
318 (decode-uint64 buffer index)
319 (values (zig-zag-decode64 val) idx)))
321 (decode-fixed32 buffer index))
323 (decode-sfixed32 buffer index))
325 (decode-fixed64 buffer index))
327 (decode-sfixed64 buffer index))
329 (decode-string buffer index))
331 (decode-octets buffer index))
333 (multiple-value-bind (val idx)
334 (decode-uint32 buffer index)
335 (values (if (zerop val) nil t) idx)))
337 (decode-single buffer index))
339 (decode-double buffer index))
340 ;; A few of our homegrown types
342 ;; Note that this is consy, avoid it if possible
343 (multiple-value-bind (val idx)
344 (decode-string buffer index)
345 (values (make-lisp-symbol val) idx)))
346 ((:date :time :datetime :timestamp)
347 (decode-uint64 buffer index)))))
349 (define-compiler-macro deserialize-prim (&whole form type buffer index)
350 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
351 :fixed32 :sfixed32 :fixed64 :sfixed64
352 :string :bytes :bool :float :double))
353 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
356 `(decode-uint32 ,buffer ,index))
358 `(decode-uint64 ,buffer ,index))
360 `(multiple-value-bind (val idx)
361 (decode-uint32 ,buffer ,index)
362 (values (zig-zag-decode32 val) idx)))
364 `(multiple-value-bind (val idx)
365 (decode-uint64 ,buffer ,index)
366 (values (zig-zag-decode64 val) idx)))
368 `(decode-fixed32 ,buffer ,index))
370 `(decode-sfixed32 ,buffer ,index))
372 `(decode-fixed64 ,buffer ,index))
374 `(decode-sfixed64 ,buffer ,index))
376 `(decode-string ,buffer ,index))
378 `(decode-octets ,buffer ,index))
380 `(multiple-value-bind (val idx)
381 (decode-uint32 ,buffer ,index)
382 (values (if (zerop val) nil t) idx)))
384 `(decode-single ,buffer ,index))
386 `(decode-double ,buffer ,index))))
389 (defun deserialize-packed (type buffer index)
390 "Deserializes the next packed values of type 'type'.
391 Deserializes from the byte vector 'buffer' starting at 'index'.
392 Returns the value and and the new index into the buffer.
393 Watch out, this function turns off most type checking and all array bounds checking."
394 (declare (type (simple-array (unsigned-byte 8)) buffer)
396 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
397 (multiple-value-bind (len idx)
398 (decode-uint32 buffer index)
399 (declare (type (unsigned-byte 32) len)
401 (let ((end (i+ idx len)))
402 (declare (type (unsigned-byte 32) end))
403 (with-collectors ((values collect-value))
406 (return-from deserialize-packed (values values idx)))
407 (multiple-value-bind (val nidx)
410 (decode-uint32 buffer idx))
412 (decode-uint64 buffer idx))
414 (multiple-value-bind (val idx)
415 (decode-uint32 buffer idx)
416 (values (zig-zag-decode32 val) idx)))
418 (multiple-value-bind (val idx)
419 (decode-uint64 buffer idx)
420 (values (zig-zag-decode64 val) idx)))
422 (decode-fixed32 buffer idx))
424 (decode-sfixed32 buffer idx))
426 (decode-fixed64 buffer idx))
428 (decode-sfixed64 buffer idx))
430 (decode-single buffer idx))
432 (decode-double buffer idx)))
434 (setq idx nidx))))))))
436 (defun deserialize-enum (values buffer index)
437 "Deserializes the next enum value take from 'values'.
438 Deserializes from the byte vector 'buffer' starting at 'index'.
439 Returns the value and and the new index into the buffer.
440 Watch out, this function turns off most type checking and all array bounds checking."
441 (declare (type (simple-array (unsigned-byte 8)) buffer)
443 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
444 (multiple-value-bind (val idx)
445 (decode-uint32 buffer index)
446 (let ((val (let ((e (find val values :key #'proto-index)))
447 (and e (proto-value e)))))
453 (defun prim-size (val type tag)
454 "Returns the size in bytes that the primitive object will take when serialized.
455 Watch out, this function turns off most type checking."
456 (declare (type (unsigned-byte 32) tag))
457 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
460 (i+ (length32 tag) (length32 val)))
462 (i+ (length32 tag) (length64 val)))
464 (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
466 (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
467 ((:fixed32 :sfixed32)
468 (i+ (length32 tag) 4))
469 ((:fixed64 :sfixed64)
470 (i+ (length32 tag) 8))
472 (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
473 (i+ (length32 tag) (length32 len) len)))
475 (let ((len (length val)))
476 (i+ (length32 tag) (length32 len) len)))
478 (i+ (length32 tag) 1))
480 (i+ (length32 tag) 4))
482 (i+ (length32 tag) 8))
483 ;; A few of our homegrown types
485 (let ((len (if (keywordp val)
486 (length (symbol-name val))
487 (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
488 (i+ (length32 tag) (length32 len) len)))
489 ((:date :time :datetime :timestamp)
490 (i+ (length32 tag) 8)))))
492 (define-compiler-macro prim-size (&whole form val type tag)
493 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
494 :fixed32 :sfixed32 :fixed64 :sfixed64
495 :string :bytes :bool :float :double))
496 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
499 `(i+ (length32 ,tag) (length32 ,val)))
501 `(i+ (length32 ,tag) (length64 ,val)))
503 `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
505 `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
506 ((:fixed32 :sfixed32)
507 `(i+ (length32 ,tag) 4))
508 ((:fixed64 :sfixed64)
509 `(i+ (length32 ,tag) 8))
511 `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
512 (i+ (length32 ,tag) (length32 len) len)))
514 `(let ((len (length ,val)))
515 (i+ (length32 ,tag) (length32 len) len)))
517 `(i+ (length32 ,tag) 1))
519 `(i+ (length32 ,tag) 4))
521 `(i+ (length32 ,tag) 8))))
524 (defun packed-size (values type tag)
525 "Returns the size in bytes that the packed object will take when serialized.
526 Watch out, this function turns off most type checking."
527 (declare (type (unsigned-byte 32) tag))
528 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
529 (let ((len (let ((len 0))
530 (declare (type fixnum len))
531 (dolist (val values len)
532 (iincf len (ecase type
533 ((:int32 :uint32) (length32 val))
534 ((:int64 :uint64) (length64 val))
535 ((:sint32) (length32 (zig-zag-encode32 val)))
536 ((:sint64) (length64 (zig-zag-encode64 val)))
537 ((:fixed32 :sfixed32) 4)
538 ((:fixed64 :sfixed64) 8)
541 (declare (type (unsigned-byte 32) len))
542 ;; Two value: the full size of the packed object, and the size
543 ;; of just the payload
544 (values (i+ (length32 tag) (length32 len) len) len))))
546 (define-compiler-macro packed-size (&whole form values type tag)
547 (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
548 :fixed32 :sfixed32 :fixed64 :sfixed64
550 `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
551 (let ((len (let ((len 0))
552 (declare (type fixnum len))
553 (dolist (val ,values len)
554 (iincf len ,(ecase type
555 ((:int32 :uint32) `(length32 val))
556 ((:int64 :uint64) `(length64 val))
557 ((:sint32) `(length32 (zig-zag-encode32 val)))
558 ((:sint64) `(length64 (zig-zag-encode64 val)))
559 ((:fixed32 :sfixed32) `4)
560 ((:fixed64 :sfixed64) `8)
563 (declare (type (unsigned-byte 32) len))
564 (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
567 (defun enum-size (val values tag)
568 "Returns the size in bytes that the enum object will take when serialized."
569 (declare (type (unsigned-byte 32) tag))
570 (let ((idx (let ((e (find val values :key #'proto-value)))
571 (and e (proto-index e)))))
572 (assert idx () "There is no enum value for ~S" val)
573 (i+ (length32 tag) (length32 idx))))
578 (defun encode-uint32 (val buffer index)
579 "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
581 Modifies the buffer, and returns the new index into the buffer.
582 Watch out, this function turns off all type checking and array bounds checking."
583 (declare (optimize (speed 3) (safety 0) (debug 0)))
584 (declare (type (unsigned-byte 32) val)
585 (type (simple-array (unsigned-byte 8)) buffer)
587 ;; Seven bits at a time, least significant bits first
588 (loop do (let ((bits (ldb (byte 7 0) val)))
589 (declare (type (unsigned-byte 8) bits))
590 (setq val (ash val -7))
591 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
594 (values index buffer)) ;return the buffer to improve 'trace'
596 (defun encode-uint64 (val buffer index)
597 "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
599 Modifies the buffer, and returns the new index into the buffer.
600 Watch out, this function turns off all type checking and array bounds checking."
601 (declare (optimize (speed 3) (safety 0) (debug 0)))
602 (declare (type (unsigned-byte 64) val)
603 (type (simple-array (unsigned-byte 8)) buffer)
605 (loop do (let ((bits (ldb (byte 7 0) val)))
606 (declare (type (unsigned-byte 8) bits))
607 (setq val (ash val -7))
608 (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
611 (values index buffer))
613 (defun encode-fixed32 (val buffer index)
614 "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
616 Modifies the buffer, and returns the new index into the buffer.
617 Watch out, this function turns off most type checking and all array bounds checking."
618 (declare (type (unsigned-byte 32) val)
619 (type (simple-array (unsigned-byte 8)) buffer)
621 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
623 (let ((byte (ldb (byte 8 0) val)))
624 (declare (type (unsigned-byte 8) byte))
625 (setq val (ash val -8))
626 (setf (aref buffer index) byte)
628 (values index buffer))
630 (defun encode-fixed64 (val buffer index)
631 "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
633 Modifies the buffer, and returns the new index into the buffer.
634 Watch out, this function turns off most type checking and all array bounds checking."
635 (declare (type (unsigned-byte 64) val)
636 (type (simple-array (unsigned-byte 8)) buffer)
638 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
640 (let ((byte (ldb (byte 8 0) val)))
641 (declare (type (unsigned-byte 8) byte))
642 (setq val (ash val -8))
643 (setf (aref buffer index) byte)
645 (values index buffer))
647 (defun encode-sfixed32 (val buffer index)
648 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
650 Modifies the buffer, and returns the new index into the buffer.
651 Watch out, this function turns off most type checking and all array bounds checking."
652 (declare (type (signed-byte 32) val)
653 (type (simple-array (unsigned-byte 8)) buffer)
655 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
657 (let ((byte (ldb (byte 8 0) val)))
658 (declare (type (unsigned-byte 8) byte))
659 (setq val (ash val -8))
660 (setf (aref buffer index) byte)
662 (values index buffer))
664 (defun encode-sfixed64 (val buffer index)
665 "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
667 Modifies the buffer, and returns the new index into the buffer.
668 Watch out, this function turns off most type checking and all array bounds checking."
669 (declare (type (signed-byte 64) val)
670 (type (simple-array (unsigned-byte 8)) buffer)
672 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
674 (let ((byte (ldb (byte 8 0) val)))
675 (declare (type (unsigned-byte 8) byte))
676 (setq val (ash val -8))
677 (setf (aref buffer index) byte)
679 (values index buffer))
681 (defun encode-single (val buffer index)
682 "Encodes the single float 'val' into the buffer at the given index.
683 Modifies the buffer, and returns the new index into the buffer.
684 Watch out, this function turns off most type checking and all array bounds checking."
685 (declare (type single-float val)
686 (type (simple-array (unsigned-byte 8)) buffer)
688 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
689 (let ((bits (single-float-bits val)))
691 (let ((byte (ldb (byte 8 0) bits)))
692 (declare (type (unsigned-byte 8) byte))
693 (setq bits (ash bits -8))
694 (setf (aref buffer index) byte)
696 (values index buffer))
698 (defun encode-double (val buffer index)
699 "Encodes the double float 'val' into the buffer at the given index.
700 Modifies the buffer, and returns the new index into the buffer.
701 Watch out, this function turns off most type checking and all array bounds checking."
702 (declare (type double-float val)
703 (type (simple-array (unsigned-byte 8)) buffer)
705 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
706 (multiple-value-bind (low high)
707 (double-float-bits val)
709 (let ((byte (ldb (byte 8 0) low)))
710 (declare (type (unsigned-byte 8) byte))
711 (setq low (ash low -8))
712 (setf (aref buffer index) byte)
715 (let ((byte (ldb (byte 8 0) high)))
716 (declare (type (unsigned-byte 8) byte))
717 (setq high (ash high -8))
718 (setf (aref buffer index) byte)
720 (values index buffer))
722 (defun encode-string (string buffer index)
723 "Encodes the octets into the buffer at the given index.
724 Modifies the buffer, and returns the new index into the buffer.
725 Watch out, this function turns off most type checking and all array bounds checking."
726 (declare (type (simple-array (unsigned-byte 8)) buffer)
728 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
729 (let* ((octets (babel:string-to-octets string :encoding :utf-8))
730 (len (length octets))
731 (idx (encode-uint32 len buffer index)))
732 (declare (type fixnum len)
733 (type (unsigned-byte 32) idx))
734 (replace buffer octets :start1 idx)
735 (values (i+ idx len) buffer))))
737 (defun encode-octets (octets buffer index)
738 "Encodes the octets 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 (simple-array (unsigned-byte 8)) buffer)
743 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
744 (let* ((len (length octets))
745 (idx (encode-uint32 len buffer index)))
746 (declare (type fixnum len)
747 (type (unsigned-byte 32) idx))
748 (replace buffer octets :start1 idx)
749 (values (i+ idx len) buffer))))
754 ;; Decode the value from the buffer at the given index,
755 ;; then return the value and new index into the buffer
756 (defun decode-uint32 (buffer index)
757 "Decodes the next 32-bit varint integer in the buffer at the given index.
758 Returns both the decoded value and the new index into the buffer.
759 Watch out, this function turns off most type checking and all array bounds checking."
760 (declare (optimize (speed 3) (safety 0) (debug 0)))
761 (declare (type (simple-array (unsigned-byte 8)) buffer)
763 ;; Seven bits at a time, least significant bits first
765 for places fixnum upfrom 0 by 7
766 for byte fixnum = (prog1 (aref buffer index) (iincf index))
767 do (setq val (logior val (ash (ldb (byte 7 0) byte) places)))
770 (assert (< val #.(ash 1 32)) ()
771 "The value ~D is longer than 32 bits" val)
772 (return (values val index)))))
774 (defun decode-uint64 (buffer index)
775 "Decodes the next 64-bit varint integer in the buffer at the given index.
776 Returns both the decoded value and the new index into the buffer.
777 Watch out, this function turns off most type checking and all array bounds checking."
778 (declare (optimize (speed 3) (safety 0) (debug 0)))
779 (declare (type (simple-array (unsigned-byte 8)) buffer)
781 ;; Seven bits at a time, least significant bits first
783 for places fixnum upfrom 0 by 7
784 for byte fixnum = (prog1 (aref buffer index) (iincf index))
785 do (setq val (logior val (ash (ldb (byte 7 0) byte) places)))
787 finally (return (values val index))))
789 (defun decode-fixed32 (buffer index)
790 "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
791 Returns both the decoded value and the new index into the buffer.
792 Watch out, this function turns off most type checking and all array bounds checking."
793 (declare (type (simple-array (unsigned-byte 8)) buffer)
795 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
796 ;; Eight bits at a time, least significant bits first
799 for places fixnum upfrom 0 by 8
800 for byte fixnum = (prog1 (aref buffer index) (iincf index))
801 do (setq val (logior val (ash byte places))))
802 (values val index))))
804 (defun decode-sfixed32 (buffer index)
805 "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
806 Returns both the decoded value and the new index into the buffer.
807 Watch out, this function turns off most type checking and all array bounds checking."
808 (declare (type (simple-array (unsigned-byte 8)) buffer)
810 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
811 ;; Eight bits at a time, least significant bits first
814 for places fixnum upfrom 0 by 8
815 for byte fixnum = (prog1 (aref buffer index) (iincf index))
816 do (setq val (logior val (ash byte places))))
817 (when (i= (ldb (byte 1 31) val) 1) ;sign bit set, so negative value
818 (decf val #.(ash 1 32)))
819 (values val index))))
821 (defun decode-fixed64 (buffer index)
822 "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
823 Returns both the decoded value and the new index into the buffer.
824 Watch out, this function turns off most type checking and all array bounds checking."
825 (declare (type (simple-array (unsigned-byte 8)) buffer)
827 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
828 ;; Eight bits at a time, least significant bits first
831 for places fixnum upfrom 0 by 8
832 for byte fixnum = (prog1 (aref buffer index) (iincf index))
833 do (setq val (logior val (ash byte places))))
834 (values val index))))
836 (defun decode-sfixed64 (buffer index)
837 "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
838 Returns both the decoded value and the new index into the buffer.
839 Watch out, this function turns off most type checking and all array bounds checking."
840 (declare (type (simple-array (unsigned-byte 8)) buffer)
842 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
843 ;; Eight bits at a time, least significant bits first
846 for places fixnum upfrom 0 by 8
847 for byte fixnum = (prog1 (aref buffer index) (iincf index))
848 do (setq val (logior val (ash byte places))))
849 (when (i= (ldb (byte 1 63) val) 1) ;sign bit set, so negative value
850 (decf val #.(ash 1 64)))
851 (values val index))))
853 (defun decode-single (buffer index)
854 "Decodes the next single float 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 most type checking and all array bounds checking."
857 (declare (type (simple-array (unsigned-byte 8)) buffer)
859 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
860 ;; Eight bits at a time, least significant bits first
863 for places fixnum upfrom 0 by 8
864 for byte fixnum = (prog1 (aref buffer index) (iincf index))
865 do (setq bits (logior bits (ash byte places))))
866 (when (i= (ldb (byte 1 31) bits) 1) ;sign bit set, so negative value
867 (decf bits #.(ash 1 32)))
868 (values (make-single-float bits) index))))
870 (defun decode-double (buffer index)
871 "Decodes the next double float in the buffer at the given index.
872 Returns both the decoded value and the new index into the buffer.
873 Watch out, this function turns off most type checking and all array bounds checking."
874 (declare (type (simple-array (unsigned-byte 8)) buffer)
876 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
877 ;; Eight bits at a time, least significant bits first
881 for places fixnum upfrom 0 by 8
882 for byte fixnum = (prog1 (aref buffer index) (iincf index))
883 do (setq low (logior low (ash byte places))))
885 for places fixnum upfrom 0 by 8
886 for byte fixnum = (prog1 (aref buffer index) (iincf index))
887 do (setq high (logior high (ash byte places))))
888 ;; High bits are signed, but low bits are unsigned
889 (when (i= (ldb (byte 1 31) high) 1) ;sign bit set, so negative value
890 (decf high #.(ash 1 32)))
891 (values (make-double-float low high) index))))
893 (defun decode-string (buffer index)
894 "Decodes the next UTF-8 encoded string in the buffer at the given index.
895 Returns both the decoded string and the new index into the buffer.
896 Watch out, this function turns off most type checking and all array bounds checking."
897 (declare (type (simple-array (unsigned-byte 8)) buffer)
899 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
900 (multiple-value-bind (len idx)
901 (decode-uint32 buffer index)
902 (declare (type (unsigned-byte 32) len)
904 (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len)))))
906 (defun decode-octets (buffer index)
907 "Decodes the next octets in the buffer at the given index.
908 Returns both the decoded value and the new index into the buffer.
909 Watch out, this function turns off most type checking and all array bounds checking."
910 (declare (type (simple-array (unsigned-byte 8)) buffer)
912 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
913 (multiple-value-bind (len idx)
914 (decode-uint32 buffer index)
915 (declare (type (unsigned-byte 32) len)
917 (values (subseq buffer idx (i+ idx len)) (i+ idx len)))))
922 (defun length32 (val)
923 "Returns the length that 'val' will take when encoded as a 32-bit integer."
924 (declare (optimize (speed 3) (safety 0) (debug 0)))
926 (declare (type fixnum size))
928 (setq val (ash val -7))
933 (defun length64 (val)
934 "Returns the length that 'val' will take when encoded as a 64-bit integer."
935 (declare (optimize (speed 3) (safety 0) (debug 0)))
937 (declare (type fixnum size))
939 (setq val (ash val -7))
945 ;;; Skipping elements
947 (defun skip-element (buffer index wire-type)
948 "Skip an element in the buffer at the index of the given wire type.
949 Returns the new index in the buffer.
950 Watch out, this function turns off all type checking and all array bounds checking."
951 (declare (optimize (speed 3) (safety 0) (debug 0)))
952 (declare (type (simple-array (unsigned-byte 8)) buffer)
954 (type (unsigned-byte 32) wire-type))
957 (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
961 (multiple-value-bind (len idx)
962 (decode-uint32 buffer index)
963 (declare (type (unsigned-byte 32) len)