char *pr_utf8_encode(pool *p, const char *in, size_t inlen, size_t *outlen) { #ifdef HAVE_ICONV_H size_t inbuflen, outbuflen; char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res; if (!p || !in || !outlen) { errno = EINVAL; return NULL; } if (encode_conv == (iconv_t) -1) { errno = EPERM; return NULL; } inbuf = pcalloc(p, inlen); memcpy(inbuf, in, inlen); inbuflen = inlen; outbuflen = sizeof(outbuf); if (utf8_convert(encode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) return NULL; *outlen = sizeof(outbuf) - outbuflen; res = pcalloc(p, *outlen); memcpy(res, outbuf, *outlen); return res; #else pr_trace_msg("utf8", 1, "missing iconv support, no UTF8 encoding possible"); return pstrdup(p, in); #endif /* !HAVE_ICONV_H */ }
int _linphone_sqlite3_open(const char *db_file, sqlite3 **db) { char* errmsg = NULL; int ret; int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; #if TARGET_OS_IPHONE /* the secured filesystem of the iPHone doesn't allow writing while the app is in background mode, which is problematic. * We workaround by asking that the open is made with no protection*/ flags |= SQLITE_OPEN_FILEPROTECTION_NONE; #endif /*since we plug our vfs into sqlite, we convert to UTF-8. * On Windows, the filename has to be converted back to windows native charset.*/ char *utf8_filename = utf8_convert(db_file); ret = sqlite3_open_v2(utf8_filename, db, flags, LINPHONE_SQLITE3_VFS); ms_free(utf8_filename); if (ret != SQLITE_OK) return ret; // Some platforms do not provide a way to create temporary files which are needed // for transactions... so we work in memory only // see http ://www.sqlite.org/compile.html#temp_store ret = sqlite3_exec(*db, "PRAGMA temp_store=MEMORY", NULL, NULL, &errmsg); if (ret != SQLITE_OK) { ms_error("Cannot set sqlite3 temporary store to memory: %s.", errmsg); sqlite3_free(errmsg); } #if TARGET_OS_IPHONE ret = sqlite3_exec(*db, "PRAGMA journal_mode = OFF", NULL, NULL, &errmsg); if (ret != SQLITE_OK) { ms_error("Cannot set sqlite3 journal_mode to off: %s.", errmsg); sqlite3_free(errmsg); } #endif return ret; }
int main(void) { char src[]="04 鏁板瓧1鍒"; char dest[255]={0}; int dest_len =255; int iresult = utf8_convert(src, dest, dest_len); printf("src : %s\n", src); printf("dest: %s\n", dest); return iresult; }
static void code_string (char const *string, char const *keyword, struct xheader *xhdr) { char *outstr; if (!utf8_convert (true, string, &outstr)) { /* FIXME: report error */ outstr = xstrdup (string); } xheader_print (xhdr, keyword, outstr); free (outstr); }
static void decode_string (char **string, char const *arg) { if (*string) { free (*string); *string = NULL; } if (!utf8_convert (false, arg, string)) { /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */ assign_string (string, arg); } }
char *sftp_utf8_decode_str(pool *p, const char *str) { #if defined(PR_USE_NLS) && defined(HAVE_ICONV_H) size_t inlen, inbuflen, outlen, outbuflen; char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res = NULL; if (p == NULL || str == NULL) { errno = EINVAL; return NULL; } if (decode_conv == (iconv_t) -1) { pr_trace_msg("sftp", 1, "decoding conversion handle is invalid, unable to " "decode UTF8 string"); return (char *) str; } /* If the local charset matches the remote charset (i.e. local_charset is * "UTF-8"), then there's no point in converting; the charsets are the * same. Indeed, on some libiconv implementations, attempting to * convert between the same charsets results in a tightly spinning CPU * (see Bug#3272). */ if (strncasecmp(local_charset, "UTF-8", 6) == 0) { return (char *) str; } inlen = strlen(str) + 1; inbuf = pcalloc(p, inlen); memcpy(inbuf, str, inlen); inbuflen = inlen; outbuflen = sizeof(outbuf); if (utf8_convert(decode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) { pr_trace_msg("sftp", 1, "error decoding string: %s", strerror(errno)); return (char *) str; } outlen = sizeof(outbuf) - outbuflen; res = pcalloc(p, outlen); memcpy(res, outbuf, outlen); return res; #else return pstrdup(p, str); #endif /* !PR_USE_NLS && !HAVE_ICONV_H */ }
char *sftp_utf8_encode_str(pool *p, const char *str) { #if defined(PR_USE_NLS) && defined(HAVE_ICONV_H) size_t inlen, inbuflen, outlen, outbuflen; char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res; if (p == NULL || str == NULL) { errno = EINVAL; return NULL; } if (encode_conv == (iconv_t) -1) { pr_trace_msg("sftp", 1, "encoding conversion handle is invalid, unable to " "encode UTF8 string"); return (char *) str; } inlen = strlen(str) + 1; inbuf = pcalloc(p, inlen); memcpy(inbuf, str, inlen); inbuflen = inlen; outbuflen = sizeof(outbuf); if (utf8_convert(encode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) { pr_trace_msg("sftp", 1, "error encoding string: %s", strerror(errno)); return (char *) str; } outlen = sizeof(outbuf) - outbuflen; res = pcalloc(p, outlen); memcpy(res, outbuf, outlen); return res; #else return pstrdup(p, str); #endif /* !PR_USE_NLS && !HAVE_ICONV_H */ }
/** * Convert a string in the named encoding into a UTF-8 string * * \param string The NULL-terminated string to convert * \param encname The encoding name (suitable for passing to iconv) * \param len Length of input string to consider (in bytes), or 0 * \param result Pointer to location to store result (allocated on heap) * \return Appropriate utf8_convert_ret value */ utf8_convert_ret utf8_from_enc(const char *string, const char *encname, size_t len, char **result) { return utf8_convert(string, len, encname, "UTF-8", result); }