Show HN: T2b – A wicked-powerful text macro language for building binary files
github.com
github.com
- the hex modifier does both turning mode on and off, which will be confusing in a longer file
- it would be good if prefixes such as 0x or bx were handled to temporarily override the current setting
- using "get var" to actually output something is weird, i'd use "put"
- add a way to handle endianness
This tool was hacked together quickly, and you can tell by using it that it's not close to perfect yet.
Of course there might be some value in something less than a full language.
Though reading both `pack` and `struct` the formats are about as obtuse as a regex. And the lack of individual bit support in `struct` is limiting.
Come to think of it, like other comments already, this could be a handy CLI streaming tool. Dealing with adjusting binary output on the CLI turns out to be a pain...
Wouldn’t it be more readable and less error-prone to have separate commands for turning hex mode on and off? Such as `hex on` and `hex off`?
As far as I could tell it's been more or less the only mode. At that point I'd probably get rid of it because global state (which modes are) has to be remembered at all times and in this case its invisible until you see output. The suggestion to use different operators or modifiers for a single numbers was a good one in that direction IMO.
if ...
hex off
endifEdit: What I mean is
hex
00
dec
00
utf8
00The only other thing I could see being useful is the ability to define "structs", for binary-headers, etc.
But agreed, this is a neat tool.
In the other direction, given the ability to spit out one binary character from the shell, you can bootstrap all the rest of this in pure shell script. I cheated and gave myself int{8..64} and did a whole ASN.1/DER in shell script. The shell has better control flow than custom language (but the custom language is a little simpler).
With an FFI (which Bash has) one could do this much more easily than it was then (about 15 years ago, I think).
Also some ability to length-prefix the output of a macro might be useful for tag-length-value-like formats.
For Unicode, ability to write out UTF-16 or UTF-32 would be useful, also CESU-8 and Java's Modified UTF-8.
Also, for a challenge, think about how you could write macros to generate ASN.1 formats such as BER. You'd probably need some more smarts in your macro language to handle such a task gracefully.
Also, how does one control the endianness of output? Maybe you need u16le and u16be, or some kind of general endianness modifier.
Output of IEEE floating point might also be useful in some applications.
Exactly. TLV (tag-length-value) support is really what I expected to see. They are hairy to get right manually.
macro tlv_string s
begin
i32 (get CONSTANT_STRING_TAG_FOO)
i32 (len s)
str s
u8 0
endmacro
By the way, there's no `len` command at this point, so that's something I'll have to add in eventually to get something like TLV to work.Shouldn't this be use output redirection (ie. >)?
This to me is real hacking, saying what the heck and writing a tool for oneself instead of looking around and getting lost in the multitude of 'mature' options that exist. I am sure hacking this was quicker, and more fun, than browsing all the available options, picking one, installing the tools, learning to use them and getting the desired result.
Now, how long till someone decides to bootstrap this - use it to hack a binary that compiles it?
Alternatively you could use some kind of fluent API to write binary data. Depends on your use case of course.
At the very least, the function to execute T2B scripts could be exposed in a simple header.
c:\mingw\MSYS64\usr\bin\echo -n hex u32 20786568 u32 36203875 u8 31 u8 0a u32 34203875 u8 31 | t2b
Output: hex u8 61[LF]u8 41
c:\mingw\MSYS64\usr\bin\echo -n hex u32 20786568 u32 36203875 u8 31 u8 0a u32 34203875 u8 31 | t2b | t2b
Output: aA
c:\mingw\MSYS64\usr\bin\echo -n hex u32 20786568 u32 36203875 u8 31 u8 0a u32 34203875 u8 31 u8 0a | t2b
Output: hex u8 61[LF]u8 41[LF]
c:\mingw\MSYS64\usr\bin\echo -n hex u32 20786568 u32 36203875 u8 31 u8 0a u32 34203875 u8 31 u8 0a | t2b | t2b
Output: aAA
I don't know very much C++ yet, so I'm not sure of the best way to fix this.For more trustworthy bootstrapping. It's why I included shell compilers on this site:
I'm not saying building these kind of lists is a bad thing, but I haven't found an efficient way to make use of lists like this and void diverting my focus.
https://speakerdeck.com/nineties/creating-a-language-using-o...
A decade late for me, but still looks cool.
What's the purpose of checking this stuff into version control?
The only downside is that not everyone uses the same editor, so in projects with more contributors, it can quickly create bloat.
Also, it seems like Kaitai is for parsing structures, whereas t2b is for emitting data.