source: project/release/4/message-digest/trunk/message-digest-port.scm @ 35341

Last change on this file since 35341 was 35341, checked in by Kon Lovett, 20 months ago

add types, message-digest-result-form -> -type, -form is symbol, -byte-order is symbol, do not type check-/error- (no no no no no)

File size: 4.3 KB
Line 
1;;;; message-digest-port.scm
2;;;; Kon Lovett, May '10
3;;;; Kon Lovett, Aug '17
4
5;; Issues
6;;
7;; - Use of sys namespace routines.
8
9(module message-digest-port
10
11(;export
12  digest-output-port? check-digest-output-port error-digest-output-port
13  digest-output-port-name
14  open-output-digest
15  get-output-digest
16  call-with-output-digest
17  with-output-to-digest)
18
19(import scheme chicken)
20(use
21  (only data-structures ->string)
22  (only ports make-output-port with-input-from-port)
23  (only srfi-13 string-suffix-length-ci)
24  (only type-checks define-check+error-type check-output-port)
25  (only type-errors error-argument-type make-error-type-message signal-type-error)
26  message-digest-primitive
27  message-digest-type
28  message-digest-bv
29  fx-utils
30  typed-define)
31
32(declare
33  (bound-to-procedure
34    ##sys#slot ##sys#setslot))
35
36;;; Support
37
38;;
39
40(include "message-digest-types")
41
42;;
43
44(define PORT-TAG 'digest)
45
46(define PRIMITIVE-NAME-SUFFIXES '("p" "-primitive"))
47
48;;
49
50(define (%port-type p)
51  (##sys#slot p 7) )
52
53(define (%port-type-set! p t)
54  (##sys#setslot p 7 t) )
55
56(define (%port-name p)
57  (##sys#slot p 3) )
58
59(define (%port-name-set! p s)
60  (##sys#setslot p 3 s) )
61
62;;
63
64(define (check-open-port loc obj #!optional argnam)
65  (if (port-closed? obj)
66    (error-argument-type loc obj "open port" argnam)
67    obj ) )
68
69(define (check-open-digest-output-port loc obj #!optional argnam)
70  (let (
71    (pt (%port-type (check-open-port loc (check-output-port loc obj argnam) argnam))) )
72    (unless (eq? PORT-TAG pt)
73      (signal-type-error loc (make-error-type-message 'digest-output-port) obj argnam) ) )
74  obj )
75
76;Synthesize a port-name from a primitive-name
77(define: (make-digest-port-name (mdp message-digest-primitive)) --> string
78  (let* (
79    (nam
80      (->string (or (message-digest-primitive-name mdp) 'md)))
81    ;strip trailing (why ?)
82    (remlen
83      ;longest suffix length or negative
84      (foldl
85        (lambda (remlen suf)
86          (fxmax remlen (string-suffix-length-ci nam suf)) )
87        -1
88        PRIMITIVE-NAME-SUFFIXES))
89    (nam
90      (if (fxpositive? remlen)
91        (substring nam 0 (fx- (string-length nam) remlen))
92        nam)) )
93    (string-append "(" nam ")") ) )
94
95;;; Message Digest Output Port API
96
97(define: (open-output-digest (mdp message-digest-primitive)) -> digest-output-port
98  (let* (
99    (md
100      (initialize-message-digest mdp))
101    (writer
102      (lambda (obj)
103        ;for now only a string
104        (if (string? obj)
105          (message-digest-update-string md obj)
106          (message-digest-update-blob md obj))))
107      ;use default close behavior
108      (port
109        (make-output-port writer void)) )
110    (##sys#set-port-data! port md)
111    (%port-type-set! port PORT-TAG)
112    (%port-name-set! port (make-digest-port-name mdp))
113    port ) )
114
115(: digest-output-port? (* -> boolean : digest-output-port))
116;
117(define (digest-output-port? obj)
118  (and
119    (output-port? obj)
120    (eq? PORT-TAG (%port-type obj)) ) )
121
122(define-check+error-type digest-output-port)
123
124(define: (digest-output-port-name (port digest-output-port)) -> string
125  (%port-name
126    (check-digest-output-port 'digest-output-port-name port)) )
127
128(define: (*close-output-digest (loc symbol) (port digest-output-port) (restyp message-digest-result-form)) -> message-digest-result-type
129  (let (
130    (res
131      (finalize-message-digest
132        (##sys#port-data
133          (check-open-digest-output-port loc port 'digest-port))
134        restyp)) )
135    (close-output-port port)
136    res ) )
137
138(define: (get-output-digest (port digest-output-port) . (opts (list message-digest-result-type))) -> message-digest-result-type
139  (let (
140    (restyp (optional opts (message-digest-result-form))) )
141    (*close-output-digest 'get-output-digest port restyp) ) )
142
143;;;
144
145(define: (call-with-output-digest (mdp message-digest-primitive) (proc procedure) . (opts (list message-digest-result-type))) -> message-digest-result-type
146  (let (
147    (restyp (optional opts (message-digest-result-form)))
148    (port (open-output-digest mdp)) )
149    (proc port)
150    (*close-output-digest 'call-with-output-digest port restyp) ) )
151
152(define: (with-output-to-digest (mdp message-digest-primitive) (thunk procedure) . (opts (list message-digest-result-type))) -> message-digest-result-type
153  (let (
154    (restyp (optional opts (message-digest-result-form))) )
155    (call-with-output-digest mdp (cut with-input-from-port <> thunk) restyp) ) )
156
157) ;module message-digest
Note: See TracBrowser for help on using the repository browser.