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 > > On Saturday, January 3, 2015 11:51:14 PM UTC-8, Tzach wrote: >> >> 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! >> > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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 the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.