| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
(defvar bindat-raw) |
|---|
| 202 |
(defvar bindat-idx) |
|---|
| 203 |
|
|---|
| 204 |
(defun bindat--unpack-u8 () |
|---|
| 205 |
(prog1 |
|---|
| 206 |
(aref bindat-raw bindat-idx) |
|---|
| 207 |
(setq bindat-idx (1+ bindat-idx)))) |
|---|
| 208 |
|
|---|
| 209 |
(defun bindat--unpack-u16 () |
|---|
| 210 |
(logior (lsh (bindat--unpack-u8) 8) (bindat--unpack-u8))) |
|---|
| 211 |
|
|---|
| 212 |
(defun bindat--unpack-u24 () |
|---|
| 213 |
(logior (lsh (bindat--unpack-u16) 8) (bindat--unpack-u8))) |
|---|
| 214 |
|
|---|
| 215 |
(defun bindat--unpack-u32 () |
|---|
| 216 |
(logior (lsh (bindat--unpack-u16) 16) (bindat--unpack-u16))) |
|---|
| 217 |
|
|---|
| 218 |
(defun bindat--unpack-u16r () |
|---|
| 219 |
(logior (bindat--unpack-u8) (lsh (bindat--unpack-u8) 8))) |
|---|
| 220 |
|
|---|
| 221 |
(defun bindat--unpack-u24r () |
|---|
| 222 |
(logior (bindat--unpack-u16r) (lsh (bindat--unpack-u8) 16))) |
|---|
| 223 |
|
|---|
| 224 |
(defun bindat--unpack-u32r () |
|---|
| 225 |
(logior (bindat--unpack-u16r) (lsh (bindat--unpack-u16r) 16))) |
|---|
| 226 |
|
|---|
| 227 |
(defun bindat--unpack-item (type len &optional vectype) |
|---|
| 228 |
(if (eq type 'ip) |
|---|
| 229 |
(setq type 'vec len 4)) |
|---|
| 230 |
(cond |
|---|
| 231 |
((memq type '(u8 byte)) |
|---|
| 232 |
(bindat--unpack-u8)) |
|---|
| 233 |
((memq type '(u16 word short)) |
|---|
| 234 |
(bindat--unpack-u16)) |
|---|
| 235 |
((eq type 'u24) |
|---|
| 236 |
(bindat--unpack-u24)) |
|---|
| 237 |
((memq type '(u32 dword long)) |
|---|
| 238 |
(bindat--unpack-u32)) |
|---|
| 239 |
((eq type 'u16r) |
|---|
| 240 |
(bindat--unpack-u16r)) |
|---|
| 241 |
((eq type 'u24r) |
|---|
| 242 |
(bindat--unpack-u24r)) |
|---|
| 243 |
((eq type 'u32r) |
|---|
| 244 |
(bindat--unpack-u32r)) |
|---|
| 245 |
((eq type 'bits) |
|---|
| 246 |
(let ((bits nil) (bnum (1- (* 8 len))) j m) |
|---|
| 247 |
(while (>= bnum 0) |
|---|
| 248 |
(if (= (setq m (bindat--unpack-u8)) 0) |
|---|
| 249 |
(setq bnum (- bnum 8)) |
|---|
| 250 |
(setq j 128) |
|---|
| 251 |
(while (> j 0) |
|---|
| 252 |
(if (/= 0 (logand m j)) |
|---|
| 253 |
(setq bits (cons bnum bits))) |
|---|
| 254 |
(setq bnum (1- bnum) |
|---|
| 255 |
j (lsh j -1))))) |
|---|
| 256 |
bits)) |
|---|
| 257 |
((eq type 'str) |
|---|
| 258 |
(let ((s (substring bindat-raw bindat-idx (+ bindat-idx len)))) |
|---|
| 259 |
(setq bindat-idx (+ bindat-idx len)) |
|---|
| 260 |
(if (stringp s) s |
|---|
| 261 |
(string-make-unibyte (concat s))))) |
|---|
| 262 |
((eq type 'strz) |
|---|
| 263 |
(let ((i 0) s) |
|---|
| 264 |
(while (and (< i len) (/= (aref bindat-raw (+ bindat-idx i)) 0)) |
|---|
| 265 |
(setq i (1+ i))) |
|---|
| 266 |
(setq s (substring bindat-raw bindat-idx (+ bindat-idx i))) |
|---|
| 267 |
(setq bindat-idx (+ bindat-idx len)) |
|---|
| 268 |
(if (stringp s) s |
|---|
| 269 |
(string-make-unibyte (concat s))))) |
|---|
| 270 |
((eq type 'vec) |
|---|
| 271 |
(let ((v (make-vector len 0)) (i 0) (vlen 1)) |
|---|
| 272 |
(if (consp vectype) |
|---|
| 273 |
(setq vlen (nth 1 vectype) |
|---|
| 274 |
vectype (nth 2 vectype)) |
|---|
| 275 |
(setq type (or vectype 'u8) |
|---|
| 276 |
vectype nil)) |
|---|
| 277 |
(while (< i len) |
|---|
| 278 |
(aset v i (bindat--unpack-item type vlen vectype)) |
|---|
| 279 |
(setq i (1+ i))) |
|---|
| 280 |
v)) |
|---|
| 281 |
(t nil))) |
|---|
| 282 |
|
|---|
| 283 |
(defun bindat--unpack-group (spec) |
|---|
| 284 |
(let (struct last) |
|---|
| 285 |
(while spec |
|---|
| 286 |
(let* ((item (car spec)) |
|---|
| 287 |
(field (car item)) |
|---|
| 288 |
(type (nth 1 item)) |
|---|
| 289 |
(len (nth 2 item)) |
|---|
| 290 |
(vectype (and (eq type 'vec) (nth 3 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) |
|---|
| 315 |
(setq bindat-idx (+ bindat-idx len))) |
|---|
| 316 |
((eq type 'align) |
|---|
| 317 |
(while (/= (% bindat-idx len) 0) |
|---|
| 318 |
(setq bindat-idx (1+ bindat-idx)))) |
|---|
| 319 |
((eq type 'struct) |
|---|
| 320 |
(setq data (bindat--unpack-group (eval len)))) |
|---|
| 321 |
((eq type 'repeat) |
|---|
| 322 |
(let ((index 0) (count len)) |
|---|
| 323 |
(while (< index count) |
|---|
| 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 vectype) |
|---|
| 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)) |
|---|
| 345 |
|
|---|
| 346 |
(defun bindat-unpack (spec bindat-raw &optional bindat-idx) |
|---|
| 347 |
"Return structured data according to SPEC for binary data in BINDAT-RAW. |
|---|
| 348 |
BINDAT-RAW is a unibyte string or vector. |
|---|
| 349 |
Optional third arg BINDAT-IDX specifies the starting offset in BINDAT-RAW." |
|---|
| 350 |
(when (multibyte-string-p bindat-raw) |
|---|
| 351 |
(error "String is multibyte")) |
|---|
| 352 |
(unless bindat-idx (setq bindat-idx 0)) |
|---|
| 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 |
|
|---|
| 371 |
|
|---|
| 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 |
(vectype (and (eq type 'vec) (nth 3 item))) |
|---|
| 388 |
(tail 3)) |
|---|
| 389 |
(setq spec (cdr spec)) |
|---|
| 390 |
(if (and (consp field) (eq (car field) 'eval)) |
|---|
| 391 |
(setq field (eval (car (cdr field))))) |
|---|
| 392 |
(if (and type (consp type) (eq (car type) 'eval)) |
|---|
| 393 |
(setq type (eval (car (cdr type))))) |
|---|
| 394 |
(if (and len (consp len) (eq (car len) 'eval)) |
|---|
| 395 |
(setq len (eval (car (cdr len))))) |
|---|
| 396 |
(if (memq field '(eval fill align struct union)) |
|---|
| 397 |
(setq tail 2 |
|---|
| 398 |
len type |
|---|
| 399 |
type field |
|---|
| 400 |
field nil)) |
|---|
| 401 |
(if (and (consp len) (not (eq type 'eval))) |
|---|
| 402 |
(setq len (apply 'bindat-get-field struct len))) |
|---|
| 403 |
(if (not len) |
|---|
| 404 |
(setq len 1)) |
|---|
| 405 |
(while (eq type 'vec) |
|---|
| 406 |
(let ((vlen 1)) |
|---|
| 407 |
(if (consp vectype) |
|---|
| 408 |
(setq len (* len (nth 1 vectype)) |
|---|
| 409 |
type (nth 2 vectype)) |
|---|
| 410 |
(setq type (or vectype 'u8) |
|---|
| 411 |
vectype nil)))) |
|---|
| 412 |
(cond |
|---|
| 413 |
((eq type 'eval) |
|---|
| 414 |
(if field |
|---|
| 415 |
(setq struct (cons (cons field (eval len)) struct)) |
|---|
| 416 |
(eval len))) |
|---|
| 417 |
((eq type 'fill) |
|---|
| 418 |
(setq bindat-idx (+ bindat-idx len))) |
|---|
| 419 |
((eq type 'align) |
|---|
| 420 |
(while (/= (% bindat-idx len) 0) |
|---|
| 421 |
(setq bindat-idx (1+ bindat-idx)))) |
|---|
| 422 |
((eq type 'struct) |
|---|
| 423 |
(bindat--length-group |
|---|
| 424 |
(if field (bindat-get-field struct field) struct) (eval len))) |
|---|
| 425 |
((eq type 'repeat) |
|---|
| 426 |
(let ((index 0) (count len)) |
|---|
| 427 |
(while (< index count) |
|---|
| 428 |
(bindat--length-group |
|---|
| 429 |
(nth index (bindat-get-field struct field)) |
|---|
| 430 |
(nthcdr tail item)) |
|---|
| 431 |
(setq index (1+ index))))) |
|---|
| 432 |
((eq type 'union) |
|---|
| 433 |
(let ((tag len) (cases (nthcdr tail item)) case cc) |
|---|
| 434 |
(while cases |
|---|
| 435 |
(setq case (car cases) |
|---|
| 436 |
cases (cdr cases) |
|---|
| 437 |
cc (car case)) |
|---|
| 438 |
(if (or (equal cc tag) (equal cc t) |
|---|
| 439 |
(and (consp cc) (eval cc))) |
|---|
| 440 |
(progn |
|---|
| 441 |
(bindat--length-group struct (cdr case)) |
|---|
| 442 |
(setq cases nil)))))) |
|---|
| 443 |
(t |
|---|
| 444 |
(if (setq type (assq type bindat--fixed-length-alist)) |
|---|
| 445 |
(setq len (* len (cdr type)))) |
|---|
| 446 |
(if field |
|---|
| 447 |
(setq last (bindat-get-field struct field))) |
|---|
| 448 |
(setq bindat-idx (+ bindat-idx len)))))))) |
|---|
| 449 |
|
|---|
| 450 |
(defun bindat-length (spec struct) |
|---|
| 451 |
"Calculate bindat-raw length for STRUCT according to bindat SPEC." |
|---|
| 452 |
(let ((bindat-idx 0)) |
|---|
| 453 |
(bindat--length-group struct spec) |
|---|
| 454 |
bindat-idx)) |
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
(defun bindat--pack-u8 (v) |
|---|
| 460 |
(aset bindat-raw bindat-idx (logand v 255)) |
|---|
| 461 |
(setq bindat-idx (1+ bindat-idx))) |
|---|
| 462 |
|
|---|
| 463 |
(defun bindat--pack-u16 (v) |
|---|
| 464 |
(aset bindat-raw bindat-idx (logand (lsh v -8) 255)) |
|---|
| 465 |
(aset bindat-raw (1+ bindat-idx) (logand v 255)) |
|---|
| 466 |
(setq bindat-idx (+ bindat-idx 2))) |
|---|
| 467 |
|
|---|
| 468 |
(defun bindat--pack-u24 (v) |
|---|
| 469 |
(bindat--pack-u8 (lsh v -16)) |
|---|
| 470 |
(bindat--pack-u16 v)) |
|---|
| 471 |
|
|---|
| 472 |
(defun bindat--pack-u32 (v) |
|---|
| 473 |
(bindat--pack-u16 (lsh v -16)) |
|---|
| 474 |
(bindat--pack-u16 v)) |
|---|
| 475 |
|
|---|
| 476 |
(defun bindat--pack-u16r (v) |
|---|
| 477 |
(aset bindat-raw (1+ bindat-idx) (logand (lsh v -8) 255)) |
|---|
| 478 |
(aset bindat-raw bindat-idx (logand v 255)) |
|---|
| 479 |
(setq bindat-idx (+ bindat-idx 2))) |
|---|
| 480 |
|
|---|
| 481 |
(defun bindat--pack-u24r (v) |
|---|
| 482 |
(bindat--pack-u16r v) |
|---|
| 483 |
(bindat--pack-u8 (lsh v -16))) |
|---|
| 484 |
|
|---|
| 485 |
(defun bindat--pack-u32r (v) |
|---|
| 486 |
(bindat--pack-u16r v) |
|---|
| 487 |
(bindat--pack-u16r (lsh v -16))) |
|---|
| 488 |
|
|---|
| 489 |
(defun bindat--pack-item (v type len &optional vectype) |
|---|
| 490 |
(if (eq type 'ip) |
|---|
| 491 |
(setq type 'vec len 4)) |
|---|
| 492 |
(cond |
|---|
| 493 |
((null v) |
|---|
| 494 |
(setq bindat-idx (+ bindat-idx len))) |
|---|
| 495 |
((memq type '(u8 byte)) |
|---|
| 496 |
(bindat--pack-u8 v)) |
|---|
| 497 |
((memq type '(u16 word short)) |
|---|
| 498 |
(bindat--pack-u16 v)) |
|---|
| 499 |
((eq type 'u24) |
|---|
| 500 |
(bindat--pack-u24 v)) |
|---|
| 501 |
((memq type '(u32 dword long)) |
|---|
| 502 |
(bindat--pack-u32 v)) |
|---|
| 503 |
((eq type 'u16r) |
|---|
| 504 |
(bindat--pack-u16r v)) |
|---|
| 505 |
((eq type 'u24r) |
|---|
| 506 |
(bindat--pack-u24r v)) |
|---|
| 507 |
((eq type 'u32r) |
|---|
| 508 |
(bindat--pack-u32r v)) |
|---|
| 509 |
((eq type 'bits) |
|---|
| 510 |
(let ((bnum (1- (* 8 len))) j m) |
|---|
| 511 |
(while (>= bnum 0) |
|---|
| 512 |
(setq m 0) |
|---|
| 513 |
(if (null v) |
|---|
| 514 |
(setq bnum (- bnum 8)) |
|---|
| 515 |
(setq j 128) |
|---|
| 516 |
(while (> j 0) |
|---|
| 517 |
(if (memq bnum v) |
|---|
| 518 |
(setq m (logior m j))) |
|---|
| 519 |
(setq bnum (1- bnum) |
|---|
| 520 |
j (lsh j -1)))) |
|---|
| 521 |
(bindat--pack-u8 m)))) |
|---|
| 522 |
((memq type '(str strz)) |
|---|
| 523 |
(let ((l (length v)) (i 0)) |
|---|
| 524 |
(if (> l len) (setq l len)) |
|---|
| 525 |
(while (< i l) |
|---|
| 526 |
(aset bindat-raw (+ bindat-idx i) (aref v i)) |
|---|
| 527 |
(setq i (1+ i))) |
|---|
| 528 |
(setq bindat-idx (+ bindat-idx len)))) |
|---|
| 529 |
((eq type 'vec) |
|---|
| 530 |
(let ((l (length v)) (i 0) (vlen 1)) |
|---|
| 531 |
(if (consp vectype) |
|---|
| 532 |
(setq vlen (nth 1 vectype) |
|---|
| 533 |
vectype (nth 2 vectype)) |
|---|
| 534 |
(setq type (or vectype 'u8) |
|---|
| 535 |
vectype nil)) |
|---|
| 536 |
(if (> l len) (setq l len)) |
|---|
| 537 |
(while (< i l) |
|---|
| 538 |
(bindat--pack-item (aref v i) type vlen vectype) |
|---|
| 539 |
(setq i (1+ i))))) |
|---|
| 540 |
(t |
|---|
| 541 |
(setq bindat-idx (+ bindat-idx len))))) |
|---|
| 542 |
|
|---|
| 543 |
(defun bindat--pack-group (struct spec) |
|---|
| 544 |
(let (last) |
|---|
| 545 |
(while spec |
|---|
| 546 |
(let* ((item (car spec)) |
|---|
| 547 |
(field (car item)) |
|---|
| 548 |
(type (nth 1 item)) |
|---|
| 549 |
(len (nth 2 item)) |
|---|
| 550 |
(vectype (and (eq type 'vec) (nth 3 item))) |
|---|
| 551 |
(tail 3)) |
|---|
| 552 |
(setq spec (cdr spec)) |
|---|
| 553 |
(if (and (consp field) (eq (car field) 'eval)) |
|---|
| 554 |
(setq field (eval (car (cdr field))))) |
|---|
| 555 |
(if (and type (consp type) (eq (car type) 'eval)) |
|---|
| 556 |
(setq type (eval (car (cdr type))))) |
|---|
| 557 |
(if (and len (consp len) (eq (car len) 'eval)) |
|---|
| 558 |
(setq len (eval (car (cdr len))))) |
|---|
| 559 |
(if (memq field '(eval fill align struct union)) |
|---|
| 560 |
(setq tail 2 |
|---|
| 561 |
len type |
|---|
| 562 |
type field |
|---|
| 563 |
field nil)) |
|---|
| 564 |
(if (and (consp len) (not (eq type 'eval))) |
|---|
| 565 |
(setq len (apply 'bindat-get-field struct len))) |
|---|
| 566 |
(if (not len) |
|---|
| 567 |
(setq len 1)) |
|---|
| 568 |
(cond |
|---|
| 569 |
((eq type 'eval) |
|---|
| 570 |
(if field |
|---|
| 571 |
(setq struct (cons (cons field (eval len)) struct)) |
|---|
| 572 |
(eval len))) |
|---|
| 573 |
((eq type 'fill) |
|---|
| 574 |
(setq bindat-idx (+ bindat-idx len))) |
|---|
| 575 |
((eq type 'align) |
|---|
| 576 |
(while (/= (% bindat-idx len) 0) |
|---|
| 577 |
(setq bindat-idx (1+ bindat-idx)))) |
|---|
| 578 |
((eq type 'struct) |
|---|
| 579 |
(bindat--pack-group |
|---|
| 580 |
(if field (bindat-get-field struct field) struct) (eval len))) |
|---|
| 581 |
((eq type 'repeat) |
|---|
| 582 |
(let ((index 0) (count len)) |
|---|
| 583 |
(while (< index count) |
|---|
| 584 |
(bindat--pack-group |
|---|
| 585 |
(nth index (bindat-get-field struct field)) |
|---|
| 586 |
(nthcdr tail item)) |
|---|
| 587 |
(setq index (1+ index))))) |
|---|
| 588 |
((eq type 'union) |
|---|
| 589 |
(let ((tag len) (cases (nthcdr tail item)) case cc) |
|---|
| 590 |
(while cases |
|---|
| 591 |
(setq case (car cases) |
|---|
| 592 |
cases (cdr cases) |
|---|
| 593 |
cc (car case)) |
|---|
| 594 |
(if (or (equal cc tag) (equal cc t) |
|---|
| 595 |
(and (consp cc) (eval cc))) |
|---|
| 596 |
(progn |
|---|
| 597 |
(bindat--pack-group struct (cdr case)) |
|---|
| 598 |
(setq cases nil)))))) |
|---|
| 599 |
(t |
|---|
| 600 |
(setq last (bindat-get-field struct field)) |
|---|
| 601 |
(bindat--pack-item last type len vectype) |
|---|
| 602 |
)))))) |
|---|
| 603 |
|
|---|
| 604 |
(defun bindat-pack (spec struct &optional bindat-raw bindat-idx) |
|---|
| 605 |
"Return binary data packed according to SPEC for structured data STRUCT. |
|---|
| 606 |
Optional third arg BINDAT-RAW is a pre-allocated unibyte string or vector to |
|---|
| 607 |
pack into. |
|---|
| 608 |
Optional fourth arg BINDAT-IDX is the starting offset into BINDAT-RAW." |
|---|
| 609 |
(when (multibyte-string-p bindat-raw) |
|---|
| 610 |
(error "Pre-allocated string is multibyte")) |
|---|
| 611 |
(let ((no-return bindat-raw)) |
|---|
| 612 |
(unless bindat-idx (setq bindat-idx 0)) |
|---|
| 613 |
(unless bindat-raw |
|---|
| 614 |
(setq bindat-raw (make-vector (+ bindat-idx (bindat-length spec struct)) 0))) |
|---|
| 615 |
(bindat--pack-group struct spec) |
|---|
| 616 |
(if no-return nil (concat bindat-raw)))) |
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
(defun bindat-format-vector (vect fmt sep &optional len) |
|---|
| 622 |
"Format vector VECT using element format FMT and separator SEP. |
|---|
| 623 |
Result is a string with each element of VECT formatted using FMT and |
|---|
| 624 |
separated by the string SEP. If optional fourth arg LEN is given, use |
|---|
| 625 |
only that many elements from VECT." |
|---|
| 626 |
(unless len |
|---|
| 627 |
(setq len (length vect))) |
|---|
| 628 |
(let ((i len) (fmt2 (concat sep fmt)) (s nil)) |
|---|
| 629 |
(while (> i 0) |
|---|
| 630 |
(setq i (1- i) |
|---|
| 631 |
s (cons (format (if (= i 0) fmt fmt2) (aref vect i)) s))) |
|---|
| 632 |
(apply 'concat s))) |
|---|
| 633 |
|
|---|
| 634 |
(defun bindat-vector-to-dec (vect &optional sep) |
|---|
| 635 |
"Format vector VECT in decimal format separated by dots. |
|---|
| 636 |
If optional second arg SEP is a string, use that as separator." |
|---|
| 637 |
(bindat-format-vector vect "%d" (if (stringp sep) sep "."))) |
|---|
| 638 |
|
|---|
| 639 |
(defun bindat-vector-to-hex (vect &optional sep) |
|---|
| 640 |
"Format vector VECT in hex format separated by dots. |
|---|
| 641 |
If optional second arg SEP is a string, use that as separator." |
|---|
| 642 |
(bindat-format-vector vect "%02x" (if (stringp sep) sep ":"))) |
|---|
| 643 |
|
|---|
| 644 |
(defun bindat-ip-to-string (ip) |
|---|
| 645 |
"Format vector IP as an ip address in dotted notation. |
|---|
| 646 |
The port (if any) is omitted. IP can be a string, as well." |
|---|
| 647 |
(if (vectorp ip) |
|---|
| 648 |
(format-network-address ip t) |
|---|
| 649 |
(format "%d.%d.%d.%d" |
|---|
| 650 |
(aref ip 0) (aref ip 1) (aref ip 2) (aref ip 3)))) |
|---|
| 651 |
|
|---|
| 652 |
(provide 'bindat) |
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|