[FFmpeg-devel] [PATCH] Add av_file_get_size() and av_file_read(), replace cmdutils.h:read_file().

Michael Niedermayer michaelni
Thu Dec 2 02:32:30 CET 2010


On Mon, Nov 29, 2010 at 12:54:37PM +0100, Stefano Sabatini wrote:
[...]
> +int av_file_read(const char *filename, char **bufptr, size_t *size, void *log_ctx)
> +{
> +    int fd = open(filename, O_RDONLY);
> +    struct stat st;
> +    off_t off_size;
> +
> +    if (fd < 0) {
> +        av_log(log_ctx, AV_LOG_ERROR,
> +               "Cannot read file '%s': %s\n", filename, strerror(errno));
> +        return AVERROR(errno);
> +    }
> +
> +    if (lstat(filename, &st) < 0) {
> +        close(fd);
> +        return AVERROR(errno);
> +    }
> +
> +    off_size = st.st_size;
> +    if (off_size >= SIZE_MAX) {
> +        av_log(log_ctx, AV_LOG_ERROR,
> +               "File size for file '%s' is too big\n", filename);
> +        close(fd);
> +        return AVERROR(EINVAL);
> +    }
> +    *size = off_size;
> +

> +    *bufptr = av_malloc(*size + 1);

this is definitly not safe currently


> +    if (!*bufptr) {
> +        close(fd);
> +        return AVERROR(ENOMEM);
> +    }
> +    read(fd, *bufptr, *size);
> +    (*bufptr)[*size++] = '\0';
> +
> +    close(fd);
> +    return 0;
> +}
> diff --git a/libavutil/file.h b/libavutil/file.h
> new file mode 100644
> index 0000000..1e74e9d
> --- /dev/null
> +++ b/libavutil/file.h
> @@ -0,0 +1,41 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#ifndef AVUTIL_FILE_H
> +#define AVUTIL_FILE_H
> +
> +#include "avutil.h"
> +
> +/**
> + * @file misc file utilities
> + */
> +
> +/**
> + * Read the file with name filename, and put its content in a newly
> + * allocated 0-terminated buffer. If filename references a link, the
> + * content of the linked file is read.
> + *
> + * @param bufptr location where pointer to buffer is returned
> + * @param size   location where size of buffer is returned
> + * @param log_ctx context used for logging
> + * @return 0 in case of success, a negative value corresponding to an
> + * AVERROR error code in case of failure
> + */
> +int av_file_read(const char *filename, char **bufptr, size_t *size, void *log_ctx);

this API is crap.
at a minimunm it should support memory mapped files on platforms supporting them

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Everything should be made as simple as possible, but not simpler.
-- Albert Einstein
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20101202/dbf59328/attachment.pgp>



More information about the ffmpeg-devel mailing list