Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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"
00033
00034 #ifdef HAVE_ICONV
00035
00036
00037 static iconv_t inbound_cd = (iconv_t)-1;
00038 static iconv_t outbound_cd = (iconv_t)-1;
00039
00040
00041 #ifndef CURL_ICONV_CODESET_OF_NETWORK
00042 # define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
00043 #endif
00044
00045
00046
00047
00048
00049 CURLcode convert_to_network(char *buffer, size_t length)
00050 {
00051
00052 char *input_ptr, *output_ptr;
00053 size_t res, in_bytes, out_bytes;
00054
00055
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
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
00077
00078
00079 CURLcode convert_from_network(char *buffer, size_t length)
00080 {
00081
00082 char *input_ptr, *output_ptr;
00083 size_t res, in_bytes, out_bytes;
00084
00085
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
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
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
00115
00116 char convert_char(curl_infotype infotype, char this_char)
00117 {
00118
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
00125 if((this_char >= 0x20) && (this_char < 0x7f)) {
00126
00127 (void)convert_from_network(&this_char, 1);
00128 }
00129 else {
00130
00131 return UNPRINTABLE_CHAR;
00132 }
00133
00134 default:
00135
00136 if(ISPRINT(this_char)
00137 && (this_char != '\t')
00138 && (this_char != '\r')
00139 && (this_char != '\n')) {
00140
00141 return this_char;
00142 }
00143 break;
00144 }
00145
00146 return UNPRINTABLE_CHAR;
00147 }
00148
00149 #endif
00150