From cee89a526e1c61f6397c83e33dd63cc4938896b9 Mon Sep 17 00:00:00 2001
From: Sven Robertz <sven@cs.lth.se>
Date: Wed, 20 Feb 2013 17:50:31 +0100
Subject: [PATCH] added a small comment on the packing + added untested code to
 the csharp lib + changed BASE_UID to 0x60

---
 lib/c/labcomm_private.h                       |  9 +++++-
 lib/csharp/se/lth/control/labcomm/LabComm.cs  |  2 +-
 .../control/labcomm/LabCommDecoderChannel.cs  | 32 +++++++++++++------
 .../control/labcomm/LabCommEncoderChannel.cs  | 19 ++++++++---
 lib/java/se/lth/control/labcomm/LabComm.java  |  4 +--
 lib/python/labcomm/LabComm.py                 |  4 +--
 6 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/lib/c/labcomm_private.h b/lib/c/labcomm_private.h
index 3c4767b..20d3fe8 100644
--- a/lib/c/labcomm_private.h
+++ b/lib/c/labcomm_private.h
@@ -30,7 +30,7 @@
 /*
  * Start index for user defined types
  */
-#define LABCOMM_USER     0x80
+#define LABCOMM_USER     0x60
 
 /*
  * Semi private decoder declarations
@@ -269,6 +269,13 @@ LABCOMM_ENCODE(long, long long)
 LABCOMM_ENCODE(float, float)
 LABCOMM_ENCODE(double, double)
 
+/* 
+ * Pack the 32 bit number data as a sequence of 7 bit chunks, represented in bytes 
+ * with the high bit meaning that more data is to come.
+ *
+ * The chunks are sent "little endian": each 7 bit chunk is more significant than
+ * the previous.
+ */ 
 static inline void labcomm_pack32(labcomm_writer_t *w, unsigned int data)
 {
   unsigned int tmp, i; 
diff --git a/lib/csharp/se/lth/control/labcomm/LabComm.cs b/lib/csharp/se/lth/control/labcomm/LabComm.cs
index 2354b50..d37db36 100644
--- a/lib/csharp/se/lth/control/labcomm/LabComm.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabComm.cs
@@ -25,7 +25,7 @@ namespace se.lth.control.labcomm {
     /*
      * start of user defined types 
      */
-    public const int FIRST_USER_INDEX = 0x80;
+    public const int FIRST_USER_INDEX = 0x60;
 
   }
 
diff --git a/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs b/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs
index 3255371..c6b149d 100644
--- a/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs
@@ -18,11 +18,11 @@ namespace se.lth.control.labcomm {
     public void runOne() {
       bool done = false;
       while (!done) {
-	int tag = decodeInt();
+	int tag = decodePacked32();
 	switch (tag) {
 	case LabComm.TYPEDEF:
         case LabComm.SAMPLE: {
-          int index = decodeInt();
+          int index = decodePacked32();
           String name = decodeString();
 	  MemoryStream signature = new MemoryStream();
 	  collectFlatSignature(new LabCommEncoderChannel(signature));
@@ -55,19 +55,19 @@ namespace se.lth.control.labcomm {
     }
 
     private void collectFlatSignature(LabCommEncoder e) {
-      int type = decodeInt();
+      int type = decodePacked32();
       e.encodeInt(type);
       switch (type) {
       case LabComm.ARRAY: {
-        int dimensions = decodeInt();
-        e.encodeInt(dimensions);
+        int dimensions = decodePacked32();
+        e.encodePacked32(dimensions);
         for (int i = 0 ; i < dimensions ; i++) {
-          e.encodeInt(decodeInt());
+          e.encodePacked32(decodePacked32());
         }
         collectFlatSignature(e);
       } break;
       case LabComm.STRUCT: {
-        int fields = decodeInt();
+        int fields = decodePacked32();
         e.encodeInt(fields);
         for (int i = 0 ; i < fields ; i++) {
           e.encodeString(decodeString());
@@ -155,12 +155,26 @@ namespace se.lth.control.labcomm {
     }
 
     public String decodeString() {
-      int length = (int)ReadInt(4);
+      //int length = (int)ReadInt(4);
+      int length = decodePacked32();
       byte[] buf = new byte[length];
       ReadBytes(buf, length);
       return Encoding.UTF8.GetString(buf);
     }
 
-  }
+    public int decodePacked32() {
+      Int64 res = 0;
+      byte i = 0;
+      bool cont = true; 
+
+      do {
+        byte c = decodeByte();
+	res |= (c & 0x7f) << 7*i;
+        cont = (c & 0x80) != 0;
+        i++;
+      } while(cont);
 
+      return (int) (res & 0xffffffff);
+    }
+  }
 } 
diff --git a/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs b/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs
index 27cc8cb..25f27c8 100644
--- a/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs
@@ -18,8 +18,8 @@ namespace se.lth.control.labcomm {
 
     public void register(LabCommDispatcher dispatcher) {
       int index = registry.add(dispatcher);
-      encodeInt(LabComm.SAMPLE);
-      encodeInt(index);
+      encodePacked32(LabComm.SAMPLE);
+      encodePacked32(index);
       encodeString(dispatcher.getName());
       byte[] signature = dispatcher.getSignature();
       for (int i = 0 ; i < signature.Length ; i++) {
@@ -29,7 +29,7 @@ namespace se.lth.control.labcomm {
     }
 
     public void begin(Type c) {
-      encodeInt(registry.getTag(c));
+      encodePacked32(registry.getTag(c));
     }
 
     public void end(Type c) {
@@ -84,9 +84,18 @@ namespace se.lth.control.labcomm {
 
     public void encodeString(String value) {
       byte[] buf = Encoding.UTF8.GetBytes(value);
-      WriteInt(buf.Length, 4);
+      EncodePacked32(buf.Length, 4);
       bytes.Write(buf, 0, buf.Length);
     }
 
+    public void encodePacked32(Int64 value) {
+      Int64 tmp = value;
+
+      while(tmp >= 0x80) {
+        encodeByte( (byte) ((tmp & 0x7f) | 0x80 ) );
+        tmp >>= 7;           
+      }
+      encodeByte( (byte) (tmp & 0x7f) );
+    }
   }
-}
\ No newline at end of file
+}
diff --git a/lib/java/se/lth/control/labcomm/LabComm.java b/lib/java/se/lth/control/labcomm/LabComm.java
index bb69df8..7a82e76 100644
--- a/lib/java/se/lth/control/labcomm/LabComm.java
+++ b/lib/java/se/lth/control/labcomm/LabComm.java
@@ -25,6 +25,6 @@ public class LabComm {
   /*
    * Start of 
    */
-  public static final int FIRST_USER_INDEX = 0x80;
+  public static final int FIRST_USER_INDEX = 0x60;
 
-}
\ No newline at end of file
+}
diff --git a/lib/python/labcomm/LabComm.py b/lib/python/labcomm/LabComm.py
index dada9ec..e0a6808 100644
--- a/lib/python/labcomm/LabComm.py
+++ b/lib/python/labcomm/LabComm.py
@@ -44,7 +44,7 @@
 # User data:
 #
 #   +----+----+----+----+
-#   | id >= 0x00000080  |
+#   | id >= 0x00000060  |
 #   +----+----+----+----+
 #   | user data
 #   | ...
@@ -107,7 +107,7 @@ i_FLOAT   = 0x25
 i_DOUBLE  = 0x26
 i_STRING  = 0x27
 
-i_USER    = 0x80
+i_USER    = 0x60
 
 def indent(i, s):
     return ("\n%s" % (" " * i)).join(s.split("\n"))
-- 
GitLab