Newbie Gloss questions - dynamic buffer structure

165 views
Skip to first unread message

Tzach

unread,
Jan 4, 2015, 2:51:14 AM1/4/15
to clo...@googlegroups.com
I'm trying to work with Gloss binary encoder/decoder, and need some help to kick start.

My first task is simple(not for me ;)
I have the following binary buffer to read/write:
  • 4 byte (32 bit) - code (uint)
  • 8 bit - misc flags
  • 3 byte (24 bit) - the entire buffer length
  • 4 byte (32 bit) uint value -  optional, depending on on of the flags.
  • List of 4 byte (uints) data elements - size dependent on the overall size 
How should I represent this structure?
Clearly I need to use prefix and repeated for the last element, but I failed to understand how.
I also struggle with the optional element and how to represent a 3 byte element.
 
Thanks!

Zach Tellman

unread,
Jan 7, 2015, 12:28:06 AM1/7/15
to clo...@googlegroups.com
Hey Tzach,

If I understand what you're trying to do, you want something like this:

[ :uint32
  :ubyte ;; or bit-seq, depending
  (delimited-block (prefix uint24 #(- % 5) #(+ % 5))) ]

And then you'll need to parse the final block separately.  I've left defining the uint24 as an exercise for the reader (sorry, long day), but using (compile-frame [:uint16 :uint8] ...) should be pretty straightforward.

Hope that helps, happy to answer any followup questions.

Zach

Tzach

unread,
Jan 12, 2015, 9:27:12 AM1/12/15
to clo...@googlegroups.com
Hi Zach
Thanks for the detailed response

On Wednesday, January 7, 2015 at 7:28:06 AM UTC+2, Zach Tellman wrote:
Hey Tzach,

If I understand what you're trying to do, you want something like this:

[ :uint32
  :ubyte ;; or bit-seq, depending
  (delimited-block (prefix uint24 #(- % 5) #(+ % 5))) ]

And then you'll need to parse the final block separately.  I've left defining the uint24 as an exercise for the reader (sorry, long day), but using (compile-frame [:uint16 :uint8] ...) should be pretty straightforward.
This is what work for me, in case anyone is interested or want to comment

(defn valid-length [l]
 (and (< l 16777216)
      (>= l 0)))

(def uint24 (compile-frame [:ubyte :uint16]
                           (fn [u] 
                             {:pre  [(valid-length u)]}
                             (let [u8 (bit-shift-right u 16)
                                   u16 (- u (bit-shift-left u8 16))]
                               [u8 u16]))
                           (fn [[u8 u16]] 
                             {:post [(valid-length %)]}
                             (+ (bit-shift-left u8 16) u16))))

(def avp
  (compile-frame
   [:uint32
    :ubyte
    (repeated :int32 :prefix (prefix uint24 #(- % 5) #(+ % 5))) ]
))
My next step is to parse the bits from the header :ubyte and base on it, read an extra uint32 before the repeated part.
The header is probably the answer.
 

Hope that helps, happy to answer any followup questions.

It did help! Thanks

BTW, I spent too much time trying to use (compile-frame [:uint16 :uint8]) before realizing :uint8 is not defined.
The result is simply encoding of the :uint8 symbol.
I wonder if its a bug or a feature.

Zach Tellman

unread,
Jan 12, 2015, 12:22:09 PM1/12/15
to clo...@googlegroups.com
I'm sorry, that should have been :ubyte. Being able to insert arbitrary data into a codec is a feature, though.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/NxsCPBk12mE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages