1 ;;; mel-ccl.el: Base64, Quoted-Printable and Q-encoding encoder/decoder for GNU Emacs
3 ;; Copyright (C) 1998 Tanaka Akira
5 ;; Author: Tanaka Akira <akr@jaist.ac.jp>
7 ;; Keywords: MIME, Base64, Quoted-Printable, Q-encoding
9 ;; This file is part of MEL (MIME Encoding Library).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
35 (defconst mel-ccl-4-table
38 (defconst mel-ccl-16-table
39 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))
41 (defconst mel-ccl-28-table
42 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
43 16 17 18 19 20 21 22 23 24 25 26 27))
45 (defconst mel-ccl-64-table
46 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
47 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
48 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
49 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63))
51 (defconst mel-ccl-256-table
52 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
53 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
54 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
55 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
56 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
57 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
58 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
59 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
60 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
61 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
62 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
63 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
64 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
65 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
66 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
67 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255))
69 (defconst mel-ccl-256-to-16-table
70 '(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
71 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
72 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
73 0 1 2 3 4 5 6 7 8 9 nil nil nil nil nil nil
74 nil 10 11 12 13 14 15 nil nil nil nil nil nil nil nil nil
75 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
76 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
77 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
78 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
79 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
80 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
81 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
82 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
83 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
84 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
85 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil))
87 (defconst mel-ccl-16-to-256-table
88 (mapcar 'char-int "0123456789ABCDEF"))
90 (defconst mel-ccl-high-table
93 (lambda (v) (nth (lsh v -4) mel-ccl-16-to-256-table))
96 (defconst mel-ccl-low-table
99 (lambda (v) (nth (logand v 15) mel-ccl-16-to-256-table))
102 (defconst mel-ccl-u-raw
106 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
107 abcdefghijklmnopqrstuvwxyz\
108 !@#$%&'()*+,-./:;<>@[\\]^`{|}~"))
110 (defconst mel-ccl-c-raw
114 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
115 abcdefghijklmnopqrstuvwxyz\
116 !@#$%&'*+,-./:;<>@[]^`{|}~"))
118 (defconst mel-ccl-p-raw
122 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
123 abcdefghijklmnopqrstuvwxyz\
126 (defconst mel-ccl-256-to-64-table
127 '(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
128 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
129 nil nil nil nil nil nil nil nil nil nil nil 62 nil nil nil 63
130 52 53 54 55 56 57 58 59 60 61 nil nil nil t nil nil
131 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
132 15 16 17 18 19 20 21 22 23 24 25 nil nil nil nil nil
133 nil 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
134 41 42 43 44 45 46 47 48 49 50 51 nil nil nil nil nil
135 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
136 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
137 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
138 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
139 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
140 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
141 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
142 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil))
144 (defconst mel-ccl-64-to-256-table
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
148 abcdefghijklmnopqrstuvwxyz\
152 (defconst mel-ccl-qp-table
153 [enc enc enc enc enc enc enc enc enc wsp lf enc enc cr enc enc
154 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
155 wsp raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
156 raw raw raw raw raw raw raw raw raw raw raw raw raw enc raw raw
157 raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
158 raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
159 raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
160 raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw enc
161 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
162 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
163 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
164 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
165 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
166 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
167 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
168 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc])
178 (define-ccl-program mel-ccl-decode-q
186 ((= r0 (char-int ?_))
188 ((= r0 (char-int ?=))
197 mel-ccl-256-to-16-table)))
204 `((write r0 ,(vconcat
207 (logior (lsh r0 4) v))
211 mel-ccl-256-to-16-table)))
214 `(write-repeat ,r0))))
215 mel-ccl-256-table))))))
219 (defun mel-ccl-encode-q-generic (raw)
228 ((= r0 32) `(write-repeat ?_))
229 ((member r0 raw) `(write-repeat ,r0))
233 (write r0 ,mel-ccl-high-table)
234 (write r0 ,mel-ccl-low-table)
237 ;; On xemacs, generated program counts iso-8859-1 8bit character as 6bytes.
238 (defun mel-ccl-count-q-length (raw)
246 (if (or (= r1 32) (member r1 raw))
247 '((r0 += 1) (repeat))
248 '((r0 += 3) (repeat))))
249 mel-ccl-256-table))))))
253 (define-ccl-program mel-ccl-encode-uq
254 (mel-ccl-encode-q-generic mel-ccl-u-raw))
255 (define-ccl-program mel-ccl-encode-cq
256 (mel-ccl-encode-q-generic mel-ccl-c-raw))
257 (define-ccl-program mel-ccl-encode-pq
258 (mel-ccl-encode-q-generic mel-ccl-p-raw))
260 (define-ccl-program mel-ccl-count-uq
261 (mel-ccl-count-q-length mel-ccl-u-raw))
262 (define-ccl-program mel-ccl-count-cq
263 (mel-ccl-count-q-length mel-ccl-c-raw))
264 (define-ccl-program mel-ccl-count-pq
265 (mel-ccl-count-q-length mel-ccl-p-raw))
271 (defun mel-ccl-decode-b-bit-ex (v)
273 (lsh (logand v (lsh 255 16)) -16)
274 (logand v (lsh 255 8))
275 (lsh (logand v 255) 16)))
277 (defconst mel-ccl-decode-b-0-table
282 (mel-ccl-decode-b-bit-ex (lsh v 18))
284 mel-ccl-256-to-64-table)))
286 (defconst mel-ccl-decode-b-1-table
291 (mel-ccl-decode-b-bit-ex (lsh v 12))
293 mel-ccl-256-to-64-table)))
295 (defconst mel-ccl-decode-b-2-table
300 (mel-ccl-decode-b-bit-ex (lsh v 6))
302 mel-ccl-256-to-64-table)))
304 (defconst mel-ccl-decode-b-3-table
309 (mel-ccl-decode-b-bit-ex v)
311 mel-ccl-256-to-64-table)))
315 (define-ccl-program mel-ccl-decode-b
319 (r4 = r0 ,mel-ccl-decode-b-0-table)
320 (r5 = r1 ,mel-ccl-decode-b-1-table)
322 (r5 = r2 ,mel-ccl-decode-b-2-table)
324 (r5 = r3 ,mel-ccl-decode-b-3-table)
326 (if (r4 & ,(lognot (1- (lsh 1 24))))
328 (if (r4 & ,(lsh 1 24))
329 ((r0 = r1) (r1 = r2) (r2 = r3) (read r3)
330 (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
331 (r5 = r3 ,mel-ccl-decode-b-3-table)
336 (if (r4 & ,(lsh 1 25))
337 ((r1 = r2) (r2 = r3) (read r3)
338 (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
339 (r5 = r3 ,mel-ccl-decode-b-3-table)
345 (if (r4 & ,(lsh 1 26))
347 (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
348 (r5 = r3 ,mel-ccl-decode-b-3-table)
357 (if (r4 & ,(lsh 1 27))
359 (r4 = r3 ,mel-ccl-decode-b-3-table)
364 (r4 = r0 ,mel-ccl-decode-b-0-table)
365 (r5 = r1 ,mel-ccl-decode-b-1-table)
370 ((r5 = r2 ,mel-ccl-decode-b-2-table)
372 (r5 = r3 ,mel-ccl-decode-b-3-table)
383 ((r5 = r2 ,mel-ccl-decode-b-2-table)
396 (write-repeat r4))))))
400 ;; Generated CCL program works not properly on 20.2 because CCL_EOF_BLOCK
402 (defun mel-ccl-encode-base64-generic (&optional quantums-per-line output-crlf terminate-with-newline)
411 `((write ,(nth (lsh r1 -2) mel-ccl-64-to-256-table))
412 (r0 = ,(logand r1 3))))
419 `((write r0 ,(vconcat
422 (nth (logior (lsh r0 4)
424 mel-ccl-64-to-256-table))
426 (r0 = ,(logand r1 15))))
433 `((write r0 ,(vconcat
436 (nth (logior (lsh r0 2)
438 mel-ccl-64-to-256-table))
439 mel-ccl-16-table)))))
445 (nth r1 mel-ccl-64-to-256-table))
448 ,@(when quantums-per-line
449 `((if (r3 == ,quantums-per-line)
450 ((write ,(if output-crlf "\r\n" "\n"))
455 ,(if terminate-with-newline
456 `(if (r3 > 0) (write ,(if output-crlf "\r\n" "\n")))
461 (nth (lsh r0 4) mel-ccl-64-to-256-table))
463 (write ,(if terminate-with-newline
464 (if output-crlf "==\r\n" "==\n")
469 (nth (lsh r0 2) mel-ccl-64-to-256-table))
471 (write ,(if terminate-with-newline
472 (if output-crlf "=\r\n" "=\n")
477 (define-ccl-program mel-ccl-encode-b
478 (mel-ccl-encode-base64-generic))
481 (define-ccl-program mel-ccl-encode-base64-crlf-crlf
482 (mel-ccl-encode-base64-generic 19 t))
484 (define-ccl-program mel-ccl-encode-base64-crlf-lf
485 (mel-ccl-encode-base64-generic 19 nil))
491 (defun mel-ccl-try-to-read-crlf (input-crlf reg eof-reg cr-eof lf-eof crlf-eof succ fail-cr fail-lf fail-crlf)
493 `((,eof-reg = ,cr-eof) (read-if (,reg == ?\r)
494 ((,eof-reg = ,lf-eof) (read-if (,reg == ?\n)
498 `((,eof-reg = ,crlf-eof) (read-if (,reg == ?\n)
502 ;; Generated CCL program works not properly on 20.2 because CCL_EOF_BLOCK
504 (defun mel-ccl-encode-quoted-printable-generic (input-crlf output-crlf)
507 (r5 = 0) ; previous character is white space
517 (let ((tmp (aref mel-ccl-qp-table r0)))
519 ((eq r0 (char-int ?F))
521 ((r4 = 15) (read-if (r0 == ?r)
522 ((r4 = 16) (read-if (r0 == ?o)
523 ((r4 = 17) (read-if (r0 == ?m)
524 ((r4 = 18) (read-if (r0 == ? )
532 (write-repeat "From"))))
534 (write-repeat "Fro"))))
536 (write-repeat "Fr"))))
538 (write-repeat "F"))))
539 ((r3 = 0) (break)) ; RAW
541 ((eq r0 (char-int ?.))
543 ,(mel-ccl-try-to-read-crlf
546 `((write ,(if output-crlf "=2E\r\n" "=2E\n"))
553 (write-repeat ".=0D"))
556 ((r3 = 0) (break)) ; RAW
558 ((eq tmp 'raw) '((r3 = 0) (break))) ; RAW
559 ((eq tmp 'enc) '((r3 = 1) (break))) ; ENC
560 ((eq tmp 'wsp) '((r3 = 2) (break))) ; WSP
561 ((eq tmp 'cr) (if input-crlf
562 '((r3 = 3) (break)) ; CR
563 '((r3 = 1) (break)))) ; ENC
564 ((eq tmp 'lf) (if input-crlf
565 '((r3 = 1) (break)) ; ENC
566 '((r3 = 3) (break)))) ; CRLF
576 (write-read-repeat r0))
583 (write r0 ,mel-ccl-high-table)
585 (write-read-repeat r0 ,mel-ccl-low-table))
588 (write ,(if output-crlf "=\r\n=" "=\n="))
589 (write r0 ,mel-ccl-high-table)
591 (write-read-repeat r0 ,mel-ccl-low-table))
598 (write-read-repeat r0))
600 (write ,(if output-crlf "=\r\n" "=\n"))
602 (write-read-repeat r0))))
606 `((if ((r6 > 73) & r5)
609 (write ,(if output-crlf "=\r\n" "=\n"))))
616 (write ,(if output-crlf "=\r\n" "=\n"))
617 ,@(if output-crlf '((write ?\r)) '())
619 (write-read-repeat r0))
620 ;; noWSP ; r0:r3=CRLF
623 ,@(if output-crlf '((write ?\r)) '())
625 (write-read-repeat r0)))
627 ;; r0:r3={RAW,ENC,CR}
630 (lambda (after-cr after-raw-enc)
643 ;; r5=WSP ; CR:r3=CR r0=LF
646 (write ,(if output-crlf "=\r\n\r\n" "=\n\n"))
650 ;; r5=noWSP ; CR:r3=CR r0=LF
653 (write ,(if output-crlf "\r\n" "\n"))
673 (write ,(if output-crlf "=0D\r\n" "=0D\n"))
680 (write ,(if output-crlf "=\r\n=0D=0D" "=\n=0D=0D"))
682 ;; CR:r3=CR r0=noLFnorCR
685 (write ,(if output-crlf "=\r\n=0D" "=\n=0D"))
689 (write ,(if output-crlf "=\r\n=0D" "=\n=0D"))
692 (lambda (after-newline after-cr-nolf after-nonewline)
697 ;; r0:r3={RAW,ENC} r1
699 ;; r0:r3={RAW,ENC} r1=CR
702 ;; r0:r3={RAW,ENC} CR r1
704 ;; r0:r3=RAW CR r1=LF
706 ;; r0:r3=RAW CR r1=noLF
708 ;; r0:r3={RAW,ENC} r1:noCR
713 ;; r0:r3={RAW,ENC} r1
715 ;; r0:r3={RAW,ENC} r1=CRLF
717 ;; r0:r3={RAW,ENC} r1:noCRLF
719 ;; r0:r3={RAW,ENC} CR r1=LF
720 ;; r0:r3={RAW,ENC} r1=CRLF
725 ;; r0:r3=RAW CR r1=LF
728 (write ,(if output-crlf "\r\n" "\n"))
732 ;; r0:r3=ENC CR r1=LF
735 (write r0 ,mel-ccl-high-table)
736 (write r0 ,mel-ccl-low-table)
737 (write ,(if output-crlf "\r\n" "\n"))
741 ;; r0:r3={RAW,ENC} CR r1=noLF
744 ;; r0:r3=RAW CR r1:noLF
747 (write ,(if output-crlf "=\r\n" "=\n"))
752 ;; r0:r3=ENC CR r1:noLF
755 (write ,(if output-crlf "=\r\n=" "=\n="))
756 (write r0 ,mel-ccl-high-table)
757 (write r0 ,mel-ccl-low-table)
761 ;; r0:r3={RAW,ENC} r1:noCR
762 ;; r0:r3={RAW,ENC} r1:noCRLF
766 ;; r0:r3=RAW r1:noCRLF
769 (write ,(if output-crlf "=\r\n" "=\n"))
774 ;; r0:r3=ENC r1:noCRLF
777 (write ,(if output-crlf "=\r\n=" "=\n="))
778 (write r0 ,mel-ccl-high-table)
779 (write r0 ,mel-ccl-low-table)
784 ( ;(write "[EOF:") (write r4 ,mel-ccl-high-table) (write r4 ,mel-ccl-low-table) (write "]")
793 ;; 3: SOFTBREAK r0:r3=ENC ;
796 ((write ,(if output-crlf "=\r\n" "=\n")) (end))
797 ;; 5: SOFTBREAK r0:r3=WSP ;
798 ((write ,(if output-crlf "=\r\n" "=\n")) (end))
801 ((write "=0D") (end))
802 ((write ,(if output-crlf "=\r\n=0D" "=\n=0D")) (end)))
803 ;; 7: r5=WSP SOFTBREAK CR:r3=CR r0=LF ;
805 ;; 8: r5=noWSP CR:r3=CR r0=LF ;
807 ;; 9: (r6=73) ; CR:r3=CR r0=CR
808 ((write ,(if output-crlf "=\r\n=0D=0D" "=\n=0D=0D")) (end))
809 ;; 10: (r6=73) CR:r3=CR CR LF ;
811 ;; 11: ; r0:r3={RAW,ENC}
816 (write r0 ,mel-ccl-high-table)
817 (write r0 ,mel-ccl-low-table)
819 ;; 12: ; r0:r3={RAW,ENC} r1=CR
823 ((write ,(if output-crlf "=\r\n" "=\n"))
828 ((write ,(if output-crlf "=\r\n=" "=\n="))
829 (write r0 ,mel-ccl-high-table)
830 (write r0 ,mel-ccl-low-table)
833 ;; 13: r0:r3=RAW CR LF ;
834 ;; 13: r0:r3=RAW CRLF ;
836 ;; 14: r0:r3=ENC CR LF ;
837 ;; 14: r0:r3=ENC CRLF ;
844 ((write "Fro") (end))
846 ((write "From") (end))
847 ;; 19: r6=0 "From " ;
852 ((write ".=0D") (end))
855 ;; 23: r6=0 ".\r\n" ;
860 (defun mel-ccl-decode-quoted-printable-generic (input-crlf output-crlf)
868 (let ((tmp (aref mel-ccl-qp-table r0)))
870 ((eq tmp 'raw) `(write-read-repeat r0))
871 ((eq tmp 'wsp) (if (eq r0 (char-int ? ))
881 ;; hard line break found.
884 (write-read-repeat r0))
885 '(write-read-repeat r0))
887 ;; invalid control character (bare CR) found.
888 ;; -> ignore it and rescan from r0.
891 ;; invalid character (bare CR) found.
898 ;; invalid character (bare LF) found.
903 ;; hard line break found.
906 (write-read-repeat r0))
907 '(write-read-repeat r0))))
908 ((eq r0 (char-int ?=))
913 (if ((r0 == ? ) | r1)
915 ;; Skip transport-padding.
916 ;; It should check CR LF after
917 ;; transport-padding.
924 ;; '=' [\t ]* r0:[^\t ]
930 ((eq r0 (char-int ?\r))
932 ;; '=' [\t ]* r0='\r'
934 ;; '=' [\t ]* '\r' r0
936 ;; '=' [\t ]* '\r' r0='\n'
937 ;; soft line break found.
940 ;; '=' [\t ]* '\r' r0:[^\n]
942 ;; output "=" and rescan from r0.
945 ;; '=' [\t ]* r0='\r'
946 ;; invalid input (bare CR found) ->
947 ;; output "=" and rescan from next.
951 ((eq r0 (char-int ?\n))
953 ;; '=' [\t ]* r0='\n'
954 ;; invalid input (bare LF found) ->
955 ;; output "=" and rescan from next.
959 ;; '=' [\t ]* r0='\r\n'
960 ;; soft line break found.
963 ((setq tmp (nth r0 mel-ccl-256-to-16-table))
964 ;; '=' [\t ]* r0:[0-9A-F]
965 ;; upper nibble of hexadecimal digit found.
969 ;; '=' [\t ]* r0:[^\r0-9A-F]
971 ;; output "=" and rescan from r0.
975 ;; '=' [\t ]* r1:r0:[0-9A-F]
980 (if (setq tmp (nth r2 mel-ccl-256-to-16-table))
981 ;; '=' [\t ]* r1:r0:[0-9A-F] r2:[0-9A-F]
987 (logior (lsh r0 4) tmp))
989 ;; '=' [\t ]* r1:r0:[0-9A-F] r2:[^0-9A-F]
994 ;; '=' [\t ]* r1:r0:[0-9A-F] r2:[^0-9A-F]
996 ;; output "=" with hex digit and rescan from r2.
1002 ;; invalid character found.
1013 (let ((reg (aref [r1 r2 r3 r4 r5] regnum)))
1026 ((r6 = ,(+ (* regnum 28) bit))
1030 (,reg |= ,(lsh 1 bit))
1032 ((r6 = ,(+ (* regnum 28) bit))
1034 mel-ccl-28-table))))
1036 ;; white space buffer exhaust.
1037 ;; error: line length limit (76bytes) violation.
1038 ;; -> ignore these white spaces.
1044 ;; trailing white spaces found.
1045 ;; -> ignore these white spacs.
1046 ((write ,(if output-crlf "\r\n" "\n"))
1049 ;; [\t ]* \r r0:[^\n]
1050 ;; error: bare CR found.
1051 ;; -> output white spaces and ignore bare CR.
1054 ;; middle white spaces found.
1057 ;; trailing white spaces found.
1058 ;; -> ignore these white spacs.
1059 ((write ,(if output-crlf "\r\n" "\n"))
1063 ;; middle white spaces found.
1069 (let ((reg (aref [r1 r2 r3 r4 r5] regnum)))
1074 `((if (,reg & ,(lsh 1 bit))
1077 (if (r6 == ,(+ (* regnum 28) bit 1))
1079 mel-ccl-28-table))))
1086 (define-ccl-program mel-ccl-encode-quoted-printable-crlf-crlf
1087 (mel-ccl-encode-quoted-printable-generic t t))
1089 (define-ccl-program mel-ccl-encode-quoted-printable-crlf-lf
1090 (mel-ccl-encode-quoted-printable-generic t nil))
1092 (define-ccl-program mel-ccl-encode-quoted-printable-lf-crlf
1093 (mel-ccl-encode-quoted-printable-generic nil t))
1095 (define-ccl-program mel-ccl-encode-quoted-printable-lf-lf
1096 (mel-ccl-encode-quoted-printable-generic nil nil))
1098 (define-ccl-program mel-ccl-decode-quoted-printable-crlf-crlf
1099 (mel-ccl-decode-quoted-printable-generic t t))
1101 (define-ccl-program mel-ccl-decode-quoted-printable-crlf-lf
1102 (mel-ccl-decode-quoted-printable-generic t nil))
1104 (define-ccl-program mel-ccl-decode-quoted-printable-lf-crlf
1105 (mel-ccl-decode-quoted-printable-generic nil t))
1107 (define-ccl-program mel-ccl-decode-quoted-printable-lf-lf
1108 (mel-ccl-decode-quoted-printable-generic nil nil))
1114 (make-ccl-coding-system
1115 'mel-ccl-uq-rev ?Q "MIME Q-encoding in unstructured field (reversed)"
1116 'mel-ccl-encode-uq 'mel-ccl-decode-q)
1118 (make-ccl-coding-system
1119 'mel-ccl-cq-rev ?Q "MIME Q-encoding in comment (reversed)"
1120 'mel-ccl-encode-cq 'mel-ccl-decode-q)
1122 (make-ccl-coding-system
1123 'mel-ccl-pq-rev ?Q "MIME Q-encoding in phrase (reversed)"
1124 'mel-ccl-encode-pq 'mel-ccl-decode-q)
1126 (make-ccl-coding-system
1127 'mel-ccl-b-rev ?B "MIME B-encoding (reversed)"
1128 'mel-ccl-encode-b 'mel-ccl-decode-b)
1130 (make-ccl-coding-system
1131 'mel-ccl-quoted-printable-crlf-crlf-rev
1132 ?Q "MIME Quoted-Printable-encoding (reversed)"
1133 'mel-ccl-encode-quoted-printable-crlf-crlf
1134 'mel-ccl-decode-quoted-printable-crlf-crlf)
1136 (make-ccl-coding-system
1137 'mel-ccl-quoted-printable-lf-crlf-rev
1138 ?Q "MIME Quoted-Printable-encoding (LF encoding) (reversed)"
1139 'mel-ccl-encode-quoted-printable-crlf-lf
1140 'mel-ccl-decode-quoted-printable-lf-crlf)
1142 (make-ccl-coding-system
1143 'mel-ccl-quoted-printable-crlf-lf-rev
1144 ?Q "MIME Quoted-Printable-encoding (LF internal) (reversed)"
1145 'mel-ccl-encode-quoted-printable-lf-crlf
1146 'mel-ccl-decode-quoted-printable-crlf-lf)
1148 (make-ccl-coding-system
1149 'mel-ccl-quoted-printable-lf-lf-rev
1150 ?Q "MIME Quoted-Printable-encoding (LF encoding) (LF internal) (reversed)"
1151 'mel-ccl-encode-quoted-printable-lf-lf
1152 'mel-ccl-decode-quoted-printable-lf-lf)
1154 (make-ccl-coding-system
1155 'mel-ccl-base64-crlf-rev
1156 ?B "MIME Base64-encoding (reversed)"
1157 'mel-ccl-encode-base64-crlf-crlf
1160 (make-ccl-coding-system
1161 'mel-ccl-base64-lf-rev
1162 ?B "MIME Base64-encoding (LF encoding) (reversed)"
1163 'mel-ccl-encode-base64-crlf-lf
1170 (unless (and (boundp 'ccl-encoder-eof-block-is-broken)
1171 ccl-encoder-eof-block-is-broken)
1173 (defun base64-ccl-encode-string (string)
1174 "Encode STRING with base64 encoding."
1175 (decode-coding-string string 'mel-ccl-base64-lf-rev))
1177 (defun base64-ccl-encode-region (start end)
1178 "Encode region from START to END with base64 encoding."
1180 (decode-coding-region start end 'mel-ccl-base64-lf-rev))
1182 (defun base64-ccl-insert-encoded-file (filename)
1183 "Encode contents of file FILENAME to base64, and insert the result."
1184 (interactive (list (read-file-name "Insert encoded file: ")))
1185 (let ((coding-system-for-read 'mel-ccl-b-rev))
1186 (insert-file-contents filename)))
1190 (defun base64-ccl-decode-string (string)
1191 "Decode base64 encoded STRING"
1192 (encode-coding-string string 'mel-ccl-b-rev))
1194 (defun base64-ccl-decode-region (start end)
1195 "Decode base64 encoded the region from START to END."
1197 (encode-coding-region start end 'mel-ccl-b-rev))
1199 (defun base64-ccl-write-decoded-region (start end filename)
1200 "Decode the region from START to END and write out to FILENAME."
1202 (list (region-beginning) (region-end)
1203 (read-file-name "Write decoded region to file: ")))
1204 (let ((coding-system-for-write 'mel-ccl-b-rev))
1205 (write-region start end filename)))
1208 ;;; @ quoted-printable
1211 (unless (and (boundp 'ccl-encoder-eof-block-is-broken)
1212 ccl-encoder-eof-block-is-broken)
1214 (defun quoted-printable-ccl-encode-string (string)
1215 "Encode STRING with quoted-printable encoding."
1216 (decode-coding-string
1218 'mel-ccl-quoted-printable-lf-lf-rev))
1220 (defun quoted-printable-ccl-encode-region (start end)
1221 "Encode the region from START to END with quoted-printable
1224 (decode-coding-region start end 'mel-ccl-quoted-printable-lf-lf-rev))
1226 (defun quoted-printable-ccl-insert-encoded-file (filename)
1227 "Encode contents of the file named as FILENAME, and insert it."
1228 (interactive (list (read-file-name "Insert encoded file: ")))
1229 (let ((coding-system-for-read 'mel-ccl-quoted-printable-lf-lf-rev))
1230 (insert-file-contents filename)))
1234 (defun quoted-printable-ccl-decode-string (string)
1235 "Decode quoted-printable encoded STRING."
1236 (encode-coding-string
1238 'mel-ccl-quoted-printable-lf-lf-rev))
1240 (defun quoted-printable-ccl-decode-region (start end)
1241 "Decode the region from START to END with quoted-printable
1244 (encode-coding-region start end 'mel-ccl-quoted-printable-lf-lf-rev))
1246 (defun quoted-printable-ccl-write-decoded-region
1247 (start end filename)
1248 "Decode quoted-printable encoded current region and write out to FILENAME."
1250 (list (region-beginning) (region-end)
1251 (read-file-name "Write decoded region to file: ")))
1252 (let ((coding-system-for-write 'mel-ccl-quoted-printable-lf-lf-rev))
1253 (write-region start end filename)))
1259 (defun q-encoding-ccl-encode-string (string &optional mode)
1260 "Encode STRING to Q-encoding of encoded-word, and return the result.
1261 MODE allows `text', `comment', `phrase' or nil. Default value is
1263 (decode-coding-string
1266 ((eq mode 'text) 'mel-ccl-uq-rev)
1267 ((eq mode 'comment) 'mel-ccl-cq-rev)
1268 (t 'mel-ccl-pq-rev))))
1270 (defun q-encoding-ccl-decode-string (string)
1271 "Decode Q encoded STRING and return the result."
1272 (encode-coding-string
1276 (unless running-xemacs
1277 (defun q-encoding-ccl-encoded-length (string &optional mode)
1278 (let ((status [nil nil nil nil nil nil nil nil nil]))
1279 (fillarray status nil)
1280 (ccl-execute-on-string
1282 ((eq mode 'text) 'mel-ccl-count-uq)
1283 ((eq mode 'comment) 'mel-ccl-count-cq)
1284 (t 'mel-ccl-count-pq))
1296 (let ((str0 "a\f \t\r
1297 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r
1298 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r
1299 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r
1300 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\f\r
1301 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\f\r
1302 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\f\r
1306 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \r
1307 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \r
1308 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \r
1309 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \r
1312 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\rccc\r
1313 ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\r\r\nccc\r
1314 ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\r\rccc\r
1315 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\rccc\r
1316 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd\r\neee\r
1317 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd\reee\r
1318 ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeee\r
1320 str1 encoded decoded)
1321 (setq str1 (ew-crlf-to-lf str0)
1324 (decode-coding-string
1326 'mel-ccl-quoted-printable-crlf-crlf-rev)
1327 (decode-coding-string
1329 'mel-ccl-quoted-printable-lf-crlf-rev)
1330 (decode-coding-string
1332 'mel-ccl-quoted-printable-crlf-lf-rev)
1333 (decode-coding-string
1335 'mel-ccl-quoted-printable-lf-lf-rev))
1338 (encode-coding-string
1340 'mel-ccl-quoted-printable-crlf-crlf-rev)
1341 (encode-coding-string
1343 'mel-ccl-quoted-printable-lf-crlf-rev)
1344 (encode-coding-string
1346 'mel-ccl-quoted-printable-crlf-lf-rev)
1347 (encode-coding-string
1349 'mel-ccl-quoted-printable-lf-lf-rev)))
1351 (string= str0 (nth 0 decoded))
1352 (string= str0 (nth 1 decoded))
1353 (string= str1 (nth 2 decoded))
1354 (string= str1 (nth 3 decoded))))
1357 (defun make-ccl-coding-system (name mnemonic doc-string decoder encoder)
1359 name 'ccl doc-string
1360 (list 'mnemonic (char-to-string mnemonic)
1361 'decode (symbol-value decoder)
1362 'encode (symbol-value encoder))))