changeset 24851:e36efda34616

Our enca code uses strdup() on the input encoding name, as we don't modify it we can use the original constant string. Uses less memory, code is simpler and faster. Fixes memory leak (noticed by ulion).
author iive
date Sun, 28 Oct 2007 14:26:05 +0000
parents 54497a1e997a
children 35902b734951
files libass/ass.c subreader.c subreader.h
diffstat 3 files changed, 10 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libass/ass.c	Sat Oct 27 20:14:22 2007 +0000
+++ b/libass/ass.c	Sun Oct 28 14:26:05 2007 +0000
@@ -804,7 +804,7 @@
 	assert(codepage);
 
 	{
-		char* cp_tmp = codepage ? strdup(codepage) : 0;
+		const char* cp_tmp = codepage;
 #ifdef HAVE_ENCA
 		char enca_lang[3], enca_fallback[100];
 		if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
@@ -816,9 +816,6 @@
 			mp_msg(MSGT_ASS,MSGL_V,"LIBSUB: opened iconv descriptor.\n");
 		} else
 			mp_msg(MSGT_ASS,MSGL_ERR,MSGTR_LIBASS_ErrorOpeningIconvDescriptor);
-#ifdef HAVE_ENCA
-		if (cp_tmp) free(cp_tmp);
-#endif
 	}
 
 	{
--- a/subreader.c	Sat Oct 27 20:14:22 2007 +0000
+++ b/subreader.c	Sun Oct 28 14:26:05 2007 +0000
@@ -1083,15 +1083,13 @@
 	char *tocp = "UTF-8";
 
 	if (sub_cp){
-		char *cp_tmp = sub_cp;
+		const char *cp_tmp = sub_cp;
 #ifdef HAVE_ENCA
 		char enca_lang[3], enca_fallback[100];
-		int free_cp_tmp = 0;
 		if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
 		     || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
 		  if (st && st->flags & STREAM_SEEK ) {
 		    cp_tmp = guess_cp(st, enca_lang, enca_fallback);
-		    free_cp_tmp = 1;
 		  } else {
 		    cp_tmp = enca_fallback;
 		    if (st)
@@ -1104,9 +1102,6 @@
 			sub_utf8 = 2;
 		} else
 			mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
-#ifdef HAVE_ENCA
-		if (free_cp_tmp && cp_tmp) free(cp_tmp);
-#endif
 	}
 }
 
@@ -1275,13 +1270,13 @@
 };
 
 #ifdef HAVE_ENCA
-void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, char *fallback)
+const char* guess_buffer_cp(unsigned char* buffer, int buflen,  char *preferred_language, const char *fallback)
 {
     const char **languages;
     size_t langcnt;
     EncaAnalyser analyser;
     EncaEncoding encoding;
-    char *detected_sub_cp = NULL;
+    const char *detected_sub_cp = NULL;
     int i;
 
     languages = enca_get_languages(&langcnt);
@@ -1299,7 +1294,7 @@
 	encoding = enca_analyse_const(analyser, buffer, buflen);
 	tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
 	if (tmp && encoding.charset != ENCA_CS_UNKNOWN) {
-	    detected_sub_cp = strdup(tmp);
+	    detected_sub_cp = tmp;
 	    mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", tmp);
 	}
 	enca_analyser_free(analyser);
@@ -1308,7 +1303,7 @@
     free(languages);
 
     if (!detected_sub_cp) {
-	detected_sub_cp = strdup(fallback);
+	detected_sub_cp = fallback;
 	mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detection failed: fallback to %s\n", fallback);
     }
 
@@ -1316,11 +1311,11 @@
 }
 
 #define MAX_GUESS_BUFFER_SIZE (256*1024)
-void* guess_cp(stream_t *st, char *preferred_language, char *fallback)
+const char* guess_cp(stream_t *st, char *preferred_language, const char *fallback)
 {
     size_t buflen;
     unsigned char *buffer;
-    char *detected_sub_cp = NULL;
+    const char *detected_sub_cp = NULL;
 
     buffer = malloc(MAX_GUESS_BUFFER_SIZE);
     buflen = stream_read(st,buffer, MAX_GUESS_BUFFER_SIZE);
--- a/subreader.h	Sat Oct 27 20:14:22 2007 +0000
+++ b/subreader.h	Sun Oct 28 14:26:05 2007 +0000
@@ -74,8 +74,8 @@
 void subcp_open (struct stream_st *st); /* for demux_ogg.c */
 void subcp_close (void); /* for demux_ogg.c */
 #ifdef HAVE_ENCA
-void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, char *fallback);
-void* guess_cp(struct stream_st *st, char *preferred_language, char *fallback);
+const char* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, const char *fallback);
+const char* guess_cp(struct stream_st *st, char *preferred_language, const char *fallback);
 #endif
 char ** sub_filenames(const char *path, char *fname);
 void list_sub_file(sub_data* subd);