Skip to content

Commit fa23ee6

Browse files
committed
add conditions for codec errors
1 parent 2863fad commit fa23ee6

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

mime/content-encoding.lisp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ from de.setf.xml suffice."))
6161
:initarg :documentation :initform nil
6262
:accessor content-encoding-documentation)))
6363

64+
(define-condition simple-encoding-error (simple-type-error)
65+
()
66+
(:default-initargs
67+
:format-control "The character ~S (code x~2,'0x) cannot be encoded as ~a."))
68+
(defun simple-encoding-error (&key datum expected-type encoding)
69+
(error 'simple-encoding-error :datum datum :expected-type expected-type
70+
:format-arguments (list datum (char-code datum) encoding)))
71+
72+
(define-condition simple-decoding-error (simple-type-error)
73+
()
74+
(:default-initargs
75+
:format-control "The code x~2,'0x cannot be decoded as ~a."))
76+
(defun simple-decoding-error (&key datum expected-type encoding)
77+
(error 'simple-encoding-error :datum datum :expected-type expected-type
78+
:format-arguments (list datum encoding)))
79+
6480
(def-class-constructor content-encoding
6581
(:method ((name string) &rest initargs)
6682
(declare (dynamic-extent initargs))
@@ -142,7 +158,7 @@ from de.setf.xml suffice."))
142158
(ash (logand #x3f (read-byte-code)) 6)
143159
(logand (read-byte-code) #x3f)))
144160
(t
145-
(error "Illegal UTF-8 data: x~2,'0x." byte1))))))
161+
(simple-decoding-error :datum byte1 :encoding :utf-8))))))
146162
(utf8-code-point-size (char)
147163
(let ((code (char-code char)))
148164
(declare (type (mod #x110000) code))
@@ -159,8 +175,11 @@ from de.setf.xml suffice."))
159175

160176
(flet ((iso-8859-1-encode (char put-byte destination)
161177
(let ((code (char-code char)))
162-
(declare (type (mod #x100) code))
163-
(assert (< code #x100) () "Cannot be encoded as iso-8859-1: ~s" char)
178+
(declare ;(type (mod #x100) code)
179+
;; permit assertion to run
180+
(optimize (speed 3) (safety 0)))
181+
(unless (< code #x100)
182+
(simple-encoding-error :datum char :encoding :iso-8859-1))
164183
(funcall put-byte destination code)))
165184
(iso-8859-1-decode (get-byte source)
166185
(code-char (or (funcall get-byte source)

0 commit comments

Comments
 (0)