Commit e190c88f authored by npzacs's avatar npzacs
Browse files

Merge file_write() from libbluray

parent bed21f3b
......@@ -42,6 +42,7 @@
#define file_seek(X,Y,Z) X->seek(X,Y,Z)
#define file_tell(X) X->tell(X)
#define file_read(X,Y,Z) X->read(X,Y,Z)
#define file_write(X,Y,Z) X->write(X,Y,Z)
BD_PRIVATE int64_t file_size(AACS_FILE_H *fp);
BD_PRIVATE extern AACS_FILE_H *(*file_open)(const char* filename, const char *mode);
......
......@@ -87,6 +87,28 @@ static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size)
return (int64_t)got;
}
static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size)
{
ssize_t written, result;
if (size <= 0 || size >= BD_MAX_SSIZE) {
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file);
return 0;
}
for (written = 0; written < (ssize_t)size; written += result) {
result = write((int)(intptr_t)file->internal, buf + written, size - written);
if (result < 0) {
if (errno != EINTR) {
BD_DEBUG(DBG_FILE, "write() failed (%p)\n", (void*)file);
break;
}
result = 0;
}
}
return (int64_t)written;
}
static AACS_FILE_H *_file_open(const char* filename, const char *cmode)
{
AACS_FILE_H *file;
......@@ -123,6 +145,7 @@ static AACS_FILE_H *_file_open(const char* filename, const char *cmode)
file->close = _file_close;
file->seek = _file_seek;
file->read = _file_read;
file->write = _file_write;
file->tell = _file_tell;
file->internal = (void*)(intptr_t)fd;
......
......@@ -75,6 +75,16 @@ static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size)
return 0;
}
static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size)
{
if (size > 0 && size < BD_MAX_SSIZE) {
return (int64_t)fwrite(buf, 1, (size_t)size, (FILE *)file->internal);
}
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file);
return 0;
}
static AACS_FILE_H *_file_open(const char* filename, const char *mode)
{
AACS_FILE_H *file;
......@@ -105,6 +115,7 @@ static AACS_FILE_H *_file_open(const char* filename, const char *mode)
file->close = _file_close;
file->seek = _file_seek;
file->read = _file_read;
file->write = _file_write;
file->tell = _file_tell;
BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment