#ifdef HAVE_CONFIG_H
# include "config.h"
# define __CDIO_CONFIG_H__ 1
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <ctype.h>
#include <cdio/logging.h>
#include <cdio/util.h>
#include "_cdio_stream.h"
#include "_cdio_stdio.h"
#include "cdio_assert.h"
#if defined(HAVE_FSEEKO64) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)
#define CDIO_FSEEK fseeko64
#elif defined(HAVE_FSEEKO)
#define CDIO_FSEEK fseeko
#else
#define CDIO_FSEEK fseek
#endif
#if defined(_WIN32)
#include <cdio/utf8.h>
#define CDIO_FOPEN fopen_utf8
#else
#define CDIO_FOPEN fopen
#endif
#if defined(HAVE__STATI64) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)
#define CDIO_STAT_STRUCT _stati64
#if defined(_WIN32)
static inline int _stati64_utf8(const char *path, struct _stati64 *buffer) {
int ret;
wchar_t* wpath = cdio_utf8_to_wchar(path);
ret = _wstati64(wpath, buffer);
cdio_free(wpath);
return ret;
}
#define CDIO_STAT_CALL _stati64_utf8
#else
#define CDIO_STAT_CALL _stati64
#endif
#else
#define CDIO_STAT_STRUCT stat
#define CDIO_STAT_CALL stat
#endif
#define _STRINGIFY(a) #a
#define STRINGIFY(a) _STRINGIFY(a)
#define CDIO_STDIO_BUFSIZE (128*1024)
typedef struct {
char *pathname;
FILE *fd;
char *fd_buf;
off_t st_size;
} _UserData;
static int
_stdio_open (void *user_data)
{
_UserData *const ud = user_data;
if ((ud->fd = CDIO_FOPEN (ud->pathname, "rb")))
{
ud->fd_buf = calloc (1, CDIO_STDIO_BUFSIZE);
setvbuf (ud->fd, ud->fd_buf, _IOFBF, CDIO_STDIO_BUFSIZE);
}
return (ud->fd == NULL);
}
static int
_stdio_close(void *user_data)
{
_UserData *const ud = user_data;
if (fclose (ud->fd))
cdio_error ("fclose (): %s", strerror (errno));
ud->fd = NULL;
free (ud->fd_buf);
ud->fd_buf = NULL;
return 0;
}
static void
_stdio_free(void *user_data)
{
_UserData *const ud = user_data;
if (ud->pathname)
free(ud->pathname);
if (ud->fd)
_stdio_close(user_data);
free(ud);
}
static int
_stdio_seek(void *p_user_data, off_t i_offset, int whence)
{
_UserData *const ud = p_user_data;
int ret;
#if !defined(HAVE_FSEEKO) && !defined(HAVE_FSEEKO64)
if ( (sizeof(off_t) > sizeof(long)) && (i_offset != (off_t)((long)i_offset)) ) {
cdio_error ( STRINGIFY(CDIO_FSEEK) " (): lossy truncation detected!");
errno = EFBIG;
return DRIVER_OP_ERROR;
}
#endif
if ( (ret=CDIO_FSEEK (ud->fd, i_offset, whence)) ) {
cdio_error ( STRINGIFY(CDIO_FSEEK) " (): %s", strerror (errno));
}
return ret;
}
static off_t
_stdio_stat(void *p_user_data)
{
const _UserData *const ud = p_user_data;
return ud->st_size;
}
static ssize_t
_stdio_read(void *user_data, void *buf, size_t count)
{
_UserData *const ud = user_data;
long read_count;
const size_t MAX_ALLOWED_COUNT = 0x100000;
if (count > MAX_ALLOWED_COUNT) {
cdio_error("Requested count exceeds maximum allowed value.\n");
return 0;
}
read_count = fread(buf, 1, count, ud->fd);
if (read_count != count)
{
if (feof (ud->fd))
{
cdio_debug ("fread (): EOF encountered");
clearerr (ud->fd);
}
else if (ferror (ud->fd))
{
cdio_error ("fread (): %s", strerror (errno));
clearerr (ud->fd);
}
else
cdio_debug ("fread (): short read and no EOF?!?");
}
return read_count;
}
void
cdio_stdio_destroy(CdioDataSource_t *p_obj)
{
cdio_stream_destroy(p_obj);
}
CdioDataSource_t *
cdio_stdio_new(const char pathname[])
{
CdioDataSource_t *new_obj = NULL;
cdio_stream_io_functions funcs = { NULL, NULL, NULL, NULL, NULL, NULL };
_UserData *ud = NULL;
struct CDIO_STAT_STRUCT statbuf;
char* pathdup;
if (pathname == NULL)
return NULL;
pathdup = _cdio_strdup_fixpath(pathname);
if (pathdup == NULL)
return NULL;
if (CDIO_STAT_CALL (pathdup, &statbuf) == -1)
{
cdio_warn ("could not retrieve file info for `%s': %s",
pathdup, strerror (errno));
cdio_free(pathdup);
return NULL;
}
ud = calloc (1, sizeof (_UserData));
cdio_assert (ud != NULL);
ud->pathname = pathdup;
ud->st_size = statbuf.st_size;
funcs.open = _stdio_open;
funcs.seek = _stdio_seek;
funcs.stat = _stdio_stat;
funcs.read = _stdio_read;
funcs.close = _stdio_close;
funcs.free = _stdio_free;
new_obj = cdio_stream_new(ud, &funcs);
return new_obj;
}