[libbluray-devel] Added file_size()
hpi1
git at videolan.org
Thu Dec 5 12:44:54 CET 2013
libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Tue Dec 3 09:49:24 2013 +0200| [9d70bc774fa23608ecfd78e17d0daef2d50903cc] | committer: hpi1
Added file_size()
> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=9d70bc774fa23608ecfd78e17d0daef2d50903cc
---
src/file/file.h | 2 ++
src/file/file_posix.c | 18 ++++++++++++++++++
src/libbluray/bdj/bdjo_parser.c | 5 +----
src/libbluray/bdnav/meta_parse.c | 4 +---
src/libbluray/bluray.c | 8 ++++++--
src/util/bits.c | 12 +++++++++---
6 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/src/file/file.h b/src/file/file.h
index 5ee8c01..55340f7 100644
--- a/src/file/file.h
+++ b/src/file/file.h
@@ -41,11 +41,13 @@
#define file_eof(X) X->eof(X)
#define file_read(X,Y,Z) (size_t)X->read(X,Y,Z)
#define file_write(X,Y,Z) (size_t)X->write(X,Y,Z)
+BD_PRIVATE int64_t file_size(BD_FILE_H *fp);
BD_PRIVATE extern BD_FILE_H* (*file_open)(const char* filename, const char *mode);
BD_PRIVATE BD_FILE_OPEN file_open_default(void);
+
/*
* directory access
*/
diff --git a/src/file/file_posix.c b/src/file/file_posix.c
index fec26ab..4bca744 100644
--- a/src/file/file_posix.c
+++ b/src/file/file_posix.c
@@ -138,3 +138,21 @@ BD_FILE_OPEN file_open_default(void)
{
return file_open_linux;
}
+
+/*
+ *
+ */
+
+int64_t file_size(BD_FILE_H *fp)
+{
+ int64_t pos = file_tell(fp);
+ int64_t res1 = file_seek(fp, 0, SEEK_END);
+ int64_t length = file_tell(fp);
+ int64_t res2 = file_seek(fp, pos, SEEK_SET);
+
+ if (res1 < 0 || res2 < 0 || pos < 0 || length < 0) {
+ return -1;
+ }
+
+ return length;
+}
diff --git a/src/libbluray/bdj/bdjo_parser.c b/src/libbluray/bdj/bdjo_parser.c
index 0abb579..450046f 100644
--- a/src/libbluray/bdj/bdjo_parser.c
+++ b/src/libbluray/bdj/bdjo_parser.c
@@ -410,15 +410,12 @@ jobject bdjo_read(JNIEnv* env, const char* file)
return NULL;
}
- file_seek(handle, 0, SEEK_END);
- int64_t length = file_tell(handle);
+ int64_t length = file_size(handle);
if (length <= 0 || length > MAX_BDJO_FILE_SIZE) {
BD_DEBUG(DBG_BDJ | DBG_CRIT, "Error reading %s\n", file);
} else {
- file_seek(handle, 0, SEEK_SET);
-
size_t size = (size_t)length;
uint8_t *data = malloc(size);
size_t size_read = file_read(handle, data, size);
diff --git a/src/libbluray/bdnav/meta_parse.c b/src/libbluray/bdnav/meta_parse.c
index 77e36b0..dd3fc48 100644
--- a/src/libbluray/bdnav/meta_parse.c
+++ b/src/libbluray/bdnav/meta_parse.c
@@ -159,11 +159,9 @@ META_ROOT *meta_parse(const char *device_path)
continue;
}
- file_seek(handle, 0, SEEK_END);
- int64_t length = file_tell(handle);
+ int64_t length = file_size(handle);
if (length > 0 && length < MAX_META_FILE_SIZE) {
- file_seek(handle, 0, SEEK_SET);
size_t size = (size_t)length;
uint8_t *data = malloc(size);
size_t size_read = file_read(handle, data, size);
diff --git a/src/libbluray/bluray.c b/src/libbluray/bluray.c
index 0055ecd..8b93706 100644
--- a/src/libbluray/bluray.c
+++ b/src/libbluray/bluray.c
@@ -526,13 +526,17 @@ static int _open_m2ts(BLURAY *bd, BD_STREAM *st)
st->fp = file_open(f_name, "rb");
X_FREE(f_name);
+ st->clip_size = 0;
st->clip_pos = (uint64_t)st->clip->start_pkt * 192;
st->clip_block_pos = (st->clip_pos / 6144) * 6144;
if (st->fp) {
- file_seek(st->fp, 0, SEEK_END);
- if ((st->clip_size = file_tell(st->fp))) {
+ int64_t clip_size = file_size(st->fp);
+ if (clip_size > 0) {
+
file_seek(st->fp, st->clip_block_pos, SEEK_SET);
+
+ st->clip_size = clip_size;
st->int_buf_off = 6144;
libaacs_select_title(bd->libaacs, bd_psr_read(bd->regs, PSR_TITLE_NUMBER));
diff --git a/src/util/bits.c b/src/util/bits.c
index cdc4ae7..a3f45e2 100644
--- a/src/util/bits.c
+++ b/src/util/bits.c
@@ -22,6 +22,8 @@
#include "bits.h"
+#include "util/logging.h"
+
#include <stdio.h>
/**
@@ -39,12 +41,16 @@ void bb_init( BITBUFFER *bb, uint8_t *p_data, size_t i_data )
void bs_init( BITSTREAM *bs, BD_FILE_H *fp )
{
+ int64_t size = file_size(fp);;
bs->fp = fp;
bs->pos = 0;
- file_seek(bs->fp, 0, SEEK_END);
- bs->end = file_tell(bs->fp);
- file_seek(bs->fp, 0, SEEK_SET);
+ bs->end = (size < 0) ? 0 : size;
bs->size = file_read(bs->fp, bs->buf, BF_BUF_SIZE);
+ if (bs->size == 0 || bs->size > BF_BUF_SIZE) {
+ bs->size = 0;
+ bs->end = 0;
+ BD_DEBUG(DBG_FILE|DBG_CRIT, "bs_init(): read error!\n");
+ }
bb_init(&bs->bb, bs->buf, bs->size);
}
More information about the libbluray-devel
mailing list