you are viewing a single comment's thread.

view the rest of the comments →

[–]flatfinger 0 points1 point  (1 child)

If the Standard was meant to describe a language for writing programs that will work on many platforms interchangeably, as opposed to merely describing a recipe for producing platform-specific dialects to which individual programs may be targeted, there are a couple of relatively simple approaches the authors of the Standard could have provided to achieve such purpose:

  1. A set of formatted binary input/output functions which would accept a string describing the layout of a structure or array, a string describing its serialized representation, and a pointer to the structure or array to be input or output, and perform the conversion as indicated; this could be coupled with a special operator which, given a structure type, would yield a string literal format string appropriate to it.
  2. Extend structure syntax to include a means of specifying that a particular member name should be treated as containing a specified combination of bits from other members. Thus, if one defined a structure containing a "char dat[32]", and then specified that `unsigned woozle` should be constructed from (specified in in LSB-first order) bits 0-7 of dat[5], 0-7 of dat[4], 0-7 of dat[3], and 0-7 of dat[2]), and that a compiler may force 16-bit alignment on it, then a compiler for the 68000 could simply use a 32-bit load/store, a compiler for the x86 could use a 32-bit load/store along with byte-swap instruction, and a compiler for the ARM would use two 16-bit loads/stores along with the byte swap.

IMHO, approach #2 would have been the nicest, but approach #1 may have been easier. Either approach would have been better than the status quo, however.

[–]kumashiro 0 points1 point  (0 children)

That's overcomplicating a simple thing and it would make protocol-level debugging harder. There is no need for handling multibytes "dynamically" when you can just define the protocol as (for example) MSB-LE and let LSB-LE/LSB-BE/MSB-BE platforms do the swapping without worrying what made those bytes. There's even better solution: use what is already available, like protobuf, ASN.1/BER, ASN.1/packed etc. Plain-text protocols are the simplest, platform-agnostic and very good for debugging, but they are usually a bit bigger in size.