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