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