summaryrefslogtreecommitdiff
path: root/uconv2.c
blob: 3fb43dfe5367db85149aa66dc3d9565d860cd464 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iconv.h>
#include <assert.h>
#include <errno.h>

static char*
convert (const char *str,
	 size_t        len,
	 iconv_t       converter,
	 size_t       *bytes_read,
	 size_t       *bytes_written)
{
    char *dest;
    char *outp;
    const char *p;
    size_t inbytes_remaining;
    size_t outbytes_remaining;
    size_t err;
    size_t outbuf_size;
    int have_error = 0;
    int done = 0;
    int reset = 0;

    p = str;
    inbytes_remaining = len;
    outbuf_size = len + 1; /* + 1 for nul in case len == 1 */

    outbytes_remaining = outbuf_size - 1; /* -1 for nul */
    outp = dest = malloc (outbuf_size);

    while (!done && !have_error) {
	if (reset)
	    err = iconv (converter, NULL, &inbytes_remaining, &outp,
			 &outbytes_remaining);
	else
	    err = iconv (converter, (char **)&p, &inbytes_remaining,
			 &outp, &outbytes_remaining);

	if (err == (size_t) -1) {
	    switch (errno)
	    {
	    case EINVAL:
		/* Incomplete text, do not report an error */
		done = 1;
		break;
	    case E2BIG:
	    {
		size_t used = outp - dest;
		printf ("used: %zd, %zd(%zd)\n",
			used, inbytes_remaining, len);

		outbuf_size *= 2;
		dest = realloc (dest, outbuf_size);

		outp = dest + used;
		outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
	    }
	    break;
	    case EILSEQ:
		have_error = 1;
		break;
	    default:
		have_error = 1;
		break;
	    }
	} else {
	    if (!reset) {
		/* call iconv with NULL inbuf to cleanup shift state */
		reset = 1;
		inbytes_remaining = 0;
	    } else {
		done = 1;
	    }
	}
    }

    *outp = '\0';
    if (bytes_read) {
	*bytes_read = p - str;
    } else {
	if ((p - str) != len) {
	    if (!have_error)
		have_error = 1;
	}
    }

    if (bytes_written)
	*bytes_written = outp - dest;	/* Doesn't include '\0' */

    if (have_error) {
	free (dest);
	return NULL;
    }

    return dest;
}

#define UCONV_BUFSZ 4096
int main(int argc, char **argv)
{
    FILE *infp, *outfp;
    const char *from, *to;
    iconv_t conv;
    char *inbuf;
    char *outbuf;
    size_t inlen;
    size_t outlen;

    if (argc < 5)
	return -1;

    from = argv[1];
    to = argv[2];

    conv = iconv_open(to, from);
    if (conv == (iconv_t)-1) {
	fprintf (stderr,
		 "Converting from %s to %s is unsupported.\n",
		 from, to);
	return -1;
    }

    infp = fopen(argv[3], "rb");
    if (!infp)
	return -1;

    fseek(infp, 0, SEEK_END);
    inlen = ftell(infp);
    fseek(infp, 0, SEEK_SET);
    inbuf = malloc(sizeof(char) * (inlen + 1));
    if (!inbuf)
	return -1;

    fread(inbuf, 1, inlen, infp);
    outbuf = convert(inbuf, inlen, conv, NULL, &outlen);
    if (!outbuf) {
	fprintf(stderr, "Failed to convert.\n");
    }

    outfp = fopen(argv[4], "wb");
    if (!outfp)
	return -1;

    if (outbuf)
	fwrite(outbuf, 1, outlen, outfp);

    free(inbuf);
    free(outbuf);

    fclose(outfp);
    fclose(infp);

    iconv_close(conv);

    return 0;
}