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 "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)))
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 read-pos) char)
134 (defun ccs-wont-hang-p (stream)
135 (declare (type codec-character-stream stream))
136 (with-slots (read-pos back buffer) stream
137 (or (and (< read-pos (length buffer)) (aref buffer read-pos))
140 (defmethod stream-read-char-no-hang ((stream codec-character-stream))
141 (if (ccs-wont-hang-p stream)
142 (stream-read-char stream)
145 (defmethod stream-peek-char ((stream codec-character-stream))
146 (unless (ccs-ensure-buffer stream 1)
147 (return-from stream-peek-char :eof))
148 (with-slots (read-pos buffer) stream
149 (aref buffer read-pos)))
151 (defmethod stream-listen ((stream codec-character-stream))
152 (if (ccs-wont-hang-p stream)
153 (let ((peek (stream-peek-char stream)))
159 (defmethod stream-write-char ((stream codec-character-stream) char)
160 (with-slots (encoder back) stream
161 (let ((seq (make-array '(1) :element-type 'character :initial-element char))
162 (outbuf (make-array '(16) :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0)))
163 (funcall encoder seq outbuf)
164 (write-sequence outbuf back))))
166 (defmethod stream-finish-output ((stream codec-character-stream))
167 (finish-output (slot-value stream 'back)))
169 (defmethod stream-force-output ((stream codec-character-stream))
170 (force-output (slot-value stream 'back)))
172 (defmethod stream-read-sequence ((stream codec-character-stream) seq &optional (start 0) (end (length seq)))
173 (ccs-ensure-buffer stream (- end start))
174 (with-slots (read-pos buffer) stream
175 (replace seq buffer :start1 start :end1 end :start2 read-pos :end2 (length buffer))
176 (let ((len (min (- end start) (- (length buffer) read-pos))))
177 (when (>= (incf read-pos len) 128)
178 (ccs-clear-buffer stream)))))
180 (defmethod stream-write-sequence ((stream codec-character-stream) seq &optional (start 0) (end (length seq)))
181 (with-slots (encoder back) stream
182 (let ((outbuf (make-array (list (- end start)) :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0)))
183 (funcall encoder seq outbuf)
184 (write-sequence outbuf back))))
186 ;;; Implementation-specific functions
188 #+(or (and clisp unicode) sbcl)
189 (defun unicode->char (unicode)
190 (declare (type (unsigned-byte 24) unicode))
193 #+(or (and clisp unicode) sbcl)
194 (defun char->unicode (char)
195 (declare (type character char))
200 (defun decode-latin-1 (byteseq charseq &key (start 0) (end (length byteseq)))
201 (declare (type (array (unsigned-byte 8)) byteseq)
202 (type (array character) charseq)
203 (type fixnum start end))
204 (do ((i start (1+ i)))
206 (vector-push-extend (unicode->char (aref byteseq i)) charseq))
209 (defun encode-latin-1 (charseq byteseq &key (start 0) (end (length charseq)))
210 (declare (type (array (unsigned-byte 8)) byteseq)
211 (type (array character) charseq)
212 (type fixnum start end))
216 (unless (< start end) (return-from encode-latin-1 t))
217 (vector-push-extend (let ((cp (char->unicode (aref charseq (prog1 start (incf start))))))
219 (coding-error charseq start byteseq "ISO-8859-1 cannot encode code-points higher than 256."))
222 (:replace-char (&optional (replacement #\?))
223 :report "Replace this character with another."
224 (vector-push-extend (char->unicode replacement) byteseq))
226 :report "Ignore this character."
229 (define-decoder (latin-1)
232 (define-encoder (latin-1)
235 (define-codec-synonyms latin-1 latin1 iso-8859-1)
239 (defun encode-utf-8 (charseq byteseq &key (start 0) (end (length charseq)))
240 (declare (type (array (unsigned-byte 8)) byteseq)
241 (type (array character) charseq)
242 (type fixnum start end))
243 (do ((i start (1+ i)))
245 (let ((cp (char->unicode (aref charseq i))))
247 (vector-push-extend cp byteseq)
251 (push (logior (ldb (byte 6 0) cp) #x80) bytes)
252 (setf cp (truncate cp 64))
254 (when (< cp (expt 2 (- 6 nbytes)))
255 (push (logior (logand #xff (lognot (1- (expt 2 (- 7 nbytes)))))
260 (vector-push-extend byte byteseq))))))
263 (define-encoder (utf-8)
266 (define-decoder (utf-8)
269 (flet ((decode (byteseq charseq &key (start 0) (end (length byteseq)))
270 (declare (type (array (unsigned-byte 8)) byteseq)
271 (type (array character) charseq)
272 (type fixnum start end))
274 (flet ((failure (format &rest args)
275 (error 'simple-coding-error
276 :input byteseq :position i :result charseq
277 :format-control format :format-arguments args)))
282 (unless (< i end) (return))
283 (let ((byte (aref byteseq (prog1 i (incf i)))))
286 (vector-push-extend (unicode->char byte) charseq)
287 (setf mlen (block zero
289 (when (= (ldb (byte 1 (- 7 i)) byte) 0)
291 (failure "UTF-8 sequence started with continuation byte: ~D" byte))
292 (return-from zero (1- i))))
293 (failure "Invalid UTF-8 sequence start byte: ~D" byte))
294 mbuf (ldb (byte (- 6 mlen) 0) byte)))
295 (progn (when (not (= (ldb (byte 2 6) byte) 2))
296 (failure "Invalid UTF-8 continuation byte: ~D" byte))
297 (setf mbuf (+ (* mbuf 64) (ldb (byte 6 0) byte)))
298 (when (= (decf mlen) 0)
300 (with-simple-restart (:accept "Accept anyway.")
301 (failure "UTF-8 multibyte sequence denoted an ASCII character ~S (either an encoding error or an attempt at breaking security)." (unicode->char mbuf))))
302 (vector-push-extend (unicode->char mbuf) charseq))))))
303 (return-from decode (= mlen 0)))
304 (:replace-char (&optional (replacement (unicode->char #xfffd)))
305 :report "Replace the invalid bytes with a character."
306 (vector-push-extend replacement charseq)
307 (loop (unless (and (< i end) (= (ldb (byte 2 6) (aref byteseq i)) 2))
312 :report "Ignore the invalid byte sequence."
313 (loop (unless (and (< i end) (= (ldb (byte 2 6) (aref byteseq i)) 2))
319 (define-codec-synonyms utf-8 utf8)