-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmime-transfer-encoding.cl
308 lines (273 loc) · 8.99 KB
/
mime-transfer-encoding.cl
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
;; -*- mode: common-lisp; package: net.post-office -*-
;;
;; See the file LICENSE for the full license governing this code.
;;
(defpackage :net.post-office
(:use #:lisp #:excl)
(:import-from #:excl #:base64-encode-stream
#+(or (version= 7 0)
(version= 8 0)
(version>= 8 1 pre-beta 5))
#:base64-decode-stream)
(:export
#:base64-encode-stream
#:base64-decode-stream
#:qp-encode-stream
#:qp-decode-stream
#:qp-decode-usb8
#:qp-decode-string
#:with-decoded-part-body-stream))
(in-package :net.post-office)
;;; Supported transfer encodings
;; encoders
(defun raw-encode-stream (instream outstream)
(declare (optimize (speed 3) (safety 0)))
(let ((buf (make-array 4096 :element-type '(unsigned-byte 8)))
got)
(declare (dynamic-extent buf)
(fixnum got))
(while (/= 0 (setf got (read-vector buf instream)))
(write-vector buf outstream :end got))))
(defconstant *qp-hex-digits* "0123456789ABCDEF")
;; wrap-at is not a hard limit but more of a suggestion. it may be
;; late by by 3 characters.
(defun qp-encode-stream (instream outstream &key (wrap-at 72))
(declare (optimize (speed 3)))
(let ((prev 0)
(outcol 0)
byte)
(declare (fixnum byte prev))
(macrolet ((whitespace (x)
(let ((xx (gensym)))
`(let ((,xx ,x))
(or (= ,xx 9) (= ,xx 32))))))
(labels ((check-linewrap ()
(if* (and wrap-at (>= outcol wrap-at))
then (format outstream "=~%" outstream)
(setf outcol 0)))
(check-deferred ()
(if* (and (= prev 13) (/= byte 10))
then ;; previous byte was bare CR. Handle
(check-linewrap)
(write-string "=0D" outstream)
(incf outcol 3))
(if* (whitespace prev)
then (if* (or (= byte 0) (= byte 10) (= byte 13))
then ;; EOF, EOL, probable EOL. Encode.
(check-linewrap)
(format outstream "=20")
(incf outcol 3)
else ;; Safe to print deferred whitespace
(check-linewrap)
(write-char (code-char prev) outstream)
(incf outcol 1)))))
(while (setf byte (read-byte instream nil nil))
(check-deferred)
(if* (or (and (>= byte 33) (<= byte 60))
(and (>= byte 62) (<= byte 126)))
then (check-linewrap)
(write-char (code-char byte) outstream)
(incf outcol)
elseif (or (= byte 13) (whitespace byte))
thenret ;; defer handling
elseif (= byte 10) ;; LF
then (write-char #\newline outstream)
(setf outcol 0)
else (check-linewrap)
(format outstream "=~c~c"
(schar *qp-hex-digits*
(ash byte -4))
(schar *qp-hex-digits*
(logand byte #xf)))
(incf outcol 3))
(setf prev byte))
;; Handle final deferred data
(setf byte 0)
(check-deferred)))))
;; Decoding stuff
;; Used by qp-decode-stream
(eval-when (compile)
(defconstant *qp-digit-values*
#.(let ((arr (make-array 257 :element-type 'fixnum)))
(dotimes (n 256)
(setf (aref arr n)
(if* (<= (char-code #\0) n (char-code #\9))
then (- n (char-code #\0))
elseif (<= (char-code #\A) n (char-code #\F))
then (- n (- (char-code #\A) 10))
elseif (<= (char-code #\a) n (char-code #\f))
then (- n (- (char-code #\a) 10))
else -1)))
(setf (aref arr 256) -2)
arr)))
(defun qp-decode-stream (instream outstream &key count)
(declare (optimize (speed 3)))
(let (unread-buf)
(macrolet ((unread (byte)
`(progn
(setf unread-buf ,byte)
(if count
(incf count))))
(get-byte (&key eof-value)
`(block get-byte
(if* count
then (if (zerop count)
(return-from get-byte ,eof-value))
(decf count))
(if* unread-buf
then (prog1 unread-buf
(setf unread-buf nil))
else (read-byte instream nil ,eof-value))))
(out (byte)
`(write-byte ,byte outstream))
(eol-p (byte)
`(or (eq ,byte 10) (eq ,byte 13))))
(let (byte)
(while (setf byte (get-byte))
(if* (eq byte #.(char-code #\=))
then (let ((nextbyte (get-byte)))
(if* (null nextbyte)
then ;; stray equal sign. just dump and terminate.
(out byte)
(return))
(if* (eol-p nextbyte)
then ;; soft line break.
(if (eq nextbyte 13) ;; CR
(setf nextbyte (get-byte)))
(if (not (eq nextbyte 10)) ;; LF
(unread nextbyte))
else ;; =XY encoding
(let* ((byte3 (get-byte :eof-value 256))
(high (aref #.*qp-digit-values* nextbyte))
(low (aref #.*qp-digit-values* byte3))
(value (logior (the fixnum (ash high 4)) low)))
(declare (fixnum byte3 high low value))
(if* (< value 0)
then ;; Invalid or truncated encoding. just dump it.
(out byte)
(out nextbyte)
(if* (eq low -2) ;; EOF
then (return)
else (out byte3))
else (out value)))))
else (out byte)))
t))))
;; 'out' should be at least the size of 'in'. If it is nil,
;; a usb8 array will be allocated and used. It is okay if 'out' is the
;; same buffer as 'in'.
;; Returns:
;; 1) the supplied or allocated array
;; 2) the just past the last byte populated in the array.
(defun qp-decode-usb8 (in out &key (start1 0) (end1 (length in))
(start2 0) end2
underscores-are-spaces)
(declare (optimize (speed 3))
((simple-array (unsigned-byte 8) (*)) in out)
(fixnum start1 end1 start2 end2))
(if (null out)
(setf out (make-array (length in) :element-type '(unsigned-byte 8))))
(if (null end2)
(setf end2 (length out)))
(let ((count (- end1 start1)))
(declare (fixnum count))
(if (< count 0)
(error "start1 must be less than end1"))
(if (> start2 end2)
(error "start2 must be less than end2"))
(if (< (the fixnum (- end2 start2)) count)
(error "Not enough room in output array"))
(macrolet ((unread (byte)
(declare (ignore byte))
`(decf start1))
(get-byte (&key eof-value)
`(if* (>= start1 end1)
then ,eof-value
else (prog1 (aref in start1)
(incf start1))))
(out (byte)
`(prog1 (setf (aref out start2) ,byte)
(incf start2)))
(eol-p (byte)
`(or (eq ,byte 10) (eq ,byte 13))))
(let (byte)
(while (setf byte (get-byte))
(if* (eq byte #.(char-code #\=))
then (let ((nextbyte (get-byte)))
(if* (null nextbyte)
then ;; stray equal sign. just dump and terminate.
(out byte)
(return))
(if* (eol-p nextbyte)
then ;; soft line break.
(if (eq nextbyte 13) ;; CR
(setf nextbyte (get-byte)))
(if (not (eq nextbyte 10)) ;; LF
(unread nextbyte))
else ;; =XY encoding
(let* ((byte3 (get-byte :eof-value 256))
(high (aref #.*qp-digit-values* nextbyte))
(low (aref #.*qp-digit-values* byte3))
(value (logior (the fixnum (ash high 4)) low)))
(declare (fixnum byte3 high low value))
(if* (< value 0)
then ;; Invalid or truncated encoding. just dump it.
(out byte)
(out nextbyte)
(if* (eq low -2) ;; EOF
then (return)
else (out byte3))
else (out value)))))
elseif (and underscores-are-spaces (eq byte #.(char-code #\_)))
then ;; See the discussion in bug18636 about why this is
;; done.
(out #.(char-code #\space))
else (out byte)))
(values out start2)))))
(defun qp-decode-string (string &key (start 0) (end (length string))
(return :string)
(external-format :default)
underscores-are-spaces)
(multiple-value-bind (vec len)
(string-to-octets string :start start :end end :null-terminate nil
:external-format :latin1)
(multiple-value-setq (vec len)
(qp-decode-usb8 vec vec :end1 len
:underscores-are-spaces underscores-are-spaces))
(ecase return
(:string
(octets-to-string vec :end len :external-format external-format))
(:usb8
(subseq vec 0 len)))))
;; 'instream' must be positioned at the beginning of the part body
;; by the caller beforehand.
(defmacro with-decoded-part-body-stream ((sym part instream) &body body)
(let ((p (gensym))
(encoding (gensym))
(count (gensym))
(charset (gensym))
(ef (gensym)))
`(let* ((,p ,part)
(,encoding (mime-part-encoding ,p))
(,count (mime-part-body-size ,p)))
(excl:with-function-input-stream (,sym #'mime-decode-transfer-encoding
,instream
,encoding
,count)
(let* ((,charset (or (cdr (assoc "charset" (mime-part-parameters ,p)
:test #'equalp))
"us-ascii"))
(,ef (or (charset-to-external-format ,charset) :latin1)))
(setf (stream-external-format ,sym) ,ef))
,@body))))
(defun mime-decode-transfer-encoding (outstream instream encoding count)
(cond
((equalp encoding "quoted-printable")
(qp-decode-stream instream outstream :count count))
#+(or (version= 7 0)
(version= 8 0)
(version>= 8 1 pre-beta 5))
((equalp encoding "base64")
(excl:base64-decode-stream instream outstream :count count :error-p nil))
(t
;; defined in mime-parse.cl
(stream-to-stream-copy outstream instream count))))