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 ;;; Protobuf serialization from Lisp objects
18 ;; Serialize the object using the given protobuf "schema"
19 (defun serialize-object-to-stream (object protobuf &key (stream *standard-output*) visited)
20 "Serializes the object 'object' as a protobuf object defined in the schema 'protobuf'
21 onto the stream 'stream' using the wire format.
22 'visited' is a hash table used to cache object sizes. If it is supplied, it will be
23 cleared before it is used; otherwise, a fresh table will be created.
24 The return value is the buffer containing the serialized object. If the stream is
25 nil, the buffer is not actually written to anywhere."
26 (let* ((visited (let ((v (or visited (make-hash-table))))
29 (size (object-size object protobuf :visited visited))
30 (buffer (make-array size :element-type '(unsigned-byte 8))))
31 (serialize-object object protobuf buffer 0 :visited visited)
33 (write-sequence buffer stream))
36 ;; Allow clients to add their own methods
37 ;; This is how we address the problem of cycles, e.g. -- if you have an object
38 ;; that may contain cycles, serialize the cyclic object using a "handle"
39 (defgeneric serialize-object (object protobuf buffer index &key visited)
41 "Serializes the object 'object' as a protobuf object defined in the schema 'protobuf'
42 into the byte array given by 'buffer' starting at the fixnum index 'index' using
44 'visited' is a hash table used to cache object sizes.
45 The return value is the buffer containing the serialized object."))
47 ;; 'visited' is used to cache object sizes
48 ;; If it's passed in explicitly, it is assumed to already have the sizes within it
49 ;; The default method uses meta-data from the protobuf "schema"
50 (defmethod serialize-object ((object standard-object) protobuf buffer index &key visited)
51 (declare (type (simple-array (unsigned-byte 8)) buffer)
53 (check-type protobuf (or protobuf protobuf-message))
54 (let* ((class (class-of object))
55 (message (find-message-for-class protobuf class))
56 (visited (or visited (make-hash-table))))
58 "There is no Protobuf message for the class ~S" class)
59 (macrolet ((read-slot (object slot reader)
60 ;; Don't do a boundp check, we assume the object is fully populated
61 ;; Unpopulated slots should be "nullable" and should contain nil
63 (funcall ,reader ,object)
64 (slot-value ,object ,slot))))
65 (labels ((do-field (object trace field)
66 ;; We don't do cycle detection here
67 ;; If the client needs it, he can define his own 'serialize-object'
68 ;; method to clean things up first
69 (let* ((cl (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
70 (slot (proto-value field))
71 (reader (proto-reader field))
73 (when (or slot reader)
74 (cond ((eq (proto-required field) :repeated)
75 (cond ((and (proto-packed field) (packed-type-p cl))
76 (let ((tag (make-tag cl (proto-index field))))
77 (setq index (serialize-packed (read-slot object slot reader)
78 cl tag buffer index))))
80 (let ((tag (make-tag cl (proto-index field))))
82 (setq index (serialize-prim v cl tag buffer index)))
83 (read-slot object slot reader))))
84 ((typep (setq msg (and cl (loop for p in trace
85 thereis (or (find-message-for-class p cl)
86 (find-enum-for-type p cl)))))
88 (dolist (v (if slot (read-slot object slot reader) (list object)))
89 ;; To serialize an embedded message, first say that it's
90 ;; a string, then encode its size, then serialize its fields
91 (let ((tag (make-tag $wire-type-string (proto-index field)))
92 (len (object-size v protobuf :visited visited)))
93 (setq index (encode-uint32 tag buffer index))
94 (setq index (encode-uint32 len buffer index)))
95 (map () (curry #'do-field v (cons msg trace))
97 ((typep msg 'protobuf-enum)
98 (let ((tag (make-tag $wire-type-varint (proto-index field))))
100 (setq index (serialize-enum v (proto-values msg) tag buffer index)))
101 (read-slot object slot reader))))))
104 (let ((v (read-slot object slot reader)))
105 (when (or v (eq cl :bool))
106 (let ((tag (make-tag cl (proto-index field))))
107 (setq index (serialize-prim v cl tag buffer index))))))
108 ((typep (setq msg (and cl (loop for p in trace
109 thereis (or (find-message-for-class p cl)
110 (find-enum-for-type p cl)))))
112 (let ((v (if slot (read-slot object slot reader) object)))
113 (let ((tag (make-tag $wire-type-string (proto-index field)))
114 (len (object-size v protobuf :visited visited)))
115 (setq index (encode-uint32 tag buffer index))
116 (setq index (encode-uint32 len buffer index)))
118 (map () (curry #'do-field v (cons msg trace))
119 (proto-fields msg)))))
120 ((typep msg 'protobuf-enum)
121 (let ((v (read-slot object slot reader)))
123 (let ((tag (make-tag $wire-type-varint (proto-index field))))
124 (setq index (serialize-enum v (proto-values msg) tag buffer index)))))))))))))
125 (declare (dynamic-extent #'do-field))
126 (map () (curry #'do-field object (list message protobuf)) (proto-fields message)))))
127 (values buffer index))
132 (defun deserialize-object-from-stream (class protobuf &key (stream *standard-input*))
133 "Deserializes an object of the give class 'class' as a protobuf object defined
134 in the schema 'protobuf' from the stream 'stream' using the wire format.
135 The return value is the object."
136 (let* ((size (file-length stream))
137 (buffer (make-array size :element-type '(unsigned-byte 8))))
138 (read-sequence buffer stream)
139 (deserialize-object class protobuf buffer 0)))
141 ;; Allow clients to add their own methods
142 ;; This is you might preserve object identity, e.g.
143 (defgeneric deserialize-object (class protobuf buffer &optional index length)
145 "Deserializes an object of the given class 'class' as a protobuf object defined
146 in the schema 'protobuf' from the byte array given by 'buffer' starting at
147 the fixnum index 'index' up to the length of the buffer, given by 'length'.
148 The return value is the object."))
150 ;; The default method uses meta-data from the protobuf "schema"
151 ;; Note that 'class' is the Lisp name of the Protobufs message (class)
152 ;; It is not the name of any overriding class ('proto-class-override')
153 (defmethod deserialize-object ((class symbol) protobuf buffer &optional (index 0) length)
154 (declare (type (simple-array (unsigned-byte 8)) buffer)
156 (check-type protobuf (or protobuf protobuf-message))
157 (let ((length (or length (length buffer))))
158 (declare (type fixnum length))
159 (labels ((deserialize (class trace &optional (end length))
160 (declare (type fixnum end))
161 (let* ((message (loop for p in trace
162 thereis (or (find-message-for-class p class)
163 (find-enum-for-type p class))))
164 (object (make-instance (or (proto-class-override message) class))))
165 (assert (eql (proto-class message) class) ()
166 "The class in message ~S does not match the Lisp class ~S"
167 (proto-class message) class)
169 "There is no Protobuf message for the class ~S" class)
172 (return-from deserialize (values object index)))
173 (multiple-value-bind (val idx)
174 (decode-uint32 buffer index)
176 (let* ((type (ilogand val #x7))
177 (fld (ilogand (iash val -3) #x1FFFFFFF))
178 (field (find fld (proto-fields message) :key #'proto-index))
179 (cl (and field (if (eq (proto-class field) 'boolean) :bool (proto-class field))))
180 ;; It's OK for this to be null
181 ;; That means we're parsing some version of a message
182 ;; that has the field, but our current message does not
183 ;; We still have to deserialize everything, though
184 (slot (and field (proto-value field)))
187 ;; If there's no field descriptor for this index,
188 ;; just skip the next element in the buffer
189 (setq index (skip-element buffer index type))
190 ;;--- Check for mismatched types, running past end of buffer, etc
191 (cond ((and field (eq (proto-required field) :repeated))
192 (cond ((and (proto-packed field) (packed-type-p cl))
193 (multiple-value-bind (values idx)
194 (deserialize-packed cl buffer index)
197 (setf (slot-value object slot) values))))
199 (multiple-value-bind (val idx)
200 (deserialize-prim cl buffer index)
203 (setf (slot-value object slot)
204 (nconc (slot-value object slot) (list val))))))
205 ((typep (setq msg (and cl (or (find-message-for-class protobuf cl)
206 (find-enum-for-type protobuf cl))))
208 (multiple-value-bind (len idx)
209 (decode-uint32 buffer index)
211 (let ((obj (deserialize cl (cons msg trace) (+ index len))))
213 (setf (slot-value object slot)
214 (nconc (slot-value object slot) (list obj)))))))
215 ((typep msg 'protobuf-enum)
216 (multiple-value-bind (val idx)
217 (deserialize-enum (proto-values msg) buffer index)
220 (setf (slot-value object slot)
221 (nconc (slot-value object slot) (list val))))))))
224 (multiple-value-bind (val idx)
225 (deserialize-prim cl buffer index)
228 (setf (slot-value object slot) val))))
229 ((typep (setq msg (and cl (or (find-message-for-class protobuf cl)
230 (find-enum-for-type protobuf cl))))
232 (multiple-value-bind (len idx)
233 (decode-uint32 buffer index)
235 (let ((obj (deserialize cl (cons msg trace) (+ index len))))
237 (setf (slot-value object slot) obj)))))
238 ((typep msg 'protobuf-enum)
239 (multiple-value-bind (val idx)
240 (deserialize-enum (proto-values msg) buffer index)
243 (setf (slot-value object slot) val))))))))))))))
244 (declare (dynamic-extent #'deserialize))
245 (deserialize class (list protobuf)))))
250 ;; Allow clients to add their own methods
251 ;; This is how we address the problem of cycles, e.g. -- if you have an object
252 ;; that may contain cycles, return the size of the "handle" to the object
253 (defgeneric object-size (object protobuf &key visited)
255 "Computes the size in bytes of the object 'object' defined in the schema 'protobuf'.
256 'visited' is a hash table used to cache object sizes.
257 The return value is the size of the object in bytes."))
259 ;; 'visited' is used to cache object sizes
260 ;; The default method uses meta-data from the protobuf "schema"
261 (defmethod object-size ((object standard-object) protobuf &key visited)
262 (check-type protobuf (or protobuf protobuf-message))
263 (let ((size (and visited (gethash object visited))))
265 (return-from object-size size)))
266 (let* ((class (class-of object))
267 (message (find-message-for-class protobuf class))
269 (declare (type fixnum size))
271 "There is no Protobuf message for the class ~S" class)
272 (macrolet ((read-slot (object slot reader)
273 ;; Don't do a boundp check, we assume the object is fully populated
274 ;; Unpopulated slots should be "nullable" and should contain nil
276 (funcall ,reader ,object)
277 (slot-value ,object ,slot))))
278 (labels ((do-field (object trace field)
279 ;; We don't do cycle detection here
280 ;; If the client needs it, he can define his own 'object-size'
281 ;; method to clean things up first
282 (let* ((cl (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
283 (slot (proto-value field))
284 (reader (proto-reader field))
286 (when (or slot reader)
287 (cond ((eq (proto-required field) :repeated)
288 (cond ((and (proto-packed field) (packed-type-p cl))
289 (let ((tag (make-tag cl (proto-index field))))
290 (iincf size (packed-size (read-slot object slot reader) cl tag))))
292 (let ((tag (make-tag cl (proto-index field))))
293 (map () #'(lambda (v)
294 (iincf size (prim-size v cl tag)))
295 (read-slot object slot reader))))
296 ((typep (setq msg (and cl (loop for p in trace
297 thereis (or (find-message-for-class p cl)
298 (find-enum-for-type p cl)))))
300 (dolist (v (if slot (read-slot object slot reader) (list object)))
301 (let ((tag (make-tag $wire-type-string (proto-index field)))
302 (len (object-size v protobuf :visited visited)))
303 (iincf size (length32 tag))
304 (iincf size (length32 len)))
305 (map () (curry #'do-field v (cons msg trace))
306 (proto-fields msg))))
307 ((typep msg 'protobuf-enum)
308 (let ((tag (make-tag $wire-type-varint (proto-index field))))
309 (map () #'(lambda (v)
310 (iincf size (enum-size v (proto-values msg) tag)))
311 (read-slot object slot reader))))))
314 (let ((v (read-slot object slot reader)))
315 (when (or v (eq cl :bool))
316 (let ((tag (make-tag cl (proto-index field))))
317 (iincf size (prim-size v cl tag))))))
318 ((typep (setq msg (and cl (loop for p in trace
319 thereis (or (find-message-for-class p cl)
320 (find-enum-for-type p cl)))))
322 (let ((v (if slot (read-slot object slot reader) object)))
324 (let ((tag (make-tag $wire-type-string (proto-index field)))
325 (len (object-size v protobuf :visited visited)))
326 (iincf size (length32 tag))
327 (iincf size (length32 len)))
328 (map () (curry #'do-field v (cons msg trace))
329 (proto-fields msg)))))
330 ((typep msg 'protobuf-enum)
331 (let ((v (read-slot object slot reader)))
333 (let ((tag (make-tag $wire-type-varint (proto-index field))))
334 (iincf size (enum-size (read-slot object slot reader) (proto-values msg) tag)))))))))))))
335 (declare (dynamic-extent #'do-field))
336 (map () (curry #'do-field object (list message protobuf)) (proto-fields message))
338 (setf (gethash object visited) size)) ;cache the size
342 ;;; Compile-time generation of serializers
344 (defun generate-serializer (protobuf message)
345 "Generate a 'serialize-object' method for the given message."
346 (with-gensyms (vobj vproto vbuf vidx vval)
347 (with-collectors ((serializers collect-serializer))
348 (dolist (field (proto-fields message))
349 (let* ((class (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
350 (msg (and class (not (keywordp class))
351 (or (or (find-message-for-class message class)
352 (find-enum-for-type message class))
353 (or (find-message-for-class protobuf class)
354 (find-enum-for-type protobuf class)))))
355 (reader (cond ((proto-reader field)
356 `(,(proto-reader field) ,vobj))
358 `(slot-value ,vobj ',(proto-value field)))))
359 (index (proto-index field)))
361 (cond ((eq (proto-required field) :repeated)
362 (cond ((and (proto-packed field) (packed-type-p class))
364 (let ((tag (make-tag class index)))
365 `(setq ,vidx (serialize-packed ,reader ,class ,tag ,vbuf ,vidx)))))
368 (let ((tag (make-tag class index)))
369 `(dolist (,vval ,reader)
370 (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))
371 ((typep msg 'protobuf-message)
373 (let ((tag (make-tag $wire-type-string index)))
374 `(dolist (,vval ,reader)
375 (let ((len (or (and visited (gethash ,vval visited))
376 (object-size ,vval ,vproto :visited visited))))
377 (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
378 (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
379 (serialize-object ,vval ,vproto ,vbuf ,vidx :visited visited)
380 (iincf ,vidx len))))))
381 ((typep msg 'protobuf-enum)
383 (let ((tag (make-tag $wire-type-varint index)))
384 `(dolist (,vval ,reader)
385 (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx))))))))
387 (cond ((keywordp class)
389 (let ((tag (make-tag class index)))
391 `(let ((,vval ,reader))
392 (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))
393 `(let ((,vval ,reader))
395 (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))
396 ((typep msg 'protobuf-message)
398 (let ((tag (make-tag $wire-type-string index)))
399 `(let ((,vval ,reader))
401 (let ((len (or (and visited (gethash ,vval visited))
402 (object-size ,vval ,vproto :visited visited))))
403 (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
404 (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
405 (serialize-object ,vval ,vproto ,vbuf ,vidx :visited visited)
406 (iincf ,vidx len)))))))
407 ((typep msg 'protobuf-enum)
409 (let ((tag (make-tag $wire-type-varint index)))
410 `(let ((,vval ,reader))
412 (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))))))))
413 `(defmethod serialize-object ((,vobj ,(proto-class message)) ,vproto ,vbuf ,vidx &key visited)
414 (declare (ignorable visited)
415 (type (simple-array (unsigned-byte 8)) ,vbuf)
417 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
419 (values ,vbuf ,vidx))))))
421 (defun generate-deserializer (protobuf message)
422 "Generate a 'deserialize-object' method for the given message."
423 (with-gensyms (vclass vproto vbuf vidx vlen vobj vval)
424 (with-collectors ((deserializers collect-deserializer))
425 (dolist (field (proto-fields message))
426 (let* ((class (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
427 (msg (and class (not (keywordp class))
428 (or (or (find-message-for-class message class)
429 (find-enum-for-type message class))
430 (or (find-message-for-class protobuf class)
431 (find-enum-for-type protobuf class)))))
432 (slot (proto-value field))
433 (index (proto-index field)))
434 (cond ((eq (proto-required field) :repeated)
435 (cond ((and (proto-packed field) (packed-type-p class))
436 (collect-deserializer
437 `((,(make-tag class index))
438 (multiple-value-bind (,vval idx)
439 (deserialize-packed ,class ,vbuf ,vidx)
442 `(setf (slot-value ,vobj ',slot) ,vval))))))
444 (collect-deserializer
445 `((,(make-tag class index))
446 (multiple-value-bind (,vval idx)
447 (deserialize-prim ,class ,vbuf ,vidx)
450 `(setf (slot-value ,vobj ',slot)
451 (nconc (slot-value ,vobj ',slot) (list ,vval))))))))
452 ((typep msg 'protobuf-message)
453 (collect-deserializer
454 `((,(make-tag $wire-type-string index))
455 (multiple-value-bind (len idx)
456 (decode-uint32 ,vbuf ,vidx)
458 (multiple-value-bind (,vval idx)
459 (deserialize-object ',class ,vproto ,vbuf ,vidx (i+ ,vidx len))
462 `(setf (slot-value ,vobj ',slot)
463 (nconc (slot-value ,vobj ',slot) (list ,vval)))))))))
464 ((typep msg 'protobuf-enum)
465 (collect-deserializer
466 `((,(make-tag $wire-type-varint index))
467 (multiple-value-bind (,vval idx)
468 (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
471 `(setf (slot-value ,vobj ',slot)
472 (nconc (slot-value ,vobj ',slot) (list ,vval))))))))))
474 (cond ((keywordp class)
475 (collect-deserializer
476 `((,(make-tag class index))
477 (multiple-value-bind (,vval idx)
478 (deserialize-prim ,class ,vbuf ,vidx)
481 `(setf (slot-value ,vobj ',slot) ,vval))))))
482 ((typep msg 'protobuf-message)
483 (collect-deserializer
484 `((,(make-tag $wire-type-string index))
485 (multiple-value-bind (len idx)
486 (decode-uint32 ,vbuf ,vidx)
488 (multiple-value-bind (,vval idx)
489 (deserialize-object ',class ,vproto ,vbuf ,vidx (i+ ,vidx len))
492 `(setf (slot-value ,vobj ',slot) ,vval)))))))
493 ((typep msg 'protobuf-enum)
494 (collect-deserializer
495 `((,(make-tag $wire-type-varint index))
496 (multiple-value-bind (,vval idx)
497 (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
500 `(setf (slot-value ,vobj ',slot) ,vval)))))))))))
501 `(defmethod deserialize-object ((,vclass (eql ',(proto-class message))) ,vproto ,vbuf
502 &optional (,vidx 0) ,vlen)
503 (declare (type (simple-array (unsigned-byte 8)) ,vbuf)
505 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
506 (let ((,vlen (or ,vlen (length ,vbuf))))
507 (declare (type fixnum ,vlen))
508 (let ((,vobj (make-instance ',(or (proto-class-override message) (proto-class message)))))
510 (when (>= ,vidx ,vlen)
511 (return-from deserialize-object (values ,vobj ,vidx)))
512 (multiple-value-bind (tag idx)
513 (decode-uint32 ,vbuf ,vidx)
518 (setq ,vidx (skip-element ,vbuf ,vidx (ilogand tag #x7))))))))))))))
520 (defun generate-object-size (protobuf message)
521 "Generate an 'object-size' method for the given message."
522 (with-gensyms (vobj vproto vsize vval)
523 (with-collectors ((sizers collect-sizer))
524 (dolist (field (proto-fields message))
525 (let* ((class (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
526 (msg (and class (not (keywordp class))
527 (or (or (find-message-for-class message class)
528 (find-enum-for-type message class))
529 (or (find-message-for-class protobuf class)
530 (find-enum-for-type protobuf class)))))
531 (reader (cond ((proto-reader field)
532 `(,(proto-reader field) ,vobj))
534 `(slot-value ,vobj ',(proto-value field)))))
535 (index (proto-index field)))
537 (cond ((eq (proto-required field) :repeated)
538 (cond ((and (proto-packed field) (packed-type-p class))
540 (let ((tag (make-tag class index)))
541 `(iincf ,vsize (packed-size ,reader ,class ,tag)))))
544 (let ((tag (make-tag class index)))
545 `(dolist (,vval ,reader)
546 (iincf ,vsize (prim-size ,vval ,class ,tag))))))
547 ((typep msg 'protobuf-message)
549 (let ((tag (make-tag $wire-type-string index)))
550 `(dolist (,vval ,reader)
551 (let ((len (or (and visited (gethash ,vval visited))
552 (object-size ,vval ,vproto :visited visited))))
553 (iincf ,vsize (length32 ,tag))
554 (iincf ,vsize (length32 len))
555 (iincf ,vsize len))))))
556 ((typep msg 'protobuf-enum)
557 (let ((tag (make-tag $wire-type-varint index)))
559 `(dolist (,vval ,reader)
560 (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag))))))))
562 (cond ((keywordp class)
563 (let ((tag (make-tag class index)))
566 `(let ((,vval ,reader))
567 (iincf ,vsize (prim-size ,vval ,class ,tag)))
568 `(let ((,vval ,reader))
570 (iincf ,vsize (prim-size ,vval ,class ,tag))))))))
571 ((typep msg 'protobuf-message)
573 (let ((tag (make-tag $wire-type-string index)))
574 `(let ((,vval ,reader))
576 (let ((len (or (and visited (gethash ,vval visited))
577 (object-size ,vval ,vproto :visited visited))))
578 (iincf ,vsize (length32 ,tag))
579 (iincf ,vsize (length32 len))
580 (iincf ,vsize len)))))))
581 ((typep msg 'protobuf-enum)
582 (let ((tag (make-tag $wire-type-varint index)))
584 `(let ((,vval ,reader))
586 (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))))))))
587 `(defmethod object-size ((,vobj ,(proto-class message)) ,vproto &key visited)
588 (declare (ignorable visited))
589 (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
590 (let ((,vsize (and visited (gethash ,vobj visited))))
592 (return-from object-size ,vsize)))
594 (declare (type fixnum ,vsize))
597 (setf (gethash ,vobj visited) ,vsize))