This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjconvert.pas
More file actions
1329 lines (1190 loc) · 33.9 KB
/
jconvert.pas
File metadata and controls
1329 lines (1190 loc) · 33.9 KB
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{**************************************************************************
*
* Unit Name: jconvert
* Purpose : 文字コード変換ライブラリとおまけ
* Author : EarthWave Soft(IKEDA Takahiro)
* E-Mail: ikeda@os.rim.or.jp
* WWW: http://www.os.rim.or.jp/~ikeda/
* Copyright(C) 1998 EarthWave Soft(IKEDA Takahiro)
* History : Ver 1.0 98/08/25 初版
* Ver 1.1 98/09/17 Result 初期化忘れ対応 他
* (Thanks kazukun@mars.dti.ne.jp)
* Ver 1.2 98/10/11 半角「ー」の全角変換ミスの修正
* Hankana2Zenkana? 上記対応とアルゴリズム変更
* 1行のみのデータ時の改行コード判断判定ミス修正
* Ver 1.3 98/11/23 EncodeBase64R, DecodeBase64, DecodeHeaderString
* 新設。
* Ver 1.4 98/11/29 EncodeUU, DecodeUU, EncodeBinHex, DecodeBinHex
* を新設(uuencode,BinHex)。
*
* 注意: ここで言う「jis」 は ISO-2022-JP に基づいた仕様による
* JIS への変換では半角カタカナは全角へ強制的に変換する
*************************************************************************}
unit jconvert;
interface
uses
Windows, Sysutils;
const
ASCII = 0;
BINARY = 1;
JIS83_IN = 2;
JIS78_IN = 3;
EUC_IN = 4;
SJIS_IN = 5;
EUCorSJIS_IN = 6;
JIS_OUT = 2;
EUC_OUT = 4;
SJIS_OUT = 5;
CRLF_R = 1;
CR_R = 2;
LF_R = 3;
{バイナリファイルを厳密にチェックするための最低チェックサイズ}
STRICT_CHECK_LEN: Integer = 512; {任意に変更して下さい}
{漢字コード判定。戻り値は定数を参照}
function InCodeCheck( const s: string ): Integer;
{2 バイト文字の JIS -> SJIS変換}
function ToSjis( c1,c2: Byte ): string;
{2 バイト文字の SJIS -> JIS変換}
function ToJis( c1, c2: Byte ): string;
{euc半角カタカナを jis 全角カタカナへ(内部使用)}
{function Hankana2Zenkana( const s: string; var index: Integer ): string;}
{sjis半角カタカナを jis 全角カタカナへ(内部使用)}
{function Hankana2Zenkana2( const s: string; var index: Integer ): string;}
{jis -> euc コンバート}
function jis2euc( const s: string ): string;
{euc -> 新jis コンバート}
function euc2jis83( const s: string ): string;
{jis -> sjis コンバート}
function jis2sjis( const s: string ): string;
{euc -> sjis コンバート}
function euc2sjis( const s: string ): string;
{sjis -> 新jis コンバート}
function sjis2jis83( const s: string ): string;
{sjis -> euc コンバート}
function sjis2euc( const s: string ): string;
{改行コードチェック}
function ReturnCodeCheck( const s: string ): Integer;
{全自動コード変換}
function ConvertJCode( s: string; outcode: Integer ): string;
{厳密なコード変換。既に元コードが判明している場合等に使用}
{意味あるのかこれ?}
function StrictConvertJCode( s: string; incode, outcode: Integer ): string;
{改行コード変換}
function ConvertReturnCode( s: string; rcode: Integer ): string;
{厳密な改行コード変換。既に元コードが判明している場合等に使用}
{意味あるのかこれ?}
function StrictConvertReturnCode( s: string; rcode_in, rcode: Integer ): string;
{おまけ}
{Base64 形式にエンコードする}
function EncodeBase64( const input: string ): string;
function EncodeBase64R( const input: string; Rcode: string ): string;
{uuencode 形式にエンコードする}
function EncodeUU( const input: string; Rcode: string ): string;
{BinHex 4.0 形式にエンコードする}
function EncodeBinHex( const input: string; Rcode: string ): string;
{Base64 形式をデコードする}
function DecodeBase64( const input: string ): string;
{uuencode 形式をデコードする。uudecode}
function DecodeUU( const input: string ): string;
{BinHex 4.0 形式をデコードする}
function DecodeBinHex( const input: string ): string;
{E-Mail のヘッダなどに使う文字列(ISO-2022-JP を Base64化したもの)を生成}
function CreateHeaderString( const s: string): string;
{E-Mail のヘッダなどに使う文字列(ISO-2022-JP を Base64化したもの)をデコード}
function DecodeHeaderString( const s: string): string;
implementation
const
CR = $0D;
LF = $0A;
ESC = $1B;
SS2 = $8E;
KI_G0 = #$1B + '$B';
KO_G0 = #$1B + '(J';
Code64: PChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
CodeUU: PChar = '`!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';
CodeBinHex: PChar = '!"#$%&''()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr';
DecBinHex: array[0..81] of BYTE = (
$00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$FF,$FF,$0D,
$0E,$0F,$10,$11,$12,$13,$FF,$14,$15,$FF,$FF,$FF,$FF,$FF,$FF,$16,
$17,$18,$19,$1A,$1B,$1C,$1D,$1E,$1F,$20,$21,$22,$23,$24,$FF,$25,
$26,$27,$28,$29,$2A,$2B,$FF,$2C,$2D,$2E,$2F,$FF,$FF,$FF,$FF,$30,
$31,$32,$33,$34,$35,$36,$FF,$37,$38,$39,$3A,$3B,$3C,$FF,$FF,$3D,
$3E,$3F );
{ JIS X0201 1 バイト仮名 から JIS X0208 右側へ}
HkanaToZkana_R: array[0..63] of Char = (
#$00,#$23,#$56,#$57,#$22,#$26,#$72,#$21,#$23,#$25,#$27,#$29,#$63,#$65,#$67,#$43,
#$3C,#$22,#$24,#$26,#$28,#$2A,#$2B,#$2D,#$2F,#$31,#$33,#$35,#$37,#$39,#$3B,#$3D,
#$3F,#$41,#$44,#$46,#$48,#$4A,#$4B,#$4C,#$4D,#$4E,#$4F,#$52,#$55,#$58,#$5B,#$5E,
#$5F,#$60,#$61,#$62,#$64,#$66,#$68,#$69,#$6A,#$6B,#$6C,#$6D,#$6F,#$73,#$2B,#$2C);
function InCodeCheck( const s: string ): Integer;
var
index,c,jmode: Integer;
begin
{バイナリチェック}
index := 1;
while (index <= STRICT_CHECK_LEN) and (index < Length(s)) do begin
c := Ord( s[index] );
if (c in [0..7]) or (c = $FF) then begin
Result := BINARY;
Exit;
end;
Inc(index);
end;
index := 1;
jmode := ASCII;
while ((jmode = ASCII) or (jmode = EUCorSJIS_IN)) and (index < Length(s)) do begin
{最後の文字は調べない(ループ内で調べるときがある)}
c := Ord( s[index] );
if c = ESC then begin
Inc(index);
c := Ord(s[index]);
if c = Ord('$') then begin
Inc(index);
c := Ord(s[index]);
if c = Ord( 'B' ) then
jmode := JIS83_IN {JIS X0208-1983}
else if c = Ord( '@' ) then
jmode := JIS78_IN; {JIS X0208-1978 Old JIS}
end;
end
else if (c in [0..7]) or (c = $FF) then begin
jmode := BINARY;
end
else if c > $7f then begin
if (c in [$81..$8D]) or (c in [$8F..$9F]) then
jmode := SJIS_IN
else if c = SS2 then begin {SS2 は EUC で JIS X0201 仮名(1Byte)}
Inc(index); {への移行を示す}
c := Ord( s[index] );
if (c in [$40..$7E]) or (c in [$80..$A0]) or (c in [$E0..$FC]) then
jmode := SJIS_IN
else if (c in [$A1..$DF]) then {EUC JIS X0201 仮名 の可能性}
jmode := EUCorSJIS_IN;
end
else if c in [$A1..$DF] then begin {SJIS では半角かな領域}
Inc(index);
c := Ord( s[index] );
if c in [$F0..$FE] then
jmode := EUC_IN
else if c in [$A1..$DF] then
jmode := EUCorSJIS_IN
else if c in [$E0..$EF] then begin
jmode := EUCorSJIS_IN;
while (c >= $40) and (index <= Length( s )) and (jmode = EUCorSJIS_IN) do begin
if c >= $81 then begin
if (c <= $8D) or ( c in [$8F..$9C]) then {EUC は A1..FF のはず}
jmode := SJIS_IN
else if c in [$FD..$FE] then {SJIS では避けている領域}
jmode := EUC_IN;
end;
Inc(index);
c := ord( s[index] );
end;
end
else if c <= $9F then
jmode := SJIS_IN;
end
else if c in [$F0..$FE] then
jmode := EUC_IN
else if c in [$E0..$EF] then begin
Inc(index);
c := Ord( s[index] );
if (c in [$40..$7E]) or (c in [$80..$A0]) then
jmode := SJIS_IN
else if c in [$FD..$FE] then
jmode := EUC_IN
else if c in [$A1..$FC] then
jmode := EUCorSJIS_IN;
end;
end;
Inc(index);
end;
Result := jmode;
end;
function ToSjis( c1,c2: Byte ): string; register;
var
c1off,c2off: Integer;
begin
if c1 < $5F then
c1off := $70
else
c1off := $B0;
if (c1 mod 2) <> 0 then begin
if c2 > $5F then
c2off := $20
else
c2off := $1F;
end
else
c2off := $7E;
Inc(c1);
c1 := c1 shr 1;
c1 := c1 + c1off;
c2 := c2 + c2off;
Result := Char(c1) + Char(c2);
end;
function ToJis( c1, c2: Byte ): string; register;
var
c1off,c2off: Integer;
begin
if c1 < 160 then
c1off := 112
else
c1off := 176;
c1 := c1 - c1off;
c1 := c1 shl 1;
if c2 < 159 then begin
if c2 > 127 then begin
c2off := 32;
end
else
c2off := 31;
Dec(c1);
end
else
c2off := 126;
c2 := c2 - c2off;
Result := Char(c1) + Char(c2);
end;
function Hankana2Zenkana( const s: string; var index: Integer ): string;
var
i,c: Integer;
c2: Char;
begin
Inc(index);
c := Ord( s[index] );
c := c and $7F;
c2 := HkanaToZkana_R[c - $20];
i := index+1;
Result := '';
case c of
$21..$25,$30,$5E..$5F: begin {記号系}
Result := #$21 + c2;
end;
$33:begin {「ヴ」対応}
if (i <= Length(s)) and ( s[i] = Char(SS2) ) then begin
if s[i+1] = #$DE then begin
index := i+1;
c2 := #$74;
end;
end;
end;
$36..$44:begin {濁点が次につく可能性のある文字}
if (i <= Length(s)) and ( s[i] = Char(SS2) ) then begin
if s[i+1] = #$DE then begin
index := i+1;
Inc(c2);
end;
end;
end;
$4A..$4E:begin {はひふへほ}
if (i <= Length(s)) and ( s[i] = Char(SS2) ) then begin
if s[i+1] = #$DE then begin
index := i+1;
Inc(c2);
end
else if s[i+1] = #$DF then begin {半濁点}
index := i+1;
Inc(c2); Inc(c2);
end;
end;
end;
end;
Inc(index);
if Result = '' then Result := #$25 + c2;
end;
function Hankana2Zenkana2( const s: string; var index: Integer ): string;
var
i,c: Integer;
c2: Char;
begin
c := Ord( s[index] );
c := c and $7F;
c2 := HkanaToZkana_R[c - $20];
i := index+1;
Result := '';
case c of
$21..$25,$30,$5E..$5F:begin {記号系}
Result := #$21 + c2;
end;
$33:begin {「ヴ」対応}
if (i <= Length(s)) and ( s[i] = #$DE ) then begin
index := i;
c2 := #$74;
end;
end;
$36..$44:begin {濁点が次につく可能性のある文字}
if (i <= Length(s)) and ( s[i] = #$DE ) then begin
index := i;
Inc(c2);
end;
end;
$4A..$4E:begin {はひふへほ}
if (i <= Length(s)) and ( (s[i] = #$DE) or (s[i] = #$DF) ) then begin
if s[i] = #$DE then begin
index := i;
Inc(c2);
end
else if s[i] = #$DF then begin {半濁点}
index := i;
Inc(c2); Inc(c2);
end;
end;
end;
end;
Inc(index);
if Result = '' then Result := #$25 + c2;
end;
{ JIS 1 Byte 仮名未対応}
function jis2euc( const s: string ): string;
var
index,c: Integer;
ki: Boolean;
begin
index := 1;
ki := False;
Result := '';
while index <= Length( s ) do begin
c := Ord(s[index]);
if c = ESC then begin
Inc(index);
c := Ord(s[index]);
if (c = $24) then
ki := True
else if (c = $28) then
ki := False;
Inc(index);
Inc(index);
c := Ord(s[index]);
end;
if ki then begin
if c in [$21..$7E] then
Result := Result + Char( c or $80 )
else
Result := Result+Char(c and $ff);
end
else begin
Result := Result+Char(c and $ff);
end;
Inc(index);
end;
end;
function euc2jis83( const s: string): string;
var
ki: Boolean;
index,c: Integer;
c1,c2: Char;
begin
ki := False;
index := 1;
Result := '';
while index <= Length(s) do begin
c := Ord( s[index] );
if (c = CR) or (c = LF) then begin
if ki then begin
Result := Result + KO_G0;
ki := False;
end;
Result := Result + Char(c and $ff);
Inc(index);
Continue;
end;
if c > $7F then begin
if not ki then begin
Result := Result + KI_G0;
ki := True;
end;
if c = SS2 then begin {半角カタカナ}
Result := Result + Hankana2Zenkana( s, index );
end
else begin
c1 := Char(c and $7F);
Inc(index);
c := Ord(s[index] );
c2 := Char(c and $7F);
Result := Result + c1 + c2;
Inc(index);
end;
end
else begin
if ki then begin
Result := Result + KO_G0;
ki := False;
end;
Result := Result + s[index];
Inc(index);
end;
end;
end;
function jis2sjis( const s: string ): string;
var
index,c: Integer;
ki: Boolean;
c1,c2: Byte;
begin
index := 1;
ki := False;
Result := '';
while index <= Length( s ) do begin
c := Ord(s[index]);
if c = ESC then begin
Inc(index);
c := Ord(s[index]);
if (c = $24) then
ki := True
else if (c = $28) then
ki := False;
Inc(index);
Inc(index);
c := Ord(s[index])
end;
if ki then begin
c1 := c;
Inc(index);
c2 := Ord(s[index]);
Result := Result + ToSjis(c1,c2);
end
else begin
Result := Result+Char(c and $ff);
end;
Inc(index);
end;
end;
function euc2sjis( const s: string ): string;
var
index,c: Integer;
c1,c2: Byte;
begin
index := 1;
Result := '';
while index <= Length(s) do begin
c := Ord(s[index]);
if (c > $80) and ( c < $FF ) then begin
if c = SS2 then begin
Inc(index);
c := Ord(s[index]);
Result := Result + Char(c and $FF);
end
else begin
c1 := Ord(s[index]);
c1 := c1 and $7F;
Inc(index);
c2 := Ord(s[index]);
c2 := c2 and $7F;
Result := Result + ToSjis(c1,c2);
end;
end
else begin
Result := Result+Char(c and $ff);
end;
Inc(index);
end;
end;
function sjis2jis83( const s: string ): string;
var
ki: Boolean;
index,c: Integer;
c1,c2: Byte;
begin
ki := False;
index := 1;
Result := '';
while index <= Length(s) do begin
c := Ord( s[index] );
if (c = CR) or (c = LF) then begin
if ki then begin
Result := Result + KO_G0;
ki := False;
end;
Result := Result + Char(c and $ff);
Inc(index);
Continue;
end;
if c > $7F then begin
if not ki then begin
Result := Result + KI_G0;
ki := True;
end;
if c in [$A1..$DF] then begin {半角カタカナ}
Result := Result + Hankana2Zenkana2( s,index)
end
else begin
c1 := c and $FF;
Inc(index);
c2 := Ord(s[index] );
Result := Result + ToJis( c1, c2 );
Inc(index);
end;
end
else begin
if ki then begin
Result := Result + KO_G0;
ki := False;
end;
Result := Result + s[index];
Inc(index);
end;
end;
end;
function sjis2euc( const s: string ): string;
var
index,c: Integer;
c1,c2: Byte;
zen: string;
begin
index := 1;
Result := '';
while index <= Length(s) do begin
c := Ord( s[index] );
if c > $7F then begin
if c in [$A1..$DF] then begin {半角カタカナ}
Result := Result + Char(SS2) + Char(c and $FF);
end
else begin
c1 := c;
Inc(index);
c2 := Ord(s[index]);
zen := ToJis( c1, c2 );
c1 := Byte(zen[1]) or $80;
c2 := Byte(zen[2]) or $80;
Result := Result + Char(c1) + Char(c2);
end;
end
else begin
Result := Result + s[index];
end;
Inc(index);
end;
end;
function ConvertJCode( s: string; outcode: Integer ): string;
var
incode: Integer;
begin
incode := InCodeCheck( s );
if (incode <= BINARY ) or ( incode = outcode ) or (incode = EUCorSJIS_IN) then begin
Result := s;
Exit;
end;
Result := '';
case outcode of
JIS_OUT:begin
case incode of
JIS83_IN..JIS78_IN: Result := s;
EUC_IN: Result := euc2jis83( s );
SJIS_IN: Result := sjis2jis83( s );
end;
end;
EUC_OUT:begin
case incode of
JIS83_IN..JIS78_IN: Result := jis2euc( s );
SJIS_IN: Result := sjis2euc( s );
end;
end;
SJIS_OUT:begin
case incode of
JIS83_IN..JIS78_IN: Result := jis2sjis( s );
EUC_IN: Result := euc2sjis( s );
end;
end;
else
Result := s;
end;
end;
function StrictConvertJCode( s: string; incode,outcode: Integer ): string;
begin
if (incode <= BINARY ) or ( incode = outcode ) or (incode = EUCorSJIS_IN) then begin
Result := s;
Exit;
end;
Result := '';
case outcode of
JIS_OUT:begin
case incode of
JIS83_IN..JIS78_IN: Result := s;
EUC_IN: Result := euc2jis83( s );
SJIS_IN: Result := sjis2jis83( s );
end;
end;
EUC_OUT:begin
case incode of
JIS83_IN..JIS78_IN: Result := jis2euc( s );
SJIS_IN: Result := sjis2euc( s );
end;
end;
SJIS_OUT:begin
case incode of
JIS83_IN..JIS78_IN: Result := jis2sjis( s );
EUC_IN: Result := euc2sjis( s );
end;
end;
else
Result := s;
end;
end;
function ReturnCodeCheck( const s: string ): Integer;
var
index: Integer;
c: char;
begin
index := 1;
c := #0;
Result := 0;
while (c <> #13) and (c <> #10) and (index <= Length(s)) do
begin
c := s[index];
Inc(index);
end;
if c = #10 then
Result := LF_R
else if c = #13 then
begin
if Length(s) = index-1 then
Result := CR_R
else if s[index] = #10 then
Result := CRLF_R
else
Result := CR_R;
end;
end;
function ConvertReturnCode( s: string; rcode: Integer ): string;
var
index, rcode_in: Integer;
RCodeStr, RCodeStr_in: string;
begin
rcode_in := ReturnCodeCheck( s );
if (rcode_in = 0) or (rcode_in = rcode) then begin {改行無しテキスト or}
Result := s;
Exit;
end
else begin
case rcode_in of
CRLF_R: RCodeStr_in := #13#10;
CR_R: RCodeStr_in := #13;
LF_R: RCodeStr_in := #10;
end;
case rcode of
CRLF_R: RCodeStr := #13#10;
CR_R: RCodeStr := #13;
LF_R: RCodeStr := #10;
end;
end;
Result := '';
index := 1;
while index <= Length(s) do
begin
if s[index] = RCodeStr_in[1] then
begin
Delete(s, index, Length(RCodeStr_in));
Insert(RCodeStr, s, index);
index := index + Length(RCodeStr);
end
else
Inc(index);
end;
Result := s;
end;
function StrictConvertReturnCode( s: string; rcode_in, rcode: Integer ): string;
var
index: Integer;
RCodeStr, RCodeStr_in: string;
begin
if (rcode_in = 0) or (rcode_in = rcode) then begin {改行無しテキスト or}
Result := s;
Exit;
end
else begin
case rcode_in of
CRLF_R: RCodeStr_in := #13#10;
CR_R: RCodeStr_in := #13;
LF_R: RCodeStr_in := #10;
end;
case rcode of
CRLF_R: RCodeStr := #13#10;
CR_R: RCodeStr := #13;
LF_R: RCodeStr := #10;
end;
end;
Result := '';
index := 1;
while index <= Length(s) do
begin
if s[index] = RCodeStr_in[1] then
begin
Delete(s, index, Length(RCodeStr_in));
Insert(RCodeStr, s, index);
index := index + Length(RCodeStr);
end
else
Inc(index);
end;
Result := s;
end;
{ここからはおまけ}
{Base64 エンコード。77文字以上の改行規則に未対応 :98/08/21}
{98/11/25: テーブル形式の変更に併せて修正。string -> PChar により Code64 }
{ が Zero origin に。}
function EncodeBase64( const input: string ): string;
var
i,j,iLen: Integer;
a,b,c: BYTE;
begin
Result := '';
//エンコード後の大きさを計算
iLen := Length(input);
i := iLen mod 3;
if i <> 0 then i := 4;
SetLength( Result, ( iLen div 3 ) * 4 + i);
i:=1; j:=1;
while i <= iLen -2 do begin
a := BYTE(input[i]); b:= BYTE(input[i+1]); c := BYTE(input[i+2]);
Result[j] := Code64[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := Code64[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := Code64[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
Result[j] := Code64[ (c and $3F) ]; Inc(j);
i := i + 3;
end;
if (iLen mod 3) = 1 then begin
a := BYTE(input[iLen]); b:=0;
Result[j] := Code64[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := Code64[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := '='; Inc(j);
Result[j] := '=';
end
else if (iLen mod 3) = 2 then begin
a := BYTE(input[iLen -1]); b := BYTE(input[iLen]); c := 0;
Result[j] := Code64[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := Code64[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := Code64[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
Result[j] := '=';
end;
end;
{Base64 エンコード。77文字以上の改行規則に対応 :98/11/23}
{Rcode には任意の改行コードをセット。ex #$0D#0A}
{98/11/25: テーブル形式の変更に併せて修正。string -> PChar により Code64 }
{ が Zero origin に。}
function EncodeBase64R( const input: string; Rcode: string ): string;
var
i,j,k,l,iLen: Integer;
a,b,c: BYTE;
begin
Result := '';
//エンコード後の大きさを計算
iLen := Length(input);
i := iLen mod 3;
if i <> 0 then i := 4;
i := i + ((( iLen div 3 ) * 4) div 76) * Length(Rcode);
SetLength( Result, ( iLen div 3 ) * 4 + i);
i:=1; j:=1; k:=0;
while i <= iLen -2 do begin
a := BYTE(input[i]); b:= BYTE(input[i+1]); c := BYTE(input[i+2]);
Result[j] := Code64[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := Code64[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := Code64[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
Result[j] := Code64[ (c and $3F) ]; Inc(j);
i := i + 3;
k := k + 4;
if k = 76 then begin
for l:=1 to Length(Rcode) do begin
Result[j] := Rcode[l]; Inc(j);
end;
k := 0;
end;
end;
if (iLen mod 3) = 1 then begin
a := BYTE(input[iLen]); b:=0;
Result[j] := Code64[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := Code64[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := '='; Inc(j);
Result[j] := '=';
end
else if (iLen mod 3) = 2 then begin
a := BYTE(input[iLen -1]); b := BYTE(input[iLen]); c := 0;
Result[j] := Code64[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := Code64[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := Code64[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
Result[j] := '=';
end;
end;
{uuencode: 98/11/25}
{Rcode には任意の改行コードをセット。ex #$0D#0A}
{先頭の begin 644 hogehoge.xxx と末尾の end は呼び出し側が処理後にどうにかする}
{ちなみに 644 は UNIX で言うところのファイルパーミッション }
function EncodeUU( const input: string; Rcode: string ): string;
var
i,j,k,l,m,iLen: Integer;
a,b,c: BYTE;
begin
Result := '';
//エンコード後の大きさを計算
iLen := (Length(input) div 3) * 4;
m := iLen div 60;
i := Length(input) mod 3;
if i <> 0 then iLen := iLen + 4;
i := m * ( Length(RCode) + 1) + Length(Rcode) * 2 +1 +1;
SetLength( Result, iLen + i);
iLen := Length(input);
i:=1; j:=1; k:=0;
while i <= iLen -2 do begin
a := BYTE(input[i]); b:= BYTE(input[i+1]); c := BYTE(input[i+2]);
if (k = 0) and (m <> 0) then begin
Result[j] := 'M'; Inc(j);
end
else if k=0 then begin
Result[j] := Char(iLen - i +1 + $20); Inc(j);
end;
Result[j] := CodeUU[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := CodeUU[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := CodeUU[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
Result[j] := CodeUU[ (c and $3F) ]; Inc(j);
i := i + 3;
k := k + 4;
if (k = 60) and (m <> 0) then begin
for l:=1 to Length(Rcode) do begin
Result[j] := Rcode[l]; Inc(j);
end;
Dec(m);
k := 0;
end;
end;
if (iLen mod 3) = 1 then begin
a := BYTE(input[iLen]); b:=0;
Result[j] := CodeUU[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := CodeUU[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := CodeUU[0]; Inc(j);
Result[j] := CodeUU[0]; Inc(j);
end
else if (iLen mod 3) = 2 then begin
a := BYTE(input[iLen -1]); b := BYTE(input[iLen]); c := 0;
Result[j] := CodeUU[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := CodeUU[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := CodeUU[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
Result[j] := CodeUU[0]; Inc(j);
end;
k := 1;
while k <= Length(Rcode) * 2 +1 do begin
for l:=1 to Length(Rcode) do begin
Result[j] := Rcode[l]; Inc(j);
end;
k := k + Length(Rcode);
if k = Length(RCode) +1 then
Result[j] := '`'; Inc(j); Inc(k);
end;
end;
{BinHex 4.0(Hqx7?) エンコード :98/11/27}
{Rcode には任意の改行コードをセット。ex #$0D#0A}
{(This file must be converted with BinHex 4.0) という先頭の文字列は}
{呼び出し側がどうにかする}
function EncodeBinHex( const input: string; Rcode: string ): string;
var
i,j,k,l,iLen: Integer;
a,b,c: BYTE;
begin
Result := '';
//エンコード後の大きさを計算
iLen := (Length(input) div 3) * 4;
i := iLen mod 3;
if i <> 0 then Inc(i);
iLen := iLen + i +2; // +2 始終端記号
iLen := iLen + (iLen div 64) * Length(Rcode); // 始終端記号と改行コード分
SetLength( Result, iLen );
iLen := Length(input);
i:=1; j:=2; k:=1;
Result[1] := ':';
while i <= iLen -2 do begin
a := BYTE(input[i]); b:= BYTE(input[i+1]); c := BYTE(input[i+2]);
Result[j] := CodeBinHex[ ((a and $FC) shr 2) ]; Inc(j);
Result[j] := CodeBinHex[ ( ((a and $03) shl 4) or ((b and $F0) shr 4) ) ]; Inc(j);
Result[j] := CodeBinHex[ ( ((b and $0F) shl 2) or ((c and $C0) shr 6) ) ]; Inc(j);
k := k + 3;
if k = 64 then begin