diff --git a/ChangeLog b/ChangeLog index 16a15b615..4884647c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2003-05-04 Moritz Bunkus + + * mkvinfo reports the FourCC for video tracks with a + CodecID of V_MS/VFW/FOURCC. + 2003-05-03 Moritz Bunkus * Released v0.3.1 diff --git a/ac3_common.cpp b/ac3_common.cpp index c4adb03c7..11c27ec20 100644 --- a/ac3_common.cpp +++ b/ac3_common.cpp @@ -13,12 +13,11 @@ /*! \file - \version \$Id: ac3_common.cpp,v 1.2 2003/02/16 17:04:38 mosu Exp $ + \version \$Id: ac3_common.cpp,v 1.3 2003/05/05 18:37:36 mosu Exp $ \brief helper function for AC3 data \author Moritz Bunkus */ -#include #include #include "ac3_common.h" diff --git a/cluster_helper.cpp b/cluster_helper.cpp index e1be473d8..4731e3e40 100644 --- a/cluster_helper.cpp +++ b/cluster_helper.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: cluster_helper.cpp,v 1.11 2003/05/03 20:01:44 mosu Exp $ + \version \$Id: cluster_helper.cpp,v 1.12 2003/05/05 18:37:36 mosu Exp $ \brief cluster helper \author Moritz Bunkus */ @@ -45,7 +45,7 @@ cluster_helper_c::~cluster_helper_c() { free_contents(clusters[i]); if (clusters != NULL) - free(clusters); + safefree(clusters); } void cluster_helper_c::free_contents(ch_contents_t *clstr) { @@ -60,12 +60,12 @@ void cluster_helper_c::free_contents(ch_contents_t *clstr) { for (i = 0; i < clstr->num_packets; i++) { p = clstr->packets[i]; if (p->data != NULL) - free(p->data); - free(p); + safefree(p->data); + safefree(p); } if (clstr->packets != NULL) - free(clstr->packets); - free(clstr); + safefree(clstr->packets); + safefree(clstr); } KaxCluster *cluster_helper_c::get_cluster() { @@ -81,10 +81,8 @@ void cluster_helper_c::add_packet(packet_t *packet) { return; c = clusters[num_clusters - 1]; - c->packets = (packet_t **)realloc(c->packets, sizeof(packet_t *) * - (c->num_packets + 1)); - if (c->packets == NULL) - die("realloc"); + c->packets = (packet_t **)saferealloc(c->packets, sizeof(packet_t *) * + (c->num_packets + 1)); c->packets[c->num_packets] = packet; c->num_packets++; @@ -139,13 +137,9 @@ void cluster_helper_c::add_cluster(KaxCluster *cluster) { if (find_cluster(cluster) != -1) return; - c = (ch_contents_t *)malloc(sizeof(ch_contents_t)); - if (c == NULL) - die("malloc"); - clusters = (ch_contents_t **)realloc(clusters, sizeof(ch_contents_t *) * - (num_clusters + 1)); - if (clusters == NULL) - die("realloc"); + c = (ch_contents_t *)safemalloc(sizeof(ch_contents_t)); + clusters = (ch_contents_t **)saferealloc(clusters, sizeof(ch_contents_t *) * + (num_clusters + 1)); memset(c, 0, sizeof(ch_contents_t)); clusters[num_clusters] = c; num_clusters++; @@ -231,7 +225,7 @@ int cluster_helper_c::render(IOCallback *out) { for (i = 0; i < clstr->num_packets; i++) { pack = clstr->packets[i]; - free(pack->data); + safefree(pack->data); pack->data = NULL; } @@ -353,13 +347,11 @@ int cluster_helper_c::free_clusters() { // Part 4 - prune the cluster list and remove all the entries freed in // part 3. if (k == 0) { - free(clusters); + safefree(clusters); num_clusters = 0; add_cluster(new KaxCluster()); } else if (k != num_clusters) { - new_clusters = (ch_contents_t **)malloc(sizeof(ch_contents_t *) * k); - if (new_clusters == NULL) - die("malloc"); + new_clusters = (ch_contents_t **)safemalloc(sizeof(ch_contents_t *) * k); idx = 0; for (i = 0; i < num_clusters; i++) @@ -368,7 +360,7 @@ int cluster_helper_c::free_clusters() { idx++; } - free(clusters); + safefree(clusters); clusters = new_clusters; num_clusters = k; } diff --git a/common.cpp b/common.cpp index 2c54775c4..3c18ec457 100644 --- a/common.cpp +++ b/common.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: common.cpp,v 1.14 2003/05/05 14:57:45 mosu Exp $ + \version \$Id: common.cpp,v 1.15 2003/05/05 18:37:36 mosu Exp $ \brief helper functions, common variables \author Moritz Bunkus */ @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -90,14 +91,10 @@ int add_mkv_conv(const char *charset, iconv_t ict_from, iconv_t ict_to) { if (!strcmp(mkv_convs[i].charset, charset)) return i; - mkv_convs = (mkv_conv_t *)realloc(mkv_convs, (num_mkv_convs + 1) * - sizeof(mkv_conv_t)); - if (mkv_convs == NULL) - die("realloc"); + mkv_convs = (mkv_conv_t *)saferealloc(mkv_convs, (num_mkv_convs + 1) * + sizeof(mkv_conv_t)); c = &mkv_convs[num_mkv_convs]; - c->charset = strdup(charset); - if (c->charset == NULL) - die("strdup"); + c->charset = safestrdup(charset); c->ict_from_utf8 = ict_from; c->ict_to_utf8 = ict_to; num_mkv_convs++; @@ -145,9 +142,9 @@ void utf8_done() { int i; for (i = 0; i < num_mkv_convs; i++) - free(mkv_convs[i].charset); + safefree(mkv_convs[i].charset); if (mkv_convs != NULL) - free(mkv_convs); + safefree(mkv_convs); } static char *convert_charset(iconv_t ict, char *src) { @@ -156,9 +153,7 @@ static char *convert_charset(iconv_t ict, char *src) { int len; len = strlen(src) * 4; - dst = (char *)malloc(len + 1); - if (dst == NULL) - die("malloc"); + dst = (char *)safemalloc(len + 1); memset(dst, 0, len + 1); iconv(ict, NULL, 0, NULL, 0); // Reset the iconv state. @@ -166,7 +161,7 @@ static char *convert_charset(iconv_t ict, char *src) { ldst = len; psrc = src; pdst = dst; - fprintf(stdout, "ic: %d\n", iconv(ict, &psrc, &lsrc, &pdst, &ldst)); + iconv(ict, &psrc, &lsrc, &pdst, &ldst); return dst; } @@ -175,9 +170,7 @@ char *to_utf8(int handle, char *local) { char *copy; if (handle == -1) { - copy = strdup(local); - if (copy == NULL) - die("strdup"); + copy = safestrdup(local); return copy; } @@ -191,9 +184,7 @@ char *from_utf8(int handle, char *utf8) { char *copy; if (handle == -1) { - copy = strdup(utf8); - if (copy == NULL) - die("strdup"); + copy = safestrdup(utf8); return copy; } @@ -221,10 +212,8 @@ int is_unique_uint32(uint32_t number) { } void add_unique_uint32(uint32_t number) { - ru_numbers = (uint32_t *)realloc(ru_numbers, (num_ru_numbers + 1) * - sizeof(uint32_t)); - if (ru_numbers == NULL) - die("realloc"); + ru_numbers = (uint32_t *)saferealloc(ru_numbers, (num_ru_numbers + 1) * + sizeof(uint32_t)); ru_numbers[num_ru_numbers] = number; num_ru_numbers++; @@ -243,3 +232,81 @@ uint32_t create_unique_uint32() { return rnumber; } + +/* + * Miscellaneous stuff + */ + +char *_safestrdup(const char *s, const char *file, int line) { + char *copy; + + if (s == NULL) + return NULL; + + copy = strdup(s); + if (copy == NULL) { + fprintf(stderr, "die @ %s/%d : in safestrdup: strdup == NULL\n", file, + line); + exit(1); + } + + return copy; +} + +unsigned char *_safestrdup(const unsigned char *s, const char *file, + int line) { + char *copy; + + if (s == NULL) + return NULL; + + copy = strdup((const char *)s); + if (copy == NULL) { + fprintf(stderr, "die @ %s/%d : in safestrdup: strdup == NULL\n", file, + line); + exit(1); + } + + return (unsigned char *)copy; +} + +void *_safememdup(const void *s, size_t size, const char *file, int line) { + void *copy; + + if (s == NULL) + return NULL; + + copy = malloc(size); + if (copy == NULL) { + fprintf(stderr, "die @ %s/%d : in safememdup: malloc == NULL\n", file, + line); + exit(1); + } + memcpy(copy, s, size); + + return copy; +} + +void *_safemalloc(size_t size, const char *file, int line) { + void *mem; + + mem = malloc(size); + if (mem == NULL) { + fprintf(stderr, "die @ %s/%d : in safemalloc: malloc == NULL\n", file, + line); + exit(1); + } + + return mem; +} + +void *_saferealloc(void *mem, size_t size, const char *file, int line) { + mem = realloc(mem, size); + if (mem == NULL) { + fprintf(stderr, "die @ %s/%d : in safemalloc: realloc == NULL\n", file, + line); + exit(1); + } + + return mem; +} diff --git a/common.h b/common.h index 54c82b3ff..f802e57b8 100644 --- a/common.h +++ b/common.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: common.h,v 1.16 2003/05/05 14:57:45 mosu Exp $ + \version \$Id: common.h,v 1.17 2003/05/05 18:37:36 mosu Exp $ \brief definitions used in all programs, helper functions \author Moritz Bunkus */ @@ -89,6 +89,17 @@ int is_unique_uint32(uint32_t number); void add_unique_uint32(uint32_t number); uint32_t create_unique_uint32(); +#define safefree(p) if ((p) != NULL) free(p); +#define safemalloc(s) _safemalloc(s, __FILE__, __LINE__) +void *_safemalloc(size_t size, const char *file, int line); +#define safestrdup(s) _safestrdup(s, __FILE__, __LINE__) +char *_safestrdup(const char *s, const char *file, int line); +unsigned char *_safestrdup(const unsigned char *s, const char *file, int line); +#define safememdup(src, size) _safememdup(src, size, __FILE__, __LINE__) +void *_safememdup(const void *src, size_t size, const char *file, int line); +#define saferealloc(mem, size) _saferealloc(mem, size, __FILE__, __LINE__) +void *_saferealloc(void *mem, size_t size, const char *file, int line); + extern int verbose; #endif // __COMMON_H diff --git a/mkvinfo.cpp b/mkvinfo.cpp index 301ec465e..ebbd6300e 100644 --- a/mkvinfo.cpp +++ b/mkvinfo.cpp @@ -12,7 +12,7 @@ /*! \file - \version \$Id: mkvinfo.cpp,v 1.32 2003/05/04 18:31:24 mosu Exp $ + \version \$Id: mkvinfo.cpp,v 1.33 2003/05/05 18:37:36 mosu Exp $ \brief retrieves and displays information about a Matroska file \author Moritz Bunkus */ @@ -95,9 +95,8 @@ int num_tracks = 0; StdIOCallback *in = NULL; void add_track(track_t *s) { - tracks = (track_t **)realloc(tracks, sizeof(track_t *) * (num_tracks + 1)); - if (tracks == NULL) - die("realloc"); + tracks = (track_t **)saferealloc(tracks, sizeof(track_t *) * + (num_tracks + 1)); tracks[num_tracks] = s; num_tracks++; } diff --git a/mkvmerge.cpp b/mkvmerge.cpp index 3cdc9ac06..e59f7fb81 100644 --- a/mkvmerge.cpp +++ b/mkvmerge.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: mkvmerge.cpp,v 1.54 2003/05/05 14:57:45 mosu Exp $ + \version \$Id: mkvmerge.cpp,v 1.55 2003/05/05 18:37:36 mosu Exp $ \brief command line parameter parsing, looping, output handling \author Moritz Bunkus */ @@ -192,6 +192,8 @@ static void usage(void) { "\n Options that only apply to text subtitle tracks:\n" " --no-utf8-subs Outputs text subtitles unmodified and do not\n" " convert the text to UTF-8.\n" + " --sub-charset Sets the charset the text subtitles are\n" + " written in for the conversion to UTF-8.\n" "\n\n Other options:\n" " -l, --list-types Lists supported input file types.\n" " --list-languages Lists all ISO639 languages and their\n" @@ -301,9 +303,7 @@ static unsigned char *parse_tracks(char *s) { n); exit(1); } - tracks = (unsigned char *)realloc(tracks, ntracks + 2); - if (tracks == NULL) - die("malloc"); + tracks = (unsigned char *)saferealloc(tracks, ntracks + 2); tracks[ntracks] = (unsigned char)n; tracks[ntracks + 1] = 0; nstart = NULL; @@ -325,9 +325,7 @@ static unsigned char *parse_tracks(char *s) { n); exit(1); } - tracks = (unsigned char *)realloc(tracks, ntracks + 2); - if (tracks == NULL) - die("malloc"); + tracks = (unsigned char *)saferealloc(tracks, ntracks + 2); tracks[ntracks] = (unsigned char)n; tracks[ntracks + 1] = 0; nstart = NULL; @@ -491,9 +489,7 @@ static void parse_args(int argc, char **argv) { fprintf(stderr, "Error: only one output file allowed.\n"); exit(1); } - outfile = strdup(argv[i + 1]); - if (outfile == NULL) - die("strdup"); + outfile = safestrdup(argv[i + 1]); i++; } else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list-types")) { fprintf(stdout, "Known file types:\n ext description\n" \ @@ -579,7 +575,7 @@ static void parse_args(int argc, char **argv) { exit(1); } if (ti.atracks != NULL) - free(ti.atracks); + safefree(ti.atracks); ti.atracks = parse_tracks(argv[i + 1]); i++; } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--vtracks")) { @@ -588,7 +584,7 @@ static void parse_args(int argc, char **argv) { exit(1); } if (ti.vtracks != NULL) - free(ti.vtracks); + safefree(ti.vtracks); ti.vtracks = parse_tracks(argv[i + 1]); i++; } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--stracks")) { @@ -597,7 +593,7 @@ static void parse_args(int argc, char **argv) { exit(1); } if (ti.stracks != NULL) - free(ti.stracks); + safefree(ti.stracks); ti.stracks = parse_tracks(argv[i + 1]); i++; } else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--fourcc")) { @@ -654,6 +650,14 @@ static void parse_args(int argc, char **argv) { ti.language = argv[i + 1]; i++; + } else if (!strcmp(argv[i], "--sub-charset")) { + if ((i + 1) >= argc) { + fprintf(stderr, "Error: --sub-charset lacks its argument.\n"); + exit(1); + } + + ti.sub_charset = argv[i + 1]; + i++; } // The argument is an input file. @@ -670,27 +674,13 @@ static void parse_args(int argc, char **argv) { fprintf(stderr, "Error: -S and -s used on the same source file.\n"); exit(1); } - if (noaudio) { - ti.atracks = (unsigned char *)malloc(1); - if (ti.atracks == NULL) - die("malloc"); - *ti.atracks = 0; - } - if (novideo) { - ti.vtracks = (unsigned char *)malloc(1); - if (ti.vtracks == NULL) - die("malloc"); - *ti.vtracks = 0; - } - if (nosubs) { - ti.stracks = (unsigned char *)malloc(1); - if (ti.stracks == NULL) - die("malloc"); - *ti.stracks = 0; - } - file = (filelist_t *)malloc(sizeof(filelist_t)); - if (file == NULL) - die("malloc"); + if (noaudio) + ti.atracks = (unsigned char *)safestrdup(""); + if (novideo) + ti.vtracks = (unsigned char *)safestrdup(""); + if (nosubs) + ti.stracks = (unsigned char *)safestrdup(""); + file = (filelist_t *)safemalloc(sizeof(filelist_t)); file->name = argv[i]; file->type = get_type(file->name); @@ -786,17 +776,17 @@ static void parse_args(int argc, char **argv) { add_file(file); } else - free(file); + safefree(file); noaudio = 0; novideo = 0; nosubs = 0; if (ti.atracks != NULL) - free(ti.atracks); + safefree(ti.atracks); if (ti.vtracks != NULL) - free(ti.vtracks); + safefree(ti.vtracks); if (ti.stracks != NULL) - free(ti.stracks); + safefree(ti.stracks); memset(&ti, 0, sizeof(track_info_t)); ti.async.linear = 1.0; ti.cues = CUES_UNSPECIFIED; @@ -814,18 +804,14 @@ static void parse_args(int argc, char **argv) { file = file->next; } vorbis_comment_clear(chapters); - free(chapters); + safefree(chapters); chapters = NULL; }*/ } static char **add_string(int &num, char **values, char *new_string) { - values = (char **)realloc(values, (num + 1) * sizeof(char *)); - if (values == NULL) - die("realloc"); - values[num] = strdup(new_string); - if (values[num] == NULL) - die("strdup"); + values = (char **)saferealloc(values, (num + 1) * sizeof(char *)); + values[num] = safestrdup(new_string); num++; return values; @@ -899,9 +885,9 @@ static void handle_args(int argc, char **argv) { parse_args(num_args, args); for (i = 0; i < num_args; i++) - free(args[i]); + safefree(args[i]); if (args != NULL) - free(args); + safefree(args); } static int write_packet(packet_t *pack) { @@ -1041,7 +1027,7 @@ int main(int argc, char **argv) { filelist_t *next = file->next; if (file->reader) delete file->reader; - free(file); + safefree(file); file = next; } diff --git a/p_ac3.cpp b/p_ac3.cpp index 15c023504..c89b1a6a5 100644 --- a/p_ac3.cpp +++ b/p_ac3.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: p_ac3.cpp,v 1.17 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: p_ac3.cpp,v 1.18 2003/05/05 18:37:36 mosu Exp $ \brief AC3 output module \author Moritz Bunkus */ @@ -43,15 +43,13 @@ ac3_packetizer_c::ac3_packetizer_c(unsigned long nsamples_per_sec, ac3_packetizer_c::~ac3_packetizer_c() { if (packet_buffer != NULL) - free(packet_buffer); + safefree(packet_buffer); } void ac3_packetizer_c::add_to_buffer(unsigned char *buf, int size) { unsigned char *new_buffer; - new_buffer = (unsigned char *)realloc(packet_buffer, buffer_size + size); - if (new_buffer == NULL) - die("realloc"); + new_buffer = (unsigned char *)saferealloc(packet_buffer, buffer_size + size); memcpy(new_buffer + buffer_size, buf, size); packet_buffer = new_buffer; @@ -76,15 +74,12 @@ void ac3_packetizer_c::remove_ac3_packet(int pos, int framesize) { unsigned char *temp_buf; new_size = buffer_size - (pos + framesize); - if (new_size != 0) { - temp_buf = (unsigned char *)malloc(new_size); - if (temp_buf == NULL) - die("malloc"); - memcpy(temp_buf, &packet_buffer[pos + framesize], - new_size); - } else + if (new_size != 0) + temp_buf = (unsigned char *)safememdup(&packet_buffer[pos + framesize], + new_size); + else temp_buf = NULL; - free(packet_buffer); + safefree(packet_buffer); packet_buffer = temp_buf; buffer_size = new_size; } @@ -124,10 +119,7 @@ unsigned char *ac3_packetizer_c::get_ac3_packet(unsigned long *header, fprintf(stdout, "ac3_packetizer: skipping %d bytes (no valid AC3 header " "found). This might make audio/video go out of sync, but this " "stream is damaged.\n", pos); - buf = (unsigned char *)malloc(ac3header->bytes); - if (buf == NULL) - die("malloc"); - memcpy(buf, packet_buffer + pos, ac3header->bytes); + buf = (unsigned char *)safememdup(packet_buffer + pos, ac3header->bytes); if (ti->async.displacement > 0) { /* @@ -180,7 +172,7 @@ int ac3_packetizer_c::process(unsigned char *buf, int size, add_packet(packet, ac3header.bytes, my_timecode); packetno++; - free(packet); + safefree(packet); } return EMOREDATA; diff --git a/p_mp3.cpp b/p_mp3.cpp index 6a5de1614..983893e48 100644 --- a/p_mp3.cpp +++ b/p_mp3.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: p_mp3.cpp,v 1.20 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: p_mp3.cpp,v 1.21 2003/05/05 18:37:36 mosu Exp $ \brief MP3 output module \author Moritz Bunkus */ @@ -43,15 +43,13 @@ mp3_packetizer_c::mp3_packetizer_c(unsigned long nsamples_per_sec, mp3_packetizer_c::~mp3_packetizer_c() { if (packet_buffer != NULL) - free(packet_buffer); + safefree(packet_buffer); } void mp3_packetizer_c::add_to_buffer(unsigned char *buf, int size) { unsigned char *new_buffer; - new_buffer = (unsigned char *)realloc(packet_buffer, buffer_size + size); - if (new_buffer == NULL) - die("realloc"); + new_buffer = (unsigned char *)saferealloc(packet_buffer, buffer_size + size); memcpy(new_buffer + buffer_size, buf, size); packet_buffer = new_buffer; @@ -80,12 +78,10 @@ void mp3_packetizer_c::remove_mp3_packet(int pos, int framesize) { unsigned char *temp_buf; new_size = buffer_size - (pos + framesize + 4) + 1; - temp_buf = (unsigned char *)malloc(new_size); - if (temp_buf == NULL) - die("malloc"); + temp_buf = (unsigned char *)safemalloc(new_size); if (new_size != 0) memcpy(temp_buf, &packet_buffer[pos + framesize + 4 - 1], new_size); - free(packet_buffer); + safefree(packet_buffer); packet_buffer = temp_buf; buffer_size = new_size; } @@ -124,10 +120,8 @@ unsigned char *mp3_packetizer_c::get_mp3_packet(unsigned long *header, if ((verbose > 1) && (pos > 1)) fprintf(stdout, "mp3_packetizer: skipping %d bytes (no valid MP3 header " "found).\n", pos); - buf = (unsigned char *)malloc(mp3header->framesize + 4); - if (buf == NULL) - die("malloc"); - memcpy(buf, packet_buffer + pos, mp3header->framesize + 4); + buf = (unsigned char *)safememdup(packet_buffer + pos, mp3header->framesize + + 4); if (ti->async.displacement > 0) { /* @@ -177,7 +171,7 @@ int mp3_packetizer_c::process(unsigned char *buf, int size, if ((4 - ((header >> 17) & 3)) != 3) { fprintf(stdout, "Warning: p_mp3: packet is not a valid MP3 packet (" \ "packet number %lld)\n", packetno); - free(packet); + safefree(packet); packetno++; return EMOREDATA; } @@ -188,7 +182,7 @@ int mp3_packetizer_c::process(unsigned char *buf, int size, add_packet(packet, mp3header.framesize + 4, my_timecode); packetno++; - free(packet); + safefree(packet); } return EMOREDATA; diff --git a/p_pcm.cpp b/p_pcm.cpp index de980e563..881a55ec7 100644 --- a/p_pcm.cpp +++ b/p_pcm.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: p_pcm.cpp,v 1.19 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: p_pcm.cpp,v 1.20 2003/05/05 18:37:36 mosu Exp $ \brief PCM output module \author Moritz Bunkus */ @@ -37,9 +37,7 @@ pcm_packetizer_c::pcm_packetizer_c(unsigned long nsamples_per_sec, generic_packetizer_c(nti) { packetno = 0; bps = nchannels * nbits_per_sample * nsamples_per_sec / 8; - tempbuf = (unsigned char *)malloc(bps + 128); - if (tempbuf == NULL) - die("malloc"); + tempbuf = (unsigned char *)safemalloc(bps + 128); tempbuf_size = bps; samples_per_sec = nsamples_per_sec; channels = nchannels; @@ -50,7 +48,7 @@ pcm_packetizer_c::pcm_packetizer_c(unsigned long nsamples_per_sec, pcm_packetizer_c::~pcm_packetizer_c() { if (tempbuf != NULL) - free(tempbuf); + safefree(tempbuf); } void pcm_packetizer_c::set_headers() { @@ -73,9 +71,7 @@ int pcm_packetizer_c::process(unsigned char *buf, int size, unsigned char *new_buf; if (size > tempbuf_size) { - tempbuf = (unsigned char *)realloc(tempbuf, size + 128); - if (tempbuf == NULL) - die("realloc"); + tempbuf = (unsigned char *)saferealloc(tempbuf, size + 128); tempbuf_size = size; } @@ -87,9 +83,7 @@ int pcm_packetizer_c::process(unsigned char *buf, int size, int pad_size; pad_size = bps * ti->async.displacement / 1000; - new_buf = (unsigned char *)malloc(size + pad_size); - if (new_buf == NULL) - die("malloc"); + new_buf = (unsigned char *)safemalloc(size + pad_size); memset(new_buf, 0, pad_size); memcpy(&new_buf[pad_size], buf, size); size += pad_size; @@ -127,7 +121,7 @@ int pcm_packetizer_c::process(unsigned char *buf, int size, } if (new_buf != buf) - free(new_buf); + safefree(new_buf); return EMOREDATA; } diff --git a/p_textsubs.cpp b/p_textsubs.cpp index 415b84d95..c825fd6fb 100644 --- a/p_textsubs.cpp +++ b/p_textsubs.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: p_textsubs.cpp,v 1.13 2003/05/05 14:57:45 mosu Exp $ + \version \$Id: p_textsubs.cpp,v 1.14 2003/05/05 18:37:36 mosu Exp $ \brief Subripper subtitle reader \author Moritz Bunkus */ @@ -87,9 +87,7 @@ int textsubs_packetizer_c::process(unsigned char *_subs, int, int64_t start, num_newlines++; idx1++; } - subs = (char *)malloc(strlen((char *)_subs) + num_newlines * 2 + 1); - if (subs == NULL) - die("malloc"); + subs = (char *)safemalloc(strlen((char *)_subs) + num_newlines * 2 + 1); // Unify the new lines into DOS style newlines. idx1 = (char *)_subs; @@ -119,11 +117,11 @@ int textsubs_packetizer_c::process(unsigned char *_subs, int, int64_t start, char *utf8_subs = to_utf8(cc_local_utf8, subs); add_packet((unsigned char *)utf8_subs, strlen(utf8_subs), start, -1, -1, length); - free(utf8_subs); + safefree(utf8_subs); } else add_packet((unsigned char *)subs, strlen(subs), start, -1, -1, length); - free(subs); + safefree(subs); return EMOREDATA; } diff --git a/p_vorbis.cpp b/p_vorbis.cpp index af1431e40..7738de880 100644 --- a/p_vorbis.cpp +++ b/p_vorbis.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: p_vorbis.cpp,v 1.17 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: p_vorbis.cpp,v 1.18 2003/05/05 18:37:36 mosu Exp $ \brief Vorbis packetizer \author Moritz Bunkus */ @@ -50,15 +50,9 @@ vorbis_packetizer_c::vorbis_packetizer_c(unsigned char *d_header, int l_header, last_bs = 0; samples = 0; memset(headers, 0, 3 * sizeof(ogg_packet)); - headers[0].packet = (unsigned char *)malloc(l_header); - headers[1].packet = (unsigned char *)malloc(l_comments); - headers[2].packet = (unsigned char *)malloc(l_codecsetup); - if ((headers[0].packet == NULL) || (headers[1].packet == NULL) || - (headers[2].packet == NULL)) - die("malloc"); - memcpy(headers[0].packet, d_header, l_header); - memcpy(headers[1].packet, d_comments, l_comments); - memcpy(headers[2].packet, d_codecsetup, l_codecsetup); + headers[0].packet = (unsigned char *)safememdup(d_header, l_header); + headers[1].packet = (unsigned char *)safememdup(d_comments, l_comments); + headers[2].packet = (unsigned char *)safememdup(d_codecsetup, l_codecsetup); headers[0].bytes = l_header; headers[1].bytes = l_comments; headers[2].bytes = l_codecsetup; @@ -78,7 +72,7 @@ vorbis_packetizer_c::~vorbis_packetizer_c() { for (i = 0; i < 3; i++) if (headers[i].packet != NULL) - free(headers[i].packet); + safefree(headers[i].packet); } void vorbis_packetizer_c::set_headers() { @@ -101,9 +95,7 @@ void vorbis_packetizer_c::set_headers() { // and that's why the first byte is (num_packets - 1). lsize = 1 + (headers[0].bytes / 255) + 1 + (headers[1].bytes / 255) + 1 + headers[0].bytes + headers[1].bytes + headers[2].bytes; - buffer = (unsigned char *)malloc(lsize); - if (buffer == NULL) - die("malloc"); + buffer = (unsigned char *)safemalloc(lsize); buffer[0] = 2; // The number of packets less one. offset = 1; @@ -122,7 +114,7 @@ void vorbis_packetizer_c::set_headers() { set_codec_private(buffer, lsize); - free(buffer); + safefree(buffer); set_audio_sampling_freq((float)vi.rate); set_audio_channels(vi.channels); diff --git a/pr_generic.cpp b/pr_generic.cpp index 1f013b89b..b76c05870 100644 --- a/pr_generic.cpp +++ b/pr_generic.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: pr_generic.cpp,v 1.32 2003/05/03 20:22:18 mosu Exp $ + \version \$Id: pr_generic.cpp,v 1.33 2003/05/05 18:37:36 mosu Exp $ \brief functions common for all readers/packetizers \author Moritz Bunkus */ @@ -63,9 +63,9 @@ generic_packetizer_c::~generic_packetizer_c() { free_track_info(ti); if (hcodec_id != NULL) - free(hcodec_id); + safefree(hcodec_id); if (hcodec_private != NULL) - free(hcodec_private); + safefree(hcodec_private); } void generic_packetizer_c::set_cue_creation(int ncreate_cue_data) { @@ -107,29 +107,22 @@ void generic_packetizer_c::set_track_type(int type) { } void generic_packetizer_c::set_codec_id(char *id) { - if (hcodec_id != NULL) - free(hcodec_id); + safefree(hcodec_id); if (id == NULL) { hcodec_id = NULL; return; } - hcodec_id = strdup(id); - if (hcodec_id == NULL) - die("malloc"); + hcodec_id = safestrdup(id); } void generic_packetizer_c::set_codec_private(unsigned char *cp, int length) { - if (hcodec_private != NULL) - free(hcodec_private); + safefree(hcodec_private); if (cp == NULL) { hcodec_private = NULL; hcodec_private_length = 0; return; } - hcodec_private = (unsigned char *)malloc(length); - if (hcodec_private == NULL) - die("malloc"); - memcpy(hcodec_private, cp, length); + hcodec_private = (unsigned char *)safememdup(cp, length); hcodec_private_length = length; } @@ -186,11 +179,8 @@ void generic_packetizer_c::set_as_default_track(char type) { } void generic_packetizer_c::set_language(char *language) { - if (ti->language != NULL) - free(ti->language); - ti->language = strdup(language); - if (ti->language == NULL) - die("strdup"); + safefree(ti->language); + ti->language = safestrdup(language); } void generic_packetizer_c::set_headers() { @@ -315,42 +305,15 @@ track_info_t *duplicate_track_info(track_info_t *src) { if (src == NULL) return NULL; - dst = (track_info_t *)malloc(sizeof(track_info_t)); - if (dst == NULL) - die("malloc"); - - memcpy(dst, src, sizeof(track_info_t)); - if (src->fname != NULL) { - dst->fname = strdup(src->fname); - if (dst->fname == NULL) - die("strdup"); - } - if (src->atracks != NULL) { - dst->atracks = (unsigned char *)strdup((char *)src->atracks); - if (dst->atracks == NULL) - die("strdup"); - } - if (src->vtracks != NULL) { - dst->vtracks = (unsigned char *)strdup((char *)src->vtracks); - if (dst->vtracks == NULL) - die("strdup"); - } - if (src->stracks != NULL) { - dst->stracks = (unsigned char *)strdup((char *)src->stracks); - if (dst->stracks == NULL) - die("strdup"); - } - if (src->private_data != NULL) { - dst->private_data = (unsigned char *)malloc(src->private_size); - if (dst->private_data == NULL) - die("malloc"); - memcpy(dst->private_data, src->private_data, src->private_size); - } - if (src->language != NULL) { - dst->language = strdup(src->language); - if (dst->language == NULL) - die("strdup"); - } + dst = (track_info_t *)safememdup(src, sizeof(track_info_t)); + dst->fname = safestrdup(src->fname); + dst->atracks = safestrdup(src->atracks); + dst->vtracks = safestrdup(src->vtracks); + dst->stracks = safestrdup(src->stracks); + dst->private_data = (unsigned char *)safememdup(src->private_data, + src->private_size); + dst->language = safestrdup(src->language); + dst->sub_charset = safestrdup(src->sub_charset); return dst; } @@ -359,19 +322,12 @@ void free_track_info(track_info_t *ti) { if (ti == NULL) return; - if (ti->fname != NULL) - free(ti->fname); - if (ti->atracks != NULL) - free(ti->atracks); - if (ti->vtracks != NULL) - free(ti->vtracks); - if (ti->stracks != NULL) - free(ti->stracks); - if (ti->private_data != NULL) - free(ti->private_data); - if (ti->language != NULL) - free(ti->language); - - free(ti); + safefree(ti->fname); + safefree(ti->atracks); + safefree(ti->vtracks); + safefree(ti->stracks); + safefree(ti->private_data); + safefree(ti->language); + safefree(ti->sub_charset); + safefree(ti); } - diff --git a/pr_generic.h b/pr_generic.h index 18c98d994..97cb4c4d3 100644 --- a/pr_generic.h +++ b/pr_generic.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: pr_generic.h,v 1.33 2003/05/03 20:22:18 mosu Exp $ + \version \$Id: pr_generic.h,v 1.34 2003/05/05 18:37:36 mosu Exp $ \brief class definition for the generic reader and packetizer \author Moritz Bunkus */ @@ -57,7 +57,7 @@ typedef struct { int default_track; - char *language; + char *language, *sub_charset; } track_info_t; class generic_packetizer_c: public q_c { diff --git a/queue.cpp b/queue.cpp index 8f580df85..2200afad3 100644 --- a/queue.cpp +++ b/queue.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: queue.cpp,v 1.18 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: queue.cpp,v 1.19 2003/05/05 18:37:36 mosu Exp $ \brief packet queueing class used by every packetizer \author Moritz Bunkus */ @@ -39,11 +39,11 @@ q_c::~q_c() { while (qpage) { if (qpage->pack != NULL) { if (qpage->pack->data != NULL) - free(qpage->pack->data); - free(qpage->pack); + safefree(qpage->pack->data); + safefree(qpage->pack); } tmppage = qpage->next; - free(qpage); + safefree(qpage); qpage = tmppage; } } @@ -56,17 +56,10 @@ void q_c::add_packet(unsigned char *data, int length, int64_t timecode, return; if (timecode < 0) die("timecode < 0"); - qpage = (q_page_t *)malloc(sizeof(q_page_t)); - if (qpage == NULL) - die("malloc"); - qpage->pack = (packet_t *)malloc(sizeof(packet_t)); - if (qpage->pack == NULL) - die("malloc"); + qpage = (q_page_t *)safemalloc(sizeof(q_page_t)); + qpage->pack = (packet_t *)safemalloc(sizeof(packet_t)); memset(qpage->pack, 0, sizeof(packet_t)); - qpage->pack->data = (unsigned char *)malloc(length); - if (qpage->pack->data == NULL) - die("malloc"); - memcpy(qpage->pack->data, data, length); + qpage->pack->data = (unsigned char *)safememdup(data, length); qpage->pack->length = length; qpage->pack->timecode = timecode; qpage->pack->bref = bref; @@ -90,7 +83,7 @@ packet_t *q_c::get_packet() { qpage = first->next; if (qpage == NULL) current = NULL; - free(first); + safefree(first); first = qpage; return pack; } diff --git a/r_ac3.cpp b/r_ac3.cpp index 86503d1c1..937ef3cfd 100644 --- a/r_ac3.cpp +++ b/r_ac3.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_ac3.cpp,v 1.15 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: r_ac3.cpp,v 1.16 2003/05/05 18:37:36 mosu Exp $ \brief AC3 demultiplexer module \author Moritz Bunkus */ @@ -68,9 +68,7 @@ ac3_reader_c::ac3_reader_c(track_info_t *nti) throw (error_c): size = ftell(file); if (fseek(file, 0, SEEK_SET) != 0) throw error_c("ac3_reader: Could not seek to beginning of file."); - chunk = (unsigned char *)malloc(4096); - if (chunk == NULL) - die("malloc"); + chunk = (unsigned char *)safemalloc(4096); if (fread(chunk, 1, 4096, file) != 4096) throw error_c("ac3_reader: Could not read 4096 bytes."); if (fseek(file, 0, SEEK_SET) != 0) @@ -91,7 +89,7 @@ ac3_reader_c::~ac3_reader_c() { if (file != NULL) fclose(file); if (chunk != NULL) - free(chunk); + safefree(chunk); if (ac3packetizer != NULL) delete ac3packetizer; } diff --git a/r_avi.cpp b/r_avi.cpp index ccfa40c66..140b02506 100644 --- a/r_avi.cpp +++ b/r_avi.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_avi.cpp,v 1.24 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: r_avi.cpp,v 1.25 2003/05/05 18:37:36 mosu Exp $ \brief AVI demultiplexer module \author Moritz Bunkus */ @@ -85,9 +85,7 @@ avi_reader_c::avi_reader_c(track_info_t *nti) throw (error_c): const char *msg = "avi_reader: Could not initialize AVI source. Reason: "; char *s, *error; error = AVI_strerror(); - s = (char *)malloc(strlen(msg) + strlen(error) + 1); - if (s == NULL) - die("malloc"); + s = (char *)safemalloc(strlen(msg) + strlen(error) + 1); sprintf(s, "%s%s", msg, error); throw error_c(s); } @@ -186,9 +184,7 @@ avi_reader_c::avi_reader_c(track_info_t *nti) throw (error_c): demuxer = demuxer->next; } max_frame_size = fsize; - chunk = (unsigned char *)malloc(fsize < 16384 ? 16384 : fsize); - if (chunk == NULL) - die("malloc"); + chunk = (unsigned char *)safemalloc(fsize < 16384 ? 16384 : fsize); act_wchar = 0; old_key = 0; old_chunk = NULL; @@ -201,7 +197,7 @@ avi_reader_c::~avi_reader_c() { if (avi != NULL) AVI_close(avi); if (chunk != NULL) - free(chunk); + safefree(chunk); if (vpacketizer != NULL) delete vpacketizer; @@ -210,12 +206,12 @@ avi_reader_c::~avi_reader_c() { if (demuxer->packetizer != NULL) delete demuxer->packetizer; tmp = demuxer->next; - free(demuxer); + safefree(demuxer); demuxer = tmp; } if (old_chunk != NULL) - free(old_chunk); + safefree(old_chunk); ti->private_data = NULL; } @@ -228,9 +224,7 @@ int avi_reader_c::add_audio_demuxer(avi_t *avi, int aid) { while ((append_to != NULL) && (append_to->next != NULL)) append_to = append_to->next; AVI_set_audio_track(avi, aid); - demuxer = (avi_demuxer_t *)malloc(sizeof(avi_demuxer_t)); - if (demuxer == NULL) - die("malloc"); + demuxer = (avi_demuxer_t *)safemalloc(sizeof(avi_demuxer_t)); memset(demuxer, 0, sizeof(avi_demuxer_t)); demuxer->aid = aid; wfe = avi->wave_format_ex[aid]; @@ -332,10 +326,7 @@ int avi_reader_c::read() { break; } key = is_keyframe(chunk, nread, key); - old_chunk = (unsigned char *)malloc(nread); - if (old_chunk == NULL) - die("malloc"); - memcpy(old_chunk, chunk, nread); + old_chunk = (unsigned char *)safememdup(chunk, nread); old_key = key; old_nread = nread; frames++; @@ -367,13 +358,10 @@ int avi_reader_c::read() { old_key ? -1 : 0); if (! last_frame) { if (old_chunk != NULL) - free(old_chunk); + safefree(old_chunk); if (nread == 0) fprintf(stdout, "hmm\n"); - old_chunk = (unsigned char *)malloc(nread); - if (old_chunk == NULL) - die("malloc"); - memcpy(old_chunk, chunk, nread); + old_chunk = (unsigned char *)safememdup(chunk, nread); old_key = key; old_nread = nread; } else if (nread > 0) diff --git a/r_matroska.cpp b/r_matroska.cpp index 057eea988..e6d584c52 100644 --- a/r_matroska.cpp +++ b/r_matroska.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_matroska.cpp,v 1.21 2003/05/05 14:57:45 mosu Exp $ + \version \$Id: r_matroska.cpp,v 1.22 2003/05/05 18:37:36 mosu Exp $ \brief Matroska reader \author Moritz Bunkus */ @@ -112,8 +112,8 @@ mkv_reader_c::~mkv_reader_c() { for (i = 0; i < num_tracks; i++) if (tracks[i] != NULL) { if (tracks[i]->private_data != NULL) - free(tracks[i]->private_data); - free(tracks[i]); + safefree(tracks[i]->private_data); + safefree(tracks[i]); } if (es != NULL) @@ -142,16 +142,12 @@ int mkv_reader_c::packets_available() { mkv_track_t *mkv_reader_c::new_mkv_track() { mkv_track_t *t; - t = (mkv_track_t *)malloc(sizeof(mkv_track_t)); - if (t != NULL) { - memset(t, 0, sizeof(mkv_track_t)); - tracks = (mkv_track_t **)realloc(tracks, (num_tracks + 1) * - sizeof(mkv_track_t *)); - if (tracks == NULL) - return NULL; - tracks[num_tracks] = t; - num_tracks++; - } + t = (mkv_track_t *)safemalloc(sizeof(mkv_track_t)); + memset(t, 0, sizeof(mkv_track_t)); + tracks = (mkv_track_t **)saferealloc(tracks, (num_tracks + 1) * + sizeof(mkv_track_t *)); + tracks[num_tracks] = t; + num_tracks++; return t; } @@ -662,7 +658,7 @@ int mkv_reader_c::read_headers() { codec_id.ReadData(es->I_O()); fprintf(stdout, "matroska_reader: | + Codec ID: %s\n", &binary(codec_id)); - track->codec_id = strdup((char *)&binary(codec_id)); + track->codec_id = safestrdup((char *)&binary(codec_id)); } else if (EbmlId(*l3) == KaxCodecPrivate::ClassInfos.GlobalId) { KaxCodecPrivate &c_priv = *static_cast(l3); @@ -670,13 +666,9 @@ int mkv_reader_c::read_headers() { fprintf(stdout, "matroska_reader: | + CodecPrivate, length " "%llu\n", c_priv.GetSize()); track->private_size = c_priv.GetSize(); - if (track->private_size > 0) { - track->private_data = malloc(track->private_size); - if (track->private_data == NULL) - return 0; - memcpy(track->private_data, c_priv.GetBuffer(), - track->private_size); - } + if (track->private_size > 0) + track->private_data = safememdup(track->private_data, + track->private_size); } else if (EbmlId(*l3) == KaxTrackMinCache::ClassInfos.GlobalId) { diff --git a/r_microdvd.cpp b/r_microdvd.cpp index 8ab61c714..ce65830cd 100644 --- a/r_microdvd.cpp +++ b/r_microdvd.cpp @@ -153,14 +153,10 @@ int microdvd_reader_c::read() { if (s2 != NULL) *s2 = 0; if (subtitles == NULL) { - subtitles = strdup(s); - if (subtitles == NULL) - die("malloc"); + subtitles = safestrdup(s); } else { - subtitles = (char *)realloc(subtitles, strlen(s) + 2 + - strlen(subtitles)); - if (subtitles == NULL) - die("malloc"); + subtitles = (char *)saferealloc(subtitles, strlen(s) + 2 + + strlen(subtitles)); sprintf(&subtitles[strlen(subtitles)], "\n%s", s); } if (s2 != NULL) @@ -170,7 +166,7 @@ int microdvd_reader_c::read() { } if (subtitles != NULL) { subs.add(start, end, subtitles); - free(subtitles); + safefree(subtitles); subtitles = NULL; } } diff --git a/r_mp3.cpp b/r_mp3.cpp index cc2040f52..2ff029fce 100644 --- a/r_mp3.cpp +++ b/r_mp3.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_mp3.cpp,v 1.13 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: r_mp3.cpp,v 1.14 2003/05/05 18:37:36 mosu Exp $ \brief MP3 reader module \author Moritz Bunkus */ @@ -75,9 +75,7 @@ mp3_reader_c::mp3_reader_c(track_info_t *nti) throw (error_c): size = ftell(file); if (fseek(file, 0, SEEK_SET) != 0) throw error_c("mp3_reader: Could not seek to beginning of file."); - chunk = (unsigned char *)malloc(4096); - if (chunk == NULL) - die("malloc"); + chunk = (unsigned char *)safemalloc(4096); if (fread(chunk, 1, 4096, file) != 4096) throw error_c("mp3_reader: Could not read 4096 bytes."); if (fseek(file, 0, SEEK_SET) != 0) @@ -100,7 +98,7 @@ mp3_reader_c::~mp3_reader_c() { if (file != NULL) fclose(file); if (chunk != NULL) - free(chunk); + safefree(chunk); if (mp3packetizer != NULL) delete mp3packetizer; } diff --git a/r_ogm.cpp b/r_ogm.cpp index 55259d6ce..39cea0d5b 100644 --- a/r_ogm.cpp +++ b/r_ogm.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_ogm.cpp,v 1.22 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: r_ogm.cpp,v 1.23 2003/05/05 18:37:36 mosu Exp $ \brief OGG media stream reader \author Moritz Bunkus */ @@ -112,10 +112,10 @@ ogm_reader_c::~ogm_reader_c() { dmx = sdemuxers[i]; ogg_stream_clear(&dmx->os); delete dmx->packetizer; - free(dmx); + safefree(dmx); } if (sdemuxers != NULL) - free(sdemuxers); + safefree(sdemuxers); ti->private_data = NULL; } @@ -156,8 +156,8 @@ void ogm_reader_c::free_demuxer(int idx) { dmx = sdemuxers[idx]; for (i = 0; i < 3; i++) if (dmx->packet_data[i] != NULL) - free(dmx->packet_data[i]); - free(dmx); + safefree(dmx->packet_data[i]); + safefree(dmx); memmove(&sdemuxers[idx], &sdemuxers[idx + 1], num_sdemuxers - idx - 1); num_sdemuxers--; @@ -203,10 +203,8 @@ int ogm_reader_c::read_page(ogg_page *og) { } void ogm_reader_c::add_new_demuxer(ogm_demuxer_t *dmx) { - sdemuxers = (ogm_demuxer_t **)realloc(sdemuxers, sizeof(ogm_demuxer_t *) * - (num_sdemuxers + 1)); - if (sdemuxers == NULL) - die ("realloc"); + sdemuxers = (ogm_demuxer_t **)saferealloc(sdemuxers, sizeof(ogm_demuxer_t *) + * (num_sdemuxers + 1)); sdemuxers[num_sdemuxers] = dmx; num_sdemuxers++; } @@ -255,7 +253,7 @@ void ogm_reader_c::create_packetizers() { fprintf(stderr, "Error: ogm_reader: could not initialize video " "packetizer for stream id %d. Will try to continue and " "ignore this stream.\n", dmx->serial); - free(dmx); + safefree(dmx); continue; } @@ -415,15 +413,10 @@ void ogm_reader_c::handle_new_stream(ogg_page *og) { ogg_stream_pagein(&new_oss, og); ogg_stream_packetout(&new_oss, &op); - dmx = (ogm_demuxer_t *)malloc(sizeof(ogm_demuxer_t)); - if (dmx == NULL) - die("malloc"); + dmx = (ogm_demuxer_t *)safemalloc(sizeof(ogm_demuxer_t)); memset(dmx, 0, sizeof(ogm_demuxer_t)); dmx->num_packets = 1; - dmx->packet_data[0] = (unsigned char *)malloc(op.bytes); - if (dmx->packet_data[0] == NULL) - die("malloc"); - memcpy(dmx->packet_data[0], op.packet, op.bytes); + dmx->packet_data[0] = (unsigned char *)safememdup(op.packet, op.bytes); dmx->packet_sizes[0] = op.bytes; /* @@ -435,8 +428,8 @@ void ogm_reader_c::handle_new_stream(ogg_page *og) { numstreams++; if (!demuxing_requested(ti->atracks, ogg_page_serialno(og))) { ogg_stream_clear(&new_oss); - free(dmx->packet_data[0]); - free(dmx); + safefree(dmx->packet_data[0]); + safefree(dmx); return; } @@ -458,8 +451,8 @@ void ogm_reader_c::handle_new_stream(ogg_page *og) { numstreams++; if (!demuxing_requested(ti->vtracks, ogg_page_serialno(og))) { ogg_stream_clear(&new_oss); - free(dmx->packet_data[0]); - free(dmx); + safefree(dmx->packet_data[0]); + safefree(dmx); return; } @@ -496,8 +489,8 @@ void ogm_reader_c::handle_new_stream(ogg_page *og) { else { fprintf(stderr, "Error: ogm_reader: Unknown audio stream type %u. " "Ignoring stream id %d.\n", codec_id, numstreams); - free(dmx->packet_data[0]); - free(dmx); + safefree(dmx->packet_data[0]); + safefree(dmx); return; } @@ -535,8 +528,8 @@ void ogm_reader_c::handle_new_stream(ogg_page *og) { // Failed to detect a supported header. ogg_stream_clear(&new_oss); - free(dmx->packet_data[0]); - free(dmx); + safefree(dmx->packet_data[0]); + safefree(dmx); return; } @@ -623,17 +616,11 @@ void ogm_reader_c::process_header_page(ogg_page *og) { while (ogg_stream_packetout(&dmx->os, &op) == 1) { if ((*op.packet & 3) == PACKET_TYPE_HEADER) { dmx->num_packets++; - dmx->packet_data[2] = (unsigned char *)malloc(op.bytes); - if (dmx->packet_data[2] == NULL) - die("malloc"); - memcpy(dmx->packet_data[2], op.packet, op.bytes); + dmx->packet_data[2] = (unsigned char *)safememdup(op.packet, op.bytes); dmx->packet_sizes[2] = op.bytes; } else if ((*op.packet & 3) == PACKET_TYPE_COMMENT) { dmx->num_packets++; - dmx->packet_data[1] = (unsigned char *)malloc(op.bytes); - if (dmx->packet_data[1] == NULL) - die("malloc"); - memcpy(dmx->packet_data[1], op.packet, op.bytes); + dmx->packet_data[1] = (unsigned char *)safememdup(op.packet, op.bytes); dmx->packet_sizes[1] = op.bytes; } } diff --git a/r_srt.cpp b/r_srt.cpp index fb60002d7..2d0256698 100644 --- a/r_srt.cpp +++ b/r_srt.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_srt.cpp,v 1.9 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: r_srt.cpp,v 1.10 2003/05/05 18:37:36 mosu Exp $ \brief Subripper subtitle reader \author Moritz Bunkus */ @@ -117,20 +117,16 @@ int srt_reader_c::read() { if ((*chunk == '\n') || (*chunk == '\r')) break; if (subtitles == NULL) { - subtitles = strdup(chunk); - if (subtitles == NULL) - die("malloc"); + subtitles = safestrdup(chunk); } else { - subtitles = (char *)realloc(subtitles, strlen(chunk) + 1 + - strlen(subtitles)); - if (subtitles == NULL) - die("malloc"); + subtitles = (char *)saferealloc(subtitles, strlen(chunk) + 1 + + strlen(subtitles)); strcat(subtitles, chunk); } } if (subtitles != NULL) { subs.add(start, end, subtitles); - free(subtitles); + safefree(subtitles); } } diff --git a/r_vobsub.cpp b/r_vobsub.cpp index 547e042ef..c9db3ee53 100644 --- a/r_vobsub.cpp +++ b/r_vobsub.cpp @@ -77,16 +77,11 @@ vobsub_reader_c::vobsub_reader_c(char *fname, audio_sync_t *nasync) if (!vobsub_reader_c::probe_file(file, 0)) throw error_c("vobsub_reader: Source is not a valid VobSub index file."); - name = strdup(fname); - if (name == NULL) - die("strdup"); + name = safestrdup(fname); if ((strlen(name) > 4) && (name[strlen(name) - 4] == '.')) name[strlen(name) - 4] = 0; - else { - name = (char *)realloc(name, strlen(name) + 5); - if (name == NULL) - die("realloc"); - } + else + name = (char *)saferealloc(name, strlen(name) + 5); strcat(name, ".sub"); if ((subfile = fopen(name, "r")) == NULL) throw error_c("vobsub_reader: Could not open the sub file."); @@ -97,7 +92,7 @@ vobsub_reader_c::vobsub_reader_c(char *fname, audio_sync_t *nasync) if (verbose) fprintf(stdout, "Using VobSub subtitle reader for %s/%s.\n+-> Using " \ "VobSub subtitle output module for subtitles.\n", fname, name); - free(name); + safefree(name); memcpy(&async, nasync, sizeof(audio_sync_t)); if (ncomments == NULL) comments = ncomments; @@ -117,11 +112,9 @@ vobsub_reader_c::~vobsub_reader_c() { void vobsub_reader_c::add_vobsub_packetizer(int width, int height, char *palette, int langidx, char *id, int index) { - all_packetizers = (vobsub_packetizer_c **)realloc(all_packetizers, - (num_packetizers + 1) * - sizeof(void *)); - if (all_packetizers == NULL) - die("realloc"); + all_packetizers = (vobsub_packetizer_c **)saferealloc(all_packetizers, + (num_packetizers + 1) * + sizeof(void *)); try { vobsub_packetizer = new vobsub_packetizer_c(width, height, palette, langidx, id, index, @@ -166,11 +159,8 @@ int vobsub_reader_c::read() { if (strlen(chunk) < 10) fprintf(stdout, "vobsub_reader: Warning: Incorrect \"palette:\" entry " "on line %d. Ignored.\n", lineno); - else { - palette = strdup(&chunk[9]); - if (palette == NULL) - die("strdup"); - } + else + palette = safestrdup(&chunk[9]); } else if (!strncmp(chunk, "langidx: ", 9)) { langidx = strtol(&chunk[9], NULL, 10); if ((langidx < 0) || (errno != 0)) { @@ -190,9 +180,7 @@ int vobsub_reader_c::read() { continue; } *s2 = 0; - id = strdup(s); - if (id == NULL) - die("strdup"); + id = safestrdup(s); s = s2 + 1; while (isspace(*s)) s++; @@ -239,12 +227,12 @@ int vobsub_reader_c::read() { width = -1; height = -1; if (palette != NULL) { - free(palette); + safefree(palette); palette = NULL; } langidx = -1; if (id != NULL) { - free(id); + safefree(id); id = NULL; } index = -1; @@ -269,9 +257,7 @@ int vobsub_reader_c::read() { fprintf(stderr, "Warning: vobsub_reader: This entry and the last " "entry start at the same position in the file. Ignored.\n"); else { - s = (char *)malloc(filepos - last_filepos); - if (s == NULL) - die("malloc"); + s = (char *)safemalloc(filepos - last_filepos); if (fread(s, 1, filepos - last_filepos, subfile) != (filepos - last_filepos)) fprintf(stderr, "Warning: vobsub_reader: Could not read entry " @@ -279,7 +265,7 @@ int vobsub_reader_c::read() { else vobsub_packetizer->process(last_start, start - last_start, s, filepos - last_filepos, 0); - free(s); + safefree(s); } } last_start = start; @@ -305,9 +291,7 @@ int vobsub_reader_c::read() { fprintf(stderr, "Warning: vobsub_reader: This entry and the last " "entry start at the same position in the file. Ignored.\n"); else { - s = (char *)malloc(filepos - last_filepos); - if (s == NULL) - die("malloc"); + s = (char *)safemalloc(filepos - last_filepos); if (fread(s, 1, filepos - last_filepos, subfile) != (filepos - last_filepos)) fprintf(stderr, "Warning: vobsub_reader: Could not read entry " @@ -315,7 +299,7 @@ int vobsub_reader_c::read() { else vobsub_packetizer->process(last_start, start - last_start, s, filepos - last_filepos, 1); - free(s); + safefree(s); } } diff --git a/r_wav.cpp b/r_wav.cpp index cbd67d4b0..432dafa0e 100644 --- a/r_wav.cpp +++ b/r_wav.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_wav.cpp,v 1.13 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: r_wav.cpp,v 1.14 2003/05/05 18:37:36 mosu Exp $ \brief MP3 reader module \author Moritz Bunkus */ @@ -69,9 +69,7 @@ wav_reader_c::wav_reader_c(track_info_t *nti) throw (error_c): throw error_c("wav_reader: could not read WAVE header."); bps = wheader.common.wChannels * wheader.common.wBitsPerSample * wheader.common.dwSamplesPerSec / 8; - chunk = (unsigned char *)malloc(bps + 1); - if (chunk == NULL) - die("malloc"); + chunk = (unsigned char *)safemalloc(bps + 1); bytes_processed = 0; pcmpacketizer = new pcm_packetizer_c(wheader.common.dwSamplesPerSec, wheader.common.wChannels, @@ -85,7 +83,7 @@ wav_reader_c::~wav_reader_c() { if (file != NULL) fclose(file); if (chunk != NULL) - free(chunk); + safefree(chunk); if (pcmpacketizer != NULL) delete pcmpacketizer; } diff --git a/subtitles.cpp b/subtitles.cpp index a1fcb938a..28d839b31 100644 --- a/subtitles.cpp +++ b/subtitles.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: subtitles.cpp,v 1.4 2003/05/04 10:05:41 mosu Exp $ + \version \$Id: subtitles.cpp,v 1.5 2003/05/05 18:37:36 mosu Exp $ \brief subtitle helper \author Moritz Bunkus */ @@ -38,20 +38,18 @@ subtitles_c::~subtitles_c() { while (current != NULL) { if (current->subs != NULL) - free(current->subs); + safefree(current->subs); last = current; current = current->next; - free(last); + safefree(last); } } void subtitles_c::add(int64_t nstart, int64_t nend, char *nsubs) { sub_t *s; - s = (sub_t *)malloc(sizeof(sub_t)); - if (s == NULL) - die("malloc"); - s->subs = strdup(nsubs); + s = (sub_t *)safemalloc(sizeof(sub_t)); + s->subs = safestrdup(nsubs); s->start = nstart; s->end = nend; s->next = NULL; @@ -138,8 +136,8 @@ void subtitles_c::process(textsubs_packetizer_c *p) { while ((current = get_next()) != NULL) { p->process((unsigned char *)current->subs, 0, current->start, current->end - current->start); - free(current->subs); - free(current); + safefree(current->subs); + safefree(current); } }