brotli-no-stdlib 1.0.0

A brotli decompressor that does not use the rust stdlib. This makes it suitable for embedded devices and kernels. It is designed with a pluggable allocator so that the standard lib's allocator may be employed. For users wishing to use the stdlib, the brotli package, which calls into and abstracts this package, is recommended.
Documentation
diff --git a/src/lib.rs b/src/lib.rs
index 953ebf5..7a22cf9 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,8 +5,8 @@
 #![allow(non_snake_case)]
 #![allow(non_upper_case_globals)]
 
-//#[macro_use] //<-- for debugging, remove println from bit_reader
-//extern crate std;
+#[macro_use] //<-- for debugging, remove println from bit_reader
+extern crate std;
 
 #[macro_use]
 extern crate alloc_no_stdlib as alloc;
@@ -1102,6 +1102,12 @@ fn DecodeContextMapInner<
           return BROTLI_FAILURE();
         }
         if (*num_htrees <= 1) {
+          let mut index : usize = 0;
+          while index < context_map_size as usize {
+              context_map_arg.slice_mut()[index] = 0;
+             index += 1;
+          }
+//FIXME no it doesnt
           // NOP -- Rust malloc does it memset(*context_map_arg, 0, (size_t)context_map_size);
           return BrotliResult::ResultSuccess;
         }
@@ -1209,10 +1215,12 @@ fn DecodeContextMapInner<
           return BrotliResult::NeedsMoreInput;
         }
         if (bits != 0) {
+println!("move to front {:?}", context_map_arg.slice());
           InverseMoveToFrontTransform(context_map_arg.slice_mut(),
                                       context_map_size,
                                       &mut s.mtf,
                                       &mut s.mtf_upper_bound);
+println!("done to front {:?}", context_map_arg.slice());
         }
         s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE;
         return BrotliResult::ResultSuccess;
@@ -1249,6 +1257,7 @@ fn DecodeContextMap<
   }
 
   let retval = DecodeContextMapInner(context_map_size, &mut num_htrees, &mut context_map_arg, &mut s, input);
+println!("cont arg {:} {:} {:} {:}\n", context_map_arg.slice()[0], context_map_arg.slice()[1], context_map_arg.slice()[2], context_map_arg.slice()[3]);
   if is_dist_context_map {
     s.num_dist_htrees = num_htrees;
     mem::replace(&mut s.dist_context_map, mem::replace(&mut context_map_arg,
@@ -1412,6 +1421,10 @@ fn DecodeDistanceBlockSwitchInternal<
   s.dist_context_map_slice_index = (s.block_type_length_state.block_type_rb[5] << kDistanceContextBits) as usize;
   s.dist_htree_index = s.dist_context_map.slice()[s.dist_context_map_slice_index
                                                   + s.distance_context as usize];
+BROTLI_LOG_UINT!(s.dist_context_map_slice_index);
+println!("COMMAND BLOCK INDEX");
+BROTLI_LOG_UINT!(s.dist_htree_index);
+BROTLI_LOG_UINT!(666);
   return true;
 }
 
@@ -1643,18 +1656,36 @@ pub fn SafeReadBits(br : &mut bit_reader::BrotliBitReader, n_bits : u32, val : &
     return true;
   }
 }
+pub fn Logme() {
+     println!("WHoy");
+
+}
+pub fn LogmeA(i : i32) {
+     println!("WHoy {:}",i);
 
+}
+static mut dist_count : i32 = 0;
 /* Precondition: s.distance_code < 0 */
 pub fn ReadDistanceInternal<'a, AllocU8 : alloc::Allocator<u8>,
            AllocU32 : alloc::Allocator<u32>,
            AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
     s : &mut BrotliState<AllocU8, AllocU32, AllocHC>,
     input : &[u8]) -> bool {
+  BROTLI_LOG_UINT!(s.br.avail_in);
+BROTLI_LOG_UINT!(s.dist_htree_index);
+unsafe{dist_count += 1;}
+unsafe{LogmeA(dist_count);}
   let mut distval : i32;
   let mut memento = bit_reader::BrotliBitReaderState::default();
+  if (s.br.avail_in == 3594) {
+BROTLI_LOG_UINT!(s.distance_hgroup.num_htrees);
+Logme();
+  }
   if (!safe) {
+    let xx = s.distance_hgroup.htrees.slice()[s.dist_htree_index as usize];
+    LogmeA(xx as i32);
     s.distance_code
-        = ReadSymbol(&s.distance_hgroup.codes.slice()[s.distance_hgroup.htrees.slice()[s.dist_htree_index as usize] as usize ..],
+        = ReadSymbol(&s.distance_hgroup.codes.slice()[xx as usize ..],
                      &mut s.br,
                      input) as i32;
   } else {
@@ -1665,6 +1696,10 @@ pub fn ReadDistanceInternal<'a, AllocU8 : alloc::Allocator<u8>,
     }
     s.distance_code = code as i32;
   }
+  if (s.br.avail_in == 3594) {
+Logme();
+  }
+
   /* Convert the distance code to the actual distance by possibly */
   /* looking up past distances from the s.ringbuffer. */
   if ((s.distance_code as u64 & 0xfffffffffffffff0) == 0) {
@@ -1672,6 +1707,9 @@ pub fn ReadDistanceInternal<'a, AllocU8 : alloc::Allocator<u8>,
     s.block_type_length_state.block_length[2] -= 1;
     return true;
   }
+  if (s.br.avail_in == 3594) {
+Logme();
+  }
   distval = s.distance_code - s.num_direct_distance_codes as i32;
   if (distval >= 0) {
     let nbits : u32;
@@ -1680,14 +1718,17 @@ pub fn ReadDistanceInternal<'a, AllocU8 : alloc::Allocator<u8>,
     if (!safe && (s.distance_postfix_bits == 0)) {
       nbits = (distval as u32 >> 1) + 1;
       offset = ((2 + (distval & 1)) << nbits) - 4;
+LogmeA(-1);
       s.distance_code = s.num_direct_distance_codes as i32 +
           offset + bit_reader::BrotliReadBits(&mut s.br, nbits, input) as i32;
+LogmeA(0);
     } else {
       /* This branch also works well when s.distance_postfix_bits == 0 */
       let mut bits : u32 = 0;
       postfix = distval & s.distance_postfix_mask;
       distval >>= s.distance_postfix_bits;
       nbits = (distval as u32 >> 1) + 1;
+LogmeA(-2);
       if (safe) {
         if (!SafeReadBits(&mut s.br, nbits, &mut bits, input)) {
           s.distance_code = -1; /* Restore precondition. */
@@ -1698,6 +1739,7 @@ pub fn ReadDistanceInternal<'a, AllocU8 : alloc::Allocator<u8>,
         bits = bit_reader::BrotliReadBits(&mut s.br, nbits, input);
       }
       offset = ((2 + (distval & 1)) << nbits) - 4;
+LogmeA(1);
       s.distance_code = s.num_direct_distance_codes as i32 +
           ((offset + bits as i32) << s.distance_postfix_bits) + postfix;
     }
@@ -1720,7 +1762,7 @@ pub fn SafeReadDistance<'a, AllocU8 : alloc::Allocator<u8>,
     s : &mut BrotliState<AllocU8, AllocU32, AllocHC>, input : &[u8]) -> bool {
   return ReadDistanceInternal(true, s, input);
 }
-
+static mut cbc :i32 = 0;
 pub fn ReadCommandInternal<'a, AllocU8 : alloc::Allocator<u8>,
            AllocU32 : alloc::Allocator<u32>,
            AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
@@ -1748,6 +1790,13 @@ pub fn ReadCommandInternal<'a, AllocU8 : alloc::Allocator<u8>,
   s.distance_context = v.context as i32;
   s.dist_htree_index = s.dist_context_map.slice()[s.dist_context_map_slice_index
                                                   + s.distance_context as usize];
+unsafe{cbc += 1;}
+unsafe{println!("CBC: {:} code:{:}", cbc,cmd_code);}
+BROTLI_LOG_UINT!(s.dist_context_map_slice_index);
+BROTLI_LOG_UINT!(s.context_map_slice_index);
+BROTLI_LOG_UINT!(s.distance_context);
+BROTLI_LOG_UINT!(s.distance_code);
+BROTLI_LOG_UINT!(s.dist_htree_index);
   *insert_length = v.insert_len_offset as i32;
   if (!safe) {
     if v.insert_len_extra_bits != 0 {
@@ -2483,9 +2532,9 @@ pub fn BrotliDecompressStream<'a, AllocU8 : alloc::Allocator<u8>,
               _ => break,
           }
           let mut is_trivial_context = 1;
-          let mut j = 0;
-          for context_map_item in s.context_map.slice()[0 .. (s.block_type_length_state.num_block_types[0] << kLiteralContextBits) as usize].iter() {
-            if (*context_map_item != j >> kLiteralContextBits) {
+          let mut j : usize = 0;
+          for context_map_item in s.context_map.slice()[0 .. ((s.block_type_length_state.num_block_types[0] as usize) << (kLiteralContextBits as usize))].iter() {
+            if (*context_map_item != (j >> kLiteralContextBits) as u8) {
               is_trivial_context = 0;
               break;
             }
@@ -2499,6 +2548,9 @@ pub fn BrotliDecompressStream<'a, AllocU8 : alloc::Allocator<u8>,
           {
             let num_distance_codes : u32 =
                 s.num_direct_distance_codes + (48u32 << s.distance_postfix_bits);
+            BROTLI_LOG_UINT!(num_distance_codes);
+            BROTLI_LOG_UINT!(s.block_type_length_state.num_block_types[2] << kDistanceContextBits);
+            BROTLI_LOG_UINT!(s.block_type_length_state.num_block_types[2]);
             result = DecodeContextMap(
                 s.block_type_length_state.num_block_types[2] << kDistanceContextBits,
                 true, s, local_input);
@@ -2506,6 +2558,7 @@ pub fn BrotliDecompressStream<'a, AllocU8 : alloc::Allocator<u8>,
               BrotliResult::ResultSuccess => {},
               _ => break,
             }
+println!("Decoding dist context map {:} {:} {:} {:}", s.dist_context_map.slice()[0],  s.dist_context_map.slice()[1],  s.dist_context_map.slice()[2],  s.dist_context_map.slice()[3]);
             s.literal_hgroup.init(&mut s.alloc_u32,
                                   &mut s.alloc_hc,
                                   kNumLiteralCodes, s.num_literal_htrees as u16);