Added silc_strncat.
[silc.git] / lib / silcutil / silcstrutil.c
index 0e2398ae1467585f1c7293a6e790d51c0a25d2bb..b685f0557d0b10613f984d09ffcb6a928030fccb 100644 (file)
@@ -206,15 +206,15 @@ SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
       ocp = (char *)utf8;
       inlen = bin_len;
       outlen = utf8_size;
-      if (icd != (iconv_t)-1 && 
-         (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1)) {
-       utf8_size -= outlen;
-       iconv_close(icd);
-       return utf8_size;
-      } else {
-       if (icd != (iconv_t)-1)
+      if (icp && ocp && icd != (iconv_t)-1) {
+       if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
+         utf8_size -= outlen;
          iconv_close(icd);
+         return utf8_size;
+       }
       }
+      if (icd != (iconv_t)-1)
+       iconv_close(icd);
     }
 #endif
 
@@ -350,15 +350,15 @@ SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
       ocp = (char *)bin;
       inlen = utf8_len;
       outlen = bin_size;
-      if (icd != (iconv_t)-1 && 
-         (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1)) {
-       bin_size -= outlen;
-       iconv_close(icd);
-       return bin_size;
-      } else {
-       if (icd != (iconv_t)-1)
+      if (icp && ocp && icd != (iconv_t)-1) {
+       if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
+         bin_size -= outlen;
          iconv_close(icd);
+         return bin_size;
+       }
       }
+      if (icd != (iconv_t)-1)
+       iconv_close(icd);
     }
 #endif
 
@@ -574,3 +574,25 @@ silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
 
   return TRUE;
 }
+
+/* Concatenates the `src' into `dest'.  If `src_len' is more than the
+   size of the `dest' (minus NULL at the end) the `src' will be
+   truncated to fit. */
+
+char *silc_strncat(char *dest, SilcUInt32 dest_size,
+                  const char *src, SilcUInt32 src_len)
+{
+  int len;
+
+  dest[dest_size - 1] = '\0';
+
+  len = dest_size - 1 - strlen(dest);
+  if (len < src_len) {
+    if (len > 0)
+      strncat(dest, src, len);
+  } else {
+    strncat(dest, src, src_len);
+  }
+
+  return dest;
+}