Remove unnecessary system header includes.
[paraslash.git] / wma_common.c
index be195e665cfb5eef751d678a8d41a00ce1701aed..6d01972d0c46f06982a300c8f95d633c00f5f857 100644 (file)
@@ -1,18 +1,12 @@
 /*
- * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2009-2014 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file wma_common.c Functions used by both the WMA afh and decoder. */
 
-#include <inttypes.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
 
 #include "para.h"
 #include "error.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.
+ *
+ * \return A pointer into \a buf or \p NULL if the pattern was not found.
+ */
+const char *search_pattern(const char *pattern, int pattern_len,
+               const char *buf, int buf_size)
 {
        const char *p, *end = buf + buf_size;
 
+       /* 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,11 +52,11 @@ static int find_audio_stream_info(const char *buf, int len)
 
        if (!p)
                return -E_WMA_NO_GUID;
-       PARA_DEBUG_LOG("found audio stream guid@%0zx\n", p - buf);
-       return p - buf;
+       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;
 
@@ -62,13 +68,19 @@ static int read_header_len(char *buf, int len)
 }
 
 /**
+ * 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. Positive header length on success.
+ * full header, 1 on success.
  */
-int read_asf_header(char *buf, int loaded, struct asf_header_info *ahi)
+int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
 {
        int ret;
-       char *start;
+       const char *start;
 
        ahi->header_len = read_header_len(buf, loaded);
        if (ahi->header_len == 0) /* too short to read header len */
@@ -78,10 +90,10 @@ int read_asf_header(char *buf, int loaded, struct asf_header_info *ahi)
        ret = find_audio_stream_info(buf, ahi->header_len);
        if (ret < 0)
                return ret;
-       /* FIXME: Check ranges */
-       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,
@@ -97,10 +109,13 @@ 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);
+       ahi->use_exp_vlc = ahi->flags2 & 0x0001;
+       ahi->use_bit_reservoir = ahi->flags2 & 0x0002;
+       ahi->use_variable_block_len = ahi->flags2 & 0x0004;
        return 1;
 }
 
-const uint8_t log2_tab[256] = {
+static const uint8_t log2_tab[256] = {
        0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4,
        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
@@ -119,7 +134,14 @@ const uint8_t log2_tab[256] = {
        7, 7, 7, 7, 7, 7, 7, 7
 };
 
-int wma_log2(unsigned int v)
+/**
+ * Compute the base-2 logarithm.
+ *
+ * \param v The value to compute the logarithm of.
+ *
+ * \return An integer approximation of log2(v).
+ */
+__a_const int wma_log2(unsigned int v)
 {
        int n = 0;
        if (v & 0xffff0000) {