about summary refs log tree commit diff
path: root/third_party/lisp/mime4cl/streams.lisp
blob: 71a32d84e461a62a83e9f2e441b6b475b4cd64fb (plain) (blame)
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
;;; streams.lisp --- En/De-coding Streams

;;; Copyright (C) 2012 by Walter C. Pelissero
;;; Copyright (C) 2021-2023 by the TVL Authors

;;; Author: Walter C. Pelissero <walter@pelissero.de>
;;; Project: mime4cl

;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public License
;;; as published by the Free Software Foundation; either version 2.1
;;; of the License, or (at your option) any later version.
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free
;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
;;; 02111-1307 USA

(in-package :mime4cl)

(defun flexi-stream-root-stream (stream)
  "Return the non FLEXI-STREAM stream a given chain of FLEXI-STREAMs is based on."
  (if (typep stream 'flexi-stream)
      (flexi-stream-root-stream (flexi-stream-stream stream))
      stream))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass coder-stream-mixin ()
  ((real-stream :type stream
                :initarg :underlying-stream
                :reader real-stream)
   (dont-close :initform nil
               :initarg :dont-close)))

(defmethod stream-file-position ((stream coder-stream-mixin))
  (file-position (slot-value stream 'real-stream)))

(defmethod (setf stream-file-position) (newval (stream coder-stream-mixin))
  (file-position (slot-value stream 'real-stream) newval))

(defclass coder-input-stream-mixin (fundamental-binary-input-stream coder-stream-mixin)
  ())
(defclass coder-output-stream-mixin (fundamental-binary-output-stream coder-stream-mixin)
  ())

;; TODO(sterni): temporary, ugly measure to make flexi-streams happy
(defmethod stream-element-type ((stream coder-input-stream-mixin))
  (declare (ignore stream))
  '(unsigned-byte 8))

(defclass quoted-printable-decoder-stream (coder-input-stream-mixin quoted-printable-decoder) ())
(defclass 8bit-decoder-stream (coder-input-stream-mixin 8bit-decoder) ())

(defclass quoted-printable-encoder-stream (coder-output-stream-mixin quoted-printable-encoder) ())
(defclass base64-encoder-stream (coder-output-stream-mixin base64-encoder) ())
(defclass 8bit-encoder-stream (coder-output-stream-mixin 8bit-encoder) ())

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmethod initialize-instance :after ((stream coder-stream-mixin) &key &allow-other-keys)
  (unless (slot-boundp stream 'real-stream)
    (error "REAL-STREAM is unbound.  Must provide a :UNDERLYING-STREAM argument.")))

(defmethod initialize-instance ((stream coder-output-stream-mixin) &key &allow-other-keys)
  (call-next-method)
  (unless (slot-boundp stream 'output-function)
    (setf (slot-value stream 'output-function)
          #'(lambda (char)
              (write-char char (slot-value stream 'real-stream))))))

(defmethod initialize-instance ((stream coder-input-stream-mixin) &key &allow-other-keys)
  (call-next-method)
  (unless (slot-boundp stream 'input-function)
    (setf (slot-value stream 'input-function)
          #'(lambda ()
              (read-char (slot-value stream 'real-stream) nil)))))

(defmethod stream-read-byte ((stream coder-input-stream-mixin))
  (or (decoder-read-byte stream)
      :eof))

(defmethod stream-write-byte ((stream coder-output-stream-mixin) byte)
  (encoder-write-byte stream byte))

(defmethod close ((stream coder-stream-mixin) &key abort)
  (with-slots (real-stream dont-close) stream
    (unless dont-close
      (close real-stream :abort abort))))

(defmethod close ((stream coder-output-stream-mixin) &key abort)
  (unless abort
    (encoder-finish-output stream))
  (call-next-method))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass encoder-input-stream (fundamental-character-input-stream coder-stream-mixin)
  ((encoder)
   (buffer-queue :initform (make-queue)))
  (:documentation
   "This is the base class for encoders with the direction swapped. It
reads from REAL-STREAM a stream of bytes, encodes it and returnes it
in a stream of character."))

(defclass quoted-printable-encoder-input-stream (encoder-input-stream) ())
(defclass base64-encoder-input-stream (encoder-input-stream) ())
(defclass 8bit-encoder-input-stream (fundamental-character-input-stream coder-stream-mixin) ())

(defmethod initialize-instance ((stream quoted-printable-encoder-input-stream) &key &allow-other-keys)
  (call-next-method)
  (with-slots (encoder buffer-queue) stream
    (setf encoder
          (make-instance 'quoted-printable-encoder
                         :output-function #'(lambda (char)
                                              (queue-append buffer-queue char))))))

(defmethod initialize-instance ((stream base64-encoder-input-stream) &key &allow-other-keys)
  (call-next-method)
  (with-slots (encoder buffer-queue) stream
    (setf encoder
          (make-instance 'base64-encoder
                         :output-function #'(lambda (char)
                                              (queue-append buffer-queue char))))))

(defmethod stream-read-char ((stream encoder-input-stream))
  (with-slots (encoder buffer-queue real-stream) stream
    (loop
       while (queue-empty-p buffer-queue)
       do (let ((byte (read-byte real-stream nil)))
            (if byte
                (encoder-write-byte encoder byte)
                (progn
                  (encoder-finish-output encoder)
                  (queue-append buffer-queue :eof)))))
    (queue-pop buffer-queue)))


(defmethod stream-read-char ((stream 8bit-encoder-input-stream))
  (with-slots (real-stream) stream
    (aif (read-byte real-stream nil)
         (code-char it)
         :eof)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun make-custom-flexi-stream (class stream other-args)
  (apply #'make-instance
         class
         :stream stream
         (mapcar (lambda (x)
                   ;; make-flexi-stream has a discrepancy between :initarg of
                   ;; make-instance and its &key which we mirror here.
                   (if (eq x :external-format) :flexi-stream-external-format x))
                 other-args)))

(defclass adapter-flexi-input-stream (flexi-input-stream)
  ((ignore-close
    :initform nil
    :initarg :ignore-close
    :documentation
    "If T, calling CLOSE on the stream does nothing.
If NIL, the underlying stream is closed."))
  (:documentation "FLEXI-STREAM that does not close the underlying stream on
CLOSE if :IGNORE-CLOSE is T."))

(defmethod close ((stream adapter-flexi-input-stream) &key abort)
  (declare (ignore abort))
  (with-slots (ignore-close) stream
    (unless ignore-close
      (call-next-method))))

(defun make-input-adapter (source)
  (etypecase source
    ;; If it's already a stream, we need to make sure it's not closed by the adapter
    (stream
     (assert (input-stream-p source))
     (if (and (typep source 'adapter-flexi-input-stream)
              (slot-value source 'ignore-close))
         source ; already ignores CLOSE
         (make-adapter-flexi-input-stream source :ignore-close t)))
    ;; TODO(sterni): is this necessary? (maybe with (not *lazy-mime-decode*)?)
    (string
     (make-input-adapter (string-to-octets source)))
    ((vector (unsigned-byte 8))
     (make-in-memory-input-stream source))
    (pathname
     (make-flexi-stream (open source :element-type '(unsigned-byte 8))))
    (file-portion
     (open-decoded-file-portion source))))

(defun make-adapter-flexi-input-stream (stream &rest args)
  "Create a ADAPTER-FLEXI-INPUT-STREAM. Accepts the same keyword arguments as
MAKE-FLEXI-STREAM as well as :IGNORE-CLOSE. If T, the underlying stream is not
closed."
  (make-custom-flexi-stream 'adapter-flexi-input-stream stream args))

(defclass positioned-flexi-input-stream (adapter-flexi-input-stream)
  ()
  (:documentation
   "FLEXI-INPUT-STREAM that automatically advances the underlying :STREAM to
the location given by :POSITION. This uses FILE-POSITION internally, so it'll
only works if the underlying stream position is tracked in bytes. Note that
the underlying stream is still advanced, so having multiple instances of
POSITIONED-FLEXI-INPUT-STREAM based with the same underlying stream won't work
reliably.
Also supports :IGNORE-CLOSE of ADAPTER-FLEXI-INPUT-STREAM."))

(defmethod initialize-instance ((stream positioned-flexi-input-stream)
                                &key &allow-other-keys)
  (call-next-method)
  ;; The :POSITION initarg is only informational for flexi-streams: It assumes
  ;; it is were the stream it got is already at and continuously updates it
  ;; for querying (via FLEXI-STREAM-POSITION) and bound checking.
  ;; Since we have streams that are not positioned correctly, we need to do this
  ;; here using FILE-POSITION. Note that assumes the underlying implementation
  ;; uses bytes for FILE-POSITION which is not guaranteed (probably some streams
  ;; even in SBCL don't).
  (file-position (flexi-stream-stream stream) (flexi-stream-position stream)))

(defun make-positioned-flexi-input-stream (stream &rest args)
  "Create a POSITIONED-FLEXI-INPUT-STREAM. Accepts the same keyword arguments as
MAKE-FLEXI-STREAM as well as :IGNORE-CLOSE. Causes the FILE-POSITION of STREAM to
be modified to match the :POSITION argument."
  (make-custom-flexi-stream 'positioned-flexi-input-stream stream args))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; TODO(sterni): test correct behavior with END NIL
(defstruct file-portion
  data                                  ; string or a pathname
  encoding
  start
  end)

(defun open-decoded-file-portion (file-portion)
  (with-slots (data encoding start end)
      file-portion
    (let* ((binary-stream
             (etypecase data
               (pathname
                (open data :element-type '(unsigned-byte 8)))
               ((vector (unsigned-byte 8))
                (flexi-streams:make-in-memory-input-stream data))
               (stream
                ;; TODO(sterni): assert that bytes/flexi-stream
                data)))
           (params (ccase encoding
                     ((:quoted-printable :base64) '(:external-format :us-ascii))
                     (:8bit '(:element-type (unsigned-byte 8)))
                     (:7bit '(:external-format :us-ascii))))
           (portion-stream (apply #'make-positioned-flexi-input-stream
                                  binary-stream
                                  :position start
                                  :bound end
                                  ;; if data is a stream we can't have a
                                  ;; FILE-PORTION without modifying it when
                                  ;; reading etc. The least we can do, though,
                                  ;; is forgo destroying it.
                                  :ignore-close (typep data 'stream)
                                  params))
           (needs-decoder-stream (member encoding '(:quoted-printable
                                                    :base64))))

      (if needs-decoder-stream
          (make-instance
           (ccase encoding
             (:quoted-printable 'quoted-printable-decoder-stream)
             (:base64 'qbase64:decode-stream))
           :underlying-stream portion-stream)
          portion-stream))))