Skip to content

Instantly share code, notes, and snippets.

@mateon1
Created July 26, 2021 15:42
Show Gist options
  • Save mateon1/2fd2c94fc4a2175f99b331c4be5b9e9f to your computer and use it in GitHub Desktop.
Save mateon1/2fd2c94fc4a2175f99b331c4be5b9e9f to your computer and use it in GitHub Desktop.
Kaitai ETF (Erlang's External Term Fomat)
meta:
id: etf
file-extension: etf
title: Erlang's External Term Format
endian: be
doc-ref: https://erlang.org/doc/apps/erts/erl_ext_dist.html
seq:
- id: messages
type: message
repeat: eos
types:
empty: {}
message:
seq:
- id: magic
contents: [131]
- id: term
type: term
term:
seq:
- id: tag
type: u1
enum: term_type
- id: data
type:
switch-on: tag
cases:
term_type::map: map
term_type::small_atom_latin1: atom(1, false)
term_type::atom_latin1: atom(2, false)
term_type::small_atom_utf8: atom(1, true)
term_type::atom_utf8: atom(2, true)
term_type::list: list
term_type::binary: binary
term_type::nil: empty
term_type::int: s4
term_type::small_int: u1
term_type::small_bignum: bignum(1)
term_type::large_bignum: bignum(4)
_: unimplemented
-webide-representation: "{tag} {data:dec}"
bignum:
instances:
sign_char:
value: "sign == 0 ? \"+\" : \"-\""
params:
- id: length_len
type: u1
seq:
- id: length
type:
switch-on: length_len
cases:
1: u1
4: u4
valid:
max: 999
- id: sign
type: u1
valid:
max: 1
- id: data
size: length
-webide-representation: "{sign_char}{data}"
binary:
instances:
text:
value: "data.to_s(\"latin1\")"
repr:
value: "length < 30 ? \" \\\"\"+text+\"\\\"\" : \"\""
seq:
- id: length
type: u4
- id: data
size: length
-webide-representation: "<{length:dec} bytes>{repr}"
list:
instances:
tail:
value: elements.last
seq:
- id: length
type: u4
- id: elements
type: term
repeat: expr
repeat-expr: "length > 999 ? 0 : length+1"
-webide-representation: "[{elements}]"
map:
seq:
- id: arity
type: u4
- id: pairs
type: pair
repeat: expr
repeat-expr: "arity > 999 ? 0 : arity"
-webide-representation: "<{arity:dec} pairs>"
pair:
seq:
- id: key
type: term
- id: value
type: term
-webide-representation: "{key}: {value}"
atom:
params:
- id: length_len
type: u8
- id: is_utf8
type: b1
instances:
text:
value: "is_utf8 ? data.to_s(\"utf8\") : data.to_s(\"latin1\")"
seq:
- id: length
type:
switch-on: length_len
cases:
1: u1
2: u2
- id: data
size: length
-webide-representation: "{text}"
unimplemented:
-webide-representation: UNIMPLEMENTED!
enums:
term_type:
82: atom_cache_ref_idx
97: small_int
98: int
99: float_str
102: port
89: new_port
120: v4_port
103: pid
88: new_pid
104: small_tuple
105: large_tuple
116: map
106: nil
107: string
108: list
109: binary
110: small_bignum
111: large_bignum
101: reference
114: new_reference
90: newer_reference
117: function
112: new_function
113: export
77: bit_binary
70: new_float
118: atom_utf8
119: small_atom_utf8
100: atom_latin1
115: small_atom_latin1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment