:position (+ position start)) out)
(decf i))
(write-char (char text i) out)))))
+
+
+;; Slice 20C: inspect only the ASCII encoding header before strict UTF-8 decoding.
+;; No new decoder, resource protocol, charset registry or I/O is introduced.
+(defun ctm-unit-code (sequence position)
+ (let ((value (aref sequence position)))
+ (if (characterp value) (char-code value) value)))
+
+(defun ctm-ascii-prefix-p (sequence position prefix)
+ (and (<= (+ position (length prefix)) (length sequence))
+ (loop for c across prefix for i from position
+ always (= (char-code c) (ctm-unit-code sequence i)))))
+
+(defun ctm-encoding-name (sequence position unit)
+ "Return the ASCII charset name, end after its quote, and name position.
+CTM 3.2/3.6 permits only SP/TAB before this initial header's opening quote."
+ (let ((cursor (+ position (length "%encoding"))) (size (length sequence)))
+ (loop while (and (< cursor size) (member (ctm-unit-code sequence cursor) '(9 32))) do (incf cursor))
+ (unless (and (< cursor size) (= 34 (ctm-unit-code sequence cursor)))
+ (problem 'ctm-rejection :invalid-encoding-declaration cursor "Expected an encoding name in double quotes." unit))
+ (let ((start (incf cursor)))
+ (loop while (and (< cursor size) (/= 34 (ctm-unit-code sequence cursor))) do
+ (unless (and (<= 33 (ctm-unit-code sequence cursor) 126)
+ (/= 92 (ctm-unit-code sequence cursor)))
+ (problem 'ctm-rejection :invalid-encoding-declaration cursor "Expected an ASCII charset name." unit))
+ (incf cursor))
+ (unless (and (< cursor size) (> cursor start))
+ (problem 'ctm-rejection :invalid-encoding-declaration cursor "Encoding name must be nonempty and have a closing quote." unit))
+ (values (map 'string (lambda (value) (if (characterp value) value (code-char value)))
+ (subseq sequence start cursor)) (1+ cursor) start))))
+
+(defun compatible-ctm-encoding (current declared position unit)
+ (when (and current (not (string-equal current declared)))
+ (problem 'ctm-rejection :encoding-conflict position
+ (format nil "Encoding ~S conflicts with explicitly selected ~S." declared current) unit))
+ declared)
+
+(defun decode-ctm-octets (bytes &key encoding)
+ "Apply CTM 3.2 ordering: supplied encoding, BOM, declaration, default, decode.
+Only UTF-8 is implemented. Matching names are case-insensitive; no alias table."
+ (check-type encoding (or null string))
+ (let* ((bom (and (>= (length bytes) 3) (= (aref bytes 0) #xef)
+ (= (aref bytes 1) #xbb) (= (aref bytes 2) #xbf)))
+ (start (if bom 3 0)) (selected encoding) (name-position 0))
+ (when bom (setf selected (compatible-ctm-encoding selected "UTF-8" 0 :byte)))
+ (when (ctm-ascii-prefix-p bytes start "%encoding")
+ (multiple-value-bind (name end position) (ctm-encoding-name bytes start :byte)
+ (declare (ignore end))
+ (setf selected (compatible-ctm-encoding selected name position :byte)
+ name-position position)))
+ (unless (or (null selected) (string-equal selected "UTF-8"))
+ (problem 'ctm-unsupported :unsupported-encoding name-position
+ (format nil "Charset ~S is recognized but only UTF-8 decoding is implemented." selected) :byte))
+ (decode-utf8 bytes :strip-bom t)))