svn commit: samba r2903 - in branches/SAMBA_4_0/source/lib: .

tridge at samba.org tridge at samba.org
Mon Oct 11 03:26:17 GMT 2004


Author: tridge
Date: 2004-10-11 03:26:17 +0000 (Mon, 11 Oct 2004)
New Revision: 2903

WebSVN: http://websvn.samba.org/websvn/changeset.php?rep=samba&path=/branches/SAMBA_4_0/source/lib&rev=2903&nolog=1

Log:
a considerably more efficient (both in terms of CPU and memory)
convert_string_talloc() implementation.

the previous version used a minimum of 512 bytes, which is way above the average
of what is needed. 

Modified:
   branches/SAMBA_4_0/source/lib/charcnv.c


Changeset:
Modified: branches/SAMBA_4_0/source/lib/charcnv.c
===================================================================
--- branches/SAMBA_4_0/source/lib/charcnv.c	2004-10-11 02:10:45 UTC (rev 2902)
+++ branches/SAMBA_4_0/source/lib/charcnv.c	2004-10-11 03:26:17 UTC (rev 2903)
@@ -168,7 +168,7 @@
  **/
 
 ssize_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
-		      		void const *src, size_t srclen, void **dest)
+			      void const *src, size_t srclen, void **dest)
 {
 	size_t i_len, o_len, destlen;
 	size_t retval;
@@ -189,20 +189,25 @@
 		return -1;
 	}
 
-	destlen = MAX(srclen, 512);
+	/* it is _very_ rare that a conversion increases the size by
+	   more than 3x */
+	destlen = srclen;
 	outbuf = NULL;
 convert:
-	destlen = destlen * 2;
+	destlen = 2 + (destlen*3);
 	ob = (char *)talloc_realloc(ctx, outbuf, destlen);
 	if (!ob) {
 		DEBUG(0, ("convert_string_talloc: realloc failed!\n"));
 		talloc_free(outbuf);
 		return (size_t)-1;
+	} else {
+		outbuf = ob;
 	}
-	else
-		outbuf = ob;
+
+	/* we give iconv 2 less bytes to allow us to terminate at the
+	   end */
 	i_len = srclen;
-	o_len = destlen;
+	o_len = destlen-2;
 	retval = smb_iconv(descriptor,
 			   &inbuf, &i_len,
 			   &outbuf, &o_len);
@@ -220,21 +225,16 @@
 		}
 		DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
 		talloc_free(ob);
-		/* smb_panic(reason); */
 		return (size_t)-1;
 	}
 	
-	destlen = destlen - o_len;
-	/* +2 for mandetory null termination, UTF8 or UTF16 */
-	*dest = (char *)talloc_realloc(ctx, ob, destlen+2);
-	if (!*dest) {
-		DEBUG(0, ("convert_string_talloc: out of memory!\n"));
-		talloc_free(ob);
-		return (size_t)-1;
-	}
-	((char *)*dest)[destlen] = '\0';
-	((char *)*dest)[destlen+1] = '\0';
+	destlen = (destlen-2) - o_len;
 
+	/* guarantee null termination in all charsets */
+	SSVAL(ob, destlen, 0);
+
+	*dest = ob;
+
 	return destlen;
 }
 



More information about the samba-cvs mailing list