source: project/release/5/message-digest-primitive/trunk/message-digest-primitive.scm

Last change on this file was 36730, checked in by Kon Lovett, 10 months ago

common message-digest-primitive-context type

File size: 6.7 KB
Line 
1;;;; message-digest-primitive.scm  -*- Scheme -*-
2;;;; Kon Lovett, Jul '18
3;;;; Kon Lovett, Aug '17
4;;;; Kon Lovett, Apr '12
5;;;; Kon Lovett, May '10 (message-digest.scm)
6;;;; Kon Lovett, Jan '06 (message-digest.scm)
7
8;; Issues
9;;
10;; - see tiger-hash , sha2 , sha1 , ripemd , md5 , hashes
11;;
12;; - synthesize raw-update from update
13
14(module message-digest-primitive
15
16(;export
17  ;
18  make-message-digest-primitive-context
19  ; Algorithm API
20  make-message-digest-primitive
21  message-digest-primitive? check-message-digest-primitive error-message-digest-primitive
22  message-digest-primitive-name
23  message-digest-primitive-block-length
24  message-digest-primitive-context-info
25  message-digest-primitive-digest-length
26  message-digest-primitive-init
27  message-digest-primitive-update
28  message-digest-primitive-final
29  message-digest-primitive-raw-update)
30
31(import scheme
32  (chicken base)
33  (chicken fixnum)
34  (chicken gc)
35  (chicken type)
36  (chicken foreign)
37  (only (chicken memory) allocate free)
38  (only type-checks define-check+error-type check-positive-fixnum check-procedure)
39  (only type-errors error-argument-type))
40
41;;; Support
42
43;;
44
45(define (positive-fixnum? obj) (and (fixnum? obj) (positive? obj)))
46
47(define (primitive-context-info? obj) (or (procedure? obj) (positive-fixnum? obj)))
48
49(define (primitive-name? obj) (or (symbol? obj) (string? obj)))
50
51;;; Message Digest Algorithm API
52
53;;
54
55(define-type primitive-name (or symbol string))
56
57(define-type data-type (not immediate))
58
59(define-type init-procedure (data-type -> *))
60;(foreign-lambda void ***Update     c-pointer   scheme-pointer  unsigned-int)
61;(foreign-lambda void ***RawUpdate  c-pointer   c-pointer       unsigned-int)
62(define-type update-procedure (data-type data-type fixnum -> *))
63(define-type final-procedure (data-type data-type -> *))
64
65(define-type context-info (or fixnum procedure))
66
67(define-type raw-update-value (or boolean update-procedure))
68
69(define-type message-digest-primitive (struct message-digest-primitive))
70
71(define-type message-digest-primitive-context *)
72
73;;
74
75;assignment of value of type `(procedure
76;message-digest-primitive#*make-message-digest-primitive (* * * * * * * *)
77;(struct message-digest-primitive#message-digest-primitive))' to toplevel
78;variable `message-digest-primitive#*make-message-digest-primitive' does not
79;match declared type `(procedure
80;message-digest-primitive#*make-message-digest-primitive ((or fixnum procedure)
81;fixnum procedure procedure procedure fixnum (or symbol string) (or boolean
82;procedure)) (struct message-digest-primitive))'
83(: *make-message-digest-primitive (context-info fixnum init-procedure update-procedure final-procedure fixnum primitive-name raw-update-value -> message-digest-primitive))
84(: message-digest-primitive? (* -> boolean : message-digest-primitive))
85(: message-digest-primitive-context-info (message-digest-primitive --> context-info))
86(: message-digest-primitive-digest-length (message-digest-primitive --> fixnum))
87(: message-digest-primitive-init (message-digest-primitive --> init-procedure))
88(: message-digest-primitive-update (message-digest-primitive --> update-procedure))
89(: message-digest-primitive-final (message-digest-primitive --> final-procedure))
90(: message-digest-primitive-block-length (message-digest-primitive --> fixnum))
91(: message-digest-primitive-name (message-digest-primitive --> primitive-name))
92(: message-digest-primitive-raw-update (message-digest-primitive --> raw-update-value))
93;
94(define-record-type message-digest-primitive
95  (*make-message-digest-primitive ctxi digest-len init update final block-len name raw-update)
96  message-digest-primitive?
97  (ctxi message-digest-primitive-context-info)
98  (digest-len message-digest-primitive-digest-length)
99  (init message-digest-primitive-init)
100  (update message-digest-primitive-update)
101  (final message-digest-primitive-final)
102  (block-len message-digest-primitive-block-length)
103  (name message-digest-primitive-name)
104  (raw-update message-digest-primitive-raw-update) )
105
106(define-check+error-type message-digest-primitive)
107
108;;
109
110(define-inline (check-message-digest-arguments loc ctx-info digest-len init update final block-len name raw-update)
111  (unless (primitive-context-info? ctx-info)
112    (error-argument-type loc ctx-info "positive-fixnum or procedure" 'context-info) )
113  (check-positive-fixnum loc digest-len 'digest-length)
114  (check-procedure loc init 'digest-initializer)
115  (when update
116    (check-procedure loc update 'digest-updater) )
117  (check-procedure loc final 'digest-finalizer)
118  (check-positive-fixnum loc block-len 'block-length)
119  (unless (primitive-name? name)
120    (error-argument-type loc name "symbol or string" 'name) )
121  (when raw-update
122    (check-procedure loc raw-update 'digest-raw-updater) ) )
123
124;;
125
126(: scheme-object-data-pointer (data-type -> pointer))
127;
128(define scheme-object-data-pointer
129  (foreign-lambda* c-pointer ((scheme-pointer psrc)) "C_return( psrc );"))
130
131;;
132
133(: make-scheme-object-updater (update-procedure -> update-procedure))
134;
135(define ((make-scheme-object-updater raw-update) ctx-info obj len)
136  (raw-update ctx-info (scheme-object-data-pointer obj) len) )
137
138;;
139
140;assignment of value of type `(procedure message-digest-primitive#make-message-digest-primitive (* * * * * #!rest) (struct message-digest-primitive#message-digest-primitive))' to toplevel variable `message-digest-primitive#make-message-digest-primitive' does not match declared type `(procedure message-digest-primitive#make-message-digest-primitive ((or fixnum procedure) fixnum procedure procedure procedure #!rest *) (struct message-digest-primitive))'
141(: make-message-digest-primitive (context-info fixnum init-procedure (or boolean update-procedure) final-procedure #!rest -> message-digest-primitive))
142;
143(define (make-message-digest-primitive ctx-info digest-len init update final
144            #!key (block-length 4) (name (gensym 'mdp)) (raw-update #f))
145  (check-message-digest-arguments 'make-message-digest-primitive
146    ctx-info digest-len init update final block-length name raw-update)
147  (let (
148    (update (or update (and raw-update (make-scheme-object-updater raw-update)))) )
149    ;we know about raw -> cooked
150    (unless update
151      (error 'make-message-digest-primitive "missing update & raw-update") )
152    (*make-message-digest-primitive
153      ctx-info digest-len init update final block-length name raw-update) ) )
154
155;;
156
157(: make-message-digest-primitive-context (message-digest-primitive -> message-digest-primitive-context))
158;
159(define (make-message-digest-primitive-context mdp)
160  (let (
161    (ctx-info
162      (message-digest-primitive-context-info
163        (check-message-digest-primitive 'make-message-digest-primitive-context mdp))) )
164    (if (procedure? ctx-info)
165      (ctx-info)
166      (set-finalizer! (allocate ctx-info) free) ) ) )
167
168) ;module message-digest-primitive
Note: See TracBrowser for help on using the repository browser.