* sasl.texi: Texinfo markup fixes. From Jesper Harder
[elisp/flim.git] / mel-q-ccl.el
1 ;;; mel-q-ccl.el --- Quoted-Printable encoder/decoder using CCL.
2
3 ;; Copyright (C) 1998,1999 Tanaka Akira
4
5 ;; Author: Tanaka Akira <akr@jaist.ac.jp>
6 ;; Created: 1998/9/17
7 ;; Keywords: MIME, Quoted-Printable, Q-encoding
8
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
10
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.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; 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.
25
26 ;;; Code:
27
28 (require 'ccl)
29 (require 'pccl)
30 (require 'mime-def)
31
32
33 ;;; @ constants
34 ;;;
35
36 (eval-when-compile
37
38 (defconst mel-ccl-16-table
39   '(  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15))
40
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))
44
45 (defconst mel-ccl-256-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
50      64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79
51      80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95
52      96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111
53     112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
54     128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
55     144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
56     160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
57     176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
58     192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
59     208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
60     224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
61     240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255))
62
63 (defconst mel-ccl-256-to-16-table
64   '(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
65     nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
66     nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
67       0   1   2   3   4   5   6   7   8   9 nil nil nil nil nil nil
68     nil  10  11  12  13  14  15 nil nil nil nil nil nil nil nil nil
69     nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
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     nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
74     nil nil nil nil nil nil nil 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
81 (defconst mel-ccl-16-to-256-table
82   (mapcar 'char-int "0123456789ABCDEF"))
83
84 (defconst mel-ccl-high-table
85   (vconcat
86    (mapcar
87     (lambda (v) (nth (lsh v -4) mel-ccl-16-to-256-table))
88     mel-ccl-256-table)))
89
90 (defconst mel-ccl-low-table
91   (vconcat
92    (mapcar
93     (lambda (v) (nth (logand v 15) mel-ccl-16-to-256-table))
94     mel-ccl-256-table)))
95
96 (defconst mel-ccl-u-raw
97   (mapcar
98    'char-int
99    "0123456789\
100 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
101 abcdefghijklmnopqrstuvwxyz\
102 !@#$%&'()*+,-./:;<>@[\\]^`{|}~"))
103
104 (defconst mel-ccl-c-raw
105   (mapcar
106    'char-int
107    "0123456789\
108 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
109 abcdefghijklmnopqrstuvwxyz\
110 !@#$%&'*+,-./:;<>@[]^`{|}~"))
111
112 (defconst mel-ccl-p-raw
113   (mapcar
114    'char-int
115    "0123456789\
116 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
117 abcdefghijklmnopqrstuvwxyz\
118 !*+-/"))
119
120 (defconst mel-ccl-qp-table
121   [enc enc enc enc enc enc enc enc enc wsp lf  enc enc cr  enc enc
122    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
123    wsp raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
124    raw raw raw raw raw raw raw raw raw raw raw raw raw enc raw raw
125    raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
126    raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
127    raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
128    raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw enc
129    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
130    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
131    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
132    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
133    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
134    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
135    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
136    enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc])
137
138 )
139
140
141 ;;; @ CCL programs
142 ;;;
143
144 ;;; Q
145
146 (define-ccl-program mel-ccl-decode-q
147   `(1
148     ((loop
149       (read-branch
150        r0
151        ,@(mapcar
152           (lambda (r0)
153             (cond
154              ((= r0 (char-int ?_))
155               `(write-repeat ? ))
156              ((= r0 (char-int ?=))
157               `((loop
158                  (read-branch
159                   r1
160                   ,@(mapcar
161                      (lambda (v)
162                        (if (integerp v)
163                            `((r0 = ,v) (break))
164                          '(repeat)))
165                      mel-ccl-256-to-16-table)))
166                 (loop
167                  (read-branch
168                   r1
169                   ,@(mapcar
170                      (lambda (v)
171                        (if (integerp v)
172                            `((write r0 ,(vconcat
173                                          (mapcar
174                                           (lambda (r0)
175                                             (logior (lsh r0 4) v))
176                                           mel-ccl-16-table)))
177                              (break))
178                          '(repeat)))
179                      mel-ccl-256-to-16-table)))
180                 (repeat)))
181              (t
182               `(write-repeat ,r0))))
183           mel-ccl-256-table))))))
184
185 (eval-when-compile
186
187 (defun mel-ccl-encode-q-generic (raw)
188   `(3
189     (loop
190      (loop
191       (read-branch
192        r0
193        ,@(mapcar
194           (lambda (r0)
195             (cond
196              ((= r0 32) `(write-repeat ?_))
197              ((member r0 raw) `(write-repeat ,r0))
198              (t '(break))))
199           mel-ccl-256-table)))
200      (write ?=)
201      (write r0 ,mel-ccl-high-table)
202      (write r0 ,mel-ccl-low-table)
203      (repeat))))
204
205 ;; On xemacs, generated program counts iso-8859-1 8bit character as 6bytes.
206 (defun mel-ccl-count-q-length (raw)
207   `(0
208     ((r0 = 0)
209      (loop
210       (read-branch
211        r1
212        ,@(mapcar
213           (lambda (r1)
214             (if (or (= r1 32) (member r1 raw))
215                 '((r0 += 1) (repeat))
216               '((r0 += 3) (repeat))))
217           mel-ccl-256-table))))))
218
219 )
220
221 (define-ccl-program mel-ccl-encode-uq
222   (mel-ccl-encode-q-generic mel-ccl-u-raw))
223 (define-ccl-program mel-ccl-encode-cq
224   (mel-ccl-encode-q-generic mel-ccl-c-raw))
225 (define-ccl-program mel-ccl-encode-pq
226   (mel-ccl-encode-q-generic mel-ccl-p-raw))
227
228 (define-ccl-program mel-ccl-count-uq
229   (mel-ccl-count-q-length mel-ccl-u-raw))
230 (define-ccl-program mel-ccl-count-cq
231   (mel-ccl-count-q-length mel-ccl-c-raw))
232 (define-ccl-program mel-ccl-count-pq
233   (mel-ccl-count-q-length mel-ccl-p-raw))
234
235 ;; Quoted-Printable
236
237 (eval-when-compile
238
239 (defvar eof-block-branches)
240 (defvar eof-block-reg)
241 (defun mel-ccl-set-eof-block (branch)
242   (let ((p (assoc branch eof-block-branches)))
243     (unless p
244       (setq p (cons branch (length eof-block-branches))
245             eof-block-branches (cons p eof-block-branches)))
246     `(,eof-block-reg = ,(cdr p))))
247
248 )
249
250 (eval-when-compile
251
252 (defun mel-ccl-try-to-read-crlf (input-crlf reg
253                                             succ
254                                             cr-eof cr-fail
255                                             lf-eof lf-fail
256                                             crlf-eof crlf-fail)
257   (if input-crlf
258       `(,(mel-ccl-set-eof-block cr-eof)
259         (read-if (,reg == ?\r)
260           (,(mel-ccl-set-eof-block lf-eof)
261            (read-if (,reg == ?\n)
262              ,succ
263              ,lf-fail))
264           ,cr-fail))
265     `(,(mel-ccl-set-eof-block crlf-eof)
266       (read-if (,reg == ?\n)
267         ,succ
268         ,crlf-fail))))
269
270 )
271
272 (eval-when-compile
273
274 ;; Generated CCL program works not properly on 20.2 because CCL_EOF_BLOCK
275 ;; is not executed.
276 (defun mel-ccl-encode-quoted-printable-generic (input-crlf output-crlf)
277   (let ((hard (if output-crlf "\r\n" "\n"))
278         (soft (if output-crlf "=\r\n" "=\n"))
279         (eof-block-branches nil)
280         (eof-block-reg 'r4)
281         (after-wsp 'r5)
282         (column 'r6)
283         (type 'r3)
284         (current 'r0)
285         (type-raw 0)
286         (type-enc 1)
287         (type-wsp 2)
288         (type-brk 3)
289         )
290     `(4
291       ((,column = 0)
292        (,after-wsp = 0)
293        ,(mel-ccl-set-eof-block '(end))
294        (read r0)
295        (loop    ; invariant: column <= 75
296         (loop
297          (loop
298           (branch
299            r0
300            ,@(mapcar
301               (lambda (r0)
302                 (let ((tmp (aref mel-ccl-qp-table r0)))
303                   (cond
304                    ((eq r0 (char-int ?F))
305                     `(if (,column == 0)
306                          (,(mel-ccl-set-eof-block '((write "F") (end)))
307                           (read-if (r0 == ?r)
308                             (,(mel-ccl-set-eof-block '((write "Fr") (end)))
309                              (read-if (r0 == ?o)
310                                (,(mel-ccl-set-eof-block '((write "Fro") (end)))
311                                 (read-if (r0 == ?m)
312                                   (,(mel-ccl-set-eof-block '((write "From") (end)))
313                                    (read-if (r0 == ? )
314                                      ((,column = 7)
315                                       (,after-wsp = 1)
316                                       ,(mel-ccl-set-eof-block '((write "From=20") (end)))
317                                       (read r0)
318                                       (write-repeat "=46rom "))
319                                      ((,column = 4)
320                                       (write-repeat "From"))))
321                                   ((,column = 3)
322                                    (write-repeat "Fro"))))
323                                ((,column = 2)
324                                 (write-repeat "Fr"))))
325                             ((,column = 1)
326                              (write-repeat "F"))))
327                        ((,type = ,type-raw) (break)) ; RAW
328                        ))
329                    ((eq r0 (char-int ?.))
330                     `(if (,column == 0)
331                          ,(mel-ccl-try-to-read-crlf
332                             input-crlf 'r0
333                             ;; "." CR LF (input-crlf: t)
334                             ;; "." LF (input-crlf: nil)
335                             `((write ,(concat "=2E" hard))
336                               ,(mel-ccl-set-eof-block '(end))
337                               (read r0)
338                               (repeat))
339                             ;; "." <EOF>
340                             '((write ".") (end))
341                             ;; "." noCR (input-crlf: t)
342                             `((,column = 1)
343                               (write-repeat "."))
344                             ;; "." CR <EOF> (input-crlf: t)
345                             '((write ".=0D") (end))
346                             ;; "." CR noLF (input-crlf: t)
347                             `((,column = 4)
348                               (write-repeat ".=0D"))
349                             ;; "." <EOF> (input-crlf: nil)
350                             '((write ".") (end))
351                             ;; "." noLF (input-crlf: nil)
352                             `((,column = 1)
353                               (write-repeat ".")))
354                        ((,type = ,type-raw) (break)) ; RAW
355                        ))
356                    ((eq tmp 'raw) `((,type = ,type-raw) (break)))
357                    ((eq tmp 'enc) `((,type = ,type-enc) (break)))
358                    ((eq tmp 'wsp) `((,type = ,type-wsp) (break)))
359                    ((eq tmp 'cr) `((,type = ,(if input-crlf type-brk type-enc))
360                                    (break)))
361                    ((eq tmp 'lf) `((,type = ,(if input-crlf type-enc type-brk))
362                                    (break)))
363                    )))
364               mel-ccl-256-table)))
365          ;; r0:type{raw,enc,wsp,brk}
366          (branch
367           ,type
368           ;; r0:type-raw
369           (if (,column < 75)
370               ((,column += 1)
371                (,after-wsp = 0)
372                ,(mel-ccl-set-eof-block '(end))
373                (write-read-repeat r0))
374             ((r1 = (r0 + 0))
375              (,after-wsp = 0)
376              ,@(mel-ccl-try-to-read-crlf
377                 input-crlf 'r0
378                 `((,column = 0)
379                   (write r1)
380                   ,(mel-ccl-set-eof-block `((write ,hard) (end)))
381                   (read r0)
382                   (write-repeat ,hard))
383                 '((write r1) (end))
384                 `((,column = 1)
385                   (write ,soft) (write-repeat r1))
386                 `((write ,soft) (write r1) (write "=0D") (end))
387                 `((,column = 4)
388                   (write ,soft) (write r1) (write-repeat "=0D"))
389                 '((write r1) (end))
390                 `((,column = 1)
391                   (write ,soft) (write-repeat r1)))))
392           ;; r0:type-enc
393           ((,after-wsp = 0)
394            (if (,column < 73)
395                ((,column += 3)
396                 (write "=")
397                 (write r0 ,mel-ccl-high-table)
398                 ,(mel-ccl-set-eof-block '(end))
399                 (write-read-repeat r0 ,mel-ccl-low-table))
400              (if (,column < 74)
401                  ((r1 = (r0 + 0))
402                   (,after-wsp = 0)
403                   ,@(mel-ccl-try-to-read-crlf
404                      input-crlf 'r0
405                      `((,column = 0)
406                        (write "=")
407                        (write r1 ,mel-ccl-high-table)
408                        (write r1 ,mel-ccl-low-table)
409                        (write ,hard)
410                        ,(mel-ccl-set-eof-block '(end))
411                        (read r0)
412                        (repeat))
413                      `((write "=")
414                        (write r1 ,mel-ccl-high-table)
415                        (write r1 ,mel-ccl-low-table)
416                        (end))
417                      `((,column = 3)
418                        (write ,(concat soft "="))
419                        (write r1 ,mel-ccl-high-table)
420                        (write r1 ,mel-ccl-low-table)
421                        (repeat))
422                      `((write ,(concat soft "="))
423                        (write r1 ,mel-ccl-high-table)
424                        (write r1 ,mel-ccl-low-table)
425                        (write "=0D")
426                        (end))
427                      `((,column = 6)
428                        (write ,(concat soft "="))
429                        (write r1 ,mel-ccl-high-table)
430                        (write r1 ,mel-ccl-low-table)
431                        (write-repeat "=0D"))
432                      `((write "=")
433                        (write r1 ,mel-ccl-high-table)
434                        (write r1 ,mel-ccl-low-table)
435                        (end))
436                      `((,column = 3)
437                        (write ,(concat soft "="))
438                        (write r1 ,mel-ccl-high-table)
439                        (write r1 ,mel-ccl-low-table)
440                        (repeat))))
441                ((,column = 3)
442                 (write ,(concat soft "="))
443                 (write r0 ,mel-ccl-high-table)
444                 ,(mel-ccl-set-eof-block '(end))
445                 (write-read-repeat r0 ,mel-ccl-low-table)))))
446           ;; r0:type-wsp
447           (if (,column < 73)
448               ((r1 = (r0 + 0))
449                ,@(mel-ccl-try-to-read-crlf
450                   input-crlf 'r0
451                   `((,column = 0)
452                     (,after-wsp = 0)
453                     (write "=")
454                     (write r1 ,mel-ccl-high-table)
455                     (write r1 ,mel-ccl-low-table)
456                     (write ,hard)
457                     ,(mel-ccl-set-eof-block `(end))
458                     (read r0)
459                     (repeat))
460                   `((write "=")
461                     (write r1 ,mel-ccl-high-table)
462                     (write r1 ,mel-ccl-low-table)
463                     (end))
464                   `((,column += 1)
465                     (,after-wsp = 1)
466                     (write-repeat r1))
467                   `((write r1)
468                     (write "=0D")
469                     (end))
470                   `((,column += 4)
471                     (,after-wsp = 0)
472                     (write r1)
473                     (write-repeat "=0D"))
474                   `((write "=")
475                     (write r1 ,mel-ccl-high-table)
476                     (write r1 ,mel-ccl-low-table)
477                     (end))
478                   `((,column += 1)
479                     (,after-wsp = 1)
480                     (write-repeat r1))))
481             (if (,column < 74)
482                 ((r1 = (r0 + 0))
483                  ,@(mel-ccl-try-to-read-crlf
484                     input-crlf 'r0
485                     `((,column = 0)
486                       (,after-wsp = 0)
487                       (write "=")
488                       (write r1 ,mel-ccl-high-table)
489                       (write r1 ,mel-ccl-low-table)
490                       (write ,hard)
491                       ,(mel-ccl-set-eof-block `(end))
492                       (read r0)
493                       (repeat))
494                     `((write "=")
495                       (write r1 ,mel-ccl-high-table)
496                       (write r1 ,mel-ccl-low-table)
497                       (end))
498                     `((,column += 1)
499                       (,after-wsp = 1)
500                       (write-repeat r1))
501                     `((write r1)
502                       (write ,(concat soft "=0D"))
503                       (end))
504                     `((,column = 3)
505                       (,after-wsp = 0)
506                       (write r1)
507                       (write-repeat ,(concat soft "=0D")))
508                     `((write "=")
509                       (write r1 ,mel-ccl-high-table)
510                       (write r1 ,mel-ccl-low-table)
511                       (end))
512                     `((,column += 1)
513                       (,after-wsp = 1)
514                       (write-repeat r1))))
515               (if (,column < 75)
516                   ((,column += 1)
517                    (,after-wsp = 1)
518                    ,(mel-ccl-set-eof-block `((write ,soft) (end)))
519                    (write-read-repeat r0))
520                 ((write ,soft)
521                  (,column = 0)
522                  (,after-wsp = 0)
523                  (repeat)))))
524           ;; r0:type-brk
525           ,(if input-crlf
526                ;; r0{CR}:type-brk
527                `((if ((,column > 73) & ,after-wsp)
528                      ((,column = 0)
529                       (,after-wsp = 0)
530                       (write ,soft)))
531                  ,(mel-ccl-set-eof-block `((if (,column > 73) (write ,soft))
532                                            (write "=0D") (end)))
533                  (read-if (r0 == ?\n)
534                    (if ,after-wsp
535                        ((,after-wsp = 0)
536                         (,column = 0)
537                         (write ,(concat soft hard))
538                         ,(mel-ccl-set-eof-block '(end))
539                         (read r0)
540                         (repeat))
541                      ((,after-wsp = 0)
542                       (,column = 0)
543                       (write ,hard)
544                       ,(mel-ccl-set-eof-block '(end))
545                       (read r0)
546                       (repeat)))
547                    (if (,column < 73)
548                        ((,after-wsp = 0)
549                         (,column += 3)
550                         (write-repeat "=0D"))
551                      (if (,column < 74)
552                          (if (r0 == ?\r)
553                              ((,after-wsp = 0)
554                               ,(mel-ccl-set-eof-block
555                                 `((write ,(concat soft "=0D=0D")) (end)))
556                               (read-if (r0 == ?\n)
557                                 ((,column = 0)
558                                  ,(mel-ccl-set-eof-block
559                                    `((write ,(concat "=0D" hard)) (end)))
560                                  (read r0)
561                                  (write-repeat ,(concat "=0D" hard)))
562                                 ((,column = 6)
563                                  (write-repeat ,(concat soft "=0D=0D")))))
564                            ((,after-wsp = 0)
565                             (,column = 3)
566                             (write-repeat ,(concat soft "=0D"))))
567                        ((,after-wsp = 0)
568                         (,column = 3)
569                         (write-repeat ,(concat soft "=0D")))))))
570              ;; r0{LF}:type-brk
571              `(if ,after-wsp
572                   ;; WSP ; r0{LF}:type-brk
573                   ((,after-wsp = 0)
574                    (,column = 0)
575                    (write ,(concat soft (if output-crlf "\r" "")))
576                    ,(mel-ccl-set-eof-block `(end))
577                    (write-read-repeat r0))
578                 ;; noWSP ; r0{LF}:type-brk
579                 ((,after-wsp = 0)
580                  (,column = 0)
581                  ,@(if output-crlf '((write ?\r)) '())
582                  ,(mel-ccl-set-eof-block `(end))
583                  (write-read-repeat r0)))
584              )))))
585       (branch
586        ,eof-block-reg
587        ,@(reverse (mapcar 'car eof-block-branches))))))
588
589 (defun mel-ccl-decode-quoted-printable-generic (input-crlf output-crlf)
590   `(1
591     ((read r0)
592      (loop
593       (branch
594        r0
595        ,@(mapcar
596           (lambda (r0)
597             (let ((tmp (aref mel-ccl-qp-table r0)))
598               (cond
599                ((eq tmp 'raw) `(write-read-repeat r0))
600                ((eq tmp 'wsp) (if (eq r0 (char-int ? ))
601                                   `(r1 = 1)
602                                 `(r1 = 0)))
603                ((eq tmp 'cr)
604                 (if input-crlf
605                     ;; r0='\r'
606                     `((read r0)
607                       ;; '\r' r0
608                       (if (r0 == ?\n)
609                           ;; '\r' r0='\n'
610                           ;; hard line break found.
611                           ,(if output-crlf
612                                '((write ?\r)
613                                  (write-read-repeat r0))
614                              '(write-read-repeat r0))
615                         ;; '\r' r0:[^\n]
616                         ;; invalid control character (bare CR) found.
617                         ;; -> ignore it and rescan from r0.
618                         (repeat)))
619                   ;; r0='\r'
620                   ;; invalid character (bare CR) found.
621                   ;; -> ignore.
622                   `((read r0)
623                     (repeat))))
624                ((eq tmp 'lf)
625                 (if input-crlf
626                     ;; r0='\n'
627                     ;; invalid character (bare LF) found.
628                     ;; -> ignore.
629                     `((read r0)
630                       (repeat))
631                   ;; r0='\r\n'
632                   ;; hard line break found.
633                   (if output-crlf
634                       '((write ?\r)
635                         (write-read-repeat r0))
636                     '(write-read-repeat r0))))
637                ((eq r0 (char-int ?=))
638                 ;; r0='='
639                 `((read r0)
640                   ;; '=' r0
641                   (r1 = (r0 == ?\t))
642                   (if ((r0 == ? ) | r1)
643                       ;; '=' r0:[\t ]
644                       ;; Skip transport-padding.
645                       ;; It should check CR LF after
646                       ;; transport-padding.
647                       (loop
648                        (read-if (r0 == ?\t)
649                                 (repeat)
650                                 (if (r0 == ? )
651                                     (repeat)
652                                   (break)))))
653                   ;; '=' [\t ]* r0:[^\t ]
654                   (branch
655                    r0
656                    ,@(mapcar
657                       (lambda (r0)
658                         (cond
659                          ((eq r0 (char-int ?\r))
660                           (if input-crlf
661                               ;; '=' [\t ]* r0='\r'
662                               `((read r0)
663                                 ;; '=' [\t ]* '\r' r0
664                                 (if (r0 == ?\n)
665                                     ;; '=' [\t ]* '\r' r0='\n'
666                                     ;; soft line break found.
667                                     ((read r0)
668                                      (repeat))
669                                   ;; '=' [\t ]* '\r' r0:[^\n]
670                                   ;; invalid input ->
671                                   ;; output "=" and rescan from r0.
672                                   ((write "=")
673                                    (repeat))))
674                             ;; '=' [\t ]* r0='\r'
675                             ;; invalid input (bare CR found) -> 
676                             ;; output "=" and rescan from next.
677                             `((write ?=)
678                               (read r0)
679                               (repeat))))
680                          ((eq r0 (char-int ?\n))
681                           (if input-crlf
682                               ;; '=' [\t ]* r0='\n'
683                               ;; invalid input (bare LF found) -> 
684                               ;; output "=" and rescan from next.
685                               `((write ?=)
686                                 (read r0)
687                                 (repeat))
688                             ;; '=' [\t ]* r0='\r\n'
689                             ;; soft line break found.
690                             `((read r0)
691                               (repeat))))
692                          ((setq tmp (nth r0 mel-ccl-256-to-16-table))
693                           ;; '=' [\t ]* r0:[0-9A-F]
694                           ;; upper nibble of hexadecimal digit found.
695                           `((r1 = (r0 + 0))
696                             (r0 = ,tmp)))
697                          (t
698                           ;; '=' [\t ]* r0:[^\r0-9A-F]
699                           ;; invalid input ->
700                           ;; output "=" and rescan from r0.
701                           `((write ?=)
702                             (repeat)))))
703                       mel-ccl-256-table))
704                   ;; '=' [\t ]* r1:r0:[0-9A-F]
705                   (read-branch
706                    r2
707                    ,@(mapcar
708                       (lambda (r2)
709                         (if (setq tmp (nth r2 mel-ccl-256-to-16-table))
710                             ;; '=' [\t ]* r1:r0:[0-9A-F] r2:[0-9A-F]
711                             `(write-read-repeat
712                               r0
713                               ,(vconcat
714                                 (mapcar
715                                  (lambda (r0)
716                                    (logior (lsh r0 4) tmp))
717                                  mel-ccl-16-table)))
718                           ;; '=' [\t ]* r1:r0:[0-9A-F] r2:[^0-9A-F]
719                           ;; invalid input
720                           `(r3 = 0)     ; nop
721                           ))
722                       mel-ccl-256-table))
723                   ;; '=' [\t ]* r1:r0:[0-9A-F] r2:[^0-9A-F]
724                   ;; invalid input ->
725                   ;; output "=" with hex digit and rescan from r2.
726                   (write ?=)
727                   (r0 = (r2 + 0))
728                   (write-repeat r1)))
729                (t
730                 ;; r0:[^\t\r -~]
731                 ;; invalid character found.
732                 ;; -> ignore.
733                 `((read r0)
734                   (repeat))))))
735           mel-ccl-256-table))
736       ;; r1[0]:[\t ]
737       (loop
738        ,@(apply
739           'append
740           (mapcar
741            (lambda (regnum)
742              (let ((reg (aref [r1 r2 r3 r4 r5] regnum)))
743                (apply
744                 'append
745                 (mapcar
746                  (lambda (bit)
747                    (if (= bit 0)
748                        (if (= regnum 0)
749                            nil
750                          `((read r0)
751                            (if (r0 == ?\t)
752                                (,reg = 0)
753                              (if (r0 == ?\ )
754                                  (,reg = 1)
755                                ((r6 = ,(+ (* regnum 28) bit))
756                                 (break))))))
757                      `((read r0)
758                        (if (r0 == ?\ )
759                            (,reg |= ,(lsh 1 bit))
760                          (if (r0 != ?\t)
761                              ((r6 = ,(+ (* regnum 28) bit))
762                               (break)))))))
763                  mel-ccl-28-table))))
764            '(0 1 2 3 4)))
765        ;; white space buffer exhaust.
766        ;; error: line length limit (76bytes) violation.
767        ;; -> ignore these white spaces.
768        (repeat))
769       ,(if input-crlf
770            `(if (r0 == ?\r)
771                 ((read r0)
772                  (if (r0 == ?\n)
773                      ;; trailing white spaces found.
774                      ;; -> ignore these white spacs.
775                      ((write ,(if output-crlf "\r\n" "\n"))
776                       (read r0)
777                       (repeat))
778                    ;; [\t ]* \r r0:[^\n]
779                    ;; error: bare CR found.
780                    ;; -> output white spaces and ignore bare CR.
781                    ))
782               ;; [\t ]* r0:[^\r]
783               ;; middle white spaces found.
784               )
785          `(if (r0 == ?\n)
786               ;; trailing white spaces found.
787               ;; -> ignore these white spacs.
788               ((write ,(if output-crlf "\r\n" "\n"))
789                (read r0)
790                (repeat))
791             ;; [\t ]* r0:[^\n]
792             ;; middle white spaces found.
793             ))
794       ,@(apply
795          'append
796          (mapcar
797           (lambda (regnum)
798             (let ((reg (aref [r1 r2 r3 r4 r5] regnum)))
799               (apply
800                'append
801                (mapcar
802                 (lambda (bit)
803                   `((if (,reg & ,(lsh 1 bit))
804                         (write ?\ )
805                       (write ?\t))
806                     (if (r6 == ,(+ (* regnum 28) bit 1))
807                         (repeat))))
808                 mel-ccl-28-table))))
809           '(0 1 2 3 4)))
810       (repeat)
811       ))))
812
813 )
814
815 (define-ccl-program mel-ccl-encode-quoted-printable-crlf-crlf
816   (mel-ccl-encode-quoted-printable-generic t t))
817
818 (define-ccl-program mel-ccl-encode-quoted-printable-crlf-lf
819   (mel-ccl-encode-quoted-printable-generic t nil))
820
821 (define-ccl-program mel-ccl-encode-quoted-printable-lf-crlf
822   (mel-ccl-encode-quoted-printable-generic nil t))
823
824 (define-ccl-program mel-ccl-encode-quoted-printable-lf-lf
825   (mel-ccl-encode-quoted-printable-generic nil nil))
826
827 (define-ccl-program mel-ccl-decode-quoted-printable-crlf-crlf
828   (mel-ccl-decode-quoted-printable-generic t t))
829
830 (define-ccl-program mel-ccl-decode-quoted-printable-crlf-lf
831   (mel-ccl-decode-quoted-printable-generic t nil))
832
833 (define-ccl-program mel-ccl-decode-quoted-printable-lf-crlf
834   (mel-ccl-decode-quoted-printable-generic nil t))
835
836 (define-ccl-program mel-ccl-decode-quoted-printable-lf-lf
837   (mel-ccl-decode-quoted-printable-generic nil nil))
838
839
840 ;;; @ coding system
841 ;;;
842
843 (make-ccl-coding-system
844  'mel-ccl-uq-rev ?Q "MIME Q-encoding in unstructured field (reversed)"
845  'mel-ccl-encode-uq 'mel-ccl-decode-q)
846
847 (make-ccl-coding-system
848  'mel-ccl-cq-rev ?Q "MIME Q-encoding in comment (reversed)"
849  'mel-ccl-encode-cq 'mel-ccl-decode-q)
850
851 (make-ccl-coding-system
852  'mel-ccl-pq-rev ?Q "MIME Q-encoding in phrase (reversed)"
853  'mel-ccl-encode-pq 'mel-ccl-decode-q)
854
855 (make-ccl-coding-system
856  'mel-ccl-quoted-printable-crlf-crlf-rev
857  ?Q "MIME Quoted-Printable-encoding (reversed)"
858  'mel-ccl-encode-quoted-printable-crlf-crlf
859  'mel-ccl-decode-quoted-printable-crlf-crlf)
860
861 (make-ccl-coding-system
862  'mel-ccl-quoted-printable-lf-crlf-rev
863  ?Q "MIME Quoted-Printable-encoding (LF encoding) (reversed)"
864  'mel-ccl-encode-quoted-printable-crlf-lf
865  'mel-ccl-decode-quoted-printable-lf-crlf)
866
867 (make-ccl-coding-system
868  'mel-ccl-quoted-printable-crlf-lf-rev
869  ?Q "MIME Quoted-Printable-encoding (LF internal) (reversed)"
870  'mel-ccl-encode-quoted-printable-lf-crlf
871  'mel-ccl-decode-quoted-printable-crlf-lf)
872
873 (make-ccl-coding-system
874  'mel-ccl-quoted-printable-lf-lf-rev
875  ?Q "MIME Quoted-Printable-encoding (LF encoding) (LF internal) (reversed)"
876  'mel-ccl-encode-quoted-printable-lf-lf
877  'mel-ccl-decode-quoted-printable-lf-lf)
878
879
880 ;;; @ quoted-printable
881 ;;;
882
883 (check-broken-facility ccl-execute-eof-block-on-decoding-some)
884
885 (unless-broken ccl-execute-eof-block-on-decoding-some
886
887   (defun quoted-printable-ccl-encode-string (string)
888     "Encode STRING with quoted-printable encoding."
889     (decode-coding-string
890      string
891      'mel-ccl-quoted-printable-lf-lf-rev))
892
893   (defun quoted-printable-ccl-encode-region (start end)
894     "Encode the region from START to END with quoted-printable encoding."
895     (interactive "*r")
896     (decode-coding-region start end 'mel-ccl-quoted-printable-lf-lf-rev))
897
898   (defun quoted-printable-ccl-insert-encoded-file (filename)
899     "Encode contents of the file named as FILENAME, and insert it."
900     (interactive "*fInsert encoded file: ")
901     (let ((coding-system-for-read 'mel-ccl-quoted-printable-lf-lf-rev)
902           format-alist)
903       (insert-file-contents filename)))
904
905   (mel-define-method-function
906    (mime-encode-string string (nil "quoted-printable"))
907    'quoted-printable-ccl-encode-string)
908   (mel-define-method-function
909    (mime-encode-region start end (nil "quoted-printable"))
910    'quoted-printable-ccl-encode-region)
911   (mel-define-method-function
912    (mime-insert-encoded-file filename (nil "quoted-printable"))
913    'quoted-printable-ccl-insert-encoded-file)
914   )
915
916 (defun quoted-printable-ccl-decode-string (string)
917   "Decode quoted-printable encoded STRING."
918   (encode-coding-string
919    string
920    'mel-ccl-quoted-printable-lf-lf-rev))
921
922 (defun quoted-printable-ccl-decode-region (start end)
923   "Decode the region from START to END with quoted-printable
924 encoding."
925   (interactive "*r")
926   (encode-coding-region start end 'mel-ccl-quoted-printable-lf-lf-rev))
927
928 (defun quoted-printable-ccl-write-decoded-region (start end filename)
929   "Decode quoted-printable encoded current region and write out to FILENAME."
930   (interactive "*r\nFWrite decoded region to file: ")
931   (let ((coding-system-for-write 'mel-ccl-quoted-printable-lf-lf-rev)
932         jka-compr-compression-info-list jam-zcat-filename-list)
933     (write-region start end filename)))
934
935 (mel-define-method-function
936  (mime-decode-string string (nil "quoted-printable"))
937  'quoted-printable-ccl-decode-string)
938 (mel-define-method-function
939  (mime-decode-region start end (nil "quoted-printable"))
940  'quoted-printable-ccl-decode-region)
941 (mel-define-method-function
942  (mime-write-decoded-region start end filename (nil "quoted-printable"))
943  'quoted-printable-ccl-write-decoded-region)
944
945
946 ;;; @ Q
947 ;;;
948
949 (defun q-encoding-ccl-encode-string (string &optional mode)
950   "Encode STRING to Q-encoding of encoded-word, and return the result.
951 MODE allows `text', `comment', `phrase' or nil.  Default value is
952 `phrase'."
953   (decode-coding-string
954    string
955    (cond
956     ((eq mode 'text) 'mel-ccl-uq-rev)
957     ((eq mode 'comment) 'mel-ccl-cq-rev)
958     (t 'mel-ccl-pq-rev))))
959
960 (defun q-encoding-ccl-decode-string (string)
961   "Decode Q encoded STRING and return the result."
962   (encode-coding-string
963    string
964    'mel-ccl-uq-rev))
965
966 (unless (featurep 'xemacs)
967   (defun q-encoding-ccl-encoded-length (string &optional mode)
968     (let ((status [nil nil nil nil nil nil nil nil nil]))
969       (fillarray status nil)            ; XXX: Is this necessary?
970       (ccl-execute-on-string
971        (cond
972         ((eq mode 'text) 'mel-ccl-count-uq)
973         ((eq mode 'comment) 'mel-ccl-count-cq)
974         (t 'mel-ccl-count-pq))
975        status
976        string)
977       (aref status 0)))
978   )
979
980 (mel-define-method-function (encoded-text-encode-string string (nil "Q"))
981                             'q-encoding-ccl-encode-string)
982
983 (mel-define-method encoded-text-decode-string (string (nil "Q"))
984   (if (string-match (eval-when-compile
985                       (concat "\\`" Q-encoded-text-regexp "\\'"))
986                     string)
987       (q-encoding-ccl-decode-string string)
988     (error "Invalid encoded-text %s" string)))
989
990
991 ;;; @ end
992 ;;;
993
994 (provide 'mel-q-ccl)
995
996 ;;; mel-q-ccl.el ends here.