From d2cea7d2898bba9f2faa1ed46cfa2b63f7acf7bf Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 27 Apr 2003 20:47:15 +0000 Subject: Add 'smbiconv' program - a clone of the 'iconv' utility that uses samba's internal iconv() functions. Useful for testing purposes. (This used to be commit ccabb7961a15c06a315bdb9ff1a79834864f67e7) --- source3/torture/smbiconv.c | 243 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 source3/torture/smbiconv.c (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c new file mode 100644 index 0000000000..ce21a09025 --- /dev/null +++ b/source3/torture/smbiconv.c @@ -0,0 +1,243 @@ +/* + Unix SMB/CIFS implementation. + Charset module tester + + Copyright (C) Jelmer Vernooij 2003 + Based on iconv/icon_prog.c from the GNU C Library, + Contributed by Ulrich Drepper , 1998. + + 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 of the License, 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; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +static int +process_block (smb_iconv_t cd, char *addr, size_t len, FILE *output) +{ +#define OUTBUF_SIZE 32768 + const char *start = addr; + char outbuf[OUTBUF_SIZE]; + char *outptr; + size_t outlen; + size_t n; + + while (len > 0) + { + outptr = outbuf; + outlen = OUTBUF_SIZE; + n = smb_iconv (cd, &addr, &len, &outptr, &outlen); + + if (outptr != outbuf) + { + /* We have something to write out. */ + int errno_save = errno; + + if (fwrite (outbuf, 1, outptr - outbuf, output) + < (size_t) (outptr - outbuf) + || ferror (output)) + { + /* Error occurred while printing the result. */ + DEBUG (0, ("conversion stopped due to problem in writing the output")); + return -1; + } + + errno = errno_save; + } + + if (errno != E2BIG) + { + /* iconv() ran into a problem. */ + switch (errno) + { + case EILSEQ: + DEBUG(0,("illegal input sequence at position %ld", + (long) (addr - start))); + break; + case EINVAL: + DEBUG(0, ("\ +incomplete character or shift sequence at end of buffer")); + break; + case EBADF: + DEBUG(0, ("internal error (illegal descriptor)")); + break; + default: + DEBUG(0, ("unknown iconv() error %d", errno)); + break; + } + + return -1; + } + } + + return 0; +} + + +static int +process_fd (iconv_t cd, int fd, FILE *output) +{ + /* we have a problem with reading from a descriptor since we must not + provide the iconv() function an incomplete character or shift + sequence at the end of the buffer. Since we have to deal with + arbitrary encodings we must read the whole text in a buffer and + process it in one step. */ + static char *inbuf = NULL; + static size_t maxlen = 0; + char *inptr = NULL; + size_t actlen = 0; + + while (actlen < maxlen) + { + ssize_t n = read (fd, inptr, maxlen - actlen); + + if (n == 0) + /* No more text to read. */ + break; + + if (n == -1) + { + /* Error while reading. */ + DEBUG(0, ("error while reading the input")); + return -1; + } + + inptr += n; + actlen += n; + } + + if (actlen == maxlen) + while (1) + { + ssize_t n; + char *new_inbuf; + + /* Increase the buffer. */ + new_inbuf = (char *) realloc (inbuf, maxlen + 32768); + if (new_inbuf == NULL) + { + DEBUG(0, ("unable to allocate buffer for input")); + return -1; + } + inbuf = new_inbuf; + maxlen += 32768; + inptr = inbuf + actlen; + + do + { + n = read (fd, inptr, maxlen - actlen); + + if (n == 0) + /* No more text to read. */ + break; + + if (n == -1) + { + /* Error while reading. */ + DEBUG(0, ("error while reading the input")); + return -1; + } + + inptr += n; + actlen += n; + } + while (actlen < maxlen); + + if (n == 0) + /* Break again so we leave both loops. */ + break; + } + + /* Now we have all the input in the buffer. Process it in one run. */ + return process_block (cd, inbuf, actlen, output); +} + +/* Main function */ + +int main(int argc, char *argv[]) +{ + const char *file = NULL; + char *from = ""; + char *to = ""; + char *output = NULL; + char *preload = NULL; + FILE *out = stdout; + int fd; + smb_iconv_t cd; + + /* make sure the vars that get altered (4th field) are in + a fixed location or certain compilers complain */ + poptContext pc; + struct poptOption long_options[] = { + POPT_AUTOHELP + { "from-code", 'f', POPT_ARG_STRING, &from, 0, "Encoding of original text" }, + { "to-code", 't', POPT_ARG_STRING, &to, 0, "Encoding for output" }, + { "output", 'o', POPT_ARG_STRING, &output, 0, "Write output to this file" }, + { "preload-modules", 'p', POPT_ARG_STRING, &preload, 0, "Modules to load" }, + POPT_COMMON_SAMBA + POPT_TABLEEND + }; + + setlinebuf(stdout); + + pc = poptGetContext("smbiconv", argc, (const char **) argv, + long_options, 0); + + poptSetOtherOptionHelp(pc, "[FILE] ..."); + + while(poptGetNextOpt(pc) != -1); + + if(preload)smb_load_modules(str_list_make(preload, NULL)); + + if(output) { + output = fopen(output, "w"); + + if(!output) { + DEBUG(0, ("Can't open output file '%s': %s, exiting...\n", output, strerror(errno))); + return 1; + } + } + + /* the following functions are part of the Samba debugging + facilities. See lib/debug.c */ + setup_logging("smbiconv", True); + + cd = smb_iconv_open(to, from); + if((int)cd == -1) { + DEBUG(0,("unable to find from or to encoding, exiting...\n")); + return 1; + } + + while((file = poptGetArg(pc))) { + if(strcmp(file, "-") == 0) fd = 0; + else { + fd = open(file, O_RDONLY); + + if(!fd) { + DEBUG(0, ("Can't open input file '%s': %s, ignoring...\n", file, strerror(errno))); + continue; + } + } + + /* Loop thru all arguments */ + process_fd(cd, fd, stdout); + + close(fd); + } + poptFreeContext(pc); + + fclose(out); + + return 0; +} -- cgit From c507ebe56741d773bf6e7ad547863a2da1aee687 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 10 May 2003 10:53:48 +0000 Subject: Patch from metze and me that adds dummy smb_register_*() functions so that is now possible to, for example, load a module which contains an auth method into a binary without the auth/ subsystem built in. (This used to be commit 74d9ecfe2dd7364643d32acb62ade957bd71cd0d) --- source3/torture/smbiconv.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index ce21a09025..613082225a 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -23,6 +23,8 @@ #include "includes.h" +#include "module_dummy.h" + static int process_block (smb_iconv_t cd, char *addr, size_t len, FILE *output) { -- cgit From 0914e541f5480834c1b0ddc98b5f71f7f7abf9cb Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 10 May 2003 11:49:51 +0000 Subject: Reverse previous patch from Stefan and me after comments by Andrew Bartlett (This used to be commit d817eaf0ecca2d878ab1ffcf7a747a02d71c811e) --- source3/torture/smbiconv.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index 613082225a..ce21a09025 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -23,8 +23,6 @@ #include "includes.h" -#include "module_dummy.h" - static int process_block (smb_iconv_t cd, char *addr, size_t len, FILE *output) { -- cgit From 29674c7fa6742a322801c1604ce63a752d9fe922 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Thu, 26 Jun 2003 17:29:09 +0000 Subject: Move up intialisation of logging, so we catch errors in handling 'preload modules' (This used to be commit 13b81d0d92b9e28eaab18f5437060e79f6075f9e) --- source3/torture/smbiconv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index ce21a09025..3524136fb1 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -198,6 +198,10 @@ int main(int argc, char *argv[]) while(poptGetNextOpt(pc) != -1); + /* the following functions are part of the Samba debugging + facilities. See lib/debug.c */ + setup_logging("smbiconv", True); + if(preload)smb_load_modules(str_list_make(preload, NULL)); if(output) { @@ -209,10 +213,6 @@ int main(int argc, char *argv[]) } } - /* the following functions are part of the Samba debugging - facilities. See lib/debug.c */ - setup_logging("smbiconv", True); - cd = smb_iconv_open(to, from); if((int)cd == -1) { DEBUG(0,("unable to find from or to encoding, exiting...\n")); -- cgit From 7e27147422e78125c2bb5b8115bd9084a657e084 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Tue, 26 Aug 2003 19:48:16 +0000 Subject: Fix for #150. We now fallback to Samba-provided CP850 charset module if CP850 or IBM850 does not exist on target system at runtime. 1. Introduce CP850 charset module based on charmaps table from GNU libc 2.2.5 2. Make CP850 charset module shared and build it by default Should fix Solaris run-time (This used to be commit e855dc8c9115fa11d315eb34d57722ff612daa11) --- source3/torture/smbiconv.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index 3524136fb1..1dd168b0bb 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -24,7 +24,7 @@ #include "includes.h" static int -process_block (smb_iconv_t cd, char *addr, size_t len, FILE *output) +process_block (smb_iconv_t cd, const char *addr, size_t len, FILE *output) { #define OUTBUF_SIZE 32768 const char *start = addr; @@ -37,7 +37,7 @@ process_block (smb_iconv_t cd, char *addr, size_t len, FILE *output) { outptr = outbuf; outlen = OUTBUF_SIZE; - n = smb_iconv (cd, &addr, &len, &outptr, &outlen); + n = smb_iconv (cd, &addr, &len, &outptr, &outlen); if (outptr != outbuf) { @@ -171,7 +171,7 @@ int main(int argc, char *argv[]) char *from = ""; char *to = ""; char *output = NULL; - char *preload = NULL; + const char *preload_modules[] = {NULL, NULL}; FILE *out = stdout; int fd; smb_iconv_t cd; @@ -184,7 +184,7 @@ int main(int argc, char *argv[]) { "from-code", 'f', POPT_ARG_STRING, &from, 0, "Encoding of original text" }, { "to-code", 't', POPT_ARG_STRING, &to, 0, "Encoding for output" }, { "output", 'o', POPT_ARG_STRING, &output, 0, "Write output to this file" }, - { "preload-modules", 'p', POPT_ARG_STRING, &preload, 0, "Modules to load" }, + { "preload-modules", 'p', POPT_ARG_STRING, &preload_modules[0], 0, "Modules to load" }, POPT_COMMON_SAMBA POPT_TABLEEND }; @@ -202,12 +202,12 @@ int main(int argc, char *argv[]) facilities. See lib/debug.c */ setup_logging("smbiconv", True); - if(preload)smb_load_modules(str_list_make(preload, NULL)); + if (preload_modules[0]) smb_load_modules(preload_modules); if(output) { - output = fopen(output, "w"); + out = fopen(output, "w"); - if(!output) { + if(!out) { DEBUG(0, ("Can't open output file '%s': %s, exiting...\n", output, strerror(errno))); return 1; } @@ -231,7 +231,7 @@ int main(int argc, char *argv[]) } /* Loop thru all arguments */ - process_fd(cd, fd, stdout); + process_fd(cd, fd, out); close(fd); } -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/torture/smbiconv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index 1dd168b0bb..89e04056c0 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -8,7 +8,7 @@ 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 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/torture/smbiconv.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index 89e04056c0..05a8c3d815 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -17,8 +17,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From dd255b427515d0f22695185bd78c16bc58de6c09 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 15 Feb 2008 11:39:25 +0100 Subject: Fix compiling torture/smbiconv.c: allow to use realloc directly by #undef realloc. Michael (This used to be commit 113377a7929975bf29c2cfff1652781fae24e826) --- source3/torture/smbiconv.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index 05a8c3d815..c4d30164e4 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#undef realloc static int process_block (smb_iconv_t cd, const char *addr, size_t len, FILE *output) -- cgit From 8a63e1322581f0adf842d92b8e57919dab7d459b Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 15 Feb 2008 13:11:41 +0100 Subject: Try to fix the build of smbiconv on systems without native iconv. Use the smb_iconv_t instead of iconv_t. Michael (This used to be commit bdc00a05b95a7a629a6cbf2af21760b6ff874bd9) --- source3/torture/smbiconv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index c4d30164e4..ad4d6b26cc 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -86,7 +86,7 @@ incomplete character or shift sequence at end of buffer")); static int -process_fd (iconv_t cd, int fd, FILE *output) +process_fd (smb_iconv_t cd, int fd, FILE *output) { /* we have a problem with reading from a descriptor since we must not provide the iconv() function an incomplete character or shift -- cgit From a10e6183aa05f26583dfe0fc7037d9190b57a69d Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 15 Feb 2008 13:12:38 +0100 Subject: Fix two const warnings. Michael (This used to be commit 03db3bd0774e0435089eef15f88355133149d658) --- source3/torture/smbiconv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index ad4d6b26cc..6e609aa851 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -168,8 +168,8 @@ process_fd (smb_iconv_t cd, int fd, FILE *output) int main(int argc, char *argv[]) { const char *file = NULL; - char *from = ""; - char *to = ""; + const char *from = ""; + const char *to = ""; char *output = NULL; const char *preload_modules[] = {NULL, NULL}; FILE *out = stdout; -- cgit From d4123ba9ee7ec59331cffb846d9c82dce3d84d0d Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 15 Feb 2008 13:14:20 +0100 Subject: Fix a pointer to int conversion warning: cast the int instead. Michael (This used to be commit 3cace4b7aa87a2f5bc7b6efd095366699d939511) --- source3/torture/smbiconv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/torture/smbiconv.c') diff --git a/source3/torture/smbiconv.c b/source3/torture/smbiconv.c index 6e609aa851..72fbdc470f 100644 --- a/source3/torture/smbiconv.c +++ b/source3/torture/smbiconv.c @@ -214,7 +214,7 @@ int main(int argc, char *argv[]) } cd = smb_iconv_open(to, from); - if((int)cd == -1) { + if (cd == (smb_iconv_t)-1) { DEBUG(0,("unable to find from or to encoding, exiting...\n")); return 1; } -- cgit