aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2018-09-20 17:43:42 -0700
committerPaul Eggert2018-09-20 17:44:24 -0700
commit7f3877e83405a089b580fe9d0342dc0b6c08cbfc (patch)
tree6d6fea8909715afa9ef441b297131e14ce20bf7b
parentd6f3c2cf0628afaefe428140d8c6615e925044ad (diff)
downloademacs-7f3877e83405a089b580fe9d0342dc0b6c08cbfc.tar.gz
emacs-7f3877e83405a089b580fe9d0342dc0b6c08cbfc.zip
Bindat examples in source, not manual
* doc/lispref/processes.texi (Bindat Examples): Remove, fixing a FIXME in the manual. The long example had bitrotted to some extent, compared to the more-up-to-date example in bindat.el commentary, which apparently what people were referring to anyway. The short example was confusing and not that useful and will be obsolescent anyway if we change timestamp format.
-rw-r--r--doc/lispref/elisp.texi1
-rw-r--r--doc/lispref/processes.texi130
2 files changed, 0 insertions, 131 deletions
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index 0a445a36bd3..a615fcb4b7c 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -1391,7 +1391,6 @@ Packing and Unpacking Byte Arrays
1391 1391
1392* Bindat Spec:: Describing data layout. 1392* Bindat Spec:: Describing data layout.
1393* Bindat Functions:: Doing the unpacking and packing. 1393* Bindat Functions:: Doing the unpacking and packing.
1394* Bindat Examples:: Samples of what bindat.el can do for you!
1395 1394
1396Emacs Display 1395Emacs Display
1397 1396
diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi
index f9ba703300b..89ad1cf8381 100644
--- a/doc/lispref/processes.texi
+++ b/doc/lispref/processes.texi
@@ -3126,7 +3126,6 @@ direction is also known as @dfn{serializing} or @dfn{packing}.
3126@menu 3126@menu
3127* Bindat Spec:: Describing data layout. 3127* Bindat Spec:: Describing data layout.
3128* Bindat Functions:: Doing the unpacking and packing. 3128* Bindat Functions:: Doing the unpacking and packing.
3129* Bindat Examples:: Samples of what bindat.el can do for you!
3130@end menu 3129@end menu
3131 3130
3132@node Bindat Spec 3131@node Bindat Spec
@@ -3369,132 +3368,3 @@ dotted notation.
3369 @result{} "127.0.0.1" 3368 @result{} "127.0.0.1"
3370@end example 3369@end example
3371@end defun 3370@end defun
3372
3373@node Bindat Examples
3374@subsection Examples of Byte Unpacking and Packing
3375@c FIXME? This seems a very long example for something that is not used
3376@c very often. As of 25.2, gdb-mi.el is the only user of bindat.el in Emacs.
3377@c Maybe one or both of these examples should just be moved to the
3378@c commentary of bindat.el.
3379
3380 Here are two complete examples that use bindat.el.
3381The first shows simple byte packing:
3382
3383@lisp
3384(require 'bindat)
3385
3386(defun rfc868-payload ()
3387 (bindat-pack
3388 '((now-hi u16)
3389 (now-lo u16))
3390 ;; Emacs uses Unix epoch, while RFC868 epoch
3391 ;; is 1900-01-01 00:00:00, which is 2208988800
3392 ;; (or #x83aa7e80) seconds more.
3393 (let ((now (time-add nil '(#x83aa #x7e80))))
3394 `((now-hi . ,(car now))
3395 (now-lo . ,(cadr now))))))
3396
3397(let ((s (rfc868-payload)))
3398 (list (multibyte-string-p s)
3399 (mapconcat (lambda (byte)
3400 (format "%02x" byte))
3401 s " ")
3402 (current-time-string)))
3403 @result{} (nil "dc 6d 17 01" "Fri Mar 10 13:13:53 2017")
3404@end lisp
3405
3406The following is an example of defining and unpacking a complex
3407structure. Consider the following C structures:
3408
3409@example
3410struct header @{
3411 unsigned long dest_ip;
3412 unsigned long src_ip;
3413 unsigned short dest_port;
3414 unsigned short src_port;
3415@};
3416
3417struct data @{
3418 unsigned char type;
3419 unsigned char opcode;
3420 unsigned short length; /* in network byte order */
3421 unsigned char id[8]; /* null-terminated string */
3422 unsigned char data[/* (length + 3) & ~3 */];
3423@};
3424
3425struct packet @{
3426 struct header header;
3427 unsigned long counters[2]; /* in little endian order */
3428 unsigned char items;
3429 unsigned char filler[3];
3430 struct data item[/* items */];
3431
3432@};
3433@end example
3434
3435The corresponding data layout specification is:
3436
3437@lisp
3438(setq header-spec
3439 '((dest-ip ip)
3440 (src-ip ip)
3441 (dest-port u16)
3442 (src-port u16)))
3443
3444(setq data-spec
3445 '((type u8)
3446 (opcode u8)
3447 (length u16) ; network byte order
3448 (id strz 8)
3449 (data vec (length))
3450 (align 4)))
3451
3452(setq packet-spec
3453 '((header struct header-spec)
3454 (counters vec 2 u32r) ; little endian order
3455 (items u8)
3456 (fill 3)
3457 (item repeat (items)
3458 (struct data-spec))))
3459@end lisp
3460
3461A binary data representation is:
3462
3463@lisp
3464(setq binary-data
3465 [ 192 168 1 100 192 168 1 101 01 28 21 32
3466 160 134 1 0 5 1 0 0 2 0 0 0
3467 2 3 0 5 ?A ?B ?C ?D ?E ?F 0 0 1 2 3 4 5 0 0 0
3468 1 4 0 7 ?B ?C ?D ?E ?F ?G 0 0 6 7 8 9 10 11 12 0 ])
3469@end lisp
3470
3471The corresponding decoded structure is:
3472
3473@lisp
3474(setq decoded (bindat-unpack packet-spec binary-data))
3475 @result{}
3476((header
3477 (dest-ip . [192 168 1 100])
3478 (src-ip . [192 168 1 101])
3479 (dest-port . 284)
3480 (src-port . 5408))
3481 (counters . [100000 261])
3482 (items . 2)
3483 (item ((data . [1 2 3 4 5])
3484 (id . "ABCDEF")
3485 (length . 5)
3486 (opcode . 3)
3487 (type . 2))
3488 ((data . [6 7 8 9 10 11 12])
3489 (id . "BCDEFG")
3490 (length . 7)
3491 (opcode . 4)
3492 (type . 1))))
3493@end lisp
3494
3495An example of fetching data from this structure:
3496
3497@lisp
3498(bindat-get-field decoded 'item 1 'id)
3499 @result{} "BCDEFG"
3500@end lisp