tool_convert.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #ifdef CURL_DOES_CONVERSIONS
00025 
00026 #ifdef HAVE_ICONV
00027 #  include <iconv.h>
00028 #endif
00029 
00030 #include "tool_convert.h"
00031 
00032 #include "memdebug.h" /* keep this as LAST include */
00033 
00034 #ifdef HAVE_ICONV
00035 
00036 /* curl tool iconv conversion descriptors */
00037 static iconv_t inbound_cd  = (iconv_t)-1;
00038 static iconv_t outbound_cd = (iconv_t)-1;
00039 
00040 /* set default codesets for iconv */
00041 #ifndef CURL_ICONV_CODESET_OF_NETWORK
00042 #  define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
00043 #endif
00044 
00045 /*
00046  * convert_to_network() is a curl tool function to convert
00047  * from the host encoding to ASCII on non-ASCII platforms.
00048  */
00049 CURLcode convert_to_network(char *buffer, size_t length)
00050 {
00051   /* translate from the host encoding to the network encoding */
00052   char *input_ptr, *output_ptr;
00053   size_t res, in_bytes, out_bytes;
00054 
00055   /* open an iconv conversion descriptor if necessary */
00056   if(outbound_cd == (iconv_t)-1) {
00057     outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
00058                              CURL_ICONV_CODESET_OF_HOST);
00059     if(outbound_cd == (iconv_t)-1) {
00060       return CURLE_CONV_FAILED;
00061     }
00062   }
00063   /* call iconv */
00064   input_ptr = output_ptr = buffer;
00065   in_bytes = out_bytes = length;
00066   res = iconv(outbound_cd, &input_ptr,  &in_bytes,
00067               &output_ptr, &out_bytes);
00068   if((res == (size_t)-1) || (in_bytes != 0)) {
00069     return CURLE_CONV_FAILED;
00070   }
00071 
00072   return CURLE_OK;
00073 }
00074 
00075 /*
00076  * convert_from_network() is a curl tool function
00077  * for performing ASCII conversions on non-ASCII platforms.
00078  */
00079 CURLcode convert_from_network(char *buffer, size_t length)
00080 {
00081   /* translate from the network encoding to the host encoding */
00082   char *input_ptr, *output_ptr;
00083   size_t res, in_bytes, out_bytes;
00084 
00085   /* open an iconv conversion descriptor if necessary */
00086   if(inbound_cd == (iconv_t)-1) {
00087     inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
00088                             CURL_ICONV_CODESET_OF_NETWORK);
00089     if(inbound_cd == (iconv_t)-1) {
00090       return CURLE_CONV_FAILED;
00091     }
00092   }
00093   /* call iconv */
00094   input_ptr = output_ptr = buffer;
00095   in_bytes = out_bytes = length;
00096   res = iconv(inbound_cd, &input_ptr,  &in_bytes,
00097               &output_ptr, &out_bytes);
00098   if((res == (size_t)-1) || (in_bytes != 0)) {
00099     return CURLE_CONV_FAILED;
00100   }
00101 
00102   return CURLE_OK;
00103 }
00104 
00105 void convert_cleanup(void)
00106 {
00107   /* close iconv conversion descriptors */
00108   if(inbound_cd != (iconv_t)-1)
00109     (void)iconv_close(inbound_cd);
00110   if(outbound_cd != (iconv_t)-1)
00111     (void)iconv_close(outbound_cd);
00112 }
00113 
00114 #endif /* HAVE_ICONV */
00115 
00116 char convert_char(curl_infotype infotype, char this_char)
00117 {
00118 /* determine how this specific character should be displayed */
00119   switch(infotype) {
00120   case CURLINFO_DATA_IN:
00121   case CURLINFO_DATA_OUT:
00122   case CURLINFO_SSL_DATA_IN:
00123   case CURLINFO_SSL_DATA_OUT:
00124     /* data, treat as ASCII */
00125     if((this_char >= 0x20) && (this_char < 0x7f)) {
00126       /* printable ASCII hex value: convert to host encoding */
00127       (void)convert_from_network(&this_char, 1);
00128     }
00129     else {
00130       /* non-printable ASCII, use a replacement character */
00131       return UNPRINTABLE_CHAR;
00132     }
00133     /* fall through to default */
00134   default:
00135     /* treat as host encoding */
00136     if(ISPRINT(this_char)
00137        &&  (this_char != '\t')
00138        &&  (this_char != '\r')
00139        &&  (this_char != '\n')) {
00140       /* printable characters excluding tabs and line end characters */
00141       return this_char;
00142     }
00143     break;
00144   }
00145   /* non-printable, use a replacement character  */
00146   return UNPRINTABLE_CHAR;
00147 }
00148 
00149 #endif /* CURL_DOES_CONVERSIONS */
00150 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06