gui: Check only once for invalid key maps.
[paraslash.git] / wma_common.c
index 9a7a22acf30bd310f2dd14f8494f93cff5adc070..781db5f4a36678f14db97a165b02e8f41df3e8b8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2009-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 #include "error.h"
 #include "afh.h"
 #include "portable_io.h"
-#include "mdct.h"
+#include "imdct.h"
 #include "wma.h"
 
-const char *search_pattern(const char *pattern, int pattern_len, const char *buf, int buf_size)
+/**
+ * Find the first occurrence of the given pattern.
+ *
+ * \param pattern The pattern to search for.
+ * \param pattern_len The length of the pattern in bytes.
+ * \param buf The buffer to search for the pattern.
+ * \param buf_size The number of bytes in \a buf.
+ */
+const char *search_pattern(const char *pattern, int pattern_len,
+               const char *buf, int buf_size)
 {
        const char *p, *end = buf + buf_size;
 
-       if (pattern_len > buf_size)
-               return NULL;
+       /* TODO: Use suffix arrays to speed up the search. */
        for (p = buf; p + pattern_len < end; p++) {
                if (memcmp(p, pattern, pattern_len))
                        continue;
                PARA_DEBUG_LOG("found %d byte pattern@%d\n",
-                       pattern_len, p - buf);
+                       pattern_len, (int)(p - buf));
                return p;
        }
        PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
@@ -46,43 +54,50 @@ static int find_audio_stream_info(const char *buf, int len)
        const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
        const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
 
-       if (!p) {
-               PARA_NOTICE_LOG("audio stream guid not found");
-               return -1;
-       }
-       PARA_DEBUG_LOG("found audio stream guid@%0zx\n", p - buf);
-       return p - buf;
+       if (!p)
+               return -E_WMA_NO_GUID;
+       PARA_DEBUG_LOG("found audio stream guid@%0x\n", (int)(p - buf));
+       return p - buf + 16;
 }
 
-static int read_header_len(char *buf, int len)
+static int read_header_len(const char *buf, int len)
 {
        uint16_t header_len;
 
        if (len < 18)
-               return -1;
+               return 0;
        header_len = read_u16(buf + 16) + 46;
-       if (header_len > len)
-               return -2;
        PARA_DEBUG_LOG("header_len: %d\n", header_len);
        return header_len;
 }
 
-int read_asf_header(char *buf, int loaded, struct asf_header_info *ahi)
+/**
+ * Read an asf audio file header.
+ *
+ * \param buf The input buffer.
+ * \param loaded Number of bytes in \a buf.
+ * \param ahi Result pointer.
+ *
+ * \return Negative on errors, zero if more data is needed in order to read the
+ * full header, 1 on success.
+ */
+int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
 {
        int ret;
-       char *start;
-
-       ret = read_header_len(buf, loaded);
-       if (ret < 0)
-               return ret;
-       ahi->header_len = ret;
+       const char *start;
 
+       ahi->header_len = read_header_len(buf, loaded);
+       if (ahi->header_len == 0) /* too short to read header len */
+               return 0;
+       if (ahi->header_len > loaded) /* too short to read header */
+               return 0;
        ret = find_audio_stream_info(buf, ahi->header_len);
        if (ret < 0)
                return ret;
-       ahi->audio_stream_info_start = ret + 16;
+       if (ret + 62 > loaded)
+               return 0;
+       ahi->audio_stream_info_start = ret;
        start = buf + ahi->audio_stream_info_start;
-
        ahi->channels = ((uint8_t *)start)[40];
        ahi->sample_rate = read_u16(start + 42);
        PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
@@ -98,7 +113,7 @@ int read_asf_header(char *buf, int loaded, struct asf_header_info *ahi)
        ahi->flags2 = read_u16(start + 60);
        PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
                ahi->flags1, ahi->flags2);
-       return 42;
+       return 1;
 }
 
 const uint8_t log2_tab[256] = {
@@ -120,6 +135,13 @@ const uint8_t log2_tab[256] = {
        7, 7, 7, 7, 7, 7, 7, 7
 };
 
+/**
+ * Compute the base-2 logarithm.
+ *
+ * \param v The value to compute the logarithm of.
+ *
+ * \return An integer approximation of log2(v).
+ */
 int wma_log2(unsigned int v)
 {
        int n = 0;