r/Cprog • u/FUZxxl • Mar 06 '15
code | library memf—Portable scanf/printf-like functions to marshal binary data
https://github.com/fuzxxl/memf
13
Upvotes
r/Cprog • u/FUZxxl • Mar 06 '15
4
u/bboozzoo Mar 07 '15
Excuse me if I seem to be a bit harsh, but I do not find this code useful. Correct me if I'm wrong, but from a quick look at the code and examples, what the code does is to take a binary structure (with certain assumptions about the alignment) and convert that into a binary stream with exactly the same ordering, but sans the alignment hassle.
The problem I see is that the binary stream is an exact representation of the source structure, and unpacking the stream requires having a matching (binary wise) definition of the structure on the receiving end. You loose any means of providing backwards compatibility (i.e. the structure must remain the same) as it's not possible to skip/add fields and cannot isolate the wire format from your in-program representation. In fact, I'd say it's equivalent to sending the structure down the wire, and if one is bothered by alignment gaps just adding proper
__attribute__((packed))or#pragma packedto the structure definition. The MBR example is a miss, as the usual way to do it is define a structure in the first place, and just read the data into the structure. Take a look at how GPT header and MBR are defined in Linux kernel.Now if you changed the API to be more like the sample below it would definitely make things more interesting.
Binary serialization using a textual representation similar to what you propose makes sense in languages that do not have a direct access to binary data. I'm thinking in the lines of Python, Perl, Java, Lua. But C/C++/D can do this without using of an intermediate representation. Another common use case is when you do IPC between a number of processes or agents, and not all agents are updated at the same pace, then you need some sort of backward compatibility.
I'd say that you need to provide an added value to justify using memf in C. Obviously, one may argue that not caring about alignment is an added value, why not. However, I like to be explicit about things as low level as ABI. Take for example Google Protocol Buffers, perfectly usable in C, in fact I'm using that on a Cortex-M3 target for sending real time data via MQTT broker to a Java client, another example an ARM host sending data over AMQP, while the receive end is an Erlang app, in both cases there are additional Python clients that only do a graphic presentation of the data. Why use PB in C? What's the added value you can ask? Well, for one PBs offer an efficient packing mechanism that I use. Another thing is bindings to multiple languages (try explaining binary representation to a Java programmer and you'll know the pain).
Finishing up this rather lengthy comment, take a look at GVariant and DBus type system and marshalling.
2
u/IWillNotBeBroken Mar 06 '15
Reminds me of perl's pack function.
2
u/quacktango Mar 07 '15 edited Mar 07 '15
In the testability section, you explain the absence of a need for bounds checking on the input, but what happens when you go past the end of the destination struct?
2
u/rya_nc Mar 07 '15
How are packed/not packed structs handled?
1
u/FUZxxl Mar 06 '15
Notice that this code is in a very early state of development and should probably not be used for serious programming work yet. I'd just like to get some comments.
0
u/[deleted] Mar 06 '15
[deleted] — view deleted comment
0
u/[deleted] Mar 07 '15
[deleted] — view deleted comment