Split off libgedcom.so.
[gedcom-parse.git] / encoding.c
index 1104f681999a6e2664f73e38c60cd0a4e0cf69ad..2efe7ec72071ecdf01bf02a618f8c40447bc76ca 100644 (file)
@@ -22,7 +22,6 @@
 #define MAXBUF 255
 
 static iconv_t cd_to_internal = (iconv_t) -1;
-static char int_buf[MAXGEDCLINELEN*2];
 static void *encoding_mapping = NULL;
 static ENCODING the_enc = ONE_BYTE;
 
@@ -46,6 +45,7 @@ void add_encoding(char *gedcom_n, char* charwidth, char *iconv_n)
   nodeptr->gedcom_name = (char *) malloc(strlen(gedcom_n)
                                         + strlen(charwidth) + 3);
   nodeptr->iconv_name  = (char *) malloc(strlen(iconv_n) + 1);
+  /* sprintf is safe here (malloc'ed before) */
   sprintf(nodeptr->gedcom_name, "%s(%s)", gedcom_n, charwidth);
   strcpy(nodeptr->iconv_name, iconv_n);
   datum = tsearch(nodeptr, &encoding_mapping, node_compare);
@@ -59,10 +59,13 @@ char* get_encoding(char* gedcom_n, ENCODING enc)
 {
   void **datum;
   struct node search_node;
-  char buffer[MAXBUF + 1];
+  char *buffer;
+  buffer = (char*)malloc(strlen(gedcom_n) + strlen(charwidth_string[enc]) + 3);
+  /* sprintf is safe here (malloc'ed before) */
   sprintf(buffer, "%s(%s)", gedcom_n, charwidth_string[enc]);
   search_node.gedcom_name = buffer;
   datum = tfind(&search_node, &encoding_mapping, node_compare);
+  free(buffer);
   if (datum == NULL) {
     gedcom_error("No encoding found for '%s'", gedcom_n);
     return NULL;
@@ -142,22 +145,22 @@ void close_conv_to_internal()
   cd_to_internal = (iconv_t) -1;
 }
 
-char* to_internal(char* str, size_t len)
+char* to_internal(char* str, size_t len,
+                 char* output_buffer, size_t out_len)
 {
-  size_t outsize = MAXGEDCLINELEN * 2;
-  char *wrptr = int_buf;
+  size_t outsize = out_len;
+  char *wrptr = output_buffer;
   char *rdptr = conv_buf;
   /* 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);
   conv_buf_size += len;
   /* set up output buffer (empty it) */
-  memset(int_buf, 0, sizeof(int_buf));
+  memset(output_buffer, 0, out_len);
   /* do the conversion */
   iconv(cd_to_internal, &rdptr, &conv_buf_size, &wrptr, &outsize);
   /* 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 int_buf;
+  return output_buffer;
 }
-