From e4819ae1efedab0340a47958460f3a7b058e0e94 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 16 Sep 2011 15:31:29 +0200 Subject: curl tool: move convert_* functions into tool_convert.[ch] Additionally fix data type of result vars for iconv() calls --- src/Makefile.inc | 5 +- src/Makefile.vc6 | 6 +++ src/main.c | 124 +------------------------------------------ src/tool_convert.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tool_convert.h | 44 ++++++++++++++++ src/vc6curlsrc.dsp | 8 +++ 6 files changed, 214 insertions(+), 124 deletions(-) create mode 100644 src/tool_convert.c create mode 100644 src/tool_convert.h diff --git a/src/Makefile.inc b/src/Makefile.inc index 058c6d28d..f13b171e1 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -15,12 +15,13 @@ CURLX_ONES = $(top_srcdir)/lib/strtoofft.c \ $(top_srcdir)/lib/nonblock.c CURL_CFILES = main.c hugehelp.c urlglob.c writeout.c writeenv.c \ - getpass.c homedir.c curlutil.c os-specific.c xattr.c + getpass.c homedir.c curlutil.c os-specific.c xattr.c \ + tool_convert.c CURL_HFILES = hugehelp.h setup.h config-win32.h config-mac.h \ config-riscos.h urlglob.h version.h os-specific.h \ writeout.h writeenv.h getpass.h homedir.h curlutil.h \ - xattr.h + xattr.h tool_convert.h curl_SOURCES = $(CURL_CFILES) $(CURLX_ONES) $(CURL_HFILES) diff --git a/src/Makefile.vc6 b/src/Makefile.vc6 index d6ef39b3d..14e9399d5 100644 --- a/src/Makefile.vc6 +++ b/src/Makefile.vc6 @@ -142,6 +142,7 @@ RELEASE_OBJS= \ os-specificr.obj \ rawstrr.obj \ strtoofftr.obj \ + tool_convertr.obj \ urlglobr.obj \ writeoutr.obj \ xattrr.obj \ @@ -157,6 +158,7 @@ DEBUG_OBJS= \ os-specificd.obj \ rawstrd.obj \ strtoofftd.obj \ + tool_convertd.obj \ urlglobd.obj \ writeoutd.obj \ xattrd.obj \ @@ -302,6 +304,8 @@ rawstrr.obj: ../lib/rawstr.c $(CCR) $(CFLAGS) /Fo"$@" ../lib/rawstr.c strtoofftr.obj: ../lib/strtoofft.c $(CCR) $(CFLAGS) /Fo"$@" ../lib/strtoofft.c +tool_convertr.obj: tool_convert.c + $(CCR) $(CFLAGS) /Fo"$@" tool_convert.c xattrr.obj: xattr.c $(CCR) $(CFLAGS) /Fo"$@" xattr.c mainr.obj: main.c @@ -330,6 +334,8 @@ rawstrd.obj: ../lib/rawstr.c $(CCD) $(CFLAGS) /Fo"$@" ../lib/rawstr.c strtoofftd.obj: ../lib/strtoofft.c $(CCD) $(CFLAGS) /Fo"$@" ../lib/strtoofft.c +tool_convertd.obj: tool_convert.c + $(CCD) $(CFLAGS) /Fo"$@" tool_convert.c xattrd.obj: xattr.c $(CCD) $(CFLAGS) /Fo"$@" xattr.c maind.obj: main.c diff --git a/src/main.c b/src/main.c index 3161cb38a..52b700fbc 100644 --- a/src/main.c +++ b/src/main.c @@ -85,14 +85,6 @@ # include #endif -#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV) -# include -/* set default codesets for iconv */ -# ifndef CURL_ICONV_CODESET_OF_NETWORK -# define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1" -# endif -#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */ - #ifdef MSDOS # include #endif @@ -119,6 +111,7 @@ #include "os-specific.h" #include "version.h" #include "xattr.h" +#include "tool_convert.h" #ifdef USE_MANUAL # include "hugehelp.h" #endif @@ -320,113 +313,6 @@ typedef enum { "If you'd like to turn off curl's verification of the certificate, use\n" \ " the -k (or --insecure) option.\n" -#ifdef CURL_DOES_CONVERSIONS -#ifdef HAVE_ICONV -iconv_t inbound_cd = (iconv_t)-1; -iconv_t outbound_cd = (iconv_t)-1; - -/* - * convert_to_network() is an internal function to convert - * from the host encoding to ASCII on non-ASCII platforms. - */ -static CURLcode -convert_to_network(char *buffer, size_t length) -{ - CURLcode rc; - - /* translate from the host encoding to the network encoding */ - char *input_ptr, *output_ptr; - size_t in_bytes, out_bytes; - - /* open an iconv conversion descriptor if necessary */ - if(outbound_cd == (iconv_t)-1) { - outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK, - CURL_ICONV_CODESET_OF_HOST); - if(outbound_cd == (iconv_t)-1) { - return CURLE_CONV_FAILED; - } - } - /* call iconv */ - input_ptr = output_ptr = buffer; - in_bytes = out_bytes = length; - rc = iconv(outbound_cd, &input_ptr, &in_bytes, - &output_ptr, &out_bytes); - if((rc == -1) || (in_bytes != 0)) { - return CURLE_CONV_FAILED; - } - - return CURLE_OK; -} - -/* - * convert_from_network() is an internal function - * for performing ASCII conversions on non-ASCII platforms. - */ -static CURLcode -convert_from_network(char *buffer, size_t length) -{ - CURLcode rc; - - /* translate from the network encoding to the host encoding */ - char *input_ptr, *output_ptr; - size_t in_bytes, out_bytes; - - /* open an iconv conversion descriptor if necessary */ - if(inbound_cd == (iconv_t)-1) { - inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, - CURL_ICONV_CODESET_OF_NETWORK); - if(inbound_cd == (iconv_t)-1) { - return CURLE_CONV_FAILED; - } - } - /* call iconv */ - input_ptr = output_ptr = buffer; - in_bytes = out_bytes = length; - rc = iconv(inbound_cd, &input_ptr, &in_bytes, - &output_ptr, &out_bytes); - if((rc == -1) || (in_bytes != 0)) { - return CURLE_CONV_FAILED; - } - - return CURLE_OK; -} -#endif /* HAVE_ICONV */ - -static -char convert_char(curl_infotype infotype, char this_char) -{ -/* determine how this specific character should be displayed */ - switch(infotype) { - case CURLINFO_DATA_IN: - case CURLINFO_DATA_OUT: - case CURLINFO_SSL_DATA_IN: - case CURLINFO_SSL_DATA_OUT: - /* data, treat as ASCII */ - if((this_char >= 0x20) && (this_char < 0x7f)) { - /* printable ASCII hex value: convert to host encoding */ - convert_from_network(&this_char, 1); - } - else { - /* non-printable ASCII, use a replacement character */ - return UNPRINTABLE_CHAR; - } - /* fall through to default */ - default: - /* treat as host encoding */ - if(ISPRINT(this_char) - && (this_char != '\t') - && (this_char != '\r') - && (this_char != '\n')) { - /* printable characters excluding tabs and line end characters */ - return this_char; - } - break; - } - /* non-printable, use a replacement character */ - return UNPRINTABLE_CHAR; -} -#endif /* CURL_DOES_CONVERSIONS */ - #if defined(WIN32) && !defined(__MINGW64__) #ifdef __BORLANDC__ @@ -716,13 +602,7 @@ static CURLcode main_init(void) static void main_free(void) { curl_global_cleanup(); -#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV) - /* close iconv conversion descriptor */ - if(inbound_cd != (iconv_t)-1) - iconv_close(inbound_cd); - if(outbound_cd != (iconv_t)-1) - iconv_close(outbound_cd); -#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */ + convert_cleanup(); } static int SetHTTPrequest(struct Configurable *config, diff --git a/src/tool_convert.c b/src/tool_convert.c new file mode 100644 index 000000000..7aa4e1309 --- /dev/null +++ b/src/tool_convert.c @@ -0,0 +1,151 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ +#include "setup.h" + +#ifdef CURL_DOES_CONVERSIONS + +#ifdef HAVE_ICONV +# include +#endif + +#include + +#include "tool_convert.h" + +#include "memdebug.h" /* keep this as LAST include */ + +#ifdef HAVE_ICONV + +/* curl tool iconv conversion descriptors */ +static iconv_t inbound_cd = (iconv_t)-1; +static iconv_t outbound_cd = (iconv_t)-1; + +/* set default codesets for iconv */ +#ifndef CURL_ICONV_CODESET_OF_NETWORK +# define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1" +#endif + +/* + * convert_to_network() is a curl tool function to convert + * from the host encoding to ASCII on non-ASCII platforms. + */ +CURLcode convert_to_network(char *buffer, size_t length) +{ + /* translate from the host encoding to the network encoding */ + char *input_ptr, *output_ptr; + size_t res, in_bytes, out_bytes; + + /* open an iconv conversion descriptor if necessary */ + if(outbound_cd == (iconv_t)-1) { + outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK, + CURL_ICONV_CODESET_OF_HOST); + if(outbound_cd == (iconv_t)-1) { + return CURLE_CONV_FAILED; + } + } + /* call iconv */ + input_ptr = output_ptr = buffer; + in_bytes = out_bytes = length; + res = iconv(outbound_cd, &input_ptr, &in_bytes, + &output_ptr, &out_bytes); + if((res == (size_t)-1) || (in_bytes != 0)) { + return CURLE_CONV_FAILED; + } + + return CURLE_OK; +} + +/* + * convert_from_network() is a curl tool function + * for performing ASCII conversions on non-ASCII platforms. + */ +CURLcode convert_from_network(char *buffer, size_t length) +{ + /* translate from the network encoding to the host encoding */ + char *input_ptr, *output_ptr; + size_t res, in_bytes, out_bytes; + + /* open an iconv conversion descriptor if necessary */ + if(inbound_cd == (iconv_t)-1) { + inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, + CURL_ICONV_CODESET_OF_NETWORK); + if(inbound_cd == (iconv_t)-1) { + return CURLE_CONV_FAILED; + } + } + /* call iconv */ + input_ptr = output_ptr = buffer; + in_bytes = out_bytes = length; + res = iconv(inbound_cd, &input_ptr, &in_bytes, + &output_ptr, &out_bytes); + if((res == (size_t)-1) || (in_bytes != 0)) { + return CURLE_CONV_FAILED; + } + + return CURLE_OK; +} + +void convert_cleanup(void) +{ + /* close iconv conversion descriptors */ + if(inbound_cd != (iconv_t)-1) + (void)iconv_close(inbound_cd); + if(outbound_cd != (iconv_t)-1) + (void)iconv_close(outbound_cd); +} + +#endif /* HAVE_ICONV */ + +char convert_char(curl_infotype infotype, char this_char) +{ +/* determine how this specific character should be displayed */ + switch(infotype) { + case CURLINFO_DATA_IN: + case CURLINFO_DATA_OUT: + case CURLINFO_SSL_DATA_IN: + case CURLINFO_SSL_DATA_OUT: + /* data, treat as ASCII */ + if((this_char >= 0x20) && (this_char < 0x7f)) { + /* printable ASCII hex value: convert to host encoding */ + (void)convert_from_network(&this_char, 1); + } + else { + /* non-printable ASCII, use a replacement character */ + return UNPRINTABLE_CHAR; + } + /* fall through to default */ + default: + /* treat as host encoding */ + if(ISPRINT(this_char) + && (this_char != '\t') + && (this_char != '\r') + && (this_char != '\n')) { + /* printable characters excluding tabs and line end characters */ + return this_char; + } + break; + } + /* non-printable, use a replacement character */ + return UNPRINTABLE_CHAR; +} + +#endif /* CURL_DOES_CONVERSIONS */ \ No newline at end of file diff --git a/src/tool_convert.h b/src/tool_convert.h new file mode 100644 index 000000000..4cc94b030 --- /dev/null +++ b/src/tool_convert.h @@ -0,0 +1,44 @@ +#ifndef HEADER_CURL_TOOL_CONVERT_H +#define HEADER_CURL_TOOL_CONVERT_H +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ +#include "setup.h" + +#ifdef CURL_DOES_CONVERSIONS + +#ifdef HAVE_ICONV + +CURLcode convert_to_network(char *buffer, size_t length); +CURLcode convert_from_network(char *buffer, size_t length); +void convert_cleanup(void); + +#endif /* HAVE_ICONV */ + +char convert_char(curl_infotype infotype, char this_char); + +#endif /* CURL_DOES_CONVERSIONS */ + +#if !defined(CURL_DOES_CONVERSIONS) || !defined(HAVE_ICONV) +#define convert_cleanup() Curl_nop_stmt +#endif + +#endif /* HEADER_CURL_TOOL_CONVERT_H */ diff --git a/src/vc6curlsrc.dsp b/src/vc6curlsrc.dsp index c117370b5..eff6b7782 100644 --- a/src/vc6curlsrc.dsp +++ b/src/vc6curlsrc.dsp @@ -175,6 +175,10 @@ SOURCE=..\lib\strtoofft.c # End Source File # Begin Source File +SOURCE=.\tool_convert.c +# End Source File +# Begin Source File + SOURCE=.\urlglob.c # End Source File # Begin Source File @@ -235,6 +239,10 @@ SOURCE=..\lib\strtoofft.h # End Source File # Begin Source File +SOURCE=.\tool_convert.h +# End Source File +# Begin Source File + SOURCE=.\urlglob.h # End Source File # Begin Source File -- cgit v1.2.3