lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've published an implementation of the arcane yet venerable BSD hexdump
utility as a simple C library with optional Lua bindings at

	http://25thandclement.com/~william/projects/hexdump.c.html

BSD hexdump formats binary blobs according to a user-provided formatting
specification. For example, the format specification

	"%08.8_ax  " 8/1 "%02x " "  " 8/1 "%02x "
	"  |" 16/1 "%_p" "|\n"

produces the much more familiar output

00000000  54 68 65 20 71 75 69 63  6b 20 62 72 6f 77 6e 20  |The quick brown |
00000010  66 6f 78 20 6a 75 6d 70  73 20 6f 76 65 72 20 74  |fox jumps over t|
00000020  68 65 20 6c 61 7a 79 20  64 6f 67                 |he lazy dog|

As Lua code it looks like:

	local hexdump = require"hexdump"

	local fmt = [[
	        "%08.8_ax  " 8/1 "%02x " "  " 8/1 "%02x "
	        "  |" 16/1 "%_p" "|\n"
	]]

	print(hexdump(fmt, "The quick brown fox jumps over the dog"))

For processing arbitrary amounts of data you can do something like:

	local dumper = hexdump.new()

	dumper:compile(fmt) --> fmt string from prior example

	for chunk in io.stdin:lines(512) do
		dumper:write(chunk)
		io.stdout:write(dumper:read())
	end

	dumper:flush()
	io.stdout:write(dumper:read())

The included Makefile requires GNU Make, and at a minimum you should provide
the Make variable $(luainclude). The module target is hexdump.so (by
defaults it builds a command-line utility). It doesn't install yet.

Please don't hesitate to send bug reports, feature requests, or patches my
way.

- Bill