]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blob - wire-format.lisp
Tie up a few loose ends with 'unittests.proto'; there are still a few
[cl-protobufs.git] / wire-format.lisp
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;                                                                  ;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc.   ;;;
4 ;;;                                                                  ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc.  All rights reserved.      ;;;
6 ;;;                                                                  ;;;
7 ;;; Original author: Scott McKay                                     ;;;
8 ;;;                                                                  ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
11 (in-package "PROTO-IMPL")
12
13
14 ;;; Protocol buffers wire format
15
16 ;;; Utilities
17
18 (eval-when (:compile-toplevel :load-toplevel :execute)
19
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)
26
27 )       ;eval-when
28
29
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)
35       (ilogior type (iash index 3))
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))))))
50
51 (define-compiler-macro make-tag (&whole form type index)
52   (setq type (fold-symbol type))
53   (cond ((typep type 'fixnum)
54          `(ilogior ,type (iash ,index 3)))
55         ((keywordp type)
56          (let ((type (ecase type
57                        ((:int32 :uint32) $wire-type-varint)
58                        ((:int64 :uint64) $wire-type-varint)
59                        ((:sint32 :sint64) $wire-type-varint)
60                        ((:fixed32 :sfixed32) $wire-type-32bit)
61                        ((:fixed64 :sfixed64) $wire-type-64bit)
62                        ((:string :bytes) $wire-type-string)
63                        ((:bool) $wire-type-varint)
64                        ((:float) $wire-type-32bit)
65                        ((:double) $wire-type-64bit)
66                        ;; A few of our homegrown types
67                        ((:symbol) $wire-type-string)
68                        ((:date :time :datetime :timestamp) $wire-type-64bit))))
69            `(ilogior ,type (iash ,index 3))))
70         (t form)))
71
72 (defun fold-symbol (x)
73   "Given an expression 'x', constant-fold it until it can be foleded no more."
74   (let ((last '#:last))
75     (loop
76       (cond ((eq x last) (return x))
77             ((and (listp x)
78                   (eq (first x) 'quote)
79                   (constantp (second x)))
80              (shiftf last x (second x)))
81             ((and (symbolp x)
82                   (boundp x))
83              (shiftf last x (symbol-value x)))
84             (t (return x))))))
85
86
87 (defun zig-zag-encode32 (val)
88   (declare (optimize (speed 3) (safety 0) (debug 0)))
89   (declare (type (signed-byte 32) val))
90   (logxor (ash val 1) (ash val -31)))
91
92 (defun zig-zag-encode64 (val)
93   (declare (optimize (speed 3) (safety 0) (debug 0)))
94   (declare (type (signed-byte 64) val))
95   (logxor (ash val 1) (ash val -63)))
96
97 (define-compiler-macro zig-zag-encode32 (&whole form val)
98   (if (atom val)
99     `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
100                        (type (signed-byte 32) ,val))
101        (logxor (ash ,val 1) (ash ,val -31)))
102     form))
103
104 (define-compiler-macro zig-zag-encode64 (&whole form val)
105   (if (atom val)
106     `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
107                        (type (signed-byte 64) ,val))
108        (logxor (ash ,val 1) (ash ,val -63)))
109     form))
110
111 (defun zig-zag-decode32 (val)
112   (declare (optimize (speed 3) (safety 0) (debug 0)))
113   (logxor (ash val -1) (- (logand val 1))))
114
115 (defun zig-zag-decode64 (val)
116   (declare (optimize (speed 3) (safety 0) (debug 0)))
117   (logxor (ash val -1) (- (logand val 1))))
118
119 (define-compiler-macro zig-zag-decode32 (&whole form val)
120   (if (atom val)
121     `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
122        (logxor (ash ,val -1) (- (logand ,val 1))))
123     form))
124
125 (define-compiler-macro zig-zag-decode64 (&whole form val)
126   (if (atom val)
127     `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
128        (logxor (ash ,val -1) (- (logand ,val 1))))
129     form))
130
131
132 ;;; Serializers
133
134 ;; Serialize 'val' of primitive type 'type' into the buffer
135 (defun serialize-prim (val type tag buffer index)
136   "Serializes a Protobufs primitive (scalar) value into the buffer at the given index.
137    The value is given by 'val', the primitive type by 'type'.
138    Modifies the buffer in place, and returns the new index into the buffer.
139    Watch out, this function turns off most type checking and all array bounds checking."
140   (declare 
141            (type (unsigned-byte 32) tag)
142            (type fixnum index))
143   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
144     (let ((idx (encode-uint32 tag buffer index)))
145       (declare (type fixnum idx))
146       (ecase type
147         ((:int32 :uint32)
148          (encode-uint32 (ldb (byte 32 0) val) buffer idx))
149         ((:int64 :uint64)
150          (encode-uint64 (ldb (byte 64 0) val) buffer idx))
151         ((:sint32)
152          (encode-uint32 (zig-zag-encode32 val) buffer idx))
153         ((:sint64)
154          (encode-uint64 (zig-zag-encode64 val) buffer idx))
155         ((:fixed32)
156          (encode-fixed32 val buffer idx))
157         ((:sfixed32)
158          (encode-sfixed32 val buffer idx))
159         ((:fixed64)
160          (encode-fixed64 val buffer idx))
161         ((:sfixed64)
162          (encode-sfixed64 val buffer idx))
163         ((:string)
164          (encode-string val buffer idx))
165         ((:bytes)
166          (encode-octets val buffer idx))
167         ((:bool)
168          (encode-uint32 (if val 1 0) buffer idx))
169         ((:float)
170          (encode-single val buffer idx))
171         ((:double)
172          (encode-double val buffer idx))
173         ;; A few of our homegrown types
174         ((:symbol)
175          (let ((val (if (keywordp val)
176                       (string val)
177                       ;; Non-keyword symbols are consy, avoid them if possible
178                       (format nil "~A:~A" (package-name (symbol-package val)) (symbol-name val)))))
179            (encode-string val buffer idx)))
180         ((:date :time :datetime :timestamp)
181          (encode-uint64 (ldb (byte 64 0) val) buffer idx))))))
182
183 (define-compiler-macro serialize-prim (&whole form val type tag buffer index)
184   (setq type (fold-symbol type)
185         tag  (fold-symbol tag))
186   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
187                      :fixed32 :sfixed32 :fixed64 :sfixed64
188                      :string :bytes :bool :float :double))
189     `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
190                        (type (simple-array (unsigned-byte 8)) ,buffer)
191                        ;; 'tag' is a constant, no need to declare its type
192                        (type fixnum ,index))
193        (let ((idx (encode-uint32 ,tag ,buffer ,index)))
194          (declare (type fixnum idx))
195          ,(ecase type
196             ((:int32 )
197              `(encode-uint32 (ldb (byte 32 0) ,val) ,buffer idx))
198             ((:int64)
199              `(encode-uint64 (ldb (byte 64 0) ,val) ,buffer idx))
200             ((:uint32)
201              `(encode-uint32 ,val ,buffer idx))
202             ((:uint64)
203              `(encode-uint64 ,val ,buffer idx))
204             ((:sint32)
205              `(encode-uint32 (zig-zag-encode32 ,val) ,buffer idx))
206             ((:sint64)
207              `(encode-uint64 (zig-zag-encode64 ,val) ,buffer idx))
208             ((:fixed32)
209              `(encode-fixed32 ,val ,buffer idx))
210             ((:sfixed32)
211              `(encode-sfixed32 ,val ,buffer idx))
212             ((:fixed64)
213              `(encode-fixed64 ,val ,buffer idx))
214             ((:sfixed64)
215              `(encode-sfixed64 ,val ,buffer idx))
216             ((:string)
217              `(encode-string ,val ,buffer idx))
218             ((:bytes)
219              `(encode-octets ,val ,buffer idx))
220             ((:bool)
221              `(encode-uint32 (if ,val 1 0) ,buffer idx))
222             ((:float)
223              `(encode-single ,val ,buffer idx))
224             ((:double)
225              `(encode-double ,val ,buffer idx)))))
226     form))
227
228 (defun serialize-packed (values type tag buffer index)
229   "Serializes a set of packed values into the buffer at the given index.
230    The values are given by 'values', the primitive type by 'type'.
231    Modifies the buffer in place, and returns the new index into the buffer.
232    Watch out, this function turns off most type checking and all array bounds checking."
233   (declare (type (simple-array (unsigned-byte 8)) buffer)
234            (type (unsigned-byte 32) tag)
235            (type fixnum index))
236   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
237     (let ((idx (encode-uint32 tag buffer index)))
238       (declare (type fixnum idx))
239       (multiple-value-bind (full-len len)
240           (packed-size values type tag)
241         (declare (type fixnum len) (ignore full-len))
242         (setq idx (encode-uint32 len buffer idx)))
243       (ecase type
244         ((:int32 :uint32)
245          (dolist (val values idx)
246            (setq idx (encode-uint32 (ldb (byte 32 0) val) buffer idx))))
247         ((:int64 :uint64)
248          (dolist (val values idx)
249            (setq idx (encode-uint64 (ldb (byte 64 0) val) buffer idx))))
250         ((:sint32)
251          (dolist (val values idx)
252            (setq idx (encode-uint32 (zig-zag-encode32 val) buffer idx))))
253         ((:sint64)
254          (dolist (val values idx)
255            (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
256         ((:fixed32)
257          (dolist (val values idx)
258            (setq idx (encode-fixed32 val buffer idx))))
259         ((:sfixed32)
260          (dolist (val values idx)
261            (setq idx (encode-sfixed32 val buffer idx))))
262         ((:fixed64)
263          (dolist (val values idx)
264            (setq idx (encode-fixed64 val buffer idx))))
265         ((:sfixed64)
266          (dolist (val values idx)
267            (setq idx (encode-sfixed64 val buffer idx))))
268         ((:bool)
269          (dolist (val values idx)
270            (setq idx (encode-uint32 (if val 1 0) buffer idx))))
271         ((:float)
272          (dolist (val values idx)
273            (setq idx (encode-single val buffer idx))))
274         ((:double)
275          (dolist (val values idx)
276            (setq idx (encode-double val buffer idx))))))))
277
278 (define-compiler-macro serialize-packed (&whole form values type tag buffer index)
279   (setq type (fold-symbol type)
280         tag  (fold-symbol tag))
281   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
282                      :fixed32 :sfixed32 :fixed64 :sfixed64
283                      :bool :float :double))
284     `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
285                        (type (simple-array (unsigned-byte 8)) ,buffer)
286                        ;; 'tag' is a constant, no need to declare its type
287                        (type fixnum ,index))
288        (let ((idx (encode-uint32 ,tag ,buffer ,index)))
289          (declare (type fixnum idx))
290          (multiple-value-bind (full-len len)
291              (packed-size ,values ,type ,tag)
292            (declare (type fixnum len) (ignore full-len))
293            (setq idx (encode-uint32 len ,buffer idx)))
294          (dolist (val ,values idx)
295            ,(ecase type
296               ((:int32)
297                `(setq idx (encode-uint32 (ldb (byte 32 0) val) ,buffer idx)))
298               ((:int64)
299                `(setq idx (encode-uint64 (ldb (byte 64 0) val) ,buffer idx)))
300               ((:uint32)
301                `(setq idx (encode-uint32 val ,buffer idx)))
302               ((:uint64)
303                `(setq idx (encode-uint64 val ,buffer idx)))
304               ((:sint32)
305                `(setq idx (encode-uint32 (zig-zag-encode32 val) ,buffer idx)))
306               ((:sint64)
307                `(setq idx (encode-uint64 (zig-zag-encode64 val) ,buffer idx)))
308               ((:fixed32)
309                `(setq idx (encode-fixed32 val ,buffer idx)))
310               ((:sfixed32)
311                `(setq idx (encode-sfixed32 val ,buffer idx)))
312               ((:fixed64)
313                `(setq idx (encode-fixed64 val ,buffer idx)))
314               ((:sfixed64)
315                `(setq idx (encode-sfixed64 val ,buffer idx)))
316               ((:bool)
317                `(setq idx (encode-uint32 (if val 1 0) ,buffer idx)))
318               ((:float)
319                `(setq idx (encode-single val ,buffer idx)))
320               ((:double)
321                `(setq idx (encode-double val ,buffer idx)))))))
322     form))
323
324 (defun serialize-enum (val values tag buffer index)
325   "Serializes a Protobufs enum value into the buffer at the given index.
326    The value is given by 'val', the enum values are in 'values'.
327    Modifies the buffer in place, and returns the new index into the buffer.
328    Watch out, this function turns off most type checking and all array bounds checking."
329   (declare (type (simple-array (unsigned-byte 8)) buffer)
330            (type (unsigned-byte 32) tag)
331            (type fixnum index))
332   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
333     (let* ((val (let ((e (find val values :key #'proto-value)))
334                   (and e (proto-index e))))
335            (idx (encode-uint32 tag buffer index)))
336       (declare (type (unsigned-byte 32) val)
337                (type fixnum idx))
338       (encode-uint32 (ldb (byte 32 0) val) buffer idx))))
339
340
341 ;;; Deserializers
342
343 ;; Deserialize the next object of type 'type'
344 (defun deserialize-prim (type buffer index)
345   "Deserializes the next object of primitive type 'type'.
346    Deserializes from the byte vector 'buffer' starting at 'index'.
347    Returns the value and and the new index into the buffer.
348    Watch out, this function turns off most type checking and all array bounds checking."
349   (declare (type (simple-array (unsigned-byte 8)) buffer)
350            (type fixnum index))
351   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
352     (ecase type
353       ((:int32)
354        (decode-int32 buffer index))
355       ((:int64)
356        (decode-int64 buffer index))
357       ((:uint32)
358        (decode-uint32 buffer index))
359       ((:uint64)
360        (decode-uint64 buffer index))
361       ((:sint32)
362        (multiple-value-bind (val idx)
363            (decode-uint32 buffer index)
364          (values (zig-zag-decode32 val) idx)))
365       ((:sint64)
366        (multiple-value-bind (val idx)
367            (decode-uint64 buffer index)
368          (values (zig-zag-decode64 val) idx)))
369       ((:fixed32)
370        (decode-fixed32 buffer index))
371       ((:sfixed32)
372        (decode-sfixed32 buffer index))
373       ((:fixed64)
374        (decode-fixed64 buffer index))
375       ((:sfixed64)
376        (decode-sfixed64 buffer index))
377       ((:string)
378        (decode-string buffer index))
379       ((:bytes)
380        (decode-octets buffer index))
381       ((:bool)
382        (multiple-value-bind (val idx)
383            (decode-uint32 buffer index)
384          (values (if (i= val 0) nil t) idx)))
385       ((:float)
386        (decode-single buffer index))
387       ((:double)
388        (decode-double buffer index))
389       ;; A few of our homegrown types
390       ((:symbol)
391        ;; Note that this is consy, avoid it if possible
392        (multiple-value-bind (val idx)
393            (decode-string buffer index)
394          (values (make-lisp-symbol val) idx)))
395       ((:date :time :datetime :timestamp)
396        (decode-uint64 buffer index)))))
397
398 (define-compiler-macro deserialize-prim (&whole form type buffer index)
399   (setq type (fold-symbol type))
400   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
401                      :fixed32 :sfixed32 :fixed64 :sfixed64
402                      :string :bytes :bool :float :double))
403     `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
404                        (type (simple-array (unsigned-byte 8)) ,buffer)
405                        (type fixnum ,index))
406        ,(ecase type
407           ((:int32)
408            `(decode-int32 ,buffer ,index))
409           ((:int64)
410            `(decode-int64 ,buffer ,index))
411           ((:uint32)
412            `(decode-uint32 ,buffer ,index))
413           ((:uint64)
414            `(decode-uint64 ,buffer ,index))
415           ((:sint32)
416            `(multiple-value-bind (val idx)
417                 (decode-uint32 ,buffer ,index)
418               (values (zig-zag-decode32 val) idx)))
419           ((:sint64)
420            `(multiple-value-bind (val idx)
421                (decode-uint64 ,buffer ,index)
422              (values (zig-zag-decode64 val) idx)))
423           ((:fixed32)
424            `(decode-fixed32 ,buffer ,index))
425           ((:sfixed32)
426            `(decode-sfixed32 ,buffer ,index))
427           ((:fixed64)
428            `(decode-fixed64 ,buffer ,index))
429           ((:sfixed64)
430            `(decode-sfixed64 ,buffer ,index))
431           ((:string)
432            `(decode-string ,buffer ,index))
433           ((:bytes)
434            `(decode-octets ,buffer ,index))
435           ((:bool)
436            `(multiple-value-bind (val idx)
437                 (decode-uint32 ,buffer ,index)
438               (values (if (i= val 0) nil t) idx)))
439           ((:float)
440            `(decode-single ,buffer ,index))
441           ((:double)
442            `(decode-double ,buffer ,index))))
443     form))
444
445 (defun deserialize-packed (type buffer index)
446   "Deserializes the next packed values of type 'type'.
447    Deserializes from the byte vector 'buffer' starting at 'index'.
448    Returns the value and and the new index into the buffer.
449    Watch out, this function turns off most type checking and all array bounds checking."
450   (declare (type (simple-array (unsigned-byte 8)) buffer)
451            (type fixnum index))
452   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
453     (multiple-value-bind (len idx)
454         (decode-uint32 buffer index)
455       (declare (type (unsigned-byte 32) len)
456                (type fixnum idx))
457       (let ((end (i+ idx len)))
458         (declare (type (unsigned-byte 32) end))
459         (with-collectors ((values collect-value))
460           (loop
461             (when (>= idx end)
462               (return-from deserialize-packed (values values idx)))
463             (multiple-value-bind (val nidx)
464                 (ecase type
465                   ((:int32)
466                    (decode-int32 buffer idx))
467                   ((:int64)
468                    (decode-int64 buffer idx))
469                   ((:uint32)
470                    (decode-uint32 buffer idx))
471                   ((:uint64)
472                    (decode-uint64 buffer idx))
473                   ((:sint32)
474                    (multiple-value-bind (val nidx)
475                        (decode-uint32 buffer idx)
476                      (values (zig-zag-decode32 val) nidx)))
477                   ((:sint64)
478                    (multiple-value-bind (val nidx)
479                        (decode-uint64 buffer idx)
480                      (values (zig-zag-decode64 val) nidx)))
481                   ((:fixed32)
482                    (decode-fixed32 buffer idx))
483                   ((:sfixed32)
484                    (decode-sfixed32 buffer idx))
485                   ((:fixed64)
486                    (decode-fixed64 buffer idx))
487                   ((:sfixed64)
488                    (decode-sfixed64 buffer idx))
489                   ((:bool)
490                    (multiple-value-bind (val nidx)
491                        (decode-uint32 buffer idx)
492                      (values (if (i= val 0) nil t) nidx)))
493                   ((:float)
494                    (decode-single buffer idx))
495                   ((:double)
496                    (decode-double buffer idx)))
497               (collect-value val)
498               (setq idx nidx))))))))
499
500 (define-compiler-macro deserialize-packed (&whole form type buffer index)
501   (setq type (fold-symbol type))
502   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
503                      :fixed32 :sfixed32 :fixed64 :sfixed64
504                      :bool :float :double))
505     `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
506                        (type (simple-array (unsigned-byte 8)) ,buffer)
507                        (type fixnum ,index))
508        (block deserialize-packed
509          (multiple-value-bind (len idx)
510              (decode-uint32 ,buffer ,index)
511            (declare (type (unsigned-byte 32) len)
512                     (type fixnum idx))
513            (let ((end (i+ idx len)))
514              (declare (type (unsigned-byte 32) end))
515              (with-collectors ((values collect-value))
516                (loop
517                  (when (>= idx end)
518                    (return-from deserialize-packed (values values idx)))
519                  (multiple-value-bind (val nidx)
520                      ,(ecase type
521                         ((:int32)
522                          `(decode-int32 ,buffer idx))
523                         ((:int64)
524                          `(decode-int64 ,buffer idx))
525                         ((:uint32)
526                          `(decode-uint32 ,buffer idx))
527                         ((:uint64)
528                          `(decode-uint64 ,buffer idx))
529                         ((:sint32)
530                          `(multiple-value-bind (val nidx)
531                               (decode-uint32 ,buffer idx)
532                             (values (zig-zag-decode32 val) nidx)))
533                         ((:sint64)
534                          `(multiple-value-bind (val nidx)
535                               (decode-uint64 ,buffer idx)
536                             (values (zig-zag-decode64 val) nidx)))
537                         ((:fixed32)
538                          `(decode-fixed32 ,buffer idx))
539                         ((:sfixed32)
540                          `(decode-sfixed32 ,buffer idx))
541                         ((:fixed64)
542                          `(decode-fixed64 ,buffer idx))
543                         ((:sfixed64)
544                          `(decode-sfixed64 ,buffer idx))
545                         ((:bool)
546                          `(multiple-value-bind (val nidx)
547                               (decode-uint32 ,buffer idx)
548                             (values (if (i= val 0) nil t) nidx)))
549                         ((:float)
550                          `(decode-single ,buffer idx))
551                         ((:double)
552                          `(decode-double ,buffer idx)))
553                    (collect-value val)
554                    (setq idx nidx))))))))
555     form))
556
557 (defun deserialize-enum (values buffer index)
558   "Deserializes the next enum value take from 'values'.
559    Deserializes from the byte vector 'buffer' starting at 'index'.
560    Returns the value and and the new index into the buffer.
561    Watch out, this function turns off most type checking and all array bounds checking."
562   (declare (type (simple-array (unsigned-byte 8)) buffer)
563            (type fixnum index))
564   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
565     (multiple-value-bind (val idx)
566         (decode-int32 buffer index)
567       (let ((val (let ((e (find val values :key #'proto-index)))
568                    (and e (proto-value e)))))
569         (values val idx)))))
570
571
572 ;;; Object sizing
573
574 (defun prim-size (val type tag)
575   "Returns the size in bytes that the primitive object will take when serialized.
576    Watch out, this function turns off most type checking."
577   (declare (type (unsigned-byte 32) tag))
578   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
579     (ecase type
580       ((:int32 :uint32)
581        (i+ (length32 tag) (length32 (ldb (byte 32 0) val))))
582       ((:int64 :uint64)
583        (i+ (length32 tag) (length64 (ldb (byte 64 0) val))))
584       ((:sint32)
585        (i+ (length32 tag) (length32 (zig-zag-encode32 val))))
586       ((:sint64)
587        (i+ (length32 tag) (length64 (zig-zag-encode64 val))))
588       ((:fixed32 :sfixed32)
589        (i+ (length32 tag) 4))
590       ((:fixed64 :sfixed64)
591        (i+ (length32 tag) 8))
592       ((:string)
593        (let ((len (babel:string-size-in-octets val :encoding :utf-8)))
594          (i+ (length32 tag) (length32 len) len)))
595       ((:bytes)
596        (let ((len (length val)))
597          (i+ (length32 tag) (length32 len) len)))
598       ((:bool)
599        (i+ (length32 tag) 1))
600       ((:float)
601        (i+ (length32 tag) 4))
602       ((:double)
603        (i+ (length32 tag) 8))
604       ;; A few of our homegrown types
605       ((:symbol)
606        (let ((len (if (keywordp val)
607                     (length (symbol-name val))
608                     (i+ (length (package-name (symbol-package val))) 1 (length (symbol-name val))))))
609          (i+ (length32 tag) (length32 len) len)))
610       ((:date :time :datetime :timestamp)
611        (i+ (length32 tag) 8)))))
612
613 (define-compiler-macro prim-size (&whole form val type tag)
614   (setq type (fold-symbol type)
615         tag  (fold-symbol tag))
616   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
617                      :fixed32 :sfixed32 :fixed64 :sfixed64
618                      :string :bytes :bool :float :double))
619     `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
620        ,(ecase type
621           ((:int32)
622            `(i+ (length32 ,tag) (length32 (ldb (byte 32 0) ,val))))
623           ((:int64)
624            `(i+ (length32 ,tag) (length64 (ldb (byte 64 0) ,val))))
625           ((:uint32)
626            `(i+ (length32 ,tag) (length32 ,val)))
627           ((:uint64)
628            `(i+ (length32 ,tag) (length64 ,val)))
629           ((:sint32)
630            `(i+ (length32 ,tag) (length32 (zig-zag-encode32 ,val))))
631           ((:sint64)
632            `(i+ (length32 ,tag) (length64 (zig-zag-encode64 ,val))))
633           ((:fixed32 :sfixed32)
634            `(i+ (length32 ,tag) 4))
635           ((:fixed64 :sfixed64)
636            `(i+ (length32 ,tag) 8))
637           ((:string)
638            `(let ((len (babel:string-size-in-octets ,val :encoding :utf-8)))
639               (i+ (length32 ,tag) (length32 len) len)))
640           ((:bytes)
641            `(let ((len (length ,val)))
642               (i+ (length32 ,tag) (length32 len) len)))
643           ((:bool)
644            `(i+ (length32 ,tag) 1))
645           ((:float)
646            `(i+ (length32 ,tag) 4))
647           ((:double)
648            `(i+ (length32 ,tag) 8))))
649     form))
650
651 (defun packed-size (values type tag)
652   "Returns the size in bytes that the packed object will take when serialized.
653    Watch out, this function turns off most type checking."
654   (declare (type (unsigned-byte 32) tag))
655   (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
656     (let ((len (let ((len 0))
657                  (declare (type fixnum len))
658                  (dolist (val values len)
659                    (iincf len (ecase type
660                                 ((:int32 :uint32) (length32 (ldb (byte 32 0) val)))
661                                 ((:int64 :uint64) (length64 (ldb (byte 64 0) val)))
662                                 ((:sint32) (length32 (zig-zag-encode32 val)))
663                                 ((:sint64) (length64 (zig-zag-encode64 val)))
664                                 ((:fixed32 :sfixed32) 4)
665                                 ((:fixed64 :sfixed64) 8)
666                                 ((:bool)   1)
667                                 ((:float)  4)
668                                 ((:double) 8)))))))
669       (declare (type (unsigned-byte 32) len))
670       ;; Two value: the full size of the packed object, and the size
671       ;; of just the payload
672       (values (i+ (length32 tag) (length32 len) len) len))))
673
674 (define-compiler-macro packed-size (&whole form values type tag)
675   (setq type (fold-symbol type)
676         tag  (fold-symbol tag))
677   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
678                      :fixed32 :sfixed32 :fixed64 :sfixed64
679                      :bool :float :double))
680     `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
681        (let ((len (let ((len 0))
682                     (declare (type fixnum len))
683                     (dolist (val ,values len)
684                       (iincf len ,(ecase type
685                                     ((:int32) `(length32 (ldb (byte 32 0) val)))
686                                     ((:int64) `(length64 (ldb (byte 64 0) val)))
687                                     ((:uint32) `(length32 val))
688                                     ((:uint64) `(length64 val))
689                                     ((:sint32) `(length32 (zig-zag-encode32 val)))
690                                     ((:sint64) `(length64 (zig-zag-encode64 val)))
691                                     ((:fixed32 :sfixed32) `4)
692                                     ((:fixed64 :sfixed64) `8)
693                                     ((:bool)   `1)
694                                     ((:float)  `4)
695                                     ((:double) `8)))))))
696          (declare (type (unsigned-byte 32) len))
697          (values (i+ (length32 (the (unsigned-byte 32) ,tag)) (length32 len) len) len)))
698     form))
699
700 (defun enum-size (val values tag)
701   "Returns the size in bytes that the enum object will take when serialized."
702   (declare (type (unsigned-byte 32) tag))
703   (let ((idx (let ((e (find val values :key #'proto-value)))
704                (and e (proto-index e)))))
705     (assert idx () "There is no enum value for ~S" val)
706     (i+ (length32 tag) (length32 (ldb (byte 32 0) idx)))))
707
708 \f
709 ;;; Wire-level encoders
710 ;;; These are called at the lowest level, so arg types are assumed to be correct
711
712 (defun encode-uint32 (val buffer index)
713   "Encodes the unsigned 32-bit integer 'val' as a varint into the buffer
714    at the given index.
715    Modifies the buffer, and returns the new index into the buffer.
716    Watch out, this function turns off all type checking and array bounds checking."
717   (declare (optimize (speed 3) (safety 0) (debug 0)))
718   (declare (type (unsigned-byte 32) val)
719            (type (simple-array (unsigned-byte 8)) buffer)
720            (type fixnum index))
721   ;; Seven bits at a time, least significant bits first
722   (loop do (let ((bits (ildb (byte 7 0) val)))
723              (declare (type (unsigned-byte 8) bits))
724              (setq val (iash val -7))
725              (setf (aref buffer index) (ilogior bits (if (i= val 0) 0 128)))
726              (iincf index))
727         until (i= val 0))
728   (values index buffer))                        ;return the buffer to improve 'trace'
729
730 (defun encode-uint64 (val buffer index)
731   "Encodes the unsigned 64-bit integer 'val' as a varint into the buffer
732    at the given index.
733    Modifies the buffer, and returns the new index into the buffer.
734    Watch out, this function turns off all type checking and array bounds checking."
735   (declare (optimize (speed 3) (safety 0) (debug 0)))
736   (declare (type (unsigned-byte 64) val)
737            (type (simple-array (unsigned-byte 8)) buffer)
738            (type fixnum index))
739   (loop do (let ((bits (ldb (byte 7 0) val)))
740              (declare (type (unsigned-byte 8) bits))
741              (setq val (ash val -7))
742              (setf (aref buffer index) (ilogior bits (if (zerop val) 0 128)))
743              (iincf index))
744         until (zerop val))
745   (values index buffer))
746
747 (defun encode-fixed32 (val buffer index)
748   "Encodes the unsigned 32-bit integer 'val' as a fixed int into the buffer
749    at the given index.
750    Modifies the buffer, and returns the new index into the buffer.
751    Watch out, this function turns off all type checking and array bounds checking."
752   (declare (optimize (speed 3) (safety 0) (debug 0)))
753   (declare (type (unsigned-byte 32) val)
754            (type (simple-array (unsigned-byte 8)) buffer)
755            (type fixnum index))
756   (loop repeat 4 doing
757     (let ((byte (ildb (byte 8 0) val)))
758       (declare (type (unsigned-byte 8) byte))
759       (setq val (iash val -8))
760       (setf (aref buffer index) byte)
761       (iincf index)))
762   (values index buffer))
763
764 (defun encode-fixed64 (val buffer index)
765   "Encodes the unsigned 64-bit integer 'val' as a fixed int into the buffer
766    at the given index.
767    Modifies the buffer, and returns the new index into the buffer.
768    Watch out, this function turns off all type checking and array bounds checking."
769   (declare (optimize (speed 3) (safety 0) (debug 0)))
770   (declare (type (unsigned-byte 64) val)
771            (type (simple-array (unsigned-byte 8)) buffer)
772            (type fixnum index))
773   (loop repeat 8 doing
774     (let ((byte (ldb (byte 8 0) val)))
775       (declare (type (unsigned-byte 8) byte))
776       (setq val (ash val -8))
777       (setf (aref buffer index) byte)
778       (iincf index)))
779   (values index buffer))
780
781 (defun encode-sfixed32 (val buffer index)
782   "Encodes the signed 32-bit integer 'val' as a fixed int into the buffer
783    at the given index.
784    Modifies the buffer, and returns the new index into the buffer.
785    Watch out, this function turns off all type checking and array bounds checking."
786   (declare (optimize (speed 3) (safety 0) (debug 0)))
787   (declare (type (signed-byte 32) val)
788            (type (simple-array (unsigned-byte 8)) buffer)
789            (type fixnum index))
790   (loop repeat 4 doing
791     (let ((byte (ildb (byte 8 0) val)))
792       (declare (type (unsigned-byte 8) byte))
793       (setq val (iash val -8))
794       (setf (aref buffer index) byte)
795       (iincf index)))
796   (values index buffer))
797
798 (defun encode-sfixed64 (val buffer index)
799   "Encodes the signed 64-bit integer 'val' as a fixed int into the buffer
800    at the given index.
801    Modifies the buffer, and returns the new index into the buffer.
802    Watch out, this function turns off all type checking and array bounds checking."
803   (declare (optimize (speed 3) (safety 0) (debug 0)))
804   (declare (type (signed-byte 64) val)
805            (type (simple-array (unsigned-byte 8)) buffer)
806            (type fixnum index))
807   (loop repeat 8 doing
808     (let ((byte (ldb (byte 8 0) val)))
809       (declare (type (unsigned-byte 8) byte))
810       (setq val (ash val -8))
811       (setf (aref buffer index) byte)
812       (iincf index)))
813   (values index buffer))
814
815 (defun encode-single (val buffer index)
816   "Encodes the single float 'val' into the buffer at the given index.
817    Modifies the buffer, and returns the new index into the buffer.
818    Watch out, this function turns off all type checking and array bounds checking."
819   (declare (optimize (speed 3) (safety 0) (debug 0)))
820   (declare (type single-float val)
821            (type (simple-array (unsigned-byte 8)) buffer)
822            (type fixnum index))
823   (let ((bits (single-float-bits val)))
824     (loop repeat 4 doing
825       (let ((byte (ldb (byte 8 0) bits)))
826         (declare (type (unsigned-byte 8) byte))
827         (setq bits (ash bits -8))
828         (setf (aref buffer index) byte)
829         (iincf index))))
830   (values index buffer))
831
832 (defun encode-double (val buffer index)
833   "Encodes the double float 'val' into the buffer at the given index.
834    Modifies the buffer, and returns the new index into the buffer.
835    Watch out, this function turns off all type checking and array bounds checking."
836   (declare (optimize (speed 3) (safety 0) (debug 0)))
837   (declare (type double-float val)
838            (type (simple-array (unsigned-byte 8)) buffer)
839            (type fixnum index))
840   (multiple-value-bind (low high)
841       (double-float-bits val)
842     (loop repeat 4 doing
843       (let ((byte (ldb (byte 8 0) low)))
844         (declare (type (unsigned-byte 8) byte))
845         (setq low (ash low -8))
846         (setf (aref buffer index) byte)
847         (iincf index)))
848     (loop repeat 4 doing
849       (let ((byte (ldb (byte 8 0) high)))
850         (declare (type (unsigned-byte 8) byte))
851         (setq high (ash high -8))
852         (setf (aref buffer index) byte)
853         (iincf index))))
854   (values index buffer))
855
856 (defun encode-string (string buffer index)
857   "Encodes the octets into the buffer at the given index.
858    Modifies the buffer, and returns the new index into the buffer.
859    Watch out, this function turns off all type checking and array bounds checking."
860   (declare (optimize (speed 3) (safety 0) (debug 0)))
861   (declare (type (simple-array (unsigned-byte 8)) buffer)
862            (type fixnum index))
863   (let* ((octets (babel:string-to-octets string :encoding :utf-8))
864          (len (length octets))
865          (idx (encode-uint32 len buffer index)))
866     (declare (type fixnum len)
867              (type (unsigned-byte 32) idx))
868     (replace buffer octets :start1 idx)
869     (values (i+ idx len) buffer)))
870
871 (defun encode-octets (octets buffer index)
872   "Encodes the octets into the buffer at the given index.
873    Modifies the buffer, and returns the new index into the buffer.
874    Watch out, this function turns off all type checking and array bounds checking."
875   (declare (optimize (speed 3) (safety 0) (debug 0)))
876   (declare (type (simple-array (unsigned-byte 8)) buffer)
877            (type fixnum index))
878   (let* ((len (length octets))
879          (idx (encode-uint32 len buffer index)))
880     (declare (type fixnum len)
881              (type (unsigned-byte 32) idx))
882     (replace buffer octets :start1 idx)
883     (values (i+ idx len) buffer)))
884
885
886 ;;; Wire-level decoders
887 ;;; These are called at the lowest level, so arg types are assumed to be correct
888
889 ;; Decode the value from the buffer at the given index,
890 ;; then return the value and new index into the buffer
891 (defun decode-uint32 (buffer index)
892   "Decodes the next 32-bit varint integer in the buffer at the given index.
893    Returns both the decoded value and the new index into the buffer.
894    Watch out, this function turns off all type checking and array bounds checking."
895   (declare (optimize (speed 3) (safety 0) (debug 0)))
896   (declare (type (simple-array (unsigned-byte 8)) buffer)
897            (type fixnum index))
898   ;; Seven bits at a time, least significant bits first
899   (loop with val fixnum = 0
900         for places fixnum upfrom 0 by 7
901         for byte fixnum = (prog1 (aref buffer index) (iincf index))
902         do (setq val (ilogior val (iash (ildb (byte 7 0) byte) places)))
903         until (i< byte 128)
904         finally (progn
905                   (assert (< val #.(ash 1 32)) ()
906                           "The value ~D is longer than 32 bits" val)
907                   (return (values val index)))))
908
909 (defun decode-uint64 (buffer index)
910   "Decodes the next 64-bit varint integer 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 all type checking and array bounds checking."
913   (declare (optimize (speed 3) (safety 0) (debug 0)))
914   (declare (type (simple-array (unsigned-byte 8)) buffer)
915            (type fixnum index))
916   ;; Seven bits at a time, least significant bits first
917   (loop with val = 0
918         for places fixnum upfrom 0 by 7
919         for byte fixnum = (prog1 (aref buffer index) (iincf index))
920         do (setq val (logior val (ash (ildb (byte 7 0) byte) places)))
921         until (i< byte 128)
922         finally (return (values val index))))
923
924 (defun decode-int32 (buffer index)
925   "Decodes the next 32-bit varint integer in the buffer at the given index.
926    Returns both the decoded value and the new index into the buffer.
927    Watch out, this function turns off all type checking and array bounds checking."
928   (declare (optimize (speed 3) (safety 0) (debug 0)))
929   (declare (type (simple-array (unsigned-byte 8)) buffer)
930            (type fixnum index))
931   (multiple-value-bind (val index)
932       (decode-uint32 buffer index)
933     (declare (type fixnum val))
934     (when (i= (ildb (byte 1 31) val) 1)
935       (idecf val #.(ash 1 32)))
936     (values val index)))
937
938 (defun decode-int64 (buffer index)
939   "Decodes the next 64-bit varint integer in the buffer at the given index.
940    Returns both the decoded value and the new index into the buffer.
941    Watch out, this function turns off all type checking and array bounds checking."
942   (declare (optimize (speed 3) (safety 0) (debug 0)))
943   (declare (type (simple-array (unsigned-byte 8)) buffer)
944            (type fixnum index))
945   (multiple-value-bind (val index)
946       (decode-uint64 buffer index)
947     (when (i= (ldb (byte 1 63 ) val) 1)
948       (decf val #.(ash 1 64)))
949     (values val index)))
950
951 (defun decode-fixed32 (buffer index)
952   "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
953    Returns both the decoded value and the new index into the buffer.
954    Watch out, this function turns off all type checking and array bounds checking."
955   (declare (optimize (speed 3) (safety 0) (debug 0)))
956   (declare (type (simple-array (unsigned-byte 8)) buffer)
957            (type fixnum index))
958   ;; Eight bits at a time, least significant bits first
959   (let ((val 0))
960     (declare (type fixnum val))
961     (loop repeat 4
962           for places fixnum upfrom 0 by 8
963           for byte fixnum = (prog1 (aref buffer index) (iincf index))
964           do (setq val (ilogior val (iash byte places))))
965     (values val index)))
966
967 (defun decode-sfixed32 (buffer index)
968   "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
969    Returns both the decoded value and the new index into the buffer.
970    Watch out, this function turns off all type checking and array bounds checking."
971   (declare (optimize (speed 3) (safety 0) (debug 0)))
972   (declare (type (simple-array (unsigned-byte 8)) buffer)
973            (type fixnum index))
974   ;; Eight bits at a time, least significant bits first
975   (let ((val 0))
976     (declare (type fixnum val))
977     (loop repeat 4
978           for places fixnum upfrom 0 by 8
979           for byte fixnum = (prog1 (aref buffer index) (iincf index))
980           do (setq val (ilogior val (iash byte places))))
981     (when (i= (ldb (byte 1 31) val) 1)              ;sign bit set, so negative value
982       (decf val #.(ash 1 32)))
983     (values val index)))
984
985 (defun decode-fixed64 (buffer index)
986   "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
987    Returns both the decoded value and the new index into the buffer.
988    Watch out, this function turns off all type checking and array bounds checking."
989   (declare (optimize (speed 3) (safety 0) (debug 0)))
990   (declare (type (simple-array (unsigned-byte 8)) buffer)
991            (type fixnum index))
992   ;; Eight bits at a time, least significant bits first
993   (let ((val 0))
994     (loop repeat 8
995           for places fixnum upfrom 0 by 8
996           for byte fixnum = (prog1 (aref buffer index) (iincf index))
997           do (setq val (logior val (ash byte places))))
998     (values val index)))
999
1000 (defun decode-sfixed64 (buffer index)
1001   "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
1002    Returns both the decoded value and the new index into the buffer.
1003    Watch out, this function turns off all type checking and array bounds checking."
1004   (declare (optimize (speed 3) (safety 0) (debug 0)))
1005   (declare (type (simple-array (unsigned-byte 8)) buffer)
1006            (type fixnum index))
1007   ;; Eight bits at a time, least significant bits first
1008   (let ((val 0))
1009     (loop repeat 8
1010           for places fixnum upfrom 0 by 8
1011           for byte fixnum = (prog1 (aref buffer index) (iincf index))
1012           do (setq val (logior val (ash byte places))))
1013     (when (i= (ldb (byte 1 63) val) 1)             ;sign bit set, so negative value
1014       (decf val #.(ash 1 64)))
1015     (values val index)))
1016
1017 (defun decode-single (buffer index)
1018   "Decodes the next single float in the buffer at the given index.
1019    Returns both the decoded value and the new index into the buffer.
1020    Watch out, this function turns off all type checking and array bounds checking."
1021   (declare (optimize (speed 3) (safety 0) (debug 0)))
1022   (declare (type (simple-array (unsigned-byte 8)) buffer)
1023            (type fixnum index))
1024   ;; Eight bits at a time, least significant bits first
1025   (let ((bits 0))
1026     (loop repeat 4
1027           for places fixnum upfrom 0 by 8
1028           for byte fixnum = (prog1 (aref buffer index) (iincf index))
1029           do (setq bits (logior bits (ash byte places))))
1030     (when (i= (ldb (byte 1 31) bits) 1)             ;sign bit set, so negative value
1031       (decf bits #.(ash 1 32)))
1032     (values (make-single-float bits) index)))
1033
1034 (defun decode-double (buffer index)
1035   "Decodes the next double float in the buffer at the given index.
1036    Returns both the decoded value and the new index into the buffer.
1037    Watch out, this function turns off all type checking and array bounds checking."
1038   (declare (optimize (speed 3) (safety 0) (debug 0)))
1039   (declare (type (simple-array (unsigned-byte 8)) buffer)
1040            (type fixnum index))
1041   ;; Eight bits at a time, least significant bits first
1042   (let ((low  0)
1043         (high 0))
1044     (loop repeat 4
1045           for places fixnum upfrom 0 by 8
1046           for byte fixnum = (prog1 (aref buffer index) (iincf index))
1047           do (setq low (logior low (ash byte places))))
1048     (loop repeat 4
1049           for places fixnum upfrom 0 by 8
1050           for byte fixnum = (prog1 (aref buffer index) (iincf index))
1051           do (setq high (logior high (ash byte places))))
1052     ;; High bits are signed, but low bits are unsigned
1053     (when (i= (ldb (byte 1 31) high) 1)             ;sign bit set, so negative value
1054       (decf high #.(ash 1 32)))
1055     (values (make-double-float low high) index)))
1056
1057 (defun decode-string (buffer index)
1058   "Decodes the next UTF-8 encoded string in the buffer at the given index.
1059    Returns both the decoded string and the new index into the buffer.
1060    Watch out, this function turns off all type checking and array bounds checking."
1061   (declare (optimize (speed 3) (safety 0) (debug 0)))
1062   (declare (type (simple-array (unsigned-byte 8)) buffer)
1063            (type fixnum index))
1064   (multiple-value-bind (len idx)
1065       (decode-uint32 buffer index)
1066     (declare (type (unsigned-byte 32) len)
1067              (type fixnum idx))
1068     (values (babel:octets-to-string buffer :start idx :end (i+ idx len) :encoding :utf-8) (i+ idx len))))
1069
1070 (defun decode-octets (buffer index)
1071   "Decodes the next octets in the buffer at the given index.
1072    Returns both the decoded value and the new index into the buffer.
1073    Watch out, this function turns off all type checking and array bounds checking."
1074   (declare (optimize (speed 3) (safety 0) (debug 0)))
1075   (declare (type (simple-array (unsigned-byte 8)) buffer)
1076            (type fixnum index))
1077   (multiple-value-bind (len idx)
1078       (decode-uint32 buffer index)
1079     (declare (type (unsigned-byte 32) len)
1080              (type fixnum idx))
1081     (values (subseq buffer idx (i+ idx len)) (i+ idx len))))
1082
1083
1084 ;;; Wire-level lengths
1085 ;;; These are called at the lowest level, so arg types are assumed to be correct
1086
1087 (defun length32 (val)
1088   "Returns the length that 'val' will take when encoded as a 32-bit integer."
1089   (declare (optimize (speed 3) (safety 0) (debug 0)))
1090   (declare (type (unsigned-byte 32) val))
1091   (let ((size 0))
1092     (declare (type fixnum size))
1093     (loop do (progn
1094                (setq val (iash val -7))
1095                (iincf size))
1096           until (i= val 0))
1097     size))
1098
1099 (defun length64 (val)
1100   "Returns the length that 'val' will take when encoded as a 64-bit integer."
1101   (declare (optimize (speed 3) (safety 0) (debug 0)))
1102   (declare (type (unsigned-byte 64) val))
1103   (let ((size 0))
1104     (declare (type fixnum size))
1105     (loop do (progn
1106                (setq val (ash val -7))
1107                (iincf size))
1108           until (zerop val))
1109     size))
1110
1111
1112 ;;; Skipping elements
1113 ;;; This is called at the lowest level, so arg types are assumed to be correct
1114
1115 (defun skip-element (buffer index tag)
1116   "Skip an element in the buffer at the index of the given wire type.
1117    Returns the new index in the buffer.
1118    Watch out, this function turns off all type checking and all array bounds checking."
1119   (declare (optimize (speed 3) (safety 0) (debug 0)))
1120   (declare (type (simple-array (unsigned-byte 8)) buffer)
1121            (type fixnum index)
1122            (type (unsigned-byte 32) tag))
1123   (case (ilogand tag #x7)
1124     ((#.$wire-type-varint)
1125      (loop for byte fixnum = (prog1 (aref buffer index) (iincf index))
1126            until (i< byte 128))
1127      index)
1128     ((#.$wire-type-string)
1129      (multiple-value-bind (len idx)
1130          (decode-uint32 buffer index)
1131        (declare (type (unsigned-byte 32) len)
1132                 (type fixnum idx))
1133        (i+ idx len)))
1134     ((#.$wire-type-32bit)
1135      (i+ index 4))
1136     ((#.$wire-type-64bit)
1137      (i+ index 8))
1138     ((#.$wire-type-start-group)
1139      (loop (multiple-value-bind (new-tag idx)
1140                (decode-uint32 buffer index)
1141              (cond ((not (i= (ilogand new-tag #x7) $wire-type-end-group))
1142                     ;; If it's not the end of a group, skip the next element
1143                     (setq index (skip-element buffer idx new-tag)))
1144                    ;; If it's the end of the expected group, we're done
1145                    ((i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group))
1146                     (return idx))
1147                    (t
1148                     (assert (i= (i- tag $wire-type-start-group) (i- new-tag $wire-type-end-group)) ()
1149                             "Couldn't find a matching end group tag"))))))
1150     (t index)))