1 ;;; bindat.el --- binary data structure packing and unpacking.
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Assignment name: struct.el
7 ;; Keywords: comm data processes
9 ;; This file is part of GNU Emacs.
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)
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.
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
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Packing and unpacking of (binary) data structures.
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.
37 ;; Encoded (binary) data is stored in a unibyte string or vector,
38 ;; while the decoded data is stored in an alist with (FIELD . VALUE)
43 ;; Consider the following C structures:
46 ;; unsigned long dest_ip;
47 ;; unsigned long src_ip;
48 ;; unsigned short dest_port;
49 ;; unsigned short src_port;
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 */];
61 ;; struct header header;
62 ;; unsigned char items;
63 ;; unsigned char filler[3];
64 ;; struct data item[/* items */];
67 ;; The corresponding Lisp bindat specification looks like this:
78 ;; (length u16r) ;; little endian order
80 ;; (data vec (length))
84 ;; '((header struct header-spec)
87 ;; (item repeat (items)
88 ;; ((struct data-spec)))))
91 ;; A binary data representation may look like
92 ;; [ 192 168 1 100 192 168 1 101 01 28 21 32 2 0 0 0
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 ]
96 ;; The corresponding decoded structure looks like
99 ;; (dest-ip . [192 168 1 100])
100 ;; (src-ip . [192 168 1 101])
102 ;; (src-port . 5408))
104 ;; (item ((data . [1 2 3 4 5])
109 ;; ((data . [6 7 8 9 10 11 12])
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)
121 ;; Binary Data Structure Specification Format
122 ;; ------------------------------------------
124 ;; The data specification is formatted as follows:
126 ;; SPEC ::= ( ITEM... )
128 ;; ITEM ::= ( [FIELD] TYPE )
129 ;; | ( [FIELD] eval FORM ) -- eval FORM for side-effect only
130 ;; | ( [FIELD] fill LEN ) -- skip LEN bytes
131 ;; | ( [FIELD] align LEN ) -- skip to next multiple of LEN bytes
132 ;; | ( [FIELD] struct SPEC_NAME )
133 ;; | ( [FIELD] union TAG_VAL (TAG SPEC)... [(t SPEC)] )
134 ;; | ( [FIELD] repeat COUNT SPEC )
136 ;; -- In (eval EXPR), the value of the last field is available in
137 ;; the dynamically bound variable `last'.
139 ;; TYPE ::= ( eval EXPR ) -- interpret result as TYPE
140 ;; | u8 | byte -- length 1
141 ;; | u16 | word | short -- length 2, network byte order
142 ;; | u24 -- 3-byte value
143 ;; | u32 | dword | long -- length 4, network byte order
144 ;; | u16r | u24r | u32r -- little endian byte order.
145 ;; | str LEN -- LEN byte string
146 ;; | strz LEN -- LEN byte (zero-terminated) string
147 ;; | vec LEN -- LEN byte vector
148 ;; | ip -- 4 byte vector
149 ;; | bits LEN -- List with bits set in LEN bytes.
151 ;; -- Note: 32 bit values may be limited by emacs' INTEGER
152 ;; implementation limits.
154 ;; -- Example: bits 2 will map bytes 0x1c 0x28 to list (2 3 7 11 13)
156 ;; FIELD ::= ( eval EXPR ) -- use result as NAME
160 ;; | <omitted> | nil -- LEN = 1
165 ;; TAG ::= LISP_CONSTANT
166 ;; | ( eval EXPR ) -- return non-nil if tag match;
167 ;; current TAG_VAL in `tag'.
169 ;; ARG ::= ( eval EXPR ) -- interpret result as ARG
170 ;; | INTEGER_CONSTANT
173 ;; DEREF ::= ( [NAME | INTEGER]... ) -- Field NAME or Array index relative to
174 ;; current structure spec.
175 ;; -- see bindat-get-field
177 ;; A `union' specification
178 ;; ([FIELD] union TAG_VAL (TAG SPEC) ... [(t SPEC)])
179 ;; is interpreted by evalling TAG_VAL and then comparing that to
180 ;; each TAG using equal; if a match is found, the corresponding SPEC
182 ;; If TAG is a form (eval EXPR), EXPR is evalled with `tag' bound to the
183 ;; value of TAG_VAL; the corresponding SPEC is used if the result is non-nil.
184 ;; Finally, if TAG is t, the corresponding SPEC is used unconditionally.
186 ;; An `eval' specification
187 ;; ([FIELD] eval FORM)
188 ;; is interpreted by evalling FORM for its side effects only.
189 ;; If FIELD is specified, the value is bound to that field.
190 ;; The FORM may access and update `raw-data' and `pos' (see `bindat-unpack'),
191 ;; as well as the lisp data structure in `struct'.
195 ;; Helper functions for structure unpacking.
196 ;; Relies on dynamic binding of RAW-DATA and POS
201 (defun bindat--unpack-u8 ()
203 (if (stringp raw-data
)
204 (string-to-char (substring raw-data pos
(1+ pos
)))
206 (setq pos
(1+ pos
))))
208 (defun bindat--unpack-u16 ()
209 (let* ((a (bindat--unpack-u8)) (b (bindat--unpack-u8)))
210 (logior (lsh a
8) b
)))
212 (defun bindat--unpack-u24 ()
213 (let* ((a (bindat--unpack-u16)) (b (bindat--unpack-u8)))
214 (logior (lsh a
8) b
)))
216 (defun bindat--unpack-u32 ()
217 (let* ((a (bindat--unpack-u16)) (b (bindat--unpack-u16)))
218 (logior (lsh a
16) b
)))
220 (defun bindat--unpack-u16r ()
221 (let* ((a (bindat--unpack-u8)) (b (bindat--unpack-u8)))
222 (logior a
(lsh b
8))))
224 (defun bindat--unpack-u24r ()
225 (let* ((a (bindat--unpack-u16r)) (b (bindat--unpack-u8)))
226 (logior a
(lsh b
16))))
228 (defun bindat--unpack-u32r ()
229 (let* ((a (bindat--unpack-u16r)) (b (bindat--unpack-u16r)))
230 (logior a
(lsh b
16))))
232 (defun bindat--unpack-item (type len
)
234 (setq type
'vec len
4))
236 ((memq type
'(u8 byte
))
238 ((memq type
'(u16 word short
))
239 (bindat--unpack-u16))
241 (bindat--unpack-u24))
242 ((memq type
'(u32 dword long
))
243 (bindat--unpack-u32))
245 (bindat--unpack-u16r))
247 (bindat--unpack-u24r))
249 (bindat--unpack-u32r))
251 (let ((bits nil
) (bnum (1- (* 8 len
))) j m
)
253 (if (= (setq m
(bindat--unpack-u8)) 0)
254 (setq bnum
(- bnum
8))
257 (if (/= 0 (logand m j
))
258 (setq bits
(cons bnum bits
)))
263 (let ((s (substring raw-data pos
(+ pos len
))))
264 (setq pos
(+ pos len
))
266 (string-make-unibyte (concat s
)))))
269 (while (and (< i len
) (/= (aref raw-data
(+ pos i
)) 0))
271 (setq s
(substring raw-data pos
(+ pos i
)))
272 (setq pos
(+ pos len
))
274 (string-make-unibyte (concat s
)))))
276 (let ((v (make-vector len
0)) (i 0))
278 (aset v i
(bindat--unpack-u8))
283 (defun bindat--unpack-group (spec)
286 (let* ((item (car spec
))
292 (setq spec
(cdr spec
))
293 (if (and (consp field
) (eq (car field
) 'eval
))
294 (setq field
(eval (car (cdr field
)))))
295 (if (and type
(consp type
) (eq (car type
) 'eval
))
296 (setq type
(eval (car (cdr type
)))))
297 (if (and len
(consp len
) (eq (car len
) 'eval
))
298 (setq len
(eval (car (cdr len
)))))
299 (if (memq field
'(eval fill align struct union
))
304 (if (and (consp len
) (not (eq type
'eval
)))
305 (setq len
(apply 'bindat-get-field struct len
)))
311 (setq data
(eval len
))
314 (setq pos
(+ pos len
)))
316 (while (/= (% pos len
) 0)
317 (setq pos
(1+ pos
))))
319 (setq data
(bindat--unpack-group (eval len
))))
323 (setq data
(cons (bindat--unpack-group (nthcdr tail item
)) data
))
324 (setq index
(1+ index
)))
325 (setq data
(nreverse data
))))
327 (let ((tag len
) (cases (nthcdr tail item
)) case cc
)
329 (setq case
(car cases
)
332 (if (or (equal cc tag
) (equal cc t
)
333 (and (consp cc
) (eval cc
)))
334 (setq data
(bindat--unpack-group (cdr case
))
337 (setq data
(bindat--unpack-item type len
)
341 (setq struct
(cons (cons field data
) struct
))
342 (setq struct
(append data struct
))))))
345 (defun bindat-unpack (spec raw-data
&optional pos
)
346 "Return structured data according to SPEC for binary data in RAW-DATA.
347 RAW-DATA is a string or vector. Optional third arg POS specifies the
348 starting offset in RAW-DATA."
349 (unless pos
(setq pos
0))
350 (bindat--unpack-group spec
))
352 (defun bindat-get-field (struct &rest field
)
353 "In structured data STRUCT, return value of field named FIELD.
354 If multiple field names are specified, use the field names to
355 lookup nested sub-structures in STRUCT, corresponding to the
356 C-language syntax STRUCT.FIELD1.FIELD2.FIELD3...
357 An integer value in the field list is taken as an array index,
358 e.g. corresponding to STRUCT.FIELD1[INDEX2].FIELD3..."
359 (while (and struct field
)
360 (setq struct
(if (integerp (car field
))
361 (nth (car field
) struct
)
362 (let ((val (assq (car field
) struct
)))
363 (if (consp val
) (cdr val
)))))
364 (setq field
(cdr field
)))
368 ;; Calculate raw-data length of structured data
370 (defvar bindat--fixed-length-alist
371 '((u8 .
1) (byte .
1)
372 (u16 .
2) (u16r .
2) (word .
2) (short .
2)
374 (u32 .
4) (u32r .
4) (dword .
4) (long .
4)
377 (defun bindat--length-group (struct spec
)
380 (let* ((item (car spec
))
385 (setq spec
(cdr spec
))
386 (if (and (consp field
) (eq (car field
) 'eval
))
387 (setq field
(eval (car (cdr field
)))))
388 (if (and type
(consp type
) (eq (car type
) 'eval
))
389 (setq type
(eval (car (cdr type
)))))
390 (if (and len
(consp len
) (eq (car len
) 'eval
))
391 (setq len
(eval (car (cdr len
)))))
392 (if (memq field
'(eval fill align struct union
))
397 (if (and (consp len
) (not (eq type
'eval
)))
398 (setq len
(apply 'bindat-get-field struct len
)))
404 (setq struct
(cons (cons field
(eval len
)) struct
))
407 (setq pos
(+ pos len
)))
409 (while (/= (% pos len
) 0)
410 (setq pos
(1+ pos
))))
412 (bindat--length-group
413 (if field
(bindat-get-field struct field
) struct
) (eval len
)))
417 (bindat--length-group (nth index
(bindat-get-field struct field
)) (nthcdr tail item
))
418 (setq index
(1+ index
)))))
420 (let ((tag len
) (cases (nthcdr tail item
)) case cc
)
422 (setq case
(car cases
)
425 (if (or (equal cc tag
) (equal cc t
)
426 (and (consp cc
) (eval cc
)))
428 (bindat--length-group struct
(cdr case
))
429 (setq cases nil
))))))
431 (if (setq type
(assq type bindat--fixed-length-alist
))
432 (setq len
(cdr type
)))
434 (setq last
(bindat-get-field struct field
)))
435 (setq pos
(+ pos len
))))))))
437 (defun bindat-length (spec struct
)
438 "Calculate raw-data length for STRUCT according to bindat specification SPEC."
440 (bindat--length-group struct spec
)
444 ;; Pack structured data into raw-data
446 (defun bindat--pack-u8 (v)
447 (aset raw-data pos
(logand v
255))
450 (defun bindat--pack-u16 (v)
451 (aset raw-data pos
(logand (lsh v -
8) 255))
452 (aset raw-data
(1+ pos
) (logand v
255))
453 (setq pos
(+ pos
2)))
455 (defun bindat--pack-u24 (v)
456 (bindat--pack-u8 (lsh v -
16))
457 (bindat--pack-u16 v
))
459 (defun bindat--pack-u32 (v)
460 (bindat--pack-u16 (lsh v -
16))
461 (bindat--pack-u16 v
))
463 (defun bindat--pack-u16r (v)
464 (aset raw-data
(1+ pos
) (logand (lsh v -
8) 255))
465 (aset raw-data pos
(logand v
255))
466 (setq pos
(+ pos
2)))
468 (defun bindat--pack-u24r (v)
469 (bindat--pack-u16r v
)
470 (bindat--pack-u8 (lsh v -
16)))
472 (defun bindat--pack-u32r (v)
473 (bindat--pack-u16r v
)
474 (bindat--pack-u16r (lsh v -
16)))
476 (defun bindat--pack-item (v type len
)
478 (setq type
'vec len
4))
481 (setq pos
(+ pos len
)))
482 ((memq type
'(u8 byte
))
484 ((memq type
'(u16 word short
))
485 (bindat--pack-u16 v
))
487 (bindat--pack-u24 v
))
488 ((memq type
'(u32 dword long
))
489 (bindat--pack-u32 v
))
491 (bindat--pack-u16r v
))
493 (bindat--pack-u24r v
))
495 (bindat--pack-u32r v
))
497 (let ((bnum (1- (* 8 len
))) j m
)
501 (setq bnum
(- bnum
8))
505 (setq m
(logior m j
)))
508 (bindat--pack-u8 m
))))
509 ((memq type
'(str strz vec
))
510 (let ((l (length v
)) (i 0))
511 (if (> l len
) (setq l len
))
513 (aset raw-data
(+ pos i
) (aref v i
))
515 (setq pos
(+ pos len
))))
517 (setq pos
(+ pos len
)))))
519 (defun bindat--pack-group (struct spec
)
522 (let* ((item (car spec
))
527 (setq spec
(cdr spec
))
528 (if (and (consp field
) (eq (car field
) 'eval
))
529 (setq field
(eval (car (cdr field
)))))
530 (if (and type
(consp type
) (eq (car type
) 'eval
))
531 (setq type
(eval (car (cdr type
)))))
532 (if (and len
(consp len
) (eq (car len
) 'eval
))
533 (setq len
(eval (car (cdr len
)))))
534 (if (memq field
'(eval fill align struct union
))
539 (if (and (consp len
) (not (eq type
'eval
)))
540 (setq len
(apply 'bindat-get-field struct len
)))
546 (setq struct
(cons (cons field
(eval len
)) struct
))
549 (setq pos
(+ pos len
)))
551 (while (/= (% pos len
) 0)
552 (setq pos
(1+ pos
))))
555 (if field
(bindat-get-field struct field
) struct
) (eval len
)))
559 (bindat--pack-group (nth index
(bindat-get-field struct field
)) (nthcdr tail item
))
560 (setq index
(1+ index
)))))
562 (let ((tag len
) (cases (nthcdr tail item
)) case cc
)
564 (setq case
(car cases
)
567 (if (or (equal cc tag
) (equal cc t
)
568 (and (consp cc
) (eval cc
)))
570 (bindat--pack-group struct
(cdr case
))
571 (setq cases nil
))))))
573 (setq last
(bindat-get-field struct field
))
574 (bindat--pack-item last type len
)
577 (defun bindat-pack (spec struct
&optional raw-data pos
)
578 "Return binary data packed according to SPEC for structured data STRUCT.
579 Optional third arg RAW-DATA is a pre-allocated string or vector to unpack into.
580 Optional fourth arg POS is the starting offset into RAW-DATA.
581 Note: The result is a multibyte string; use `string-make-unibyte' on it
582 to make it unibyte if necessary."
583 (let ((no-return raw-data
))
584 (unless pos
(setq pos
0))
585 (unless raw-data
(setq raw-data
(make-vector (+ pos
(bindat-length spec struct
)) 0)))
586 (bindat--pack-group struct spec
)
587 (if no-return nil
(concat raw-data
))))
590 ;; Misc. format conversions
592 (defun bindat-format-vector (vect fmt sep
&optional len
)
593 "Format vector VECT using element format FMT and separator SEP.
594 Result is a string with each element of VECT formatted using FMT and
595 separated by the string SEP. If optional fourth arg LEN is given, use
596 only that many elements from VECT."
598 (setq len
(length vect
)))
599 (let ((i len
) (fmt2 (concat sep fmt
)) (s nil
))
602 s
(cons (format (if (= i
0) fmt fmt2
) (aref vect i
)) s
)))
605 (defun bindat-vector-to-dec (vect &optional sep
)
606 "Format vector VECT in decimal format separated by dots.
607 If optional second arg SEP is a string, use that as separator."
608 (bindat-format-vector vect
"%d" (if (stringp sep
) sep
".")))
610 (defun bindat-vector-to-hex (vect &optional sep
)
611 "Format vector VECT in hex format separated by dots.
612 If optional second arg SEP is a string, use that as separator."
613 (bindat-format-vector vect
"%02x" (if (stringp sep
) sep
":")))
615 (defun bindat-ip-to-string (ip)
616 "Format vector IP as an ip address in dotted notation."
617 (format "%d.%d.%d.%d"
618 (aref ip
0) (aref ip
1) (aref ip
2) (aref ip
3)))
622 ;;; bindat.el ends here