Debian Codegraph @

lib/rfc/quoted-printable.scm

raw
1 ;;;
2 ;;; quoted-printable.scm - quoted-printable encoding/decoding routine
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34
35 ;; Ref: RFC2045 section 6.7 <http://www.rfc-editor.org/rfc/rfc2045.txt>
36
37 (define-module rfc.quoted-printable
38 (export quoted-printable-encode quoted-printable-encode-message
39 quoted-printable-decode quoted-printable-decode-string-to
40
41 ;; deprecated, kept for the backward compatibility
42 quoted-printable-encode-string
43 quoted-printable-decode-string)
44 )
45 (select-module rfc.quoted-printable)
46
47 (autoload gauche.vport open-input-uvector open-output-uvector get-output-uvector)
48
49 ;; The minimum line width is 4, since one encoded octed and one soft
50 ;; line break requires 4 characters.
51 ;; If binary is #f, we encode CR and LF. See RFC2045 for this consideration.
52 (define (quoted-printable-encode :key (line-width 76) (binary #f))
53 (let1 limit (if (and line-width (>= line-width 4)) (- line-width 3) #f)
54 (let loop ([c (read-byte)]
55 [lcnt 0])
56 (cond [(eof-object? c)]
57 [(and limit (>= lcnt limit)) (display "=\r\n") (loop c 0)]
58 [(and limit (>= lcnt limit) (or (= c #x20) (= c #x09)))
59 ;; space or tab at the end of line
60 (write-byte c) (display "=\r\n") (loop (read-byte) 0)]
61 [(and binary (or (= c #x0a) (= c #x0d)))
62 (format #t "=0~X" c) (loop (read-byte) (+ lcnt 1))]
63 [(= c #x0d)
64 (let1 c1 (read-byte)
65 (cond ((= c1 #x0a) (display "\r\n") (loop (read-byte) 0))
66 (else (display "\r\n") (loop c1 0))))]
67 [(= c #x0a)
68 (display "\r\n") (loop (read-byte) 0)]
69 ;; NB: we escape '?' as well, for it interferes the header
70 ;; field encoding defined in RFC2047.
71 [(or (< #x20 c #x3d) (= c #x3e) (< #x3f c #x7f))
72 (write-byte c) (loop (read-byte) (+ lcnt 1))]
73 [else
74 (format #t "=~2,'0X" c) (loop (read-byte) (+ lcnt 3))]))
75 ))
76
77 (define (quoted-printable-encode-message msg . args)
78 (etypecase msg
79 [<string> (with-string-io msg (cut apply quoted-printable-encode args))]
80 [<u8vector> (with-output-to-string
81 (^[] (with-input-from-port (open-input-uvector msg)
82 (cut apply quoted-printable-encode args))))]))
83
84 ;; DEPRECATED
85 (define (quoted-printable-encode-string string . args)
86 (with-string-io string (cut apply quoted-printable-encode args)))
87
88 (define (quoted-printable-decode)
89 (let loop ([c (read-char)])
90 (cond [(eof-object? c)]
91 [(char=? c #\=)
92 (let1 c1 (read-char)
93 (cond
94 [(eof-object? c1)] ; illegal, but we recognize it as a soft newline
95 [(char=? c1 #\newline) (loop (read-char))] ; soft newline
96 [(char=? c1 #\return) ; soft newline
97 (let1 c2 (read-char)
98 (if (char=? c2 #\newline) (loop (read-char)) (loop c2)))]
99 [(memv c1 '(#\tab #\space)) ; possibly soft newline
100 (let loop2 ([c2 (read-char)]
101 [r (list c1 c)])
102 (cond [(eof-object? c2)]
103 [(char=? c2 #\newline) (loop (read-char))]
104 [(char=? c2 #\return)
105 (let1 c3 (read-char)
106 (if (char=? c3 #\newline)
107 (loop (read-char))
108 (loop c3)))]
109 [(memv c2 '(#\tab #\space))
110 (loop2 (read-char) (cons c2 r))]
111 [else
112 (for-each write-char (reverse r))
113 (loop c2)]))]
114 [(digit->integer c1 16)
115 => (^[num1]
116 (let1 c2 (read-char)
117 (cond [(eof-object? c2) (write-char c) (write-char c1)]
118 [(digit->integer c2 16)
119 => (^[num2]
120 (write-byte (+ (* num1 16) num2))
121 (loop (read-char)))]
122 [else (write-char c) (write-char c1) (loop c2)])))]
123 [else (write-char c) (loop c1)]))]
124 [else (write-char c) (loop (read-char))])
125 ))
126
127 (define (quoted-printable-decode-string-to target string)
128 (cond
129 [(eqv? target <string>) (with-string-io string quoted-printable-decode)]
130 [(eqv? target <u8vector>) (let1 out (open-output-uvector)
131 (with-input-from-string string
132 quoted-printable-decode)
133 (get-output-uvector out))]
134 [else (error "invalid target:" target)]))
135
136 ;; DEPRECATED
137 (define (quoted-printable-decode-string string)
138 (with-string-io string quoted-printable-decode))