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