Update FSF's address in GPL notices.
[elisp/flim.git] / mel-b-ccl.el
1 ;;; mel-b-ccl.el --- Base64 encoder/decoder using CCL.
2
3 ;; Copyright (C) 1998,1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Tanaka Akira <akr@m17n.org>
6 ;; Created: 1998/9/17
7 ;; Keywords: MIME, Base64
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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-4-table
39   '(  0   1   2   3))
40
41 (defconst mel-ccl-16-table
42   '(  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15))
43
44 (defconst mel-ccl-64-table
45   '(  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
46      16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
47      32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47
48      48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63))
49
50 (defconst mel-ccl-256-table
51   '(  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
52      16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
53      32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47
54      48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63
55      64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79
56      80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95
57      96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111
58     112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
59     128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
60     144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
61     160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
62     176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
63     192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
64     208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
65     224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
66     240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255))
67
68 (defconst mel-ccl-256-to-64-table
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  62 nil nil nil  63
72      52  53  54  55  56  57  58  59  60  61 nil nil nil   t nil nil
73     nil   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14
74      15  16  17  18  19  20  21  22  23  24  25 nil nil nil nil nil
75     nil  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40
76      41  42  43  44  45  46  47  48  49  50  51 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
86 (defconst mel-ccl-64-to-256-table
87   (mapcar
88    'char-int
89    "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
90 abcdefghijklmnopqrstuvwxyz\
91 0123456789\
92 +/"))
93
94 )
95
96
97 ;;; @ CCL programs
98 ;;;
99
100 (eval-when-compile
101
102 (defun mel-ccl-decode-b-bit-ex (v)
103   (logior
104    (lsh (logand v (lsh 255 16)) -16)
105    (logand v (lsh 255 8))
106    (lsh (logand v 255) 16)))
107
108 )
109
110 (eval-when-compile
111
112 (defconst mel-ccl-decode-b-0-table
113   (vconcat
114    (mapcar
115     (lambda (v)
116       (if (integerp v)
117           (mel-ccl-decode-b-bit-ex (lsh v 18))
118         (lsh 1 24)))
119     mel-ccl-256-to-64-table)))
120
121 (defconst mel-ccl-decode-b-1-table
122   (vconcat
123    (mapcar
124     (lambda (v)
125       (if (integerp v)
126           (mel-ccl-decode-b-bit-ex (lsh v 12))
127         (lsh 1 25)))
128     mel-ccl-256-to-64-table)))
129
130 (defconst mel-ccl-decode-b-2-table
131   (vconcat
132    (mapcar
133     (lambda (v)
134       (if (integerp v)
135           (mel-ccl-decode-b-bit-ex (lsh v 6))
136         (lsh 1 26)))
137     mel-ccl-256-to-64-table)))
138
139 (defconst mel-ccl-decode-b-3-table
140   (vconcat
141    (mapcar
142     (lambda (v)
143       (if (integerp v)
144           (mel-ccl-decode-b-bit-ex v)
145         (lsh 1 27)))
146     mel-ccl-256-to-64-table)))
147
148 )
149
150 (check-broken-facility ccl-cascading-read)
151
152 (if-broken ccl-cascading-read
153     (define-ccl-program mel-ccl-decode-b
154       `(1
155         (loop
156          (loop
157           (read-branch
158            r1
159            ,@(mapcar
160               (lambda (v)
161                 (cond
162                  ((or (eq v nil) (eq v t)) '(repeat))
163                  (t `((r0 = ,(lsh v 2)) (break)))))
164               mel-ccl-256-to-64-table)))
165          (loop
166           (read-branch
167            r1
168            ,@(mapcar
169               (lambda (v)
170                 (cond
171                  ((or (eq v nil) (eq v t)) '(repeat))
172                  ((= (lsh v -4) 0) `((write r0) (r0 = ,(lsh (logand v 15) 4)) (break)))
173                  (t `((r0 |= ,(lsh v -4)) (write r0) (r0 = ,(lsh (logand v 15) 4)) (break)))))
174               mel-ccl-256-to-64-table)))
175          (loop
176           (read-branch
177            r1
178            ,@(mapcar
179               (lambda (v)
180                 (cond
181                  ((eq v nil) '(repeat))
182                  ((eq v t) '(end))
183                  ((= (lsh v -2) 0) `((write r0) (r0 = ,(lsh (logand v 3) 6)) (break)))
184                  (t `((r0 |= ,(lsh v -2)) (write r0) (r0 = ,(lsh (logand v 3) 6)) (break)))))
185               mel-ccl-256-to-64-table)))
186          (loop
187           (read-branch
188            r1
189            ,@(mapcar
190               (lambda (v)
191                 (cond
192                  ((eq v nil) '(repeat))
193                  ((eq v t) '(end))
194                  (t `((r0 |= ,v) (write r0) (break)))))
195               mel-ccl-256-to-64-table)))
196          (repeat))))
197   (define-ccl-program mel-ccl-decode-b
198     `(1
199       (loop
200        (read r0 r1 r2 r3)
201        (r4 = r0 ,mel-ccl-decode-b-0-table)
202        (r5 = r1 ,mel-ccl-decode-b-1-table)
203        (r4 |= r5)
204        (r5 = r2 ,mel-ccl-decode-b-2-table)
205        (r4 |= r5)
206        (r5 = r3 ,mel-ccl-decode-b-3-table)
207        (r4 |= r5)
208        (if (r4 & ,(lognot (1- (lsh 1 24))))
209            ((loop
210              (if (r4 & ,(lsh 1 24))
211                  ((r0 = r1) (r1 = r2) (r2 = r3) (read r3)
212                   (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
213                   (r5 = r3 ,mel-ccl-decode-b-3-table)
214                   (r4 |= r5)
215                   (repeat))
216                (break)))
217             (loop
218              (if (r4 & ,(lsh 1 25))
219                  ((r1 = r2) (r2 = r3) (read r3)
220                   (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
221                   (r5 = r3 ,mel-ccl-decode-b-3-table)
222                   (r4 |= r5)
223                   (repeat))
224                (break)))
225             (loop
226              (if (r2 != ?=)
227                  (if (r4 & ,(lsh 1 26))
228                      ((r2 = r3) (read r3)
229                       (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
230                       (r5 = r3 ,mel-ccl-decode-b-3-table)
231                       (r4 |= r5)
232                       (repeat))
233                    ((r6 = 0)
234                     (break)))
235                ((r6 = 1)
236                 (break))))
237             (loop
238              (if (r3 != ?=)
239                  (if (r4 & ,(lsh 1 27))
240                      ((read r3)
241                       (r4 = r3 ,mel-ccl-decode-b-3-table)
242                       (repeat))
243                    (break))
244                ((r6 |= 2)
245                 (break))))
246             (r4 = r0 ,mel-ccl-decode-b-0-table)
247             (r5 = r1 ,mel-ccl-decode-b-1-table)
248             (r4 |= r5)
249             (branch
250              r6
251              ;; BBBB
252              ((r5 = r2 ,mel-ccl-decode-b-2-table)
253               (r4 |= r5)
254               (r5 = r3 ,mel-ccl-decode-b-3-table)
255               (r4 |= r5)
256               (r4 >8= 0)
257               (write r7)
258               (r4 >8= 0)
259               (write r7)
260               (write-repeat r4))
261              ;; error: BB=B 
262              ((write (r4 & 255))
263               (end))
264              ;; BBB=
265              ((r5 = r2 ,mel-ccl-decode-b-2-table)
266               (r4 |= r5)
267               (r4 >8= 0)
268               (write r7)
269               (write (r4 & 255))
270               (end)                     ; Excessive (end) is workaround for XEmacs 21.0.
271                                         ; Without this, "AAA=" is converted to "^@^@^@".
272               (end))
273              ;; BB==
274              ((write (r4 & 255))
275               (end))))
276          ((r4 >8= 0)
277           (write r7)
278           (r4 >8= 0)
279           (write r7)
280           (write-repeat r4))))))
281   )
282
283 (eval-when-compile
284
285 ;; Generated CCL program works not properly on 20.2 because CCL_EOF_BLOCK
286 ;; is not executed.
287 (defun mel-ccl-encode-base64-generic
288   (&optional quantums-per-line output-crlf terminate-with-newline)
289   `(2
290     ((r3 = 0)
291      (r2 = 0)
292      (read r1)
293      (loop
294       (branch
295        r1
296        ,@(mapcar
297           (lambda (r1)
298             `((write ,(nth (lsh r1 -2) mel-ccl-64-to-256-table))
299               (r0 = ,(logand r1 3))))
300           mel-ccl-256-table))
301       (r2 = 1)
302       (read-branch
303        r1
304        ,@(mapcar
305           (lambda (r1)
306             `((write r0 ,(vconcat
307                           (mapcar
308                            (lambda (r0)
309                              (nth (logior (lsh r0 4)
310                                           (lsh r1 -4))
311                                   mel-ccl-64-to-256-table))
312                            mel-ccl-4-table)))
313               (r0 = ,(logand r1 15))))
314           mel-ccl-256-table))
315       (r2 = 2)
316       (read-branch
317        r1
318        ,@(mapcar
319           (lambda (r1)
320             `((write r0 ,(vconcat
321                           (mapcar
322                            (lambda (r0)
323                              (nth (logior (lsh r0 2)
324                                           (lsh r1 -6))
325                                   mel-ccl-64-to-256-table))
326                            mel-ccl-16-table)))))
327           mel-ccl-256-table))
328       (r1 &= 63)
329       (write r1 ,(vconcat
330                   (mapcar
331                    (lambda (r1)
332                      (nth r1 mel-ccl-64-to-256-table))
333                    mel-ccl-64-table)))
334       (r3 += 1)
335       (r2 = 0)
336       (read r1)
337       ,@(when quantums-per-line
338           `((if (r3 == ,quantums-per-line)
339                 ((write ,(if output-crlf "\r\n" "\n"))
340                  (r3 = 0)))))
341       (repeat)))
342     (branch
343      r2
344      ,(if terminate-with-newline
345           `(if (r3 > 0) (write ,(if output-crlf "\r\n" "\n")))
346         `(r0 = 0))
347      ((write r0 ,(vconcat
348                   (mapcar
349                    (lambda (r0)
350                      (nth (lsh r0 4) mel-ccl-64-to-256-table))
351                    mel-ccl-4-table)))
352       (write ,(if terminate-with-newline
353                   (if output-crlf "==\r\n" "==\n")
354                 "==")))
355      ((write r0 ,(vconcat
356                   (mapcar
357                    (lambda (r0)
358                      (nth (lsh r0 2) mel-ccl-64-to-256-table))
359                    mel-ccl-16-table)))
360       (write ,(if terminate-with-newline
361                   (if output-crlf "=\r\n" "=\n")
362                 "="))))
363     ))
364 )
365
366 (define-ccl-program mel-ccl-encode-b
367   (mel-ccl-encode-base64-generic))
368
369 ;; 19 * 4 = 76
370 (define-ccl-program mel-ccl-encode-base64-crlf-crlf
371   (mel-ccl-encode-base64-generic 19 t))
372
373 (define-ccl-program mel-ccl-encode-base64-crlf-lf
374   (mel-ccl-encode-base64-generic 19 nil))
375
376
377 ;;; @ coding system
378 ;;;
379
380 (make-ccl-coding-system
381  'mel-ccl-b-rev ?B "MIME B-encoding (reversed)"
382  'mel-ccl-encode-b 'mel-ccl-decode-b)
383
384 (make-ccl-coding-system
385  'mel-ccl-base64-crlf-rev
386  ?B "MIME Base64-encoding (reversed)"
387  'mel-ccl-encode-base64-crlf-crlf
388  'mel-ccl-decode-b)
389
390 (make-ccl-coding-system
391  'mel-ccl-base64-lf-rev
392  ?B "MIME Base64-encoding (LF encoding) (reversed)"
393  'mel-ccl-encode-base64-crlf-lf
394  'mel-ccl-decode-b)
395
396
397 ;;; @ B
398 ;;;
399
400 (check-broken-facility ccl-execute-eof-block-on-decoding-some)
401
402 (unless-broken ccl-execute-eof-block-on-decoding-some
403
404   (defun base64-ccl-encode-string (string &optional no-line-break)
405     "Encode STRING with base64 encoding."
406     (if no-line-break
407         (decode-coding-string string 'mel-ccl-b-rev)
408       (decode-coding-string string 'mel-ccl-base64-lf-rev)))
409   (defalias-maybe 'base64-encode-string 'base64-ccl-encode-string)
410
411   (defun base64-ccl-encode-region (start end &optional no-line-break)
412     "Encode region from START to END with base64 encoding."
413     (interactive "*r")
414     (if no-line-break
415         (decode-coding-region start end 'mel-ccl-b-rev)
416       (decode-coding-region start end 'mel-ccl-base64-lf-rev)))
417   (defalias-maybe 'base64-encode-region 'base64-ccl-encode-region)
418
419   (defun base64-ccl-insert-encoded-file (filename)
420     "Encode contents of file FILENAME to base64, and insert the result."
421     (interactive "*fInsert encoded file: ")
422     (let ((coding-system-for-read 'mel-ccl-base64-lf-rev)
423           format-alist)
424       (insert-file-contents filename)))
425
426   (mel-define-method-function (mime-encode-string string (nil "base64"))
427                               'base64-ccl-encode-string)
428   (mel-define-method-function (mime-encode-region start end (nil "base64"))
429                               'base64-ccl-encode-region)
430   (mel-define-method-function
431    (mime-insert-encoded-file filename (nil "base64"))
432    'base64-ccl-insert-encoded-file)
433
434   (mel-define-method-function (encoded-text-encode-string string (nil "B"))
435                               'base64-ccl-encode-string)
436   )
437
438 (defun base64-ccl-decode-string (string)
439   "Decode base64 encoded STRING"
440   (encode-coding-string string 'mel-ccl-b-rev))
441 (defalias-maybe 'base64-decode-string 'base64-ccl-decode-string)
442
443 (defun base64-ccl-decode-region (start end)
444   "Decode base64 encoded the region from START to END."
445   (interactive "*r")
446   (encode-coding-region start end 'mel-ccl-b-rev))
447 (defalias-maybe 'base64-decode-region 'base64-ccl-decode-region)
448
449 (defun base64-ccl-write-decoded-region (start end filename)
450   "Decode the region from START to END and write out to FILENAME."
451   (interactive "*r\nFWrite decoded region to file: ")
452   (let ((coding-system-for-write 'mel-ccl-b-rev)
453         jka-compr-compression-info-list jam-zcat-filename-list)
454     (write-region start end filename)))
455
456 (mel-define-method-function (mime-decode-string string (nil "base64"))
457                             'base64-ccl-decode-string)
458 (mel-define-method-function (mime-decode-region start end (nil "base64"))
459                             'base64-ccl-decode-region)
460 (mel-define-method-function
461  (mime-write-decoded-region start end filename (nil "base64"))
462  'base64-ccl-write-decoded-region)
463
464 (mel-define-method encoded-text-decode-string (string (nil "B"))
465   (if (string-match (eval-when-compile
466                       (concat "\\`" B-encoded-text-regexp "\\'"))
467                     string)
468       (base64-ccl-decode-string string)
469     (error "Invalid encoded-text %s" string)))
470
471
472 ;;; @ end
473 ;;;
474
475 (provide 'mel-b-ccl)
476
477 ;;; mel-b-ccl.el ends here.