bindat.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. ;;; bindat.el --- binary data structure packing and unpacking.
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Kim F. Storm <storm@cua.dk>
  4. ;; Assignment name: struct.el
  5. ;; Keywords: comm data processes
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Packing and unpacking of (binary) data structures.
  19. ;;
  20. ;; The data formats used in binary files and network protocols are
  21. ;; often structed data which can be described by a C-style structure
  22. ;; such as the one shown below. Using the bindat package, decoding
  23. ;; and encoding binary data formats like these is made simple using a
  24. ;; structure specification which closely resembles the C style
  25. ;; structure declarations.
  26. ;;
  27. ;; Encoded (binary) data is stored in a unibyte string or vector,
  28. ;; while the decoded data is stored in an alist with (FIELD . VALUE)
  29. ;; pairs.
  30. ;; Example:
  31. ;; Consider the following C structures:
  32. ;;
  33. ;; struct header {
  34. ;; unsigned long dest_ip;
  35. ;; unsigned long src_ip;
  36. ;; unsigned short dest_port;
  37. ;; unsigned short src_port;
  38. ;; };
  39. ;;
  40. ;; struct data {
  41. ;; unsigned char type;
  42. ;; unsigned char opcode;
  43. ;; unsigned long length; /* In little endian order */
  44. ;; unsigned char id[8]; /* nul-terminated string */
  45. ;; unsigned char data[/* (length + 3) & ~3 */];
  46. ;; };
  47. ;;
  48. ;; struct packet {
  49. ;; struct header header;
  50. ;; unsigned char items;
  51. ;; unsigned char filler[3];
  52. ;; struct data item[/* items */];
  53. ;; };
  54. ;;
  55. ;; The corresponding Lisp bindat specification looks like this:
  56. ;;
  57. ;; (setq header-bindat-spec
  58. ;; '((dest-ip ip)
  59. ;; (src-ip ip)
  60. ;; (dest-port u16)
  61. ;; (src-port u16)))
  62. ;;
  63. ;; (setq data-bindat-spec
  64. ;; '((type u8)
  65. ;; (opcode u8)
  66. ;; (length u16r) ;; little endian order
  67. ;; (id strz 8)
  68. ;; (data vec (length))
  69. ;; (align 4)))
  70. ;;
  71. ;; (setq packet-bindat-spec
  72. ;; '((header struct header-bindat-spec)
  73. ;; (items u8)
  74. ;; (fill 3)
  75. ;; (item repeat (items)
  76. ;; (struct data-bindat-spec))))
  77. ;;
  78. ;;
  79. ;; A binary data representation may look like
  80. ;; [ 192 168 1 100 192 168 1 101 01 28 21 32 2 0 0 0
  81. ;; 2 3 5 0 ?A ?B ?C ?D ?E ?F 0 0 1 2 3 4 5 0 0 0
  82. ;; 1 4 7 0 ?B ?C ?D ?E ?F ?G 0 0 6 7 8 9 10 11 12 0 ]
  83. ;;
  84. ;; The corresponding decoded structure looks like
  85. ;;
  86. ;; ((header
  87. ;; (dest-ip . [192 168 1 100])
  88. ;; (src-ip . [192 168 1 101])
  89. ;; (dest-port . 284)
  90. ;; (src-port . 5408))
  91. ;; (items . 2)
  92. ;; (item ((data . [1 2 3 4 5])
  93. ;; (id . "ABCDEF")
  94. ;; (length . 5)
  95. ;; (opcode . 3)
  96. ;; (type . 2))
  97. ;; ((data . [6 7 8 9 10 11 12])
  98. ;; (id . "BCDEFG")
  99. ;; (length . 7)
  100. ;; (opcode . 4)
  101. ;; (type . 1))))
  102. ;;
  103. ;; To access a specific value in this structure, use the function
  104. ;; bindat-get-field with the structure as first arg followed by a list
  105. ;; of field names and array indexes, e.g. using the data above,
  106. ;; (bindat-get-field decoded-structure 'item 1 'id)
  107. ;; returns "BCDEFG".
  108. ;; Binary Data Structure Specification Format
  109. ;; ------------------------------------------
  110. ;; We recommend using names that end in `-bindat-spec'; such names
  111. ;; are recognized automatically as "risky" variables.
  112. ;; The data specification is formatted as follows:
  113. ;; SPEC ::= ( ITEM... )
  114. ;; ITEM ::= ( [FIELD] TYPE )
  115. ;; | ( [FIELD] eval FORM ) -- eval FORM for side-effect only
  116. ;; | ( [FIELD] fill LEN ) -- skip LEN bytes
  117. ;; | ( [FIELD] align LEN ) -- skip to next multiple of LEN bytes
  118. ;; | ( [FIELD] struct SPEC_NAME )
  119. ;; | ( [FIELD] union TAG_VAL (TAG SPEC)... [(t SPEC)] )
  120. ;; | ( [FIELD] repeat COUNT ITEM... )
  121. ;; -- In (eval EXPR), the value of the last field is available in
  122. ;; the dynamically bound variable `last'.
  123. ;; TYPE ::= ( eval EXPR ) -- interpret result as TYPE
  124. ;; | u8 | byte -- length 1
  125. ;; | u16 | word | short -- length 2, network byte order
  126. ;; | u24 -- 3-byte value
  127. ;; | u32 | dword | long -- length 4, network byte order
  128. ;; | u16r | u24r | u32r -- little endian byte order.
  129. ;; | str LEN -- LEN byte string
  130. ;; | strz LEN -- LEN byte (zero-terminated) string
  131. ;; | vec LEN [TYPE] -- vector of LEN items of TYPE (default: u8)
  132. ;; | ip -- 4 byte vector
  133. ;; | bits LEN -- List with bits set in LEN bytes.
  134. ;;
  135. ;; -- Note: 32 bit values may be limited by emacs' INTEGER
  136. ;; implementation limits.
  137. ;;
  138. ;; -- Example: `bits 2' will unpack 0x28 0x1c to (2 3 4 11 13)
  139. ;; and 0x1c 0x28 to (3 5 10 11 12).
  140. ;; FIELD ::= ( eval EXPR ) -- use result as NAME
  141. ;; | NAME
  142. ;; LEN ::= ARG
  143. ;; | <omitted> | nil -- LEN = 1
  144. ;; TAG_VAL ::= ARG
  145. ;; TAG ::= LISP_CONSTANT
  146. ;; | ( eval EXPR ) -- return non-nil if tag match;
  147. ;; current TAG_VAL in `tag'.
  148. ;; ARG ::= ( eval EXPR ) -- interpret result as ARG
  149. ;; | INTEGER_CONSTANT
  150. ;; | DEREF
  151. ;; DEREF ::= ( [NAME | INTEGER]... ) -- Field NAME or Array index relative
  152. ;; to current structure spec.
  153. ;; -- see bindat-get-field
  154. ;; A `union' specification
  155. ;; ([FIELD] union TAG_VAL (TAG SPEC) ... [(t SPEC)])
  156. ;; is interpreted by evalling TAG_VAL and then comparing that to
  157. ;; each TAG using equal; if a match is found, the corresponding SPEC
  158. ;; is used.
  159. ;; If TAG is a form (eval EXPR), EXPR is evalled with `tag' bound to the
  160. ;; value of TAG_VAL; the corresponding SPEC is used if the result is non-nil.
  161. ;; Finally, if TAG is t, the corresponding SPEC is used unconditionally.
  162. ;;
  163. ;; An `eval' specification
  164. ;; ([FIELD] eval FORM)
  165. ;; is interpreted by evalling FORM for its side effects only.
  166. ;; If FIELD is specified, the value is bound to that field.
  167. ;; The FORM may access and update `bindat-raw' and `bindat-idx' (see `bindat-unpack').
  168. ;;; Code:
  169. ;; Helper functions for structure unpacking.
  170. ;; Relies on dynamic binding of BINDAT-RAW and BINDAT-IDX
  171. (defvar bindat-raw)
  172. (defvar bindat-idx)
  173. (defun bindat--unpack-u8 ()
  174. (prog1
  175. (aref bindat-raw bindat-idx)
  176. (setq bindat-idx (1+ bindat-idx))))
  177. (defun bindat--unpack-u16 ()
  178. (logior (lsh (bindat--unpack-u8) 8) (bindat--unpack-u8)))
  179. (defun bindat--unpack-u24 ()
  180. (logior (lsh (bindat--unpack-u16) 8) (bindat--unpack-u8)))
  181. (defun bindat--unpack-u32 ()
  182. (logior (lsh (bindat--unpack-u16) 16) (bindat--unpack-u16)))
  183. (defun bindat--unpack-u16r ()
  184. (logior (bindat--unpack-u8) (lsh (bindat--unpack-u8) 8)))
  185. (defun bindat--unpack-u24r ()
  186. (logior (bindat--unpack-u16r) (lsh (bindat--unpack-u8) 16)))
  187. (defun bindat--unpack-u32r ()
  188. (logior (bindat--unpack-u16r) (lsh (bindat--unpack-u16r) 16)))
  189. (defun bindat--unpack-item (type len &optional vectype)
  190. (if (eq type 'ip)
  191. (setq type 'vec len 4))
  192. (cond
  193. ((memq type '(u8 byte))
  194. (bindat--unpack-u8))
  195. ((memq type '(u16 word short))
  196. (bindat--unpack-u16))
  197. ((eq type 'u24)
  198. (bindat--unpack-u24))
  199. ((memq type '(u32 dword long))
  200. (bindat--unpack-u32))
  201. ((eq type 'u16r)
  202. (bindat--unpack-u16r))
  203. ((eq type 'u24r)
  204. (bindat--unpack-u24r))
  205. ((eq type 'u32r)
  206. (bindat--unpack-u32r))
  207. ((eq type 'bits)
  208. (let ((bits nil) (bnum (1- (* 8 len))) j m)
  209. (while (>= bnum 0)
  210. (if (= (setq m (bindat--unpack-u8)) 0)
  211. (setq bnum (- bnum 8))
  212. (setq j 128)
  213. (while (> j 0)
  214. (if (/= 0 (logand m j))
  215. (setq bits (cons bnum bits)))
  216. (setq bnum (1- bnum)
  217. j (lsh j -1)))))
  218. bits))
  219. ((eq type 'str)
  220. (let ((s (substring bindat-raw bindat-idx (+ bindat-idx len))))
  221. (setq bindat-idx (+ bindat-idx len))
  222. (if (stringp s) s
  223. (string-make-unibyte (concat s)))))
  224. ((eq type 'strz)
  225. (let ((i 0) s)
  226. (while (and (< i len) (/= (aref bindat-raw (+ bindat-idx i)) 0))
  227. (setq i (1+ i)))
  228. (setq s (substring bindat-raw bindat-idx (+ bindat-idx i)))
  229. (setq bindat-idx (+ bindat-idx len))
  230. (if (stringp s) s
  231. (string-make-unibyte (concat s)))))
  232. ((eq type 'vec)
  233. (let ((v (make-vector len 0)) (i 0) (vlen 1))
  234. (if (consp vectype)
  235. (setq vlen (nth 1 vectype)
  236. vectype (nth 2 vectype))
  237. (setq type (or vectype 'u8)
  238. vectype nil))
  239. (while (< i len)
  240. (aset v i (bindat--unpack-item type vlen vectype))
  241. (setq i (1+ i)))
  242. v))
  243. (t nil)))
  244. (defun bindat--unpack-group (spec)
  245. (let (struct last)
  246. (while spec
  247. (let* ((item (car spec))
  248. (field (car item))
  249. (type (nth 1 item))
  250. (len (nth 2 item))
  251. (vectype (and (eq type 'vec) (nth 3 item)))
  252. (tail 3)
  253. data)
  254. (setq spec (cdr spec))
  255. (if (and (consp field) (eq (car field) 'eval))
  256. (setq field (eval (car (cdr field)))))
  257. (if (and type (consp type) (eq (car type) 'eval))
  258. (setq type (eval (car (cdr type)))))
  259. (if (and len (consp len) (eq (car len) 'eval))
  260. (setq len (eval (car (cdr len)))))
  261. (if (memq field '(eval fill align struct union))
  262. (setq tail 2
  263. len type
  264. type field
  265. field nil))
  266. (if (and (consp len) (not (eq type 'eval)))
  267. (setq len (apply 'bindat-get-field struct len)))
  268. (if (not len)
  269. (setq len 1))
  270. (cond
  271. ((eq type 'eval)
  272. (if field
  273. (setq data (eval len))
  274. (eval len)))
  275. ((eq type 'fill)
  276. (setq bindat-idx (+ bindat-idx len)))
  277. ((eq type 'align)
  278. (while (/= (% bindat-idx len) 0)
  279. (setq bindat-idx (1+ bindat-idx))))
  280. ((eq type 'struct)
  281. (setq data (bindat--unpack-group (eval len))))
  282. ((eq type 'repeat)
  283. (let ((index 0) (count len))
  284. (while (< index count)
  285. (setq data (cons (bindat--unpack-group (nthcdr tail item)) data))
  286. (setq index (1+ index)))
  287. (setq data (nreverse data))))
  288. ((eq type 'union)
  289. (let ((tag len) (cases (nthcdr tail item)) case cc)
  290. (while cases
  291. (setq case (car cases)
  292. cases (cdr cases)
  293. cc (car case))
  294. (if (or (equal cc tag) (equal cc t)
  295. (and (consp cc) (eval cc)))
  296. (setq data (bindat--unpack-group (cdr case))
  297. cases nil)))))
  298. (t
  299. (setq data (bindat--unpack-item type len vectype)
  300. last data)))
  301. (if data
  302. (if field
  303. (setq struct (cons (cons field data) struct))
  304. (setq struct (append data struct))))))
  305. struct))
  306. (defun bindat-unpack (spec bindat-raw &optional bindat-idx)
  307. "Return structured data according to SPEC for binary data in BINDAT-RAW.
  308. BINDAT-RAW is a unibyte string or vector.
  309. Optional third arg BINDAT-IDX specifies the starting offset in BINDAT-RAW."
  310. (when (multibyte-string-p bindat-raw)
  311. (error "String is multibyte"))
  312. (unless bindat-idx (setq bindat-idx 0))
  313. (bindat--unpack-group spec))
  314. (defun bindat-get-field (struct &rest field)
  315. "In structured data STRUCT, return value of field named FIELD.
  316. If multiple field names are specified, use the field names to
  317. lookup nested sub-structures in STRUCT, corresponding to the
  318. C-language syntax STRUCT.FIELD1.FIELD2.FIELD3...
  319. An integer value in the field list is taken as an array index,
  320. e.g. corresponding to STRUCT.FIELD1[INDEX2].FIELD3..."
  321. (while (and struct field)
  322. (setq struct (if (integerp (car field))
  323. (nth (car field) struct)
  324. (let ((val (assq (car field) struct)))
  325. (if (consp val) (cdr val)))))
  326. (setq field (cdr field)))
  327. struct)
  328. ;; Calculate bindat-raw length of structured data
  329. (defvar bindat--fixed-length-alist
  330. '((u8 . 1) (byte . 1)
  331. (u16 . 2) (u16r . 2) (word . 2) (short . 2)
  332. (u24 . 3) (u24r . 3)
  333. (u32 . 4) (u32r . 4) (dword . 4) (long . 4)
  334. (ip . 4)))
  335. (defun bindat--length-group (struct spec)
  336. (let (last)
  337. (while spec
  338. (let* ((item (car spec))
  339. (field (car item))
  340. (type (nth 1 item))
  341. (len (nth 2 item))
  342. (vectype (and (eq type 'vec) (nth 3 item)))
  343. (tail 3))
  344. (setq spec (cdr spec))
  345. (if (and (consp field) (eq (car field) 'eval))
  346. (setq field (eval (car (cdr field)))))
  347. (if (and type (consp type) (eq (car type) 'eval))
  348. (setq type (eval (car (cdr type)))))
  349. (if (and len (consp len) (eq (car len) 'eval))
  350. (setq len (eval (car (cdr len)))))
  351. (if (memq field '(eval fill align struct union))
  352. (setq tail 2
  353. len type
  354. type field
  355. field nil))
  356. (if (and (consp len) (not (eq type 'eval)))
  357. (setq len (apply 'bindat-get-field struct len)))
  358. (if (not len)
  359. (setq len 1))
  360. (while (eq type 'vec)
  361. (let ((vlen 1))
  362. (if (consp vectype)
  363. (setq len (* len (nth 1 vectype))
  364. type (nth 2 vectype))
  365. (setq type (or vectype 'u8)
  366. vectype nil))))
  367. (cond
  368. ((eq type 'eval)
  369. (if field
  370. (setq struct (cons (cons field (eval len)) struct))
  371. (eval len)))
  372. ((eq type 'fill)
  373. (setq bindat-idx (+ bindat-idx len)))
  374. ((eq type 'align)
  375. (while (/= (% bindat-idx len) 0)
  376. (setq bindat-idx (1+ bindat-idx))))
  377. ((eq type 'struct)
  378. (bindat--length-group
  379. (if field (bindat-get-field struct field) struct) (eval len)))
  380. ((eq type 'repeat)
  381. (let ((index 0) (count len))
  382. (while (< index count)
  383. (bindat--length-group
  384. (nth index (bindat-get-field struct field))
  385. (nthcdr tail item))
  386. (setq index (1+ index)))))
  387. ((eq type 'union)
  388. (let ((tag len) (cases (nthcdr tail item)) case cc)
  389. (while cases
  390. (setq case (car cases)
  391. cases (cdr cases)
  392. cc (car case))
  393. (if (or (equal cc tag) (equal cc t)
  394. (and (consp cc) (eval cc)))
  395. (progn
  396. (bindat--length-group struct (cdr case))
  397. (setq cases nil))))))
  398. (t
  399. (if (setq type (assq type bindat--fixed-length-alist))
  400. (setq len (* len (cdr type))))
  401. (if field
  402. (setq last (bindat-get-field struct field)))
  403. (setq bindat-idx (+ bindat-idx len))))))))
  404. (defun bindat-length (spec struct)
  405. "Calculate bindat-raw length for STRUCT according to bindat SPEC."
  406. (let ((bindat-idx 0))
  407. (bindat--length-group struct spec)
  408. bindat-idx))
  409. ;; Pack structured data into bindat-raw
  410. (defun bindat--pack-u8 (v)
  411. (aset bindat-raw bindat-idx (logand v 255))
  412. (setq bindat-idx (1+ bindat-idx)))
  413. (defun bindat--pack-u16 (v)
  414. (aset bindat-raw bindat-idx (logand (lsh v -8) 255))
  415. (aset bindat-raw (1+ bindat-idx) (logand v 255))
  416. (setq bindat-idx (+ bindat-idx 2)))
  417. (defun bindat--pack-u24 (v)
  418. (bindat--pack-u8 (lsh v -16))
  419. (bindat--pack-u16 v))
  420. (defun bindat--pack-u32 (v)
  421. (bindat--pack-u16 (lsh v -16))
  422. (bindat--pack-u16 v))
  423. (defun bindat--pack-u16r (v)
  424. (aset bindat-raw (1+ bindat-idx) (logand (lsh v -8) 255))
  425. (aset bindat-raw bindat-idx (logand v 255))
  426. (setq bindat-idx (+ bindat-idx 2)))
  427. (defun bindat--pack-u24r (v)
  428. (bindat--pack-u16r v)
  429. (bindat--pack-u8 (lsh v -16)))
  430. (defun bindat--pack-u32r (v)
  431. (bindat--pack-u16r v)
  432. (bindat--pack-u16r (lsh v -16)))
  433. (defun bindat--pack-item (v type len &optional vectype)
  434. (if (eq type 'ip)
  435. (setq type 'vec len 4))
  436. (cond
  437. ((null v)
  438. (setq bindat-idx (+ bindat-idx len)))
  439. ((memq type '(u8 byte))
  440. (bindat--pack-u8 v))
  441. ((memq type '(u16 word short))
  442. (bindat--pack-u16 v))
  443. ((eq type 'u24)
  444. (bindat--pack-u24 v))
  445. ((memq type '(u32 dword long))
  446. (bindat--pack-u32 v))
  447. ((eq type 'u16r)
  448. (bindat--pack-u16r v))
  449. ((eq type 'u24r)
  450. (bindat--pack-u24r v))
  451. ((eq type 'u32r)
  452. (bindat--pack-u32r v))
  453. ((eq type 'bits)
  454. (let ((bnum (1- (* 8 len))) j m)
  455. (while (>= bnum 0)
  456. (setq m 0)
  457. (if (null v)
  458. (setq bnum (- bnum 8))
  459. (setq j 128)
  460. (while (> j 0)
  461. (if (memq bnum v)
  462. (setq m (logior m j)))
  463. (setq bnum (1- bnum)
  464. j (lsh j -1))))
  465. (bindat--pack-u8 m))))
  466. ((memq type '(str strz))
  467. (let ((l (length v)) (i 0))
  468. (if (> l len) (setq l len))
  469. (while (< i l)
  470. (aset bindat-raw (+ bindat-idx i) (aref v i))
  471. (setq i (1+ i)))
  472. (setq bindat-idx (+ bindat-idx len))))
  473. ((eq type 'vec)
  474. (let ((l (length v)) (i 0) (vlen 1))
  475. (if (consp vectype)
  476. (setq vlen (nth 1 vectype)
  477. vectype (nth 2 vectype))
  478. (setq type (or vectype 'u8)
  479. vectype nil))
  480. (if (> l len) (setq l len))
  481. (while (< i l)
  482. (bindat--pack-item (aref v i) type vlen vectype)
  483. (setq i (1+ i)))))
  484. (t
  485. (setq bindat-idx (+ bindat-idx len)))))
  486. (defun bindat--pack-group (struct spec)
  487. (let (last)
  488. (while spec
  489. (let* ((item (car spec))
  490. (field (car item))
  491. (type (nth 1 item))
  492. (len (nth 2 item))
  493. (vectype (and (eq type 'vec) (nth 3 item)))
  494. (tail 3))
  495. (setq spec (cdr spec))
  496. (if (and (consp field) (eq (car field) 'eval))
  497. (setq field (eval (car (cdr field)))))
  498. (if (and type (consp type) (eq (car type) 'eval))
  499. (setq type (eval (car (cdr type)))))
  500. (if (and len (consp len) (eq (car len) 'eval))
  501. (setq len (eval (car (cdr len)))))
  502. (if (memq field '(eval fill align struct union))
  503. (setq tail 2
  504. len type
  505. type field
  506. field nil))
  507. (if (and (consp len) (not (eq type 'eval)))
  508. (setq len (apply 'bindat-get-field struct len)))
  509. (if (not len)
  510. (setq len 1))
  511. (cond
  512. ((eq type 'eval)
  513. (if field
  514. (setq struct (cons (cons field (eval len)) struct))
  515. (eval len)))
  516. ((eq type 'fill)
  517. (setq bindat-idx (+ bindat-idx len)))
  518. ((eq type 'align)
  519. (while (/= (% bindat-idx len) 0)
  520. (setq bindat-idx (1+ bindat-idx))))
  521. ((eq type 'struct)
  522. (bindat--pack-group
  523. (if field (bindat-get-field struct field) struct) (eval len)))
  524. ((eq type 'repeat)
  525. (let ((index 0) (count len))
  526. (while (< index count)
  527. (bindat--pack-group
  528. (nth index (bindat-get-field struct field))
  529. (nthcdr tail item))
  530. (setq index (1+ index)))))
  531. ((eq type 'union)
  532. (let ((tag len) (cases (nthcdr tail item)) case cc)
  533. (while cases
  534. (setq case (car cases)
  535. cases (cdr cases)
  536. cc (car case))
  537. (if (or (equal cc tag) (equal cc t)
  538. (and (consp cc) (eval cc)))
  539. (progn
  540. (bindat--pack-group struct (cdr case))
  541. (setq cases nil))))))
  542. (t
  543. (setq last (bindat-get-field struct field))
  544. (bindat--pack-item last type len vectype)
  545. ))))))
  546. (defun bindat-pack (spec struct &optional bindat-raw bindat-idx)
  547. "Return binary data packed according to SPEC for structured data STRUCT.
  548. Optional third arg BINDAT-RAW is a pre-allocated unibyte string or vector to
  549. pack into.
  550. Optional fourth arg BINDAT-IDX is the starting offset into BINDAT-RAW."
  551. (when (multibyte-string-p bindat-raw)
  552. (error "Pre-allocated string is multibyte"))
  553. (let ((no-return bindat-raw))
  554. (unless bindat-idx (setq bindat-idx 0))
  555. (unless bindat-raw
  556. (setq bindat-raw (make-string (+ bindat-idx (bindat-length spec struct)) 0)))
  557. (bindat--pack-group struct spec)
  558. (if no-return nil bindat-raw)))
  559. ;; Misc. format conversions
  560. (defun bindat-format-vector (vect fmt sep &optional len)
  561. "Format vector VECT using element format FMT and separator SEP.
  562. Result is a string with each element of VECT formatted using FMT and
  563. separated by the string SEP. If optional fourth arg LEN is given, use
  564. only that many elements from VECT."
  565. (unless len
  566. (setq len (length vect)))
  567. (let ((i len) (fmt2 (concat sep fmt)) (s nil))
  568. (while (> i 0)
  569. (setq i (1- i)
  570. s (cons (format (if (= i 0) fmt fmt2) (aref vect i)) s)))
  571. (apply 'concat s)))
  572. (defun bindat-vector-to-dec (vect &optional sep)
  573. "Format vector VECT in decimal format separated by dots.
  574. If optional second arg SEP is a string, use that as separator."
  575. (bindat-format-vector vect "%d" (if (stringp sep) sep ".")))
  576. (defun bindat-vector-to-hex (vect &optional sep)
  577. "Format vector VECT in hex format separated by dots.
  578. If optional second arg SEP is a string, use that as separator."
  579. (bindat-format-vector vect "%02x" (if (stringp sep) sep ":")))
  580. (defun bindat-ip-to-string (ip)
  581. "Format vector IP as an ip address in dotted notation.
  582. The port (if any) is omitted. IP can be a string, as well."
  583. (if (vectorp ip)
  584. (format-network-address ip t)
  585. (format "%d.%d.%d.%d"
  586. (aref ip 0) (aref ip 1) (aref ip 2) (aref ip 3))))
  587. (provide 'bindat)
  588. ;;; bindat.el ends here