[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] Changes to emacs/lisp/emacs-lisp/bindat.el
From: |
Miles Bader |
Subject: |
[Emacs-diffs] Changes to emacs/lisp/emacs-lisp/bindat.el |
Date: |
Fri, 04 Apr 2003 01:21:56 -0500 |
Index: emacs/lisp/emacs-lisp/bindat.el
diff -c emacs/lisp/emacs-lisp/bindat.el:1.2 emacs/lisp/emacs-lisp/bindat.el:1.3
*** emacs/lisp/emacs-lisp/bindat.el:1.2 Sun Oct 27 16:58:18 2002
--- emacs/lisp/emacs-lisp/bindat.el Tue Feb 4 07:53:34 2003
***************
*** 33,54 ****
;; and encoding binary data formats like these is made simple using a
;; structure specification which closely resembles the C style
;; structure declarations.
! ;;
;; Encoded (binary) data is stored in a unibyte string or vector,
! ;; while the decoded data is stored in an alist with (FIELD . VALUE)
;; pairs.
;; Example:
!
;; Consider the following C structures:
! ;;
;; struct header {
;; unsigned long dest_ip;
;; unsigned long src_ip;
;; unsigned short dest_port;
;; unsigned short src_port;
;; };
! ;;
;; struct data {
;; unsigned char type;
;; unsigned char opcode;
--- 33,54 ----
;; and encoding binary data formats like these is made simple using a
;; structure specification which closely resembles the C style
;; structure declarations.
! ;;
;; Encoded (binary) data is stored in a unibyte string or vector,
! ;; while the decoded data is stored in an alist with (FIELD . VALUE)
;; pairs.
;; Example:
!
;; Consider the following C structures:
! ;;
;; struct header {
;; unsigned long dest_ip;
;; unsigned long src_ip;
;; unsigned short dest_port;
;; unsigned short src_port;
;; };
! ;;
;; struct data {
;; unsigned char type;
;; unsigned char opcode;
***************
*** 56,77 ****
;; unsigned char id[8]; /* nul-terminated string */
;; unsigned char data[/* (length + 3) & ~3 */];
;; };
! ;;
;; struct packet {
;; struct header header;
;; unsigned char items;
;; unsigned char filler[3];
;; struct data item[/* items */];
;; };
! ;;
;; The corresponding Lisp bindat specification looks like this:
! ;;
;; (setq header-spec
;; '((dest-ip ip)
;; (src-ip ip)
;; (dest-port u16)
;; (src-port u16)))
! ;;
;; (setq data-spec
;; '((type u8)
;; (opcode u8)
--- 56,77 ----
;; unsigned char id[8]; /* nul-terminated string */
;; unsigned char data[/* (length + 3) & ~3 */];
;; };
! ;;
;; struct packet {
;; struct header header;
;; unsigned char items;
;; unsigned char filler[3];
;; struct data item[/* items */];
;; };
! ;;
;; The corresponding Lisp bindat specification looks like this:
! ;;
;; (setq header-spec
;; '((dest-ip ip)
;; (src-ip ip)
;; (dest-port u16)
;; (src-port u16)))
! ;;
;; (setq data-spec
;; '((type u8)
;; (opcode u8)
***************
*** 79,98 ****
;; (id strz 8)
;; (data vec (length))
;; (align 4)))
! ;;
;; (setq packet-spec
;; '((header struct header-spec)
;; (items u8)
;; (fill 3)
;; (item repeat (items)
;; ((struct data-spec)))))
! ;;
;;
;; A binary data representation may look like
! ;; [ 192 168 1 100 192 168 1 101 01 28 21 32 2 0 0 0
;; 2 3 5 0 ?A ?B ?C ?D ?E ?F 0 0 1 2 3 4 5 0 0 0
;; 1 4 7 0 ?B ?C ?D ?E ?F ?G 0 0 6 7 8 9 10 11 12 0 ]
! ;;
;; The corresponding decoded structure looks like
;;
;; ((header
--- 79,98 ----
;; (id strz 8)
;; (data vec (length))
;; (align 4)))
! ;;
;; (setq packet-spec
;; '((header struct header-spec)
;; (items u8)
;; (fill 3)
;; (item repeat (items)
;; ((struct data-spec)))))
! ;;
;;
;; A binary data representation may look like
! ;; [ 192 168 1 100 192 168 1 101 01 28 21 32 2 0 0 0
;; 2 3 5 0 ?A ?B ?C ?D ?E ?F 0 0 1 2 3 4 5 0 0 0
;; 1 4 7 0 ?B ?C ?D ?E ?F ?G 0 0 6 7 8 9 10 11 12 0 ]
! ;;
;; The corresponding decoded structure looks like
;;
;; ((header
***************
*** 176,182 ****
;; A `union' specification
;; ([FIELD] union TAG_VAL (TAG SPEC) ... [(t SPEC)])
! ;; is interpreted by evalling TAG_VAL and then comparing that to
;; each TAG using equal; if a match is found, the corresponding SPEC
;; is used.
;; If TAG is a form (eval EXPR), EXPR is evalled with `tag' bound to the
--- 176,182 ----
;; A `union' specification
;; ([FIELD] union TAG_VAL (TAG SPEC) ... [(t SPEC)])
! ;; is interpreted by evalling TAG_VAL and then comparing that to
;; each TAG using equal; if a match is found, the corresponding SPEC
;; is used.
;; If TAG is a form (eval EXPR), EXPR is evalled with `tag' bound to the
***************
*** 204,210 ****
(string-to-char (substring raw-data pos (1+ pos)))
(aref raw-data pos))
(setq pos (1+ pos))))
!
(defun bindat--unpack-u16 ()
(let* ((a (bindat--unpack-u8)) (b (bindat--unpack-u8)))
(logior (lsh a 8) b)))
--- 204,210 ----
(string-to-char (substring raw-data pos (1+ pos)))
(aref raw-data pos))
(setq pos (1+ pos))))
!
(defun bindat--unpack-u16 ()
(let* ((a (bindat--unpack-u8)) (b (bindat--unpack-u8)))
(logior (lsh a 8) b)))
***************
*** 341,347 ****
(setq struct (cons (cons field data) struct))
(setq struct (append data struct))))))
struct))
!
(defun bindat-unpack (spec raw-data &optional pos)
"Return structured data according to SPEC for binary data in RAW-DATA.
RAW-DATA is a string or vector. Optional third arg POS specifies the
--- 341,347 ----
(setq struct (cons (cons field data) struct))
(setq struct (append data struct))))))
struct))
!
(defun bindat-unpack (spec raw-data &optional pos)
"Return structured data according to SPEC for binary data in RAW-DATA.
RAW-DATA is a string or vector. Optional third arg POS specifies the
***************
*** 365,371 ****
struct)
! ;; Calculate raw-data length of structured data
(defvar bindat--fixed-length-alist
'((u8 . 1) (byte . 1)
--- 365,371 ----
struct)
! ;; Calculate raw-data length of structured data
(defvar bindat--fixed-length-alist
'((u8 . 1) (byte . 1)
***************
*** 398,404 ****
(setq len (apply 'bindat-get-field struct len)))
(if (not len)
(setq len 1))
! (cond
((eq type 'eval)
(if field
(setq struct (cons (cons field (eval len)) struct))
--- 398,404 ----
(setq len (apply 'bindat-get-field struct len)))
(if (not len)
(setq len 1))
! (cond
((eq type 'eval)
(if field
(setq struct (cons (cons field (eval len)) struct))
***************
*** 446,452 ****
(defun bindat--pack-u8 (v)
(aset raw-data pos (logand v 255))
(setq pos (1+ pos)))
!
(defun bindat--pack-u16 (v)
(aset raw-data pos (logand (lsh v -8) 255))
(aset raw-data (1+ pos) (logand v 255))
--- 446,452 ----
(defun bindat--pack-u8 (v)
(aset raw-data pos (logand v 255))
(setq pos (1+ pos)))
!
(defun bindat--pack-u16 (v)
(aset raw-data pos (logand (lsh v -8) 255))
(aset raw-data (1+ pos) (logand v 255))
***************
*** 513,519 ****
(aset raw-data (+ pos i) (aref v i))
(setq i (1+ i)))
(setq pos (+ pos len))))
! (t
(setq pos (+ pos len)))))
(defun bindat--pack-group (struct spec)
--- 513,519 ----
(aset raw-data (+ pos i) (aref v i))
(setq i (1+ i)))
(setq pos (+ pos len))))
! (t
(setq pos (+ pos len)))))
(defun bindat--pack-group (struct spec)
***************
*** 540,546 ****
(setq len (apply 'bindat-get-field struct len)))
(if (not len)
(setq len 1))
! (cond
((eq type 'eval)
(if field
(setq struct (cons (cons field (eval len)) struct))
--- 540,546 ----
(setq len (apply 'bindat-get-field struct len)))
(if (not len)
(setq len 1))
! (cond
((eq type 'eval)
(if field
(setq struct (cons (cons field (eval len)) struct))
***************
*** 601,607 ****
(setq i (1- i)
s (cons (format (if (= i 0) fmt fmt2) (aref vect i)) s)))
(apply 'concat s)))
!
(defun bindat-vector-to-dec (vect &optional sep)
"Format vector VECT in decimal format separated by dots.
If optional second arg SEP is a string, use that as separator."
--- 601,607 ----
(setq i (1- i)
s (cons (format (if (= i 0) fmt fmt2) (aref vect i)) s)))
(apply 'concat s)))
!
(defun bindat-vector-to-dec (vect &optional sep)
"Format vector VECT in decimal format separated by dots.
If optional second arg SEP is a string, use that as separator."
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] Changes to emacs/lisp/emacs-lisp/bindat.el,
Miles Bader <=