diff options
author | wm4 <wm4@nowhere> | 2013-06-11 12:16:42 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2013-06-23 22:33:59 +0200 |
commit | 4f5e12136de717896bf322e75d42de1af09e1c3e (patch) | |
tree | f6b40f0ae7ab2ef410f1449e9626ab1140862836 /stream/stream.c | |
parent | d064c69e1c958514c3ef4d7871bcd92c8ff92de9 (diff) | |
download | mpv-4f5e12136de717896bf322e75d42de1af09e1c3e.tar.bz2 mpv-4f5e12136de717896bf322e75d42de1af09e1c3e.tar.xz |
stream: remove padding parameter from stream_read_complete()
Seems like a completely unnecessary complication. Instead, always add a
1 byte padding (could be extended if a caller needs it), and clear it.
Also add some documentation. There was some, but it was outdated and
incomplete.
Diffstat (limited to 'stream/stream.c')
-rw-r--r-- | stream/stream.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/stream/stream.c b/stream/stream.c index 3281e50fe6..99ae6ed103 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -898,20 +898,27 @@ unsigned char *stream_read_line(stream_t *s, unsigned char *mem, int max, return mem; } +// Read the rest of the stream into memory (current pos to EOF), and return it. +// talloc_ctx: used as talloc parent for the returned allocation +// max_size: must be set to >0. If the file is larger than that, it is treated +// as error. This is a minor robustness measure. +// returns: stream contents, or .start/.len set to NULL on error +// If the file was empty, but no error happened, .start will be non-NULL and +// .len will be 0. +// For convenience, the returned buffer is padded with a 0 byte. The padding +// is not included in the returned length. struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, - int max_size, int padding_bytes) + int max_size) { if (max_size > 1000000000) abort(); int bufsize; int total_read = 0; - int padding = FFMAX(padding_bytes, 1); + int padding = 1; char *buf = NULL; if (s->end_pos > max_size) - return (struct bstr){ - NULL, 0 - }; + return (struct bstr){NULL, 0}; if (s->end_pos > 0) bufsize = s->end_pos + padding; else @@ -924,16 +931,13 @@ struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, break; if (bufsize > max_size) { talloc_free(buf); - return (struct bstr){ - NULL, 0 - }; + return (struct bstr){NULL, 0}; } bufsize = FFMIN(bufsize + (bufsize >> 1), max_size + padding); } buf = talloc_realloc_size(talloc_ctx, buf, total_read + padding); - return (struct bstr){ - buf, total_read - }; + memset(&buf[total_read], 0, padding); + return (struct bstr){buf, total_read}; } bool stream_manages_timeline(struct stream *s) |