shared_dictionary.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright 2017 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* (Opaque) Shared Dictionary definition and utilities. */
  6. #ifndef BROTLI_COMMON_SHARED_DICTIONARY_H_
  7. #define BROTLI_COMMON_SHARED_DICTIONARY_H_
  8. #include <brotli/port.h>
  9. #include <brotli/types.h>
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. #define SHARED_BROTLI_MIN_DICTIONARY_WORD_LENGTH 4
  14. #define SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH 31
  15. #define SHARED_BROTLI_NUM_DICTIONARY_CONTEXTS 64
  16. #define SHARED_BROTLI_MAX_COMPOUND_DICTS 15
  17. /**
  18. * Opaque structure that holds shared dictionary data.
  19. *
  20. * Allocated and initialized with ::BrotliSharedDictionaryCreateInstance.
  21. * Cleaned up and deallocated with ::BrotliSharedDictionaryDestroyInstance.
  22. */
  23. typedef struct BrotliSharedDictionaryStruct BrotliSharedDictionary;
  24. /**
  25. * Input data type for ::BrotliSharedDictionaryAttach.
  26. */
  27. typedef enum BrotliSharedDictionaryType {
  28. /** Raw LZ77 prefix dictionary. */
  29. BROTLI_SHARED_DICTIONARY_RAW = 0,
  30. /** Serialized shared dictionary.
  31. *
  32. * DO NOT USE: methods accepting this value will fail.
  33. */
  34. BROTLI_SHARED_DICTIONARY_SERIALIZED = 1
  35. } BrotliSharedDictionaryType;
  36. /**
  37. * Creates an instance of ::BrotliSharedDictionary.
  38. *
  39. * Fresh instance has default word dictionary and transforms
  40. * and no LZ77 prefix dictionary.
  41. *
  42. * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
  43. * case they are both zero, default memory allocators are used. @p opaque is
  44. * passed to @p alloc_func and @p free_func when they are called. @p free_func
  45. * has to return without doing anything when asked to free a NULL pointer.
  46. *
  47. * @param alloc_func custom memory allocation function
  48. * @param free_func custom memory free function
  49. * @param opaque custom memory manager handle
  50. * @returns @c 0 if instance can not be allocated or initialized
  51. * @returns pointer to initialized ::BrotliSharedDictionary otherwise
  52. */
  53. BROTLI_COMMON_API BrotliSharedDictionary* BrotliSharedDictionaryCreateInstance(
  54. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
  55. /**
  56. * Deinitializes and frees ::BrotliSharedDictionary instance.
  57. *
  58. * @param dict shared dictionary instance to be cleaned up and deallocated
  59. */
  60. BROTLI_COMMON_API void BrotliSharedDictionaryDestroyInstance(
  61. BrotliSharedDictionary* dict);
  62. /**
  63. * Attaches dictionary to a given instance of ::BrotliSharedDictionary.
  64. *
  65. * Dictionary to be attached is represented in a serialized format as a region
  66. * of memory.
  67. *
  68. * Provided data it partially referenced by a resulting (compound) dictionary,
  69. * and should be kept untouched, while at least one compound dictionary uses it.
  70. * This way memory overhead is kept minimal by the cost of additional resource
  71. * management.
  72. *
  73. * @param dict dictionary to extend
  74. * @param type type of dictionary to attach
  75. * @param data_size size of @p data
  76. * @param data serialized dictionary of type @p type, with at least @p data_size
  77. * addressable bytes
  78. * @returns ::BROTLI_TRUE if provided dictionary is successfully attached
  79. * @returns ::BROTLI_FALSE otherwise
  80. */
  81. BROTLI_COMMON_API BROTLI_BOOL BrotliSharedDictionaryAttach(
  82. BrotliSharedDictionary* dict, BrotliSharedDictionaryType type,
  83. size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)]);
  84. #if defined(__cplusplus) || defined(c_plusplus)
  85. } /* extern "C" */
  86. #endif
  87. #endif /* BROTLI_COMMON_SHARED_DICTIONARY_H_ */