hash_free(encodings);
}
+/* Let function be called before main() */
+void update_gconv_search_path() __attribute__ ((constructor));
+
+/* Note:
+
+ The environment variable GCONV_PATH has to be adjusted before the very
+ first call of iconv_open. For the most general case, it means that we
+ have to make our own constructor here (in case some of the other library
+ constructors would use iconv_open).
+
+ However, it looks like a change of an environment variable in a constructor
+ doesn't always survive until the main() function. This is the case if
+ the environment variable is a new one, for which there was no room yet
+ in the initial environment. The initial environment is located on the
+ stack, but when variables are added, it is moved to the heap (to be able
+ to grow). Now, the main function takes again the one from the stack, not
+ from the heap, so changes are lost.
+
+ For this, the function below will also be called in gedcom_init(), which
+ needs to be called as early as possible in the program.
+ */
+
+void update_gconv_search_path()
+{
+ char *gconv_path;
+ /* Add gedcom data directory to gconv search path */
+ gconv_path = getenv(GCONV_SEARCH_PATH);
+ if (gconv_path == NULL || strstr(gconv_path, PKGDATADIR) == NULL) {
+ char *new_gconv_path;
+ if (gconv_path == NULL) {
+ new_gconv_path = (char *)malloc(strlen(GCONV_SEARCH_PATH)
+ + strlen(PKGDATADIR)
+ + 2);
+ sprintf(new_gconv_path, "%s=%s", GCONV_SEARCH_PATH, PKGDATADIR);
+ }
+ else {
+ new_gconv_path = (char *)malloc(strlen(GCONV_SEARCH_PATH)
+ + strlen(gconv_path)
+ + strlen(PKGDATADIR)
+ + 3);
+ sprintf(new_gconv_path, "%s=%s:%s",
+ GCONV_SEARCH_PATH, gconv_path, PKGDATADIR);
+ }
+ /* Ignore failures of putenv (can't do anything about it anyway) */
+ putenv(new_gconv_path);
+ }
+}
+
void init_encodings()
{
if (encodings == NULL) {
char gedcom_n[MAXBUF + 1];
char charwidth[MAXBUF + 1];
char iconv_n[MAXBUF + 1];
- char *gconv_path;
atexit(cleanup_encodings);
- /* Add gedcom data directory to gconv search path */
- gconv_path = getenv(GCONV_SEARCH_PATH);
- if (gconv_path == NULL || strstr(gconv_path, PKGDATADIR) == NULL) {
- char *new_gconv_path;
- if (gconv_path == NULL) {
- new_gconv_path = (char *)malloc(strlen(GCONV_SEARCH_PATH)
- + strlen(PKGDATADIR)
- + 2);
- sprintf(new_gconv_path, "%s=%s", GCONV_SEARCH_PATH, PKGDATADIR);
- }
- else {
- new_gconv_path = (char *)malloc(strlen(GCONV_SEARCH_PATH)
- + strlen(gconv_path)
- + strlen(PKGDATADIR)
- + 3);
- sprintf(new_gconv_path, "%s=%s:%s",
- GCONV_SEARCH_PATH, gconv_path, PKGDATADIR);
- }
- if (putenv(new_gconv_path) != 0) {
- gedcom_warning(_("Failed updating conversion module path"));
- }
- }
-
encodings = hash_create(HASHCOUNT_T_MAX, NULL, NULL);
hash_set_allocator(encodings, node_alloc, node_free, NULL);
char* to_internal(char* str, size_t len,
char* output_buffer, size_t out_len)
{
+ size_t res;
size_t outsize = out_len;
char *wrptr = output_buffer;
char *rdptr = conv_buf;
+ char *retval = output_buffer;
/* set up input buffer (concatenate to what was left previous time) */
/* can't use strcpy, because possible null bytes from unicode */
memcpy(conv_buf + conv_buf_size, str, len);
/* set up output buffer (empty it) */
memset(output_buffer, 0, out_len);
/* do the conversion */
- iconv(cd_to_internal, &rdptr, &conv_buf_size, &wrptr, &outsize);
+ res = iconv(cd_to_internal, &rdptr, &conv_buf_size, &wrptr, &outsize);
+ if (res == (size_t)-1) {
+ if (errno == EILSEQ) {
+ /* restart from an empty state and return NULL */
+ iconv(cd_to_internal, NULL, NULL, NULL, NULL);
+ retval = NULL;
+ rdptr++;
+ conv_buf_size--;
+ }
+ }
/* then shift what is left over to the head of the input buffer */
memmove(conv_buf, rdptr, conv_buf_size);
memset(conv_buf + conv_buf_size, 0, sizeof(conv_buf) - conv_buf_size);
- return output_buffer;
+ return retval;
}