shithub: opus

Download patch

ref: 5215623dcd5a26474239f6f817c153b7f3e02d73
parent: 7eb3c2b7d036a6c3bc63e269d3283cc117773498
author: Conrad Parker <[email protected]>
date: Wed Feb 13 11:31:36 EST 2008

ensures that the celt header is written as little endian, and also checks
that the passed size parameter is not < 56.

--- a/libcelt/header.c
+++ b/libcelt/header.c
@@ -29,6 +29,10 @@
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "celt_header.h"
 #include "os_support.h"
 
@@ -44,6 +48,19 @@
    celt_int32_t extra_headers;
 } CELTHeader;*/
 
+static  celt_uint32_t
+_le_32 (celt_uint32_t i)
+{
+   celt_uint32_t ret=i;
+#ifdef WORDS_BIGENDIAN
+   ret =  (i>>24);
+   ret += (i>>8) & 0x0000ff00;
+   ret += (i<<8) & 0x00ff0000;
+   ret += (i<<24);
+#endif
+   return ret;
+}
+
 void celt_header_init(CELTHeader *header, celt_int32_t rate, celt_int32_t nb_channels, const CELTMode *m)
 {
    CELT_COPY(header->codec_id, "CELT    ", 8);
@@ -60,9 +77,26 @@
 
 int celt_header_to_packet(const CELTHeader *header, unsigned char *packet, celt_uint32_t size)
 {
+   celt_int32_t * h;
+
+   if (size < 56) return CELT_BAD_ARG; /* FAIL */
+
    CELT_MEMSET(packet, 0, sizeof(*header));
-   /* FIXME: Do it in a endian-safe, alignment-safe, overflow-safe manner */
-   CELT_COPY(packet, (unsigned char*)header, sizeof(*header));
+   /* FIXME: Do it in an alignment-safe manner */
+
+   /* Copy ident and version */
+   CELT_COPY(packet, (unsigned char*)header, 28);
+
+   /* Copy the int32 fields */
+   h = (celt_int32_t*)(packet+28);
+   *h++ = _le_32 (header->version_id);
+   *h++ = _le_32 (header->header_size);
+   *h++ = _le_32 (header->mode);
+   *h++ = _le_32 (header->sample_rate);
+   *h++ = _le_32 (header->nb_channels);
+   *h++ = _le_32 (header->bytes_per_packet);
+   *h++ = _le_32 (header->extra_headers);
+
    return sizeof(*header);
 }