]> asedeno.scripts.mit.edu Git - cl-protobufs.git/commitdiff
After confirming my suspicions with Robert Brown,
authorScott McKay <swm@google.com>
Thu, 15 Mar 2012 22:45:41 +0000 (22:45 +0000)
committerScott McKay <swm@google.com>
Thu, 15 Mar 2012 22:45:41 +0000 (22:45 +0000)
fix the wire protocol to (de)serialize fixed integers
with a fixed nnumber of bytes. These don't get used
much, so this went unnoticed even by his test suite.

git-svn-id: http://svn.internal.itasoftware.com/svn/ita/branches/qres/swm/borgify-1/qres/lisp/quux/protobufs@534383 f8382938-511b-0410-9cdd-bb47b084005c

proto-pkgdcl.lisp
wire-format.lisp

index 5622a66e54be36b646342e56a9f08a37f45e00db..45201393b441e04316300d586a720ca912468e44 100644 (file)
    ;; Raw encoding and decoding
    "ENCODE-UINT32"
    "ENCODE-UINT64"
+   "ENCODE-FIXED32"
+   "ENCODE-FIXED64"
    "ENCODE-SINGLE"
    "ENCODE-DOUBLE"
    "ENCODE-OCTETS"
    "ZIG-ZAG-ENCODE64"
    "DECODE-UINT32"
    "DECODE-UINT64"
+   "DECODE-FIXED32"
+   "DECODE-FIXED64"
    "DECODE-SINGLE"
    "DECODE-DOUBLE"
    "DECODE-OCTETS"
index a202485db0e4b550b9bd576a136f09028449c80c..8cf269c0520101b08f28178b82e707273bb59bdb 100644 (file)
        (let* ((tag (ilogior $wire-type-32bit (iash (proto-index field) 3)))
               (idx (encode-uint32 tag buffer index)))
          (declare (type fixnum tag idx))
-         ;;---*** Shouldn't this always be writing 4 bytes?
-         (encode-uint32 val buffer idx)))
+         (encode-fixed32 val buffer idx)))
       ((:fixed64 :sfixed64)
        (let* ((tag (ilogior $wire-type-64bit (iash (proto-index field) 3)))
               (idx (encode-uint32 tag buffer index)))
          (declare (type fixnum tag idx))
-         ;;---*** Shouldn't this always be writing 8 bytes?
-         (encode-uint64 val buffer idx)))
+         (encode-fixed64 val buffer idx)))
       ((:string)
        (let* ((tag (ilogior $wire-type-string (iash (proto-index field) 3)))
               (idx (encode-uint32 tag buffer index)))
            (setq idx (encode-uint64 (zig-zag-encode64 val) buffer idx))))
         ((:fixed32 :sfixed32)
          (dolist (val values idx)
-           (setq idx (encode-uint32 val buffer idx))))
+           (setq idx (encode-fixed32 val buffer idx))))
         ((:fixed64 :sfixed64)
          (dolist (val values idx)
-           (setq idx (encode-uint64 val buffer idx))))
+           (setq idx (encode-fixed64 val buffer idx))))
         ((:float)
          (dolist (val values idx)
            (setq idx (encode-single val buffer idx))))
            (decode-uint64 buffer index)
          (values (zig-zag-decode64 val) idx)))
       ((:fixed32 :sfixed32)
-       ;;---*** Shouldn't this always be reading 4 bytes?
-       (decode-uint32 buffer index))
+       (decode-fixed32 buffer index))
       ((:fixed64 :sfixed64)
-       ;;---*** Shouldn't this always be reading 8 bytes?
-       (decode-uint64 buffer index))
+       (decode-fixed64 buffer index))
       ((:string)
        (multiple-value-bind (val idx)
            (decode-octets buffer index)
                        (decode-uint64 buffer idx)
                      (values (zig-zag-decode64 val) idx)))
                   ((:fixed32 :sfixed32)
-                   (decode-uint32 buffer idx))
+                   (decode-fixed32 buffer idx))
                   ((:fixed64 :sfixed64)
-                   (decode-uint64 buffer idx))
+                   (decode-fixed64 buffer idx))
                   ((:float)
                    (decode-single buffer idx))
                   ((:double)
 ;;; Raw encoders
 
 (defun encode-uint32 (val buffer index)
-  "Encodes the 32-bit integer 'val' into the buffer at the given index.
+  "Encodes the 32-bit integer 'val' as a varint into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer."
   (declare (type fixnum index)
            (type (simple-array (unsigned-byte 8)) buffer))
   (values index buffer))                        ;return the buffer to improve 'trace'
 
 (defun encode-uint64 (val buffer index)
-  "Encodes the 64-bit integer 'val' into the buffer at the given index.
+  "Encodes the 64-bit integer 'val' as a varint into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer."
   (declare (type fixnum index)
            (type (simple-array (unsigned-byte 8)) buffer))
           until (zerop val)))
   (values index buffer))
 
+(defun encode-fixed32 (val buffer index)
+  "Encodes the 32-bit integer 'val' as a fixed int into the buffer at the given index.
+   Modifies the buffer, and returns the new index into the buffer."
+  (declare (type fixnum index)
+           (type (simple-array (unsigned-byte 8)) buffer))
+  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    (loop repeat 4 doing
+      (let ((byte (ldb #.(byte 8 0) val)))
+        (declare (type fixnum byte))
+        (setq val (ash val -8))
+        (setf (aref buffer index) byte)
+        (iincf index))))
+  (values index buffer))
+
+(defun encode-fixed64 (val buffer index)
+  "Encodes the 64-bit integer 'val' as a fixed int into the buffer at the given index.
+   Modifies the buffer, and returns the new index into the buffer."
+  (declare (type fixnum index)
+           (type (simple-array (unsigned-byte 8)) buffer))
+  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    (loop repeat 8 doing
+      (let ((byte (ldb #.(byte 8 0) val)))
+        (declare (type fixnum byte))
+        (setq val (ash val -8))
+        (setf (aref buffer index) byte)
+        (iincf index))))
+  (values index buffer))
+
 (defun encode-single (val buffer index)
   "Encodes the single float 'val' into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer."
 ;; Decode the value from the buffer at the given index,
 ;; then return the value and new index into the buffer
 (defun decode-uint32 (buffer index)
-  "Decodes the next 32-bit integer in the buffer at the given index.
+  "Decodes the next 32-bit varint integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer."
   (declare (type fixnum index)
            (type (simple-array (unsigned-byte 8)) buffer))
                     (return (values val index))))))
 
 (defun decode-uint64 (buffer index)
-  "Decodes the next 64-bit integer in the buffer at the given index.
+  "Decodes the next 64-bit varint integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer."
   (declare (type fixnum index)
            (type (simple-array (unsigned-byte 8)) buffer))
           until (i< byte 128)
           finally (return (values val index)))))
 
+(defun decode-fixed32 (buffer index)
+  "Decodes the next 32-bit fixed integer in the buffer at the given index.
+   Returns both the decoded value and the new index into the buffer."
+  (declare (type fixnum index)
+           (type (simple-array (unsigned-byte 8)) buffer))
+  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    ;; Eight bits at a time, least significant bits first
+    (let ((bits 0))
+      (loop repeat 4
+            for places fixnum upfrom 0 by 8
+            for byte fixnum = (prog1 (aref buffer index) (iincf index))
+            do (setq bits (logior bits (ash byte places))))
+      (when (i= (ldb #.(byte 1 31) bits) 1)             ;sign bit set, so negative value
+        (decf bits #.(ash 1 32)))
+      (values bits index))))
+
+(defun decode-fixed64 (buffer index)
+  "Decodes the next 64-bit fixed integer in the buffer at the given index.
+   Returns both the decoded value and the new index into the buffer."
+  (declare (type fixnum index)
+           (type (simple-array (unsigned-byte 8)) buffer))
+  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    ;; Eight bits at a time, least significant bits first
+    (let ((bits 0))
+      (loop repeat 8
+            for places fixnum upfrom 0 by 8
+            for byte fixnum = (prog1 (aref buffer index) (iincf index))
+            do (setq bits (logior bits (ash byte places))))
+      (when (i= (ldb #.(byte 1 63) bits) 1)             ;sign bit set, so negative value
+        (decf bits #.(ash 1 64)))
+      (values bits index))))
+
 (defun decode-single (buffer index)
   "Decodes the next single float in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer."