-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrux.c
executable file
·883 lines (782 loc) · 20.4 KB
/
rux.c
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
/*-
* Copyright ¿ 2005-2014 Vyacheslav Anikin. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
//#define DEBUG
#include "version.h"
static char *version = VERSION;
#include <sys/types.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
/* #include <libgen.h> *//* Uncomment if you want to use basename() */
#ifdef USE_ICONV
#include <iconv.h>
#endif
#ifdef MDEBUG
#include "dmalloc.h"
#else
void *my_alloc(size_t size);
#define dmalloc my_alloc
#define dfree free
#endif /* MDEBUG */
#include "cptaba.h"
#include "cpdetect.h"
struct cpinfo *detect_codepage(struct cp_detect *detinf, FILE *in, FILE *out, int fdoconv, float *cpexp);
void doconv_all(u_char *conv_tab, FILE *in, FILE *out);
struct cpinfo *get_cp_info(const char *cpid);
u_char *init_tab(u_char *tab, int len, struct cpinfo *in, struct cpinfo *out);
void print_usage(void);
void print_cplist(struct cpinfo *cplist);
void add_buf(u_char *buf, int size);
void free_bufs();
int recode_buf(u_char *conv_tab, u_char *buf, int len, FILE *out, char *rch);
int pwarnf(const char *fmt,...);
/* Default flags' values */
static int f_graph = 0; /* Recode pseudo-graphics too */
static int f_quiet = 1; /* Quiet mode, shut stderr */
static int f_doconv = 1; /* Do convert by default */
static int f_detect = 1; /* Auto detect by default */
static int f_lax = 0; /* Don't touch lax-files */
static int f_compl = 1; /* Touch all including supported characters */
static struct cpinfo *i_cp = NULL;
static struct cpinfo *o_cp = NULL;
/* Emanate on minimal file block */
static int fbuf_size = 512;
struct databuf {
u_char *data;
int size;
struct databuf *next;
};
static struct databuf *head = NULL;
static struct databuf *last = NULL;
/*
* OK! Are you want to recode or detect code page of some file or files?
* No problem. I'll do it for you!
*/
int main(int argc, char **argv)
{
u_char conv_tab[_CPINFO_TSIZE];
int readstdin = 0; /* Don't read from stdin by default */
FILE *in = stdin;
FILE *out = stdout;
extern char *optarg;
extern int optind;
u_char *iobuf = NULL; /* File input buffer */
int flag; /* An option */
#ifndef DEFAULT_OUTPUT_CP
# ifdef USE_ICONV
# define DEFAULT_OUTPUT_CP 5
# else
# define DEFAULT_OUTPUT_CP 2
# endif
#endif
struct cpinfo *def_ocp = &codepage[DEFAULT_OUTPUT_CP];
/* Info about detection of a code page */
struct cp_detect detinf;
o_cp = def_ocp; /* Initialize o_cp using default cp */
while ((flag = getopt(argc, argv, "i:o:thegTs:qvlc")) != -1) {
switch (flag) {
case 'e': /* Universal recoding pseudo-graphics */
f_graph = 0;
break;
case 'g': /* Normal recoding pseudo-graphics */
f_graph = 1;
break;
case 'i': /* An input code page */
i_cp = get_cp_info(optarg);
f_detect = 0;
break;
case 'o': /* An output code page */
o_cp = get_cp_info(optarg);
break;
case 't': /* Detect and recode */
f_doconv = 1;
f_detect = 1;
break;
case 'T': /* Detect only */
f_doconv = 0;
f_detect = 1;
break;
case 's': /* Block size in bytes */
/* Using -s option is deprecated! */
break;
case 'q': /* Suspress all warnings */
f_quiet = 1;
break;
case 'v': /* Verbose meaning */
f_quiet = 0;
break;
case 'l':
f_lax = 1;
break;
case 'c':
f_compl = 0;
break;
case 'h':
default:
print_usage();
return 0;
}
}
argc -= optind;
argv += optind;
/* Brings to default output code page if it doesn't specified. */
if (o_cp == NULL) {
pwarnf("Brings to default output code page (%s).\n",
def_ocp->cp_name);
o_cp = def_ocp;
}
if (f_detect == 0) {
if (i_cp == NULL) {
pwarnf("Could not continue without input code page. "
"Specify -i or -t option.\n");
return -1;
}
} else {
if ((iobuf = (u_char *) dmalloc(fbuf_size)) == NULL) {
perror(NULL);
return errno;
}
detinf.cp_list = (struct cpinfo *)&codepage;
detinf.cp_default = o_cp;
detinf.cp_databuf = (u_char *) iobuf;
}
if (argc == 0) {
readstdin = 1; /* read from stdin if not enough any
arguments */
}
/* This cycle is controlled by return and break ops. Cycle breaks
after first loop if we reading from stdin (flag readstdin is on) */
while (1) {
const char *fname = *argv++;
#ifdef DEBUG
printf("\nDEBUG: %s\n", readstdin ? "<stdin>" : /* basename */ (fname));
#endif
if (readstdin) {
in = stdin;
} else if ((in = fopen(fname, "r")) == NULL) {
perror(fname);
if (*argv == NULL) {
break;
}
continue;
}
if (f_detect != 0) {
float cpexp = .0;
i_cp = detect_codepage(&detinf, in, out, f_doconv, &cpexp);
if (f_doconv == 0) {
#ifdef DEBUG
printf("%s: %s (%.8f)\n", readstdin ? "<stdin>" :
/* basename */ (fname),
cpexp == .0 ? "us-ascii (no hits)" :
i_cp->cp_name, i_cp->cp_exp);
#else
printf("%s: %s\n", readstdin ? "<stdin>" :
/* basename */ (fname),
cpexp == .0 ? "us-ascii (no hits)" :
i_cp->cp_name);
#endif
}
}
if (f_doconv != 0) {
init_tab(conv_tab, sizeof(conv_tab), i_cp, o_cp);
doconv_all(conv_tab, in, out);
}
fclose(in);
if (readstdin) {
break;
}
if (*argv == NULL) {
break;
}
}
if (f_detect != 0) {
dfree(iobuf);
}
return 0;
}
/* This routine is used for determine a code page info from an identifier */
struct cpinfo *
get_cp_info(const char *cpid)
{
int i = 0;
while (1) {
if (codepage[i].cp_name == NULL) {
break;
}
if (strcmp(codepage[i].cp_name, cpid) == 0) {
return &codepage[i];
}
++i;
}
pwarnf("Wrong code page identifier: %s\n", cpid);
print_cplist(codepage);
return NULL;
}
/* Table completion routine. Used for filling up the alternative table
within additional data */
void compl_tab(u_char * tab, struct cpinfo *incp, struct cpinfo *outcp)
{
int i;
int t_gr = f_graph;
/* The pseudo-graphics will be lost (or segmentation fault occure) if
we'll recode using -g option from the code page with
pseudo-graphics chars to without. */
if ((f_graph) && (incp->cp_graphsize != 0) && (outcp->cp_graphsize == 0)) {
pwarnf("Output code page has no pseudo-graphics. Assuming a "
"-g option off.\n");
f_graph = 0;
}
if ((i = incp->cp_graphsize) != 0) {
for (--i; i >= 0; i--) {
tab[incp->cp_gdata[i]] = f_graph ? outcp->cp_gdata[i] :
boxdr_map[i];
}
}
/* Always restore an old f_graph value. We'll don't loss any time to
additional check. */
f_graph = t_gr;
}
/* The alternative table initialization routine. Used for speed up recoding */
u_char *
init_tab(u_char * tab, int len, struct cpinfo *in, struct cpinfo *out)
{
int i = len - 1;
for (; i >= 0; i--) {
tab[i] = i;
}
for (i = out->cp_size; i >= 0; i--) {
tab[in->cp_data[i]] = out->cp_data[i];
}
if (f_compl) {
compl_tab(tab, in, out);
}
return tab;
}
void print_usage(void)
{
/* DATETIME was defined in version.h */
printf (" rux %s -- " DATETIME "\n", version);
/* Characters specific help message */
printf (" Usage: rux [-egT] [-i incp] [-o outcp] [file ...]\n\n"
" -e -- Replace box-drawing characters by non-graphic. Default.\n"
" -g -- Inverse of -e option. (Overrides any previous -e option).\n"
" -c -- Work with alphabet characters only. Skips non-russian characters\n"
);
/* Codepage specific help message*/
printf (/*" -t -- Attempt to detect a code page of the input files. Default.\n"*/
" -T -- Just show the code pages of the input files.\n\n"
" -i -- Specify an input code page. Overrides by -[tT]\n"
" -o -- Specify an output code page. Default is `%s'.\n\n",
codepage[DEFAULT_OUTPUT_CP].cp_name);
/* How to make it quiet and How to get help */
printf (/*" -q -- Quiet mode (suspress warnings)\n"*/
" -v -- Be verbose. Show all warnings.\n\n"
" -h -- Show this help message.\n\n");
print_cplist(codepage);
printf("\nSample: cat book.html | rux | less");
printf("\n rux book.html | less");
printf("\n rux -T *");
printf("\n--\nVyacheslav Anikin <[email protected]>\n");
}
void print_cplist(struct cpinfo *cplist)
{
struct cpinfo *curcp = cplist;
printf("The valid identifiers of the code pages are:\n");
for (; curcp->cp_name; curcp++) {
/*printf(" %s\t(%s)\n", curcp->cp_name, curcp->cp_desc);*/
printf(" %s", curcp->cp_name);
}
printf("\n");
}
struct cpinfo *
detect_codepage(struct cp_detect *detinf, FILE * in, FILE * out, int fdoconv, float *cpexp)
{
int plank = 20;
float cp_exp = .0;
struct cpinfo *cur = codepage;
for (; cur->cp_name; cur++) {
cur->cp_exp = .0;
}
while (cp_exp < plank) {
detinf->cp_datalen = fread(detinf->cp_databuf, 1, fbuf_size, in);
i_cp = detect_cp(detinf, &cp_exp);
if (cp_exp < 0.78 && f_lax != 0) {
#ifdef DEBUG
printf("> debug: used an -l option. Assuming cp_exp = .0\n");
#endif
cp_exp = .0;
}
#ifdef DEBUG
printf("> debug: cpexp = %12.8f\n", i_cp->cp_exp);
printf("> debug: codepage = %s\n", i_cp->cp_name);
printf("> debug: plank = %d\n", plank);
#endif
if (fdoconv != 0) {
if (cp_exp == .0) {
#ifdef DEBUG
printf("> debug: output buffer: %d, cause cpexp = %11.8f\n",
detinf->cp_datalen, i_cp->cp_exp);
#else
fwrite(detinf->cp_databuf, detinf->cp_datalen, 1, out);
#endif
} else {
add_buf(detinf->cp_databuf, detinf->cp_datalen);
}
}
if (feof(in))
break;
if (cp_exp != .0)
plank -= 3;
}
*cpexp = i_cp->cp_exp;
return i_cp;
}
/* Routine for converting of all buffers (if they exists)
* and continuous converting of remaining part of file.
*/
void doconv_all(u_char * conv_tab, FILE * in, FILE * out)
{
int c; /* Input/output buffer */
//printf("\ni_cp = %s, o_cp = %s\n", i_cp->cp_name, o_cp->cp_name);
#ifdef USE_ICONV
char inbuf[4096 + 4096];
size_t inbufrest = 0;
char outbuf[4096];
int status = 0;
char output_cp_flags[64];
char input_cp_flags[64];
snprintf(output_cp_flags, sizeof(output_cp_flags), "%s//TRANSLIT", o_cp->cp_name);
iconv_t enc = iconv_open(output_cp_flags, i_cp->cp_name);
#ifndef ICONV_SET_TRANSLITERATE
#define ICONV_SET_TRANSLITERATE 2
#endif
#ifdef HAVE_LIBICONV
const int iconv_enable_translit = 1;
iconvctl(enc, ICONV_SET_TRANSLITERATE, &iconv_enable_translit);
#endif
iconv(enc, NULL, NULL, NULL, NULL);
if (head != NULL) {
struct databuf *cur = head;
int rest = 0;
while (cur) {
char rch;
#ifdef DEBUG
printf("\n!!buf!!\n");
printf("\ncur->data=%x", *cur->data);
printf("\nrest=%d, cur->data+rest=%x\n\n", rest, *(cur->data + rest));
#endif
rest = recode_buf(conv_tab, cur->data + rest,
cur->size - rest, out, &rch);
if (rest > 0) {
u_char char_buf;
if (cur->next) {
char_buf = *cur->next->data;
} else {
int r = fread(&char_buf, 1, 1, in);
if (r <= 0) {
/**/
}
}
#ifdef DEBUG
printf("\n\nrch=%x, char_buf=%x\n\n", rch, char_buf);
#endif
unsigned short wch = (char_buf << 8) | rch;
#ifdef DEBUG
printf("\n\nwch=%x\n\n", (unsigned short)wch);
#endif
const char *inptr = (char *)&wch;
size_t insize = sizeof(wch);
char *outptr = outbuf;
size_t outsize = sizeof(outbuf);
#ifdef DEBUG
printf("\ninsize=%d, outsize=%d\n", insize, outsize);
#endif
size_t res = iconv(enc, (const char **)&inptr, &insize, &outptr, &outsize);
#ifdef DEBUG
printf("\ninsize=%d, outsize=%d\n", insize, outsize);
#endif
#ifdef DEBUG
printf("> debug: output buffer: %d\n",
outptr - outbuf);
#else
fwrite(outbuf, 1, outptr - outbuf, out);
#endif
#ifdef DEBUG
printf("\noutbuf=%x, %d\n\n", *(unsigned short *)(outbuf), outptr - outbuf);
#endif
}
cur = cur->next;
}
free_bufs();
}
#if 0
int r = 0;
char inbuf[4096 + 4096];
int rest = 0;
while (r = fread(inbuf + 4096, 1, 4096, in)) {
if (r > 0) {
//printf("\nrest = %d\n", rest);
rest = recode_buf(NULL, inbuf + 4096, r, out);
//printf("\nrest = %d\n", rest);
}
}
#endif
for (;;) {
#ifdef DEBUG
printf("Loop for...\n");
#endif
size_t inbufsize = fread(inbuf + 4096, 1, 4096, in);
#ifdef DEBUG
printf("inbufsize=%d\n", inbufsize);
#endif
if (inbufsize == 0) {
if (inbufrest == 0)
break;
else {
return;
}
} else {
const char *inptr = inbuf + 4096 - inbufrest;
size_t insize = inbufrest + inbufsize;
inbufrest = 0;
while (insize > 0) {
#ifdef DEBUG
printf("insize=%d\n", insize);
#endif
char *outptr = outbuf;
size_t outsize = sizeof(outbuf);
size_t res = iconv(enc, (const char **)&inptr, &insize, &outptr, &outsize);
#ifdef DEBUG
printf("%d\n", res);
#endif
if (outptr != outbuf) {
#ifdef DEBUG
printf("> debug: output buffer: %d\n", outptr - outbuf);
#else
if (fwrite (outbuf, 1, outptr - outbuf, stdout) < outptr - outbuf) {
return;
}
#endif
}
if (res == (size_t)(-1)) {
#ifdef DEBUG
printf("%d == -1\n", res);
#endif
if (errno == EILSEQ) {
/* discard unconvertible */
fwrite(inptr, 1, insize, stdout);
} else if (errno == EINVAL) {
if (inbufsize == 0 || insize > 4096) {
return;
} else {
inbufrest = insize;
if (insize > 0) {
char *restptr = inbuf + 4096 - insize;
do { *restptr++ = *inptr++; } while (--insize > 0);
}
break;
}
} else if (errno != E2BIG) {
return;
} else {
goto done;
}
break;
}
}
}
}
{
}
done:
#ifdef DEBUG
printf("before iconv_close...\n");
#endif
iconv_close(enc);
#else /* USE_ICONV */
/* XXX */
if (strcmp(i_cp->cp_name, UTF8) == 0) {
/*char hook[] = {'£', 'â'};*/
char hook[4];
hook[0] = o_cp->cp_data[6];
hook[1] = o_cp->cp_data[34];
hook[2] = o_cp->cp_data[18];
hook[3] = o_cp->cp_data[39];
while ((c = getc(in)) != EOF) {
if (c == (u_char) 0xd0 || c == (u_char) 0xd1) {
conv_tab[0x91] = c == 0xd1 ? hook[0] : hook[1];
conv_tab[0x81] = c == 0xd1 ? hook[2] : hook[3];
} else {
putc(conv_tab[(u_char) c], out);
}
}
} else {
while ((c = getc(in)) != EOF) {
putc(conv_tab[(u_char) c], out);
}
}
#endif
}
/* Program standard error/warning format output */
int pwarnf(const char *fmt,...)
{
int vf_ret;
va_list args;
if (f_quiet) {
return 0;
}
va_start(args, fmt);
fprintf(stderr, "rux: ");
vf_ret = vfprintf(stderr, fmt, args);
va_end(args);
/* We need to flush stderr output before fatal exit :-) */
fflush(stderr);
return vf_ret;
}
/*
* Functions for supporting recoding buffers. Also there's
* implemented the simplest one-way list routines (adding one node
* and clearing of all).
*/
int recode_buf(u_char * conv_tab, u_char * buf, int len, FILE * out, char *rch)
{
#ifdef USE_ICONV
int j=0;
//printf("\n\n");
for (; j < 2; j++) {
//printf("0x%02x ", buf[j]);
}
//printf(" .. ");
for (j = len - 1; j > len - 3; j--) {
//printf("0x%02x ", buf[j]);
}
//printf("\n\n");
char output_cp_flags[64];
snprintf(output_cp_flags, sizeof(output_cp_flags), "%s//TRANSLIT", o_cp->cp_name);
iconv_t enc = iconv_open(output_cp_flags, i_cp->cp_name);
char inbuf[4096 + 4096];
memcpy(inbuf + 4096, buf, len);
size_t inbufrest = 0;
char outbuf[4096];
int status = 0;
iconv(enc, NULL, NULL, NULL, NULL);
size_t inbufsize = len;
if (inbufsize == 0) {
return 0;
} else {
const char *inptr = inbuf + 4096 - inbufrest;
size_t insize = inbufrest + inbufsize;
inbufrest = 0;
while (insize > 0) {
char *outptr = outbuf;
size_t outsize = sizeof(outbuf);
size_t res = iconv(enc, (const char **)&inptr, &insize, &outptr, &outsize);
#ifdef DEBUG
printf("\ninsize=%d\n", insize);
#endif
if (outptr != outbuf) {
#ifdef DEBUG
printf("\noutptr != outbuf\n");
#endif
#ifdef DEBUG
printf("> debug: output buffer: %d\n", outptr - outbuf);
#else
if (fwrite (outbuf, 1, outptr - outbuf, stdout) < outptr - outbuf) {
return 0;
}
#endif
}
if (res == (size_t)(-1)) {
#ifdef DEBUG
printf("\nres == (size_t)(-1)\n");
#endif
if (errno == EILSEQ) {
/* discard unconvertible */
fwrite(inptr, 1, insize, stdout);
#ifdef DEBUG
printf("\nerrno == EILSEQ\n");
#endif
return 0;
//int one = 1;
//iconvctl(enc,4,&one);
} else if (errno == EINVAL) {
#ifdef DEBUG
printf("\nerrno == EINVAL\n");
#endif
if (inbufsize == 0 || insize > 4096) {
#ifdef DEBUG
printf("\nreturn after insize>4096\n");
#endif
return 0;
} else {
#ifdef DEBUG
printf("\ninbufsize=%d, insize=%d\n", inbufsize, insize);
#endif
inbufrest = insize;
if (insize > 0) {
*rch = *inptr;
//char *restptr = inbuf - insize;
//do { *restptr++ = *inptr++; } while (--insize > 0);
return inbufrest;
return 0;
}
break;
}
} else if (errno != E2BIG) {
#ifdef DEBUG
printf("\nerrno != E2BIG\n");
#endif
return 0;
}
}
}
}
{
}
iconv_close(enc);
#if 0
iconv_t enc = iconv_open(o_cp->cp_name, i_cp->cp_name);
char outbuf[4096];
int inbufrest = 0;
iconv(enc, NULL, NULL, NULL, NULL);
size_t inbufsize = len;
const char *inptr = buf;
size_t insize = len + inbufrest;
while (insize > 0) {
printf("\nAAA\n");
char *outptr = outbuf;
size_t outsize = sizeof(outbuf);
size_t res = iconv(enc, (char **)&inptr, &insize, &outptr, &outsize);
if (outptr != outbuf) {
if (fwrite (outbuf, 1, outptr - outbuf, stdout) < outptr - outbuf) {
return;
}
}
if (res == (size_t)(-1)) {
if (errno == EILSEQ) {
/* discard unconvertible */
} else if (errno == EINVAL) {
if (inbufsize == 0 || insize > 4096) {
return;
} else {
inbufrest = insize;
if (insize > 0) {
char *restptr = buf + 4096 - insize;
do { *restptr++ = *inptr++; } while (--insize > 0);
}
break;
}
} else if (errno != E2BIG) {
return;
}
}
}
{
}
iconv_close(enc);
#endif
#if 0
//printf("%s, %s\n", i_cp->cp_name, o_cp->cp_name);
iconv_t enc = iconv_open(o_cp->cp_name, i_cp->cp_name);
char outbuf[2048];
char *outs = outbuf;
int insize = len;
int outsize, bufsize;
outsize = sizeof outbuf;
int k = iconv(enc, &buf, &insize, &outs, &outsize);
int lout = 2048 - outsize;
printf("%s", outbuf);
//fwrite(outbuf, 1, lout, out);
iconv_close(enc);
#endif
#else
if (strcmp(i_cp->cp_name, UTF8) == 0) {
char hook[4];
hook[0] = o_cp->cp_data[6];
hook[1] = o_cp->cp_data[34];
hook[2] = o_cp->cp_data[18];
hook[3] = o_cp->cp_data[39];
while (len--) {
if (*buf == (u_char) 0xd0 || *buf == (u_char) 0xd1) {
conv_tab[0x91] = *buf == 0xd1 ? hook[0] : hook[1];
conv_tab[0x81] = *buf == 0xd1 ? hook[2] : hook[3];
++buf;
} else {
putc(conv_tab[(u_char) *buf++], out);
}
}
} else
{
while (len--) {
putc(conv_tab[*buf++], out);
}
}
#endif
return 0;
}
void add_buf(u_char * buf, int size)
{
#ifdef DEBUG
printf("> debug: buffering...\n");
#endif
struct databuf *t = (struct databuf *)dmalloc(sizeof(struct databuf));
t->data = (u_char *) dmalloc(size);
t->size = size;
t->next = NULL;
memcpy(t->data, buf, size);
if (head == NULL) {
head = t;
} else {
last->next = t;
}
last = t;
}
void free_bufs()
{
struct databuf *cur = head;
while (cur) {
struct databuf *t = cur;
cur = cur->next;
dfree(t->data);
dfree(t);
t = NULL;
}
head = NULL;
last = NULL;
}
void *my_alloc(size_t size)
{
void *ret = (void *)malloc(size);
if (ret == NULL)
perror("rux");
return ret;
}