10 (defconst mel-ccl-4-table
13 (defconst mel-ccl-16-table
14 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))
16 (defconst mel-ccl-64-table
17 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
19 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
20 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63))
22 (defconst mel-ccl-256-table
23 '( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
24 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
25 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
26 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
27 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
28 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
29 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
30 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
31 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
32 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
33 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
34 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
35 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
36 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
37 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
38 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255))
40 (defconst mel-ccl-256-to-16-table
41 '(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
42 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
43 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
44 0 1 2 3 4 5 6 7 8 9 nil nil nil nil nil nil
45 nil 10 11 12 13 14 15 nil nil nil nil nil nil nil nil nil
46 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
47 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
48 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
49 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
50 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
51 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
52 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
53 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
54 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
55 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
56 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil))
58 (defconst mel-ccl-16-to-256-table
59 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
61 (defconst mel-ccl-high-table
64 (lambda (v) (nth (lsh v -4) mel-ccl-16-to-256-table))
67 (defconst mel-ccl-low-table
70 (lambda (v) (nth (logand v 15) mel-ccl-16-to-256-table))
73 (defconst mel-ccl-u-raw
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
77 "abcdefghijklmnopqrstuvwxyz"
78 "!@#$%&'()*+,-./:;<>@[\\]^`{|}~"
81 (defconst mel-ccl-c-raw
84 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
85 "abcdefghijklmnopqrstuvwxyz"
86 "!@#$%&'*+,-./:;<>@[]^`{|}~"
89 (defconst mel-ccl-p-raw
92 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
93 "abcdefghijklmnopqrstuvwxyz"
97 (defconst mel-ccl-256-to-64-table
98 '(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
99 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
100 nil nil nil nil nil nil nil nil nil nil nil 62 nil nil nil 63
101 52 53 54 55 56 57 58 59 60 61 nil nil nil t nil nil
102 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
103 15 16 17 18 19 20 21 22 23 24 25 nil nil nil nil nil
104 nil 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
105 41 42 43 44 45 46 47 48 49 50 51 nil nil nil nil nil
106 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
107 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
108 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
109 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
110 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
111 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
112 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
113 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil))
115 (defconst mel-ccl-64-to-256-table
116 '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P
117 ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z ?a ?b ?c ?d ?e ?f
118 ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v
119 ?w ?x ?y ?z ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?+ ?/))
121 (defconst mel-ccl-qp-table
122 [enc enc enc enc enc enc enc enc enc wsp enc enc enc cr enc enc
123 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc
124 wsp raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw
125 raw raw raw raw raw raw raw raw raw raw raw raw raw enc 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 raw
129 raw raw raw raw raw raw raw raw raw raw raw raw raw raw raw 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 enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc enc])
145 (define-ccl-program mel-ccl-decode-q
164 mel-ccl-256-to-16-table)))
171 `((write r0 ,(vconcat
174 (logior (lsh r0 4) v))
178 mel-ccl-256-to-16-table)))
181 `(write-repeat ,r0))))
182 mel-ccl-256-table))))))
184 (define-ccl-program mel-ccl-encode-uq
193 ((= r0 32) `(write-repeat ?_))
194 ((member r0 mel-ccl-u-raw) `(write-repeat ,r0))
198 (write r0 ,mel-ccl-high-table)
199 (write r0 ,mel-ccl-low-table)
202 (define-ccl-program mel-ccl-encode-cq
211 ((= r0 32) `(write-repeat ?_))
212 ((member r0 mel-ccl-c-raw) `(write-repeat ,r0))
216 (write r0 ,mel-ccl-high-table)
217 (write r0 ,mel-ccl-low-table)
220 (define-ccl-program mel-ccl-encode-pq
229 ((= r0 32) `(write-repeat ?_))
230 ((member r0 mel-ccl-p-raw) `(write-repeat ,r0))
234 (write r0 ,mel-ccl-high-table)
235 (write r0 ,mel-ccl-low-table)
239 (defun mel-ccl-decode-b-bit-ex (v)
241 (lsh (logand v (lsh 255 16)) -16)
242 (logand v (lsh 255 8))
243 (lsh (logand v 255) 16)))
245 (defconst mel-ccl-decode-b-0-table
250 (mel-ccl-decode-b-bit-ex (lsh v 18))
252 mel-ccl-256-to-64-table)))
254 (defconst mel-ccl-decode-b-1-table
259 (mel-ccl-decode-b-bit-ex (lsh v 12))
261 mel-ccl-256-to-64-table)))
263 (defconst mel-ccl-decode-b-2-table
268 (mel-ccl-decode-b-bit-ex (lsh v 6))
270 mel-ccl-256-to-64-table)))
272 (defconst mel-ccl-decode-b-3-table
277 (mel-ccl-decode-b-bit-ex v)
279 mel-ccl-256-to-64-table)))
283 (define-ccl-program mel-ccl-decode-b
287 (r4 = r0 ,mel-ccl-decode-b-0-table)
288 (r5 = r1 ,mel-ccl-decode-b-1-table)
290 (r5 = r2 ,mel-ccl-decode-b-2-table)
292 (r5 = r3 ,mel-ccl-decode-b-3-table)
294 (if (r4 & ,(lognot (1- (lsh 1 24))))
296 (if (r4 & ,(lsh 1 24))
297 ((r0 = r1) (r1 = r2) (r2 = r3) (read r3)
298 (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
299 (r5 = r3 ,mel-ccl-decode-b-3-table)
304 (if (r4 & ,(lsh 1 25))
305 ((r1 = r2) (r2 = r3) (read r3)
306 (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
307 (r5 = r3 ,mel-ccl-decode-b-3-table)
313 (if (r4 & ,(lsh 1 26))
315 (r4 >>= 1) (r4 &= ,(logior (lsh 7 24)))
316 (r5 = r3 ,mel-ccl-decode-b-3-table)
325 (if (r4 & ,(lsh 1 27))
327 (r4 = r3 ,mel-ccl-decode-b-3-table)
332 (r4 = r0 ,mel-ccl-decode-b-0-table)
333 (r5 = r1 ,mel-ccl-decode-b-1-table)
338 ((r5 = r2 ,mel-ccl-decode-b-2-table)
340 (r5 = r3 ,mel-ccl-decode-b-3-table)
351 ((r5 = r2 ,mel-ccl-decode-b-2-table)
364 (write-repeat r4))))))
366 ;; mel-ccl-encode-b works only 20.3 or later because CCL_EOF_BLOCK
367 ;; is not executed on 20.2 (or former?).
368 (define-ccl-program mel-ccl-encode-b
376 `((write ,(nth (lsh r1 -2) mel-ccl-64-to-256-table))
377 (r0 = ,(logand r1 3))))
384 `((write r0 ,(vconcat
387 (nth (logior (lsh r0 4)
389 mel-ccl-64-to-256-table))
391 (r0 = ,(logand r1 15))))
398 `((write r0 ,(vconcat
401 (nth (logior (lsh r0 2)
403 mel-ccl-64-to-256-table))
404 mel-ccl-16-table)))))
410 (nth r1 mel-ccl-64-to-256-table))
419 (nth (lsh r0 4) mel-ccl-64-to-256-table))
425 (nth (lsh r0 2) mel-ccl-64-to-256-table))
430 ;; mel-ccl-encode-base64 does not works on 20.2 by same reason of mel-ccl-encode-b
431 (define-ccl-program mel-ccl-encode-base64
440 `((write ,(nth (lsh r1 -2) mel-ccl-64-to-256-table))
441 (r0 = ,(logand r1 3))))
448 `((write r0 ,(vconcat
451 (nth (logior (lsh r0 4)
453 mel-ccl-64-to-256-table))
455 (r0 = ,(logand r1 15))))
462 `((write r0 ,(vconcat
465 (nth (logior (lsh r0 2)
467 mel-ccl-64-to-256-table))
468 mel-ccl-16-table)))))
474 (nth r1 mel-ccl-64-to-256-table))
477 (if (r3 == 19) ; 4 * 19 = 76 --> line break.
483 (if (r0 > 0) (write "\r\n"))
487 (nth (lsh r0 4) mel-ccl-64-to-256-table))
493 (nth (lsh r0 2) mel-ccl-64-to-256-table))
498 ;; mel-ccl-encode-quoted-printable does not works on 20.2 by same reason of mel-ccl-encode-b
499 (define-ccl-program mel-ccl-encode-quoted-printable
502 (r5 = 0) ; previous character is white space
512 (let ((tmp (aref mel-ccl-qp-table r0)))
514 ((eq tmp 'raw) '((r3 = 0) (break))) ; RAW
515 ((eq tmp 'enc) '((r3 = 1) (break))) ; ENC
516 ((eq tmp 'wsp) '((r3 = 2) (break))) ; WSP
517 ((eq tmp 'cr) '((r3 = 3) (break))) ; CR
527 (write-read-repeat r0))
534 (write r0 ,mel-ccl-high-table)
536 (write-read-repeat r0 ,mel-ccl-low-table))
540 (write r0 ,mel-ccl-high-table)
542 (write-read-repeat r0 ,mel-ccl-low-table))
549 (write-read-repeat r0))
553 (write-read-repeat r0))))
555 ((if ((r6 > 73) & r5)
560 ;; r0:r3={RAW,ENC,CR}
570 ;; r5=WSP ; CR:r3=CR r0=LF
577 ;; r5=noWSP ; CR:r3=CR r0=LF
607 (write "=\r\n=0D=0D")
609 ;; CR:r3=CR r0=noLFnorCR
621 ;; r0:r3={RAW,ENC} r1
623 ;; r0:r3={RAW,ENC} r1=CR
626 ;; r0:r3={RAW,ENC} CR r1
628 ;; r0:r3={RAW,ENC} CR r1=LF
633 ;; r0:r3=RAW CR r1=LF
639 ;; r0:r3=ENC CR r1=LF
641 (write r0 ,mel-ccl-high-table)
642 (write r0 ,mel-ccl-low-table)
647 ;; r0:r3={RAW,ENC} CR r1=noLF
650 ;; r0:r3=RAW CR r1:noLF
658 ;; r0:r3=ENC CR r1:noLF
662 (write r0 ,mel-ccl-high-table)
663 (write r0 ,mel-ccl-low-table)
668 ;; r0:r3={RAW,ENC} r1:noCR
682 (write r0 ,mel-ccl-high-table)
683 (write r0 ,mel-ccl-low-table)
687 (;(write "[EOF:") (write r4 ,mel-ccl-high-table) (write r4 ,mel-ccl-low-table) (write "]")
696 ;; 3: SOFTBREAK r0:r3=ENC ;
699 ((write "=\r\n") (end))
700 ;; 5: SOFTBREAK r0:r3=WSP ;
701 ((write "=\r\n") (end))
704 ((write "=0D") (end))
705 ((write "=\r\n=0D") (end)))
706 ;; 7: r5=WSP SOFTBREAK CR:r3=CR r0=LF ;
708 ;; 8: r5=noWSP CR:r3=CR r0=LF ;
710 ;; 9: (r6=73) ; CR:r3=CR r0=CR
711 ((write "=\r\n=0D=0D") (end))
712 ;; 10: (r6=73) CR:r3=CR CR LF ;
714 ;; 11: ; r0:r3={RAW,ENC}
719 (write r0 ,mel-ccl-high-table)
720 (write r0 ,mel-ccl-low-table)
722 ;; 12: ; r0:r3={RAW,ENC} r1=CR
730 (write r0 ,mel-ccl-high-table)
731 (write r0 ,mel-ccl-low-table)
734 ;; 13: r0:r3=RAW CR LF ;
736 ;; 14: r0:r3=ENC CR LF ;
741 (define-ccl-program mel-ccl-decode-quoted-printable
749 (let ((tmp (aref mel-ccl-qp-table r0)))
751 ((or (eq tmp 'raw) (eq tmp 'wsp)) `(write-read-repeat r0))
757 (if ((r0 == ? ) | r1)
759 ;; Skip transport-padding.
760 ;; It should check CR LF after
761 ;; transport-padding.
768 ;; '=' [\t ]* r0:[^\t ]
775 ;; '=' [\t ]* r0='\r'
777 ;; '=' [\t ]* '\r' r0
779 ;; '=' [\t ]* '\r' r0='\n'
780 ;; soft line break found.
783 ;; '=' [\t ]* '\r' r0:[^\n]
785 ;; output "=\r" and rescan from r0.
788 ((setq tmp (nth r0 mel-ccl-256-to-16-table))
789 ;; '=' [\t ]* r0:[0-9A-F]
792 ;; '=' [\t ]* r0:[^\r0-9A-F]
794 ;; output "=" and rescan from r0.
798 ;; '=' [\t ]* r0:[0-9A-F]
803 (if (setq tmp (nth r1 mel-ccl-256-to-16-table))
804 ;; '=' [\t ]* [0-9A-F] r1:[0-9A-F]
810 (logior (lsh r0 4) tmp))
812 ;; '=' [\t ]* [0-9A-F] r1:[^0-9A-F]
817 ;; '=' [\t ]* [0-9A-F] r1:[^0-9A-F]
820 (write r0 ,(vconcat mel-ccl-16-to-256-table))
830 ;; hard line break found.
832 (write-read-repeat r0))
834 ;; invalid control character (bare CR) found.
835 ;; -> ignore it and rescan from r0.
839 ;; invalid character found.
843 mel-ccl-256-table))))))
849 (make-ccl-coding-system
850 'mel-ccl-uq ?Q "MIME Q-encoding in unstructured field"
851 'mel-ccl-decode-q 'mel-ccl-encode-uq)
853 (make-ccl-coding-system
854 'mel-ccl-cq ?Q "MIME Q-encoding in comment"
855 'mel-ccl-decode-q 'mel-ccl-encode-cq)
857 (make-ccl-coding-system
858 'mel-ccl-pq ?Q "MIME Q-encoding in phrase"
859 'mel-ccl-decode-q 'mel-ccl-encode-pq)
861 (make-ccl-coding-system
862 'mel-ccl-b ?B "MIME B-encoding"
863 'mel-ccl-decode-b 'mel-ccl-encode-b)
865 (make-ccl-coding-system
866 'mel-ccl-quoted-printable ?Q "MIME Quoted-Printable-encoding"
867 'mel-ccl-decode-quoted-printable 'mel-ccl-encode-quoted-printable)
869 (make-ccl-coding-system
870 'mel-ccl-base64 ?B "MIME Base64-encoding"
871 'mel-ccl-decode-b 'mel-ccl-encode-base64)
873 (make-ccl-coding-system
874 'mel-ccl-b-rev ?B "MIME B-encoding (reversed)"
875 'mel-ccl-encode-b 'mel-ccl-decode-b)
877 (make-ccl-coding-system
878 'mel-ccl-quoted-printable-rev ?Q "MIME Quoted-Printable-encoding (reversed)"
879 'mel-ccl-encode-quoted-printable 'mel-ccl-decode-quoted-printable)
881 (make-ccl-coding-system
882 'mel-ccl-base64-rev ?B "MIME Base64-encoding (reversed)"
883 'mel-ccl-encode-base64 'mel-ccl-decode-b)
889 (unless (and (boundp 'ccl-encoder-eof-block-is-broken)
890 ccl-encoder-eof-block-is-broken)
892 (defun base64-ccl-encode-string (string)
893 "Encode STRING with base64 encoding."
894 (encode-coding-string string 'mel-ccl-b))
896 (defun base64-ccl-encode-region (start end)
897 "Encode region from START to END with base64 encoding."
899 (encode-coding-region start end 'mel-ccl-b))
901 (defun base64-ccl-insert-encoded-file (filename)
902 "Encode contents of file FILENAME to base64, and insert the result."
903 (interactive (list (read-file-name "Insert encoded file: ")))
904 (let ((coding-system-for-read 'mel-ccl-b))
905 (insert-file-contents filename)))
909 (defun base64-ccl-decode-string (string)
910 "Decode base64 encoded STRING"
911 (string-as-unibyte (decode-coding-string string 'mel-ccl-b)))
913 (defun base64-ccl-decode-region (start end)
914 "Decode base64 encoded the region from START to END."
916 (decode-coding-region start end 'mel-ccl-b))
918 (defun base64-ccl-write-decoded-region (start end filename)
919 "Decode the region from START to END and write out to FILENAME."
921 (list (region-beginning) (region-end)
922 (read-file-name "Write decoded region to file: ")))
923 (let ((coding-system-for-write 'mel-ccl-b-rev))
924 (write-region start end filename)))
927 ;;; @ quoted-printable
930 (unless (and (boundp 'ccl-encoder-eof-block-is-broken)
931 ccl-encoder-eof-block-is-broken)
933 (defun quoted-printable-ccl-encode-string (string)
934 "Encode STRING with quoted-printable encoding."
935 (decode-coding-string
936 (encode-coding-string
937 (encode-coding-string
940 'mel-ccl-quoted-printable)
943 (defun quoted-printable-ccl-encode-region (start end)
944 "Encode the region from START to END with quoted-printable
947 (setq end (+ start (encode-coding-region start end 'raw-text-dos)))
948 (setq end (+ start (encode-coding-region start end 'mel-ccl-quoted-printable)))
949 (decode-coding-region start end 'raw-text-dos))
951 (defun quoted-printable-ccl-insert-encoded-file (filename)
952 "Encode contents of the file named as FILENAME, and insert it."
953 (interactive (list (read-file-name "Insert encoded file: ")))
954 (let ((start (point)) end
955 (coding-system-for-read 'mel-ccl-quoted-printable-rev))
956 (setq end (+ start (cadr (insert-file-contents filename))))
957 (decode-coding-region start end 'raw-text-dos)))
961 (defun quoted-printable-ccl-decode-string (string)
962 "Decode quoted-printable encoded STRING."
963 (decode-coding-string
964 (decode-coding-string
965 (encode-coding-string
968 'mel-ccl-quoted-printable)
971 (defun quoted-printable-ccl-decode-region (start end)
972 "Decode the region from START to END with quoted-printable
975 (setq end (+ start (encode-coding-region start end 'raw-text-dos)))
976 (setq end (+ start (decode-coding-region start end 'mel-ccl-quoted-printable)))
977 (decode-coding-region start end 'raw-text-dos))
979 (defun quoted-printable-ccl-write-decoded-region
981 "Decode quoted-printable encoded current region and write out to FILENAME."
983 (list (region-beginning) (region-end)
984 (read-file-name "Write decoded region to file: ")))
985 (let ((buf (current-buffer)) tmp)
987 (setq tmp (current-buffer))
990 (copy-to-buffer tmp start end))
991 (encode-coding-region (point-min) (point-max) 'raw-text-dos)
992 (decode-coding-region (point-min) (point-max) 'mel-ccl-quoted-printable)
993 (decode-coding-region (point-min) (point-max) 'raw-text-dos)
994 (write-region-as-binary (point-min) (point-max) filename))))
1000 (defun q-encoding-ccl-encode-string (string &optional mode)
1001 "Encode STRING to Q-encoding of encoded-word, and return the result.
1002 MODE allows `text', `comment', `phrase' or nil. Default value is
1004 (encode-coding-string
1007 ((eq mode 'text) 'mel-ccl-uq)
1008 ((eq mode 'comment) 'mel-ccl-cq)
1011 (defun q-encoding-ccl-decode-string (string)
1012 "Decode Q encoded STRING and return the result."
1014 (decode-coding-string