[libbluray-devel] factorize metadata reading code

Petri Hintukainen git at videolan.org
Sun Jun 26 11:38:48 CEST 2016


libudfread | branch: master | Petri Hintukainen <phintuka at gmail.com> | Wed Jun 22 22:14:02 2016 +0300| [64ac239e7aa741ad3e2e2d48eafd6e26fb202ee7] | committer: Petri Hintukainen

factorize metadata reading code

> http://git.videolan.org/gitweb.cgi/libudfread.git/?a=commit;h=64ac239e7aa741ad3e2e2d48eafd6e26fb202ee7
---

 src/udfread.c |   48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/udfread.c b/src/udfread.c
index ba43801..a7ce87d 100644
--- a/src/udfread.c
+++ b/src/udfread.c
@@ -771,15 +771,11 @@ static int _read_metadata_blocks(udfread *udf, uint8_t *buf,
     return tag_id;
 }
 
-static struct file_entry *_read_file_entry(udfread *udf,
-                                           const struct long_ad *icb)
+static uint8_t *_read_metadata(udfread *udf, const struct long_ad *icb, int *tag_id)
 {
-    struct file_entry *fe = NULL;
     uint32_t  num_blocks = (icb->length + UDF_BLOCK_SIZE - 1) / UDF_BLOCK_SIZE;
     uint8_t  *buf;
-    int       tag_id;
 
-    udf_trace("file entry size %u bytes\n", icb->length);
     if (num_blocks < 1) {
         return NULL;
     }
@@ -790,13 +786,31 @@ static struct file_entry *_read_file_entry(udfread *udf,
         return NULL;
     }
 
-    tag_id = _read_metadata_blocks(udf, buf, icb);
-    if (tag_id < 0) {
-        udf_error("reading file entry failed\n");
+    *tag_id = _read_metadata_blocks(udf, buf, icb);
+    if (*tag_id < 0) {
+        udf_log("reading icb blocks failed\n");
         free(buf);
         return NULL;
     }
 
+    return buf;
+}
+
+static struct file_entry *_read_file_entry(udfread *udf,
+                                           const struct long_ad *icb)
+{
+    struct file_entry *fe = NULL;
+    uint8_t  *buf;
+    int       tag_id;
+
+    udf_trace("file entry size %u bytes\n", icb->length);
+
+    buf = _read_metadata(udf, icb, &tag_id);
+    if (!buf) {
+        udf_error("reading file entry failed\n");
+        return NULL;
+    }
+
     switch (tag_id) {
         case ECMA_FileEntry:
             fe = decode_file_entry(buf, UDF_BLOCK_SIZE, icb->partition);
@@ -857,28 +871,18 @@ static int _parse_dir(const uint8_t *data, uint32_t length, struct udf_dir *dir)
 
 static struct udf_dir *_read_dir_file(udfread *udf, const struct long_ad *loc)
 {
-    uint32_t        num_blocks = (loc->length + UDF_BLOCK_SIZE - 1) / UDF_BLOCK_SIZE;
-    uint8_t        *data;
     struct udf_dir *dir = NULL;
+    uint8_t        *data;
+    int             tag_id;
 
-    if (num_blocks < 1) {
-        return NULL;
-    }
+    udf_trace("directory size %u bytes\n", loc->length);
 
-    data = (uint8_t *)malloc(num_blocks * UDF_BLOCK_SIZE);
+    data = _read_metadata(udf, loc, &tag_id);
     if (!data) {
-        udf_error("out of memory\n");
-        return NULL;
-    }
-
-    if (_read_metadata_blocks(udf, data, loc) < 0) {
         udf_error("reading directory file failed\n");
-        free(data);
         return NULL;
     }
 
-    udf_trace("directory size %u bytes\n", loc->length);
-
     dir = (struct udf_dir *)calloc(1, sizeof(struct udf_dir));
     if (dir) {
         if (_parse_dir(data, loc->length, dir) < 0) {



More information about the libbluray-devel mailing list