90484b363069f57e483e8e72839ec5748eb77acc
[elisp/flim.git] / mel-b-ccl.el
1 ;;; mel-b-ccl.el --- Base64 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, 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., 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-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)
173                       (` ((write r0)
174                           (r0 = (, (lsh (logand v 15) 4)))
175                           (break))))
176                      (t 
177                       (` ((r0 |= (, (lsh v -4))) 
178                           (write r0) 
179                           (r0 = (, (lsh (logand v 15) 4))) 
180                           (break))))))
181                   mel-ccl-256-to-64-table))))
182            (loop
183             (read-branch
184              r1
185              (,@ (mapcar
186                   (lambda (v)
187                     (cond
188                      ((eq v nil) '(repeat))
189                      ((eq v t) '(end))
190                      ((= (lsh v -2) 0)
191                       (` ((write r0) 
192                           (r0 = (, (lsh (logand v 3) 6)))
193                           (break))))
194                      (t
195                       (` ((r0 |= (, (lsh v -2)))
196                           (write r0) 
197                           (r0 = (, (lsh (logand v 3) 6)))
198                           (break))))))
199                   mel-ccl-256-to-64-table))))
200            (loop
201             (read-branch
202              r1
203              (,@ (mapcar
204                   (lambda (v)
205                     (cond
206                      ((eq v nil) '(repeat))
207                      ((eq v t) '(end))
208                      (t (` ((r0 |= (, v)) (write r0) (break))))))
209                   mel-ccl-256-to-64-table))))
210            (repeat)))))
211     )
212
213 (eval-when-compile
214
215 ;; Generated CCL program works not properly on 20.2 because CCL_EOF_BLOCK
216 ;; is not executed.
217 (defun mel-ccl-encode-base64-generic
218   (&optional quantums-per-line output-crlf terminate-with-newline)
219   (` (2
220       ((r3 = 0)
221        (r2 = 0)
222        (read r1)
223        (loop
224         (branch
225          r1
226          (,@ (mapcar
227               (lambda (r1)
228                 (` ((write (, (nth (lsh r1 -2) mel-ccl-64-to-256-table)))
229                     (r0 = (, (logand r1 3))))))
230               mel-ccl-256-table)))
231         (r2 = 1)
232         (read-branch
233          r1
234          (,@ (mapcar
235               (lambda (r1)
236                 (` ((write r0 (, (vconcat
237                                   (mapcar
238                                    (lambda (r0)
239                                      (nth (logior (lsh r0 4)
240                                                   (lsh r1 -4))
241                                           mel-ccl-64-to-256-table))
242                                    mel-ccl-4-table))))
243                     (r0 = (, (logand r1 15))))))
244               mel-ccl-256-table)))
245         (r2 = 2)
246         (read-branch
247          r1
248          (,@ (mapcar
249               (lambda (r1)
250                 (` ((write r0 (, (vconcat
251                                   (mapcar
252                                    (lambda (r0)
253                                      (nth (logior (lsh r0 2)
254                                                   (lsh r1 -6))
255                                           mel-ccl-64-to-256-table))
256                                    mel-ccl-16-table)))))))
257               mel-ccl-256-table)))
258         (r1 &= 63)
259         (write r1 (, (vconcat
260                       (mapcar
261                        (lambda (r1)
262                          (nth r1 mel-ccl-64-to-256-table))
263                        mel-ccl-64-table))))
264         (r3 += 1)
265         (r2 = 0)
266         (read r1)
267         (,@ (when quantums-per-line
268               (` ((if (r3 == (, quantums-per-line))
269                       ((write (, (if output-crlf "\r\n" "\n")))
270                        (r3 = 0)))))))
271         (repeat)))
272       (branch
273        r2
274        (, (if terminate-with-newline
275               (` (if (r3 > 0) (write (, (if output-crlf "\r\n" "\n")))))
276             (` (r0 = 0))))
277        ((write r0 (, (vconcat
278                       (mapcar
279                        (lambda (r0)
280                          (nth (lsh r0 4) mel-ccl-64-to-256-table))
281                        mel-ccl-4-table))))
282         (write (, (if terminate-with-newline
283                       (if output-crlf "==\r\n" "==\n")
284                     "=="))))
285        ((write r0 (, (vconcat
286                       (mapcar
287                        (lambda (r0)
288                          (nth (lsh r0 2) mel-ccl-64-to-256-table))
289                        mel-ccl-16-table))))
290         (write (, (if terminate-with-newline
291                       (if output-crlf "=\r\n" "=\n")
292                     "=")))))
293       )))
294 )
295
296 (define-ccl-program mel-ccl-encode-b
297   (mel-ccl-encode-base64-generic))
298
299 ;; 19 * 4 = 76
300 (define-ccl-program mel-ccl-encode-base64-crlf-crlf
301   (mel-ccl-encode-base64-generic 19 t))
302
303 (define-ccl-program mel-ccl-encode-base64-crlf-lf
304   (mel-ccl-encode-base64-generic 19 nil))
305
306
307 ;;; @ coding system
308 ;;;
309
310 (make-ccl-coding-system
311  'mel-ccl-b-rev ?B "MIME B-encoding (reversed)"
312  'mel-ccl-encode-b 'mel-ccl-decode-b)
313
314 (make-ccl-coding-system
315  'mel-ccl-base64-crlf-rev
316  ?B "MIME Base64-encoding (reversed)"
317  'mel-ccl-encode-base64-crlf-crlf
318  'mel-ccl-decode-b)
319
320 (make-ccl-coding-system
321  'mel-ccl-base64-lf-rev
322  ?B "MIME Base64-encoding (LF encoding) (reversed)"
323  'mel-ccl-encode-base64-crlf-lf
324  'mel-ccl-decode-b)
325
326
327 ;;; @ B
328 ;;;
329
330 (check-broken-facility ccl-execute-eof-block-on-decoding-some)
331
332 (unless-broken ccl-execute-eof-block-on-decoding-some
333
334   (defun base64-ccl-encode-string (string &optional no-line-break)
335     "Encode STRING with base64 encoding."
336     (if no-line-break
337         (decode-coding-string string 'mel-ccl-b-rev)
338       (decode-coding-string string 'mel-ccl-base64-lf-rev)))
339   (defalias-maybe 'base64-encode-string 'base64-ccl-encode-string)
340
341   (defun base64-ccl-encode-region (start end &optional no-line-break)
342     "Encode region from START to END with base64 encoding."
343     (interactive "*r")
344     (if no-line-break
345         (decode-coding-region start end 'mel-ccl-b-rev)
346       (decode-coding-region start end 'mel-ccl-base64-lf-rev)))
347   (defalias-maybe 'base64-encode-region 'base64-ccl-encode-region)
348
349   (defun base64-ccl-insert-encoded-file (filename)
350     "Encode contents of file FILENAME to base64, and insert the result."
351     (interactive "*fInsert encoded file: ")
352     (insert-file-contents-as-coding-system 'mel-ccl-base64-lf-rev filename))
353
354   (mel-define-method-function (mime-encode-string string (nil "base64"))
355                               'base64-ccl-encode-string)
356   (mel-define-method-function (mime-encode-region start end (nil "base64"))
357                               'base64-ccl-encode-region)
358   (mel-define-method-function
359    (mime-insert-encoded-file filename (nil "base64"))
360    'base64-ccl-insert-encoded-file)
361
362   (mel-define-method-function (encoded-text-encode-string string (nil "B"))
363                               'base64-ccl-encode-string)
364   )
365
366 (defun base64-ccl-decode-string (string)
367   "Decode base64 encoded STRING"
368   (encode-coding-string string 'mel-ccl-b-rev))
369 (defalias-maybe 'base64-decode-string 'base64-ccl-decode-string)
370
371 (defun base64-ccl-decode-region (start end)
372   "Decode base64 encoded the region from START to END."
373   (interactive "*r")
374   (encode-coding-region start end 'mel-ccl-b-rev))
375 (defalias-maybe 'base64-decode-region 'base64-ccl-decode-region)
376
377 (defun base64-ccl-write-decoded-region (start end filename)
378   "Decode the region from START to END and write out to FILENAME."
379   (interactive "*r\nFWrite decoded region to file: ")
380   (write-region-as-coding-system 'mel-ccl-b-rev start end filename))
381
382 (mel-define-method-function (mime-decode-string string (nil "base64"))
383                             'base64-ccl-decode-string)
384 (mel-define-method-function (mime-decode-region start end (nil "base64"))
385                             'base64-ccl-decode-region)
386 (mel-define-method-function
387  (mime-write-decoded-region start end filename (nil "base64"))
388  'base64-ccl-write-decoded-region)
389
390 (mel-define-method encoded-text-decode-string (string (nil "B"))
391   (if (string-match (eval-when-compile
392                       (concat "\\`" B-encoded-text-regexp "\\'"))
393                     string)
394       (base64-ccl-decode-string string)
395     (error "Invalid encoded-text %s" string)))
396
397
398 ;;; @ end
399 ;;;
400
401 (provide 'mel-b-ccl)
402
403 ;;; mel-b-ccl.el ends here.