1
0
mirror of https://github.com/mpv-player/mpv synced 2025-04-04 23:40:47 +00:00

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).


git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@24879 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
iive 2007-10-28 14:26:05 +00:00
parent 41951932f9
commit b43c16f8b6
3 changed files with 10 additions and 18 deletions

View File

@ -804,7 +804,7 @@ static char* sub_recode(char* data, size_t size, char* codepage)
assert(codepage); assert(codepage);
{ {
char* cp_tmp = codepage ? strdup(codepage) : 0; const char* cp_tmp = codepage;
#ifdef HAVE_ENCA #ifdef HAVE_ENCA
char enca_lang[3], enca_fallback[100]; char enca_lang[3], enca_fallback[100];
if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2 if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
@ -816,9 +816,6 @@ static char* sub_recode(char* data, size_t size, char* codepage)
mp_msg(MSGT_ASS,MSGL_V,"LIBSUB: opened iconv descriptor.\n"); mp_msg(MSGT_ASS,MSGL_V,"LIBSUB: opened iconv descriptor.\n");
} else } else
mp_msg(MSGT_ASS,MSGL_ERR,MSGTR_LIBASS_ErrorOpeningIconvDescriptor); mp_msg(MSGT_ASS,MSGL_ERR,MSGTR_LIBASS_ErrorOpeningIconvDescriptor);
#ifdef HAVE_ENCA
if (cp_tmp) free(cp_tmp);
#endif
} }
{ {

View File

@ -1083,15 +1083,13 @@ void subcp_open (stream_t *st)
char *tocp = "UTF-8"; char *tocp = "UTF-8";
if (sub_cp){ if (sub_cp){
char *cp_tmp = sub_cp; const char *cp_tmp = sub_cp;
#ifdef HAVE_ENCA #ifdef HAVE_ENCA
char enca_lang[3], enca_fallback[100]; char enca_lang[3], enca_fallback[100];
int free_cp_tmp = 0;
if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
|| 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 ) { if (st && st->flags & STREAM_SEEK ) {
cp_tmp = guess_cp(st, enca_lang, enca_fallback); cp_tmp = guess_cp(st, enca_lang, enca_fallback);
free_cp_tmp = 1;
} else { } else {
cp_tmp = enca_fallback; cp_tmp = enca_fallback;
if (st) if (st)
@ -1104,9 +1102,6 @@ void subcp_open (stream_t *st)
sub_utf8 = 2; sub_utf8 = 2;
} else } else
mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n"); 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 @@ struct subreader {
}; };
#ifdef HAVE_ENCA #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; const char **languages;
size_t langcnt; size_t langcnt;
EncaAnalyser analyser; EncaAnalyser analyser;
EncaEncoding encoding; EncaEncoding encoding;
char *detected_sub_cp = NULL; const char *detected_sub_cp = NULL;
int i; int i;
languages = enca_get_languages(&langcnt); languages = enca_get_languages(&langcnt);
@ -1299,7 +1294,7 @@ void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_languag
encoding = enca_analyse_const(analyser, buffer, buflen); encoding = enca_analyse_const(analyser, buffer, buflen);
tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV); tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
if (tmp && encoding.charset != ENCA_CS_UNKNOWN) { 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); mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", tmp);
} }
enca_analyser_free(analyser); enca_analyser_free(analyser);
@ -1308,7 +1303,7 @@ void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_languag
free(languages); free(languages);
if (!detected_sub_cp) { 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); mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detection failed: fallback to %s\n", fallback);
} }
@ -1316,11 +1311,11 @@ void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_languag
} }
#define MAX_GUESS_BUFFER_SIZE (256*1024) #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; size_t buflen;
unsigned char *buffer; unsigned char *buffer;
char *detected_sub_cp = NULL; const char *detected_sub_cp = NULL;
buffer = malloc(MAX_GUESS_BUFFER_SIZE); buffer = malloc(MAX_GUESS_BUFFER_SIZE);
buflen = stream_read(st,buffer, MAX_GUESS_BUFFER_SIZE); buflen = stream_read(st,buffer, MAX_GUESS_BUFFER_SIZE);

View File

@ -74,8 +74,8 @@ struct stream_st;
void subcp_open (struct stream_st *st); /* for demux_ogg.c */ void subcp_open (struct stream_st *st); /* for demux_ogg.c */
void subcp_close (void); /* for demux_ogg.c */ void subcp_close (void); /* for demux_ogg.c */
#ifdef HAVE_ENCA #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);
void* guess_cp(struct stream_st *st, char *preferred_language, char *fallback); const char* guess_cp(struct stream_st *st, char *preferred_language, const char *fallback);
#endif #endif
char ** sub_filenames(const char *path, char *fname); char ** sub_filenames(const char *path, char *fname);
void list_sub_file(sub_data* subd); void list_sub_file(sub_data* subd);