]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c
MdeModulePkg: Copy Brotli algorithm 3rd party source code for library
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / dec / bit_reader.c
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c
new file mode 100644 (file)
index 0000000..83dcc36
--- /dev/null
@@ -0,0 +1,48 @@
+/* Copyright 2013 Google Inc. All Rights Reserved.\r
+\r
+   Distributed under MIT license.\r
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r
+*/\r
+\r
+/* Bit reading helpers */\r
+\r
+#include "./bit_reader.h"\r
+\r
+#include "../common/types.h"\r
+#include "./port.h"\r
+\r
+#if defined(__cplusplus) || defined(c_plusplus)\r
+extern "C" {\r
+#endif\r
+\r
+void BrotliInitBitReader(BrotliBitReader* const br) {\r
+  br->val_ = 0;\r
+  br->bit_pos_ = sizeof(br->val_) << 3;\r
+}\r
+\r
+BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {\r
+  size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1;\r
+  /* Fixing alignment after unaligned BrotliFillWindow would result accumulator\r
+     overflow. If unalignment is caused by BrotliSafeReadBits, then there is\r
+     enough space in accumulator to fix aligment. */\r
+  if (!BROTLI_ALIGNED_READ) {\r
+    aligned_read_mask = 0;\r
+  }\r
+  if (BrotliGetAvailableBits(br) == 0) {\r
+    if (!BrotliPullByte(br)) {\r
+      return BROTLI_FALSE;\r
+    }\r
+  }\r
+\r
+  while ((((size_t)br->next_in) & aligned_read_mask) != 0) {\r
+    if (!BrotliPullByte(br)) {\r
+      /* If we consumed all the input, we don't care about the alignment. */\r
+      return BROTLI_TRUE;\r
+    }\r
+  }\r
+  return BROTLI_TRUE;\r
+}\r
+\r
+#if defined(__cplusplus) || defined(c_plusplus)\r
+}  /* extern "C" */\r
+#endif\r