From 6c29d71f456bb0fc6e2f33a3b8e7caacae0a6bb0 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 3 Jun 2017 14:12:20 -0700 Subject: [PATCH] more --- pitfall/binparser/gif-parse.rkt | 6 ++++++ pitfall/binparser/main.rkt | 29 ++++++++++++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/pitfall/binparser/gif-parse.rkt b/pitfall/binparser/gif-parse.rkt index 1c9eb15d..f7fe95fd 100644 --- a/pitfall/binparser/gif-parse.rkt +++ b/pitfall/binparser/gif-parse.rkt @@ -29,3 +29,9 @@ (make-hasheq (list (cons 'logical-screen-descriptor '(162 162 (#f #t #f #f #f #t #f #t) 0 0)) '(signature . "GIF") '(version . "87a"))))) + + +(define-rule bad-bitfield (:bitfield [reserved (:bits 3)] + [disposal (:bits 3 #:type integer?)])) + +(bad-bitfield (bad-bitfield (open-input-bytes #"A"))) \ No newline at end of file diff --git a/pitfall/binparser/main.rkt b/pitfall/binparser/main.rkt index c8ed278c..9ad7dd86 100644 --- a/pitfall/binparser/main.rkt +++ b/pitfall/binparser/main.rkt @@ -158,7 +158,10 @@ (define-seq-style-rule :bitfield bitfield-inner) (define (bitfield-inner #:type [type list?] . rule-procs) - ((make-inner-proc (λ (xs) (bitfield->bytes (append* xs))) 'bitfield) type rule-procs)) + ((make-inner-proc (λ (xs) (let ([bf (append* xs)]) + (unless (zero? (modulo (length bf) 8)) + (raise-result-error ':bitfield (format "total field length is multiple of 8, got length ~a" (length bf)) bf)) + (bitfield->bytes bf))) 'bitfield) type rule-procs)) (define-seq-style-rule :seq seq-inner) @@ -170,18 +173,18 @@ (procedure-rename (λ (x) (define-values (input-proc output-proc output-check) (case-proc type - [hash? - (define rule-proc-names (resolve-duplicates (map procedure-name rule-procs))) - (values (curry list->hash-with-keys rule-proc-names) - (curry hash->list-with-keys rule-proc-names) - (λ (x) - (unless (and (hash? x) (hash-has-keys? x rule-proc-names)) - (raise (binary-problem (format "hash with ~a keys, namely ~a" (length rule-procs) rule-proc-names) x)))))] - [list? (values identity identity - (λ (x) - (unless (and (list? x) (= (length rule-procs) (length x))) - (raise (binary-problem (format "list of ~a values" (length rule-procs)) x)))))] - [else (raise-argument-error sym "not a supported type" type)])) + [hash? + (define rule-proc-names (resolve-duplicates (map procedure-name rule-procs))) + (values (curry list->hash-with-keys rule-proc-names) + (curry hash->list-with-keys rule-proc-names) + (λ (x) + (unless (and (hash? x) (hash-has-keys? x rule-proc-names)) + (raise (binary-problem (format "hash with ~a keys, namely ~a" (length rule-procs) rule-proc-names) x)))))] + [list? (values identity identity + (λ (x) + (unless (and (list? x) (= (length rule-procs) (length x))) + (raise (binary-problem (format "list of ~a values" (length rule-procs)) x)))))] + [else (raise-argument-error sym "not a supported type" type)])) (match x [(? input-port? p) (input-proc (map (λ (rule-proc) (rule-proc p)) rule-procs))] [else