5c8d05
diff --git a/Brewfile b/Brewfile
5c8d05
new file mode 100644
5c8d05
index 0000000..af81e5b
5c8d05
--- /dev/null
5c8d05
+++ b/Brewfile
5c8d05
@@ -0,0 +1,3 @@
5c8d05
+brew 'doxygen'
5c8d05
+brew 'libogg'
5c8d05
+brew 'xz'
5c8d05
diff --git a/Makefile.am b/Makefile.am
5c8d05
index c35131a..3feaf72 100644
5c8d05
--- a/Makefile.am
5c8d05
+++ b/Makefile.am
5c8d05
@@ -26,7 +26,7 @@ EXTRA_DIST = \
5c8d05
 	vorbisenc-uninstalled.pc.in \
5c8d05
 	vorbisfile-uninstalled.pc.in \
5c8d05
 	symbian \
5c8d05
-	macosx win32
5c8d05
+	macosx win32 CMakeLists.txt
5c8d05
 
5c8d05
 
5c8d05
 DISTCHECK_CONFIGURE_FLAGS = --enable-docs
5c8d05
diff --git a/contrib/oss-fuzz/build.sh b/contrib/oss-fuzz/build.sh
5c8d05
new file mode 100755
5c8d05
index 0000000..29e7f38
5c8d05
--- /dev/null
5c8d05
+++ b/contrib/oss-fuzz/build.sh
5c8d05
@@ -0,0 +1,23 @@
5c8d05
+#!/bin/bash -eu
5c8d05
+
5c8d05
+pushd $SRC
5c8d05
+mv people.xiph.org/*.ogg decode_corpus/
5c8d05
+zip -r "$OUT/decode_fuzzer_seed_corpus.zip" decode_corpus/
5c8d05
+popd
5c8d05
+
5c8d05
+pushd $SRC/ogg
5c8d05
+./autogen.sh
5c8d05
+./configure --prefix="$WORK" --enable-static --disable-shared --disable-crc
5c8d05
+make clean
5c8d05
+make -j$(nproc)
5c8d05
+make install
5c8d05
+popd
5c8d05
+
5c8d05
+
5c8d05
+./autogen.sh
5c8d05
+./configure --prefix="$WORK" --enable-static --disable-shared
5c8d05
+make clean
5c8d05
+make -j$(nproc)
5c8d05
+make install
5c8d05
+
5c8d05
+$CXX $CXXFLAGS $SRC/vorbis/contrib/oss-fuzz/decode_fuzzer.cc -o $OUT/decode_fuzzer -L"$WORK/lib" -I"$WORK/include" -lFuzzingEngine -lvorbisfile -lvorbis -logg
5c8d05
diff --git a/contrib/oss-fuzz/decode_fuzzer.cc b/contrib/oss-fuzz/decode_fuzzer.cc
5c8d05
new file mode 100644
5c8d05
index 0000000..b8840c1
5c8d05
--- /dev/null
5c8d05
+++ b/contrib/oss-fuzz/decode_fuzzer.cc
5c8d05
@@ -0,0 +1,48 @@
5c8d05
+#include <stdio.h>
5c8d05
+#include <string.h>
5c8d05
+#include <cstdint>
5c8d05
+#include <vorbis/vorbisfile.h>
5c8d05
+
5c8d05
+struct vorbis_data {
5c8d05
+  const uint8_t *current;
5c8d05
+  const uint8_t *data;
5c8d05
+  size_t size;
5c8d05
+};
5c8d05
+
5c8d05
+size_t read_func(void *ptr, size_t size1, size_t size2, void *datasource) {
5c8d05
+  vorbis_data* vd = (vorbis_data *)(datasource);
5c8d05
+  size_t len = size1 * size2;
5c8d05
+  if (vd->current + len > vd->data + vd->size) {
5c8d05
+      len = vd->data + vd->size - vd->current;
5c8d05
+  }
5c8d05
+  memcpy(ptr, vd->current, len);
5c8d05
+  vd->current += len;
5c8d05
+  return len;
5c8d05
+}
5c8d05
+
5c8d05
+
5c8d05
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
5c8d05
+  ov_callbacks memory_callbacks = {0};
5c8d05
+  memory_callbacks.read_func = read_func;
5c8d05
+  vorbis_data data_st;
5c8d05
+  data_st.size = Size;
5c8d05
+  data_st.current = Data;
5c8d05
+  data_st.data = Data;
5c8d05
+  OggVorbis_File vf;
5c8d05
+  int result = ov_open_callbacks(&data_st, &vf, NULL, 0, memory_callbacks);
5c8d05
+  if (result < 0) {
5c8d05
+    return 0;
5c8d05
+  }
5c8d05
+  int current_section = 0;
5c8d05
+  int eof = 0;
5c8d05
+  char buf[4096];
5c8d05
+  int read_result;
5c8d05
+  while (!eof) {
5c8d05
+    read_result = ov_read(&vf, buf, sizeof(buf), 0, 2, 1, &current_section);
5c8d05
+    if (read_result != OV_HOLE && read_result <= 0) {
5c8d05
+      eof = 1;
5c8d05
+    }
5c8d05
+  }
5c8d05
+  ov_clear(&vf);
5c8d05
+  return 0;
5c8d05
+}
5c8d05
diff --git a/lib/Makefile.am b/lib/Makefile.am
5c8d05
index cd5afdf..e22895e 100644
5c8d05
--- a/lib/Makefile.am
5c8d05
+++ b/lib/Makefile.am
5c8d05
@@ -35,7 +35,7 @@ psytune_SOURCES = psytune.c
5c8d05
 psytune_LDFLAGS = -static
5c8d05
 psytune_LDADD = libvorbis.la
5c8d05
 
5c8d05
-EXTRA_DIST = lookups.pl 
5c8d05
+EXTRA_DIST = lookups.pl CMakeLists.txt
5c8d05
 
5c8d05
 # build and run the self tests on 'make check'
5c8d05
 
5c8d05
diff --git a/lib/info.c b/lib/info.c
5c8d05
index 3fbb7c7..23efa25 100644
5c8d05
--- a/lib/info.c
5c8d05
+++ b/lib/info.c
5c8d05
@@ -203,6 +203,7 @@ void vorbis_info_clear(vorbis_info *vi){
5c8d05
 
5c8d05
 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
5c8d05
   codec_setup_info     *ci=vi->codec_setup;
5c8d05
+  int bs;
5c8d05
   if(!ci)return(OV_EFAULT);
5c8d05
 
5c8d05
   vi->version=oggpack_read(opb,32);
5c8d05
@@ -215,8 +216,12 @@ static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
5c8d05
   vi->bitrate_nominal=(ogg_int32_t)oggpack_read(opb,32);
5c8d05
   vi->bitrate_lower=(ogg_int32_t)oggpack_read(opb,32);
5c8d05
 
5c8d05
-  ci->blocksizes[0]=1<
5c8d05
-  ci->blocksizes[1]=1<
5c8d05
+  bs = oggpack_read(opb,4);
5c8d05
+  if(bs<0)goto err_out;
5c8d05
+  ci->blocksizes[0]=1<
5c8d05
+  bs = oggpack_read(opb,4);
5c8d05
+  if(bs<0)goto err_out;
5c8d05
+  ci->blocksizes[1]=1<
5c8d05
 
5c8d05
   if(vi->rate<1)goto err_out;
5c8d05
   if(vi->channels<1)goto err_out;
5c8d05
diff --git a/lib/os.h b/lib/os.h
5c8d05
index 416a401..e098926 100644
5c8d05
--- a/lib/os.h
5c8d05
+++ b/lib/os.h
5c8d05
@@ -120,7 +120,7 @@ static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
5c8d05
 /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
5c8d05
  * 64 bit compiler and doesn't work on arm. */
5c8d05
 #if defined(_MSC_VER) && !defined(_WIN64) && \
5c8d05
-      !defined(_WIN32_WCE) && !defined(_M_ARM)
5c8d05
+      !defined(_WIN32_WCE) && !defined(_M_ARM) && !defined(_M_ARM64)
5c8d05
 #  define VORBIS_FPU_CONTROL
5c8d05
 
5c8d05
 typedef ogg_int16_t vorbis_fpu_control;
5c8d05
diff --git a/lib/psy.c b/lib/psy.c
5c8d05
index 422c6f1..1310123 100644
5c8d05
--- a/lib/psy.c
5c8d05
+++ b/lib/psy.c
5c8d05
@@ -602,8 +602,9 @@ static void bark_noise_hybridmp(int n,const long *b,
5c8d05
   for (i = 0, x = 0.f;; i++, x += 1.f) {
5c8d05
 
5c8d05
     lo = b[i] >> 16;
5c8d05
-    if( lo>=0 ) break;
5c8d05
     hi = b[i] & 0xffff;
5c8d05
+    if( lo>=0 ) break;
5c8d05
+    if( hi>=n ) break;
5c8d05
 
5c8d05
     tN = N[hi] + N[-lo];
5c8d05
     tX = X[hi] - X[-lo];
5c8d05
diff --git a/lib/sharedbook.c b/lib/sharedbook.c
5c8d05
index 4545d4f..8d73daa 100644
5c8d05
--- a/lib/sharedbook.c
5c8d05
+++ b/lib/sharedbook.c
5c8d05
@@ -62,7 +62,15 @@ float _float32_unpack(long val){
5c8d05
   int    sign=val&0x80000000;
5c8d05
   long   exp =(val&0x7fe00000L)>>VQ_FMAN;
5c8d05
   if(sign)mant= -mant;
5c8d05
-  return(ldexp(mant,exp-(VQ_FMAN-1)-VQ_FEXP_BIAS));
5c8d05
+  exp=exp-(VQ_FMAN-1)-VQ_FEXP_BIAS;
5c8d05
+  /* clamp excessive exponent values */
5c8d05
+  if (exp>63){
5c8d05
+    exp=63;
5c8d05
+  }
5c8d05
+  if (exp<-63){
5c8d05
+    exp-63;
5c8d05
+  }
5c8d05
+  return(ldexp(mant,exp));
5c8d05
 }
5c8d05
 
5c8d05
 /* given a list of word lengths, generate a list of codewords.  Works
5c8d05
diff --git a/lib/vorbisenc.c b/lib/vorbisenc.c
5c8d05
index 4a4607c..64a51b5 100644
5c8d05
--- a/lib/vorbisenc.c
5c8d05
+++ b/lib/vorbisenc.c
5c8d05
@@ -684,6 +684,7 @@ int vorbis_encode_setup_init(vorbis_info *vi){
5c8d05
   highlevel_encode_setup *hi=&ci->hi;
5c8d05
 
5c8d05
   if(ci==NULL)return(OV_EINVAL);
5c8d05
+  if(vi->channels<1||vi->channels>255)return(OV_EINVAL);
5c8d05
   if(!hi->impulse_block_p)i0=1;
5c8d05
 
5c8d05
   /* too low/high an ATH floater is nonsensical, but doesn't break anything */
5c8d05
@@ -1210,7 +1211,7 @@ int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
5c8d05
                                           hi->req,
5c8d05
                                           hi->managed,
5c8d05
                                           &new_base);
5c8d05
-        if(!hi->setup)return OV_EIMPL;
5c8d05
+        if(!new_template)return OV_EIMPL;
5c8d05
         hi->setup=new_template;
5c8d05
         hi->base_setting=new_base;
5c8d05
         vorbis_encode_setup_setting(vi,vi->channels,vi->rate);