2 Unix SMB/CIFS implementation.
5 Copyright (C) Jelmer Vernooij 2003
6 Based on iconv/icon_prog.c from the GNU C Library,
7 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 process_block (smb_iconv_t cd, const char *addr, size_t len, FILE *output)
28 #define OUTBUF_SIZE 32768
29 const char *start = addr;
30 char outbuf[OUTBUF_SIZE];
39 n = smb_iconv (cd, &addr, &len, &outptr, &outlen);
43 /* We have something to write out. */
44 int errno_save = errno;
46 if (fwrite (outbuf, 1, outptr - outbuf, output)
47 < (size_t) (outptr - outbuf)
50 /* Error occurred while printing the result. */
51 DEBUG (0, ("conversion stopped due to problem in writing the output"));
60 /* iconv() ran into a problem. */
64 DEBUG(0,("illegal input sequence at position %ld",
65 (long) (addr - start)));
69 incomplete character or shift sequence at end of buffer"));
72 DEBUG(0, ("internal error (illegal descriptor)"));
75 DEBUG(0, ("unknown iconv() error %d", errno));
88 process_fd (iconv_t cd, int fd, FILE *output)
90 /* we have a problem with reading from a descriptor since we must not
91 provide the iconv() function an incomplete character or shift
92 sequence at the end of the buffer. Since we have to deal with
93 arbitrary encodings we must read the whole text in a buffer and
94 process it in one step. */
95 static char *inbuf = NULL;
96 static size_t maxlen = 0;
100 while (actlen < maxlen)
102 ssize_t n = read (fd, inptr, maxlen - actlen);
105 /* No more text to read. */
110 /* Error while reading. */
111 DEBUG(0, ("error while reading the input"));
119 if (actlen == maxlen)
125 /* Increase the buffer. */
126 new_inbuf = (char *) realloc (inbuf, maxlen + 32768);
127 if (new_inbuf == NULL)
129 DEBUG(0, ("unable to allocate buffer for input"));
134 inptr = inbuf + actlen;
138 n = read (fd, inptr, maxlen - actlen);
141 /* No more text to read. */
146 /* Error while reading. */
147 DEBUG(0, ("error while reading the input"));
154 while (actlen < maxlen);
157 /* Break again so we leave both loops. */
161 /* Now we have all the input in the buffer. Process it in one run. */
162 return process_block (cd, inbuf, actlen, output);
167 int main(int argc, char *argv[])
169 const char *file = NULL;
173 const char *preload_modules[] = {NULL, NULL};
178 /* make sure the vars that get altered (4th field) are in
179 a fixed location or certain compilers complain */
181 struct poptOption long_options[] = {
183 { "from-code", 'f', POPT_ARG_STRING, &from, 0, "Encoding of original text" },
184 { "to-code", 't', POPT_ARG_STRING, &to, 0, "Encoding for output" },
185 { "output", 'o', POPT_ARG_STRING, &output, 0, "Write output to this file" },
186 { "preload-modules", 'p', POPT_ARG_STRING, &preload_modules[0], 0, "Modules to load" },
193 pc = poptGetContext("smbiconv", argc, (const char **) argv,
196 poptSetOtherOptionHelp(pc, "[FILE] ...");
198 while(poptGetNextOpt(pc) != -1);
200 /* the following functions are part of the Samba debugging
201 facilities. See lib/debug.c */
202 setup_logging("smbiconv", True);
204 if (preload_modules[0]) smb_load_modules(preload_modules);
207 out = fopen(output, "w");
210 DEBUG(0, ("Can't open output file '%s': %s, exiting...\n", output, strerror(errno)));
215 cd = smb_iconv_open(to, from);
217 DEBUG(0,("unable to find from or to encoding, exiting...\n"));
221 while((file = poptGetArg(pc))) {
222 if(strcmp(file, "-") == 0) fd = 0;
224 fd = open(file, O_RDONLY);
227 DEBUG(0, ("Can't open input file '%s': %s, ignoring...\n", file, strerror(errno)));
232 /* Loop thru all arguments */
233 process_fd(cd, fd, out);