Changeset 160 for trunk/src/common.c
- Timestamp:
- 12/06/09 20:00:58 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/common.c
r159 r160 145 145 146 146 /* 147 * Convert from UTF-16LE to ASCII. Accepts a Unicode buffer, uni, and148 * it's length, uni_max. Writes ASCII to the buffer ascii, whose size149 * is ascii_max. Writes at most (ascii_max-1) bytes to ascii, and null150 * terminates the string. Returns the length of the data written to151 * ascii. On error, returns a negative errno code.152 */153 static int uni_to_ascii(unsigned char* uni, char* ascii,154 uint32 uni_max, uint32 ascii_max)155 {156 char* inbuf = (char*)uni;157 char* outbuf = ascii;158 size_t in_len = (size_t)uni_max;159 size_t out_len = (size_t)(ascii_max-1);160 int ret;161 162 conv_desc = iconv_open("US-ASCII//TRANSLIT", "UTF-16LE");163 164 ret = iconv(conv_desc, &inbuf, &in_len, &outbuf, &out_len);165 if(ret == -1)166 {167 iconv_close(conv_desc);168 return -errno;169 }170 *outbuf = '\0';171 172 iconv_close(conv_desc);173 return ascii_max-out_len-1;174 }175 176 177 static char* quote_unicode(unsigned char* uni, uint32 length,178 const char* special, char** error_msg)179 {180 char* ret_val;181 char* ascii = NULL;182 char* tmp_err;183 int ret_err;184 *error_msg = NULL;185 186 if(length+1 > 0)187 ascii = malloc(length+1);188 if(ascii == NULL)189 {190 *error_msg = (char*)malloc(27);191 if(*error_msg == NULL)192 return NULL;193 strcpy(*error_msg, "Memory allocation failure.");194 return NULL;195 }196 197 ret_err = uni_to_ascii(uni, ascii, length, length+1);198 if(ret_err < 0)199 {200 free(ascii);201 tmp_err = strerror(-ret_err);202 *error_msg = (char*)malloc(61+strlen(tmp_err));203 if(*error_msg == NULL)204 return NULL;205 206 sprintf(*error_msg,207 "Unicode conversion failed with '%s'. Quoting as binary.", tmp_err);208 ret_val = quote_buffer(uni, length, special);209 }210 else211 {212 ret_val = quote_string(ascii, special);213 free(ascii);214 }215 216 return ret_val;217 }218 219 220 /*221 147 * Convert a data value to a string for display. Returns NULL on error, 222 148 * and the string to display if there is no error, or a non-fatal
Note: See TracChangeset
for help on using the changeset viewer.