1 ;;;; CHAR-CODE -- Conversions between characters and byte
2 ;;;; representations thereof
5 (:use :cl #+sbcl :sb-gray #-sbcl :gray)
6 (:export "MAKE-ENCODER" "MAKE-DECODER" "ENCODE-STRING" "DECODE-STRING" "SYSTEM-CHARSET"
8 "MAKE-CODEC-CHARACTER-STREAM"
9 "ASCII" "LATIN-1" "LATIN1" "UTF-8" "UTF8"))
10 (in-package :charcode)
14 (define-condition coding-error (error)
15 ((input :initarg :input)
16 (position :initarg :position)
17 (result :initarg :result)))
19 (define-condition simple-coding-error (coding-error simple-error) ())
21 (defun coding-error (input position result format &rest format-args)
22 (error 'simple-coding-error
23 :input input :position position :result result
24 :format-control format :format-arguments format-args))
26 (deftype decoder-fun () `(function ((array (unsigned-byte 8))
28 &key (start fixnum) (end fixnum))
30 (deftype encoder-fun () `(function ((array character)
31 (array (unsigned-byte 8))
32 &key (start fixnum) (end fixnum))
35 (defmacro define-encoder ((name) &body make-encoder)
36 `(setf (get ',name 'make-encoder) #'(lambda () ,@make-encoder)))
38 (defmacro define-decoder ((name) &body make-decoder)
39 `(setf (get ',name 'make-decoder) #'(lambda () ,@make-decoder)))
41 (defmacro define-codec-synonyms (name &rest synonyms)
42 `(eval-when (:load-toplevel :execute)
43 ,@(mapcar #'(lambda (sym)
44 `(setf (get ',sym 'make-encoder) (get ',name 'make-encoder)
45 (get ',sym 'make-decoder) (get ',name 'make-decoder)))
48 (defun make-encoder (name)
49 (the encoder-fun (values (funcall (get name 'make-encoder)))))
51 (defun make-decoder (name)
52 (the decoder-fun (values (funcall (get name 'make-decoder)))))
54 (defun system-charset ()
55 ;; XXX: Replace me with something perhaps more sensible.
58 (defun encode-string (string &optional (coding (system-charset)))
59 (declare (type string string))
60 (let ((encoder (make-encoder coding))
61 (buf (make-array (list (length string)) :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0)))
62 (unless (funcall encoder string buf)
63 (coding-error string (length string) buf "Encoding of string in ~A ended prematurely." coding))
66 (defun decode-string (buffer &optional (coding (system-charset)))
67 (declare (type (array (unsigned-byte 8)) buffer))
68 (let ((decoder (make-decoder coding))
69 (buf (make-array (list (length buffer)) :element-type 'character :adjustable t :fill-pointer 0)))
70 (unless (funcall decoder buffer buf)
71 (coding-error buffer (length buffer) buf "~A byte sequence ended prematurely." coding))
74 ;;; Gray stream implementation
76 (defclass codec-character-stream (fundamental-character-input-stream fundamental-character-output-stream)
77 ((decoder :initarg :decoder)
78 (encoder :initarg :encoder)
80 (read-pos :initform 0)
81 (buffer :initform (make-array '(64) :element-type 'character :adjustable t :fill-pointer 0))))
83 (defun make-codec-character-stream (real-stream &optional (charset (system-charset)))
84 (declare (type stream real-stream))
85 (make-instance 'codec-character-stream :decoder (make-decoder charset) :encoder (make-encoder charset) :back real-stream))
87 (defmethod close ((stream codec-character-stream) &key abort)
88 (with-slots (back) stream
89 (close back :abort abort))
92 (defmethod open-stream-p ((stream codec-character-stream))
93 (with-slots (back) stream
94 (open-stream-p stream)))
96 (defun ccs-ensure-buffer (stream len)
97 (declare (type codec-character-stream stream)
99 (with-slots (decoder back buffer read-pos) stream
100 (let ((readbuf (make-array (list len) :element-type '(unsigned-byte 8))))
101 (loop (unless (< (- (length buffer) read-pos) len) (return t))
102 (let ((readlen (read-sequence readbuf back :end (- len (- (length buffer) read-pos)))))
104 (return-from ccs-ensure-buffer nil))
105 (funcall decoder readbuf buffer :end readlen))))))
107 (defun ccs-clear-buffer (stream)
108 (declare (type codec-character-stream stream))
109 (with-slots (read-pos buffer) stream
110 (replace buffer buffer :start2 read-pos)
111 (setf (fill-pointer buffer) (- (fill-pointer buffer) read-pos)
114 (defmethod stream-read-char ((stream codec-character-stream))
115 (unless (ccs-ensure-buffer stream 1)
116 (return-from stream-read-char :eof))
117 (with-slots (read-pos buffer) stream
118 (prog1 (aref buffer read-pos)
119 (when (>= (incf read-pos) 16)
120 (ccs-clear-buffer stream)))))
122 (defmethod stream-unread-char ((stream codec-character-stream) char)
123 (with-slots (read-pos buffer) stream
125 (let ((len (length buffer)))
126 (when (< (array-dimension buffer 0) (+ len 16))
127 (adjust-array buffer (list (setf (fill-pointer buffer)
129 (replace buffer buffer :start1 16 :end2 len)))
130 (setf (aref buffer (decf read-pos)) char)
133 (defun ccs-wont-hang-p (stream)
134 (declare (type codec-character-stream stream))
135 (with-slots (read-pos back buffer) stream
136 (or (and (< read-pos (length buffer)) (aref buffer read-pos))
139 (defmethod stream-read-char-no-hang ((stream codec-character-stream))
140 (if (ccs-wont-hang-p stream)
141 (stream-read-char stream)
144 (defmethod stream-peek-char ((stream codec-character-stream))
145 (unless (ccs-ensure-buffer stream 1)
146 (return-from stream-peek-char :eof))
147 (with-slots (read-pos buffer) stream
148 (aref buffer read-pos)))
150 (defmethod stream-listen ((stream codec-character-stream))
151 (if (ccs-wont-hang-p stream)
152 (let ((peek (stream-peek-char stream)))
158 (defmethod stream-write-char ((stream codec-character-stream) char)
159 (with-slots (encoder back) stream
160 (let ((seq (make-array '(1) :element-type 'character :initial-element char))
161 (outbuf (make-array '(16) :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0)))
162 (funcall encoder seq outbuf)
163 (write-sequence outbuf back))))
165 (defmethod stream-finish-output ((stream codec-character-stream))
166 (finish-output (slot-value stream 'back)))
168 (defmethod stream-force-output ((stream codec-character-stream))
169 (force-output (slot-value stream 'back)))
171 (defmethod stream-read-sequence ((stream codec-character-stream) seq &optional (start 0) (end (length seq)))
172 (ccs-ensure-buffer stream (- end start))
173 (with-slots (read-pos buffer) stream
174 (replace seq buffer :start1 start :end1 end :start2 read-pos :end2 (length buffer))
175 (let ((len (min (- end start) (- (length buffer) read-pos))))
176 (when (>= (incf read-pos len) 128)
177 (ccs-clear-buffer stream)))))
179 (defmethod stream-write-sequence ((stream codec-character-stream) seq &optional (start 0) (end (length seq)))
180 (with-slots (encoder back) stream
181 (let ((outbuf (make-array (list (- end start)) :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0)))
182 (funcall encoder seq outbuf)
183 (write-sequence outbuf back))))
185 ;;; Implementation-specific functions
187 #+(or (and clisp unicode) sbcl)
188 (defun unicode->char (unicode)
189 (declare (type (unsigned-byte 24) unicode))
192 #+(or (and clisp unicode) sbcl)
193 (defun char->unicode (char)
194 (declare (type character char))
199 (defun decode-ascii (byteseq charseq &key (start 0) (end (length byteseq)))
200 (declare (type (array (unsigned-byte 8)) byteseq)
201 (type (array character) charseq)
202 (type fixnum start end))
206 (unless (< start end) (return-from decode-ascii t))
207 (let ((byte (aref byteseq (prog1 start (incf start)))))
209 (coding-error byteseq start charseq "Invalid byte ~D in ASCII stream." byte))
210 (vector-push-extend (unicode->char byte) charseq)))
211 (:replace-char (&optional (replacement (unicode->char #xfffd)))
212 :report "Replace the invalid byte with a character."
213 (vector-push-extend replacement charseq))
215 :report "Ignore the invalid byte."
218 (defun encode-ascii (charseq byteseq &key (start 0) (end (length charseq)))
219 (declare (type (array (unsigned-byte 8)) byteseq)
220 (type (array character) charseq)
221 (type fixnum start end))
225 (unless (< start end) (return-from encode-ascii t))
226 (vector-push-extend (let ((cp (char->unicode (aref charseq (prog1 start (incf start))))))
228 (coding-error charseq start byteseq "ASCII cannot encode code-points higher than 128."))
231 (:replace-char (&optional (replacement #\?))
232 :report "Replace this character with another."
233 (vector-push-extend (char->unicode replacement) byteseq))
235 :report "Ignore this character."
238 (define-decoder (ascii)
241 (define-encoder (ascii)
246 (defun decode-latin-1 (byteseq charseq &key (start 0) (end (length byteseq)))
247 (declare (type (array (unsigned-byte 8)) byteseq)
248 (type (array character) charseq)
249 (type fixnum start end))
250 (do ((i start (1+ i)))
252 (vector-push-extend (unicode->char (aref byteseq i)) charseq))
255 (defun encode-latin-1 (charseq byteseq &key (start 0) (end (length charseq)))
256 (declare (type (array (unsigned-byte 8)) byteseq)
257 (type (array character) charseq)
258 (type fixnum start end))
262 (unless (< start end) (return-from encode-latin-1 t))
263 (vector-push-extend (let ((cp (char->unicode (aref charseq (prog1 start (incf start))))))
265 (coding-error charseq start byteseq "ISO-8859-1 cannot encode code-points higher than 256."))
268 (:replace-char (&optional (replacement #\?))
269 :report "Replace this character with another."
270 (vector-push-extend (char->unicode replacement) byteseq))
272 :report "Ignore this character."
275 (define-decoder (latin-1)
278 (define-encoder (latin-1)
281 (define-codec-synonyms latin-1 latin1 iso-8859-1)
285 (defun encode-utf-8 (charseq byteseq &key (start 0) (end (length charseq)))
286 (declare (type (array (unsigned-byte 8)) byteseq)
287 (type (array character) charseq)
288 (type fixnum start end))
289 (do ((i start (1+ i)))
291 (let ((cp (char->unicode (aref charseq i))))
293 (vector-push-extend cp byteseq)
297 (push (logior (ldb (byte 6 0) cp) #x80) bytes)
298 (setf cp (truncate cp 64))
300 (when (< cp (expt 2 (- 6 nbytes)))
301 (push (logior (logand #xff (lognot (1- (expt 2 (- 7 nbytes)))))
306 (vector-push-extend byte byteseq))))))
309 (define-encoder (utf-8)
312 (define-decoder (utf-8)
315 (flet ((decode (byteseq charseq &key (start 0) (end (length byteseq)))
316 (declare (type (array (unsigned-byte 8)) byteseq)
317 (type (array character) charseq)
318 (type fixnum start end))
320 (flet ((failure (format &rest args)
321 (error 'simple-coding-error
322 :input byteseq :position i :result charseq
323 :format-control format :format-arguments args)))
328 (unless (< i end) (return))
329 (let ((byte (aref byteseq (prog1 i (incf i)))))
332 (vector-push-extend (unicode->char byte) charseq)
333 (setf mlen (block zero
335 (when (= (ldb (byte 1 (- 7 i)) byte) 0)
337 (failure "UTF-8 sequence started with continuation byte: ~D" byte))
338 (return-from zero (1- i))))
339 (failure "Invalid UTF-8 sequence start byte: ~D" byte))
340 mbuf (ldb (byte (- 6 mlen) 0) byte)))
341 (progn (when (not (= (ldb (byte 2 6) byte) 2))
342 (failure "Invalid UTF-8 continuation byte: ~D" byte))
343 (setf mbuf (+ (* mbuf 64) (ldb (byte 6 0) byte)))
344 (when (= (decf mlen) 0)
346 (with-simple-restart (:accept "Accept anyway.")
347 (failure "UTF-8 multibyte sequence denoted an ASCII character ~S (either an encoding error or an attempt at breaking security)." (unicode->char mbuf))))
348 (vector-push-extend (unicode->char mbuf) charseq))))))
349 (return-from decode (= mlen 0)))
350 (:replace-char (&optional (replacement (unicode->char #xfffd)))
351 :report "Replace the invalid bytes with a character."
352 (vector-push-extend replacement charseq)
353 (loop (unless (and (< i end) (= (ldb (byte 2 6) (aref byteseq i)) 2))
358 :report "Ignore the invalid byte sequence."
359 (loop (unless (and (< i end) (= (ldb (byte 2 6) (aref byteseq i)) 2))
365 (define-codec-synonyms utf-8 utf8)