]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/BrotliCompress/enc/prefix.h
BaseTools: Copy Brotli algorithm 3rd party source code for tool
[mirror_edk2.git] / BaseTools / Source / C / BrotliCompress / enc / prefix.h
diff --git a/BaseTools/Source/C/BrotliCompress/enc/prefix.h b/BaseTools/Source/C/BrotliCompress/enc/prefix.h
new file mode 100644 (file)
index 0000000..850c21e
--- /dev/null
@@ -0,0 +1,52 @@
+/* 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
+/* Functions for encoding of integers into prefix codes the amount of extra\r
+   bits, and the actual values of the extra bits. */\r
+\r
+#ifndef BROTLI_ENC_PREFIX_H_\r
+#define BROTLI_ENC_PREFIX_H_\r
+\r
+#include "../common/constants.h"\r
+#include "../common/port.h"\r
+#include "../common/types.h"\r
+#include "./fast_log.h"\r
+\r
+#if defined(__cplusplus) || defined(c_plusplus)\r
+extern "C" {\r
+#endif\r
+\r
+static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code,\r
+                                                   size_t num_direct_codes,\r
+                                                   size_t postfix_bits,\r
+                                                   uint16_t* code,\r
+                                                   uint32_t* extra_bits) {\r
+  if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) {\r
+    *code = (uint16_t)distance_code;\r
+    *extra_bits = 0;\r
+    return;\r
+  } else {\r
+    size_t dist = ((size_t)1 << (postfix_bits + 2u)) +\r
+        (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes);\r
+    size_t bucket = Log2FloorNonZero(dist) - 1;\r
+    size_t postfix_mask = (1u << postfix_bits) - 1;\r
+    size_t postfix = dist & postfix_mask;\r
+    size_t prefix = (dist >> bucket) & 1;\r
+    size_t offset = (2 + prefix) << bucket;\r
+    size_t nbits = bucket - postfix_bits;\r
+    *code = (uint16_t)(\r
+        (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes +\r
+         ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix));\r
+    *extra_bits = (uint32_t)(\r
+        (nbits << 24) | ((dist - offset) >> postfix_bits));\r
+  }\r
+}\r
+\r
+#if defined(__cplusplus) || defined(c_plusplus)\r
+}  /* extern "C" */\r
+#endif\r
+\r
+#endif  /* BROTLI_ENC_PREFIX_H_ */\r