Request POSIX_FADV_DONTNEED, now correct
[lms] / lightmediascanner / src / plugins / asf / asf.c
index 6aeb249..462bcb9 100644 (file)
@@ -28,6 +28,7 @@
 #include "config.h"
 #endif
 
+#define _XOPEN_SOURCE 600
 #include <lightmediascanner_plugin.h>
 #include <lightmediascanner_db.h>
 #include <sys/types.h>
@@ -36,6 +37,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 enum StreamTypes {
     STREAM_TYPE_UNKNOWN = 0,
@@ -75,6 +77,20 @@ static const struct lms_string_size _exts[] = {
     LMS_STATIC_STRING_SIZE(".asf")
 };
 
+/* ASF GUIDs
+ *
+ * Microsoft defines these 16-byte (128-bit) GUIDs as:
+ * first 8 bytes are in little-endian order
+ * next 8 bytes are in big-endian order
+ *
+ * Eg.: AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp:
+ *
+ * to convert to byte string do as follow:
+ *
+ * $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
+ *
+ * See http://www.microsoft.com/windows/windowsmedia/forpros/format/asfspec.aspx
+ */
 static const char header_guid[16] = "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
 static const char file_properties_guid[16] = "\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65";
 static const char stream_properties_guid[16] = "\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65";
@@ -88,15 +104,19 @@ static const char metadata_library_guid[16] = "\224\034#D\230\224\321I\241A\x1d\
 static const char content_encryption_object_guid[16] = "\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E";
 static const char extended_content_encryption_object_guid[16] = "\x14\xE6\x8A\x29\x22\x26\x17\x4C\xB9\x35\xDA\xE0\x7E\xE9\x28\x9C";
 
+static const char attr_name_wm_album_title[26] = "\x57\x00\x4d\x00\x2f\x00\x41\x00\x6c\x00\x62\x00\x75\x00\x6d\x00\x54\x00\x69\x00\x74\x00\x6c\x00\x65\x00";
+static const char attr_name_wm_genre[16] = "\x57\x00\x4d\x00\x2f\x00\x47\x00\x65\x00\x6e\x00\x72\x00\x65\x00";
+static const char attr_name_wm_track_number[28] = "\x57\x00\x4d\x00\x2f\x00\x54\x00\x72\x00\x61\x00\x63\x00\x6b\x00\x4e\x00\x75\x00\x6d\x00\x62\x00\x65\x00\x72\x00";
+
 static long long
 _to_number(const char *data, unsigned int type_size, unsigned int data_size)
 {
     long long sum = 0;
     unsigned int last, i;
 
-    last = data_size > type_size ? type_size - 1 : data_size - 1;
+    last = data_size > type_size ? type_size : data_size;
 
-    for (i = 0; i <= last; i++)
+    for (i = 0; i < last; i++)
         sum |= (unsigned char) (data[i]) << (i * 8);
 
     return sum;
@@ -106,19 +126,17 @@ static short
 _read_word(int fd)
 {
     char v[2];
-    if (read(fd, &v, 2) != 2) {
+    if (read(fd, &v, 2) != 2)
         return 0;
-    }
-    return (unsigned int) _to_number(v, sizeof(unsigned short), 2);
+    return (short) _to_number(v, sizeof(unsigned short), 2);
 }
 
 static unsigned int
 _read_dword(int fd)
 {
     char v[4];
-    if (read(fd, &v, 4) != 4) {
+    if (read(fd, &v, 4) != 4)
         return 0;
-    }
     return (unsigned int) _to_number(v, sizeof(unsigned int), 4);
 }
 
@@ -126,10 +144,9 @@ static long long
 _read_qword(int fd)
 {
     char v[8];
-    if (read(fd, &v, 8) != 8) {
+    if (read(fd, &v, 8) != 8)
         return 0;
-    }
-    return (unsigned int) _to_number(v, sizeof(unsigned long long), 8);
+    return _to_number(v, sizeof(unsigned long long), 8);
 }
 
 static int
@@ -138,7 +155,12 @@ _read_string(int fd, size_t count, char **str, size_t *len)
     char *data;
     size_t data_size, size;
 
-    data = (char *) malloc(sizeof(char) * count);
+    if (!str) {
+        lseek(fd, count, SEEK_CUR);
+        return 0;
+    }
+
+    data = malloc(sizeof(char) * count);
     data_size = read(fd, data, count);
     if (data_size == -1) {
         free(data);
@@ -147,9 +169,8 @@ _read_string(int fd, size_t count, char **str, size_t *len)
 
     size = data_size;
     while (size >= 2) {
-        if (data[size - 1] != '\0' || data[size - 2] != '\0') {
+        if (data[size - 1] != '\0' || data[size - 2] != '\0')
             break;
-        }
         size -= 2;
     }
 
@@ -167,16 +188,13 @@ _parse_content_description(lms_charset_conv_t *cs_conv, int fd, struct asf_info
     int copyright_length = _read_word(fd);
     int comment_length = _read_word(fd);
     int rating_length = _read_word(fd);
-    int len;
-    char *copyright, *comment, *rating;
 
     _read_string(fd, title_length, &info->title.str, &info->title.len);
     lms_charset_conv_force(cs_conv, &info->title.str, &info->title.len);
     _read_string(fd, artist_length, &info->artist.str, &info->artist.len);
     lms_charset_conv_force(cs_conv, &info->artist.str, &info->artist.len);
-    _read_string(fd, copyright_length, &copyright, &len);
-    _read_string(fd, comment_length, &comment, &len);
-    _read_string(fd, rating_length, &rating, &len);
+    /* ignore copyright, comment and rating */
+    lseek(fd, copyright_length + comment_length + rating_length, SEEK_CUR);
 }
 
 static void
@@ -192,24 +210,17 @@ _parse_attribute_name(int fd,
     /* extended content descriptor */
     if (kind == 0) {
         attr_name_length = _read_word(fd);
-        if (attr_name)
-            _read_string(fd, attr_name_length, attr_name, attr_name_len);
-        else
-            lseek(fd, attr_name_length, SEEK_CUR);
+        _read_string(fd, attr_name_length, attr_name, attr_name_len);
         *attr_type = _read_word(fd);
         *attr_size = _read_word(fd);
     }
     /* metadata & metadata library */
     else {
-        lseek(fd, 2, SEEK_CUR); /* language */
-        lseek(fd, 2, SEEK_CUR); /* stream */
+        lseek(fd, 2 + 2, SEEK_CUR); /* language and stream */
         attr_name_length = _read_word(fd);
         *attr_type = _read_word(fd);
         *attr_size = _read_dword(fd);
-        if (attr_name)
-            _read_string(fd, attr_name_length, attr_name, attr_name_len);
-        else
-            lseek(fd, attr_name_length, SEEK_CUR);
+        _read_string(fd, attr_name_length, attr_name, attr_name_len);
     }
 }
 
@@ -233,12 +244,10 @@ _skip_attribute_data(int fd, int kind, int attr_type, int attr_size)
         break;
 
     case ATTR_TYPE_BOOL:
-        if (kind == 0) {
+        if (kind == 0)
             lseek(fd, 4, SEEK_CUR);
-        }
-        else {
+        else
             lseek(fd, 2, SEEK_CUR);
-        }
         break;
 
     case ATTR_TYPE_DWORD:
@@ -281,18 +290,17 @@ _parse_extended_content_description_object(lms_charset_conv_t *cs_conv, int fd,
                               &attr_name, &attr_name_len,
                               &attr_type, &attr_size);
         if (attr_type == ATTR_TYPE_UNICODE) {
-            lms_charset_conv_force(cs_conv, &attr_name, &attr_name_len);
-            if (strcmp(attr_name, "WM/AlbumTitle") == 0)
+            if (memcmp(attr_name, attr_name_wm_album_title, attr_name_len) == 0)
                 _parse_attribute_string_data(cs_conv,
                                              fd, attr_size,
                                              &info->album.str,
                                              &info->album.len);
-            else if (strcmp(attr_name, "WM/Genre") == 0)
+            else if (memcmp(attr_name, attr_name_wm_genre, attr_name_len) == 0)
                 _parse_attribute_string_data(cs_conv,
                                              fd, attr_size,
                                              &info->genre.str,
                                              &info->genre.len);
-            else if (strcmp(attr_name, "WM/TrackNumber") == 0) {
+            else if (memcmp(attr_name, attr_name_wm_track_number, attr_name_len) == 0) {
                 char *trackno;
                 size_t trackno_len;
                 _parse_attribute_string_data(cs_conv,
@@ -318,18 +326,16 @@ static void
 _skip_metadata(int fd)
 {
     int count = _read_word(fd);
-    while (count--) {
+    while (count--)
         _skip_attribute(fd, 1);
-    }
 }
 
 static void
 _skip_metadata_library(int fd)
 {
     int count = _read_word(fd);
-    while (count--) {
+    while (count--)
         _skip_attribute(fd, 2);
-    }
 }
 
 static void
@@ -344,15 +350,14 @@ _skip_header_extension(int fd)
     while (data_pos < data_size) {
         read(fd, &guid, 16);
         size = _read_qword(fd);
-        if (memcmp(guid, metadata_guid, 16) == 0) {
+        if (size == 0)
+            break;
+        if (memcmp(guid, metadata_guid, 16) == 0)
             _skip_metadata(fd);
-        }
-        else if (memcmp(guid, metadata_library_guid, 16) == 0) {
+        else if (memcmp(guid, metadata_library_guid, 16) == 0)
             _skip_metadata_library(fd);
-        }
-        else {
+        else
             lseek(fd, size - 24, SEEK_CUR);
-        }
         data_pos += size;
     }
 }
@@ -384,7 +389,7 @@ _match(struct plugin *p, const char *path, int len, int base)
 static int
 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
 {
-    struct asf_info info = {0};
+    struct asf_info info = {{0}, {0}, {0}, {0}, 0};
     struct lms_audio_info audio_info = {0};
     struct lms_video_info video_info = {0};
     int r, fd, num_objects, i;
@@ -456,14 +461,12 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in
     }
 
     _strstrip(&info.title.str, &info.title.len);
-    _strstrip(&info.artist.str, &info.genre.len);
+    _strstrip(&info.artist.str, &info.artist.len);
     _strstrip(&info.album.str, &info.album.len);
     _strstrip(&info.genre.str, &info.genre.len);
 
     if (!info.title.str) {
         int ext_idx;
-        if (info.title.str)
-            free(info.title.str);
         ext_idx = ((int)match) - 1;
         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
         info.title.str = malloc((info.title.len + 1) * sizeof(char));
@@ -474,7 +477,7 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in
 
 #if 0
     fprintf(stderr, "file %s info\n", finfo->path);
-    fprintf(stderr, "\ttitle='%s' len=%d\n", info.title.str, info.title.len);
+    fprintf(stderr, "\ttitle='%s'\n", info.title.str);
     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
     fprintf(stderr, "\talbum='%s'\n", info.album.str);
     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);