Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/brotlicffi/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,14 @@ def __init__(self, dictionary=b''):
if dictionary:
self._dictionary = ffi.new("uint8_t []", dictionary)
self._dictionary_size = len(dictionary)
lib.BrotliDecoderSetCustomDictionary(
attached = lib.BrotliDecoderAttachDictionary(
self._decoder,
lib.BROTLI_SHARED_DICTIONARY_RAW,
self._dictionary_size,
self._dictionary
)
if attached != lib.BROTLI_TRUE:
raise error("Error attaching custom dictionary.")

@staticmethod
def _calculate_buffer_size(
Expand Down
11 changes: 11 additions & 0 deletions src/brotlicffi/_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"_brotlicffi",
"""#include <brotli/decode.h>
#include <brotli/encode.h>
#include <brotli/shared_dictionary.h>
""",
libraries=libraries,
include_dirs=["libbrotli/c", "libbrotli/c/include", "libbrotli/c/common"]
Expand Down Expand Up @@ -112,6 +113,16 @@

const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c);

typedef enum BrotliSharedDictionaryType {
BROTLI_SHARED_DICTIONARY_RAW = 0,
BROTLI_SHARED_DICTIONARY_SERIALIZED = 1
} BrotliSharedDictionaryType;

BROTLI_BOOL BrotliDecoderAttachDictionary(BrotliDecoderState* state,
BrotliSharedDictionaryType type,
size_t data_size,
const uint8_t* data);

/* enc/encode.h */
typedef ... BrotliEncoderState;

Expand Down
22 changes: 22 additions & 0 deletions test/test_custom_dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
"""
Regression tests for Decompressor custom dictionary support (#215).
"""
import brotlicffi


def test_decompressor_accepts_custom_dictionary():
"""
Constructing Decompressor(dictionary=...) must not raise AttributeError
for the removed BrotliDecoderSetCustomDictionary API.
"""
decompressor = brotlicffi.Decompressor(dictionary=b"custom dictionary")
assert decompressor is not None


def test_decompressor_without_dictionary_still_works():
"""Attaching no dictionary leaves ordinary decompression working."""
payload = b"hello world from brotlicffi custom dictionary regression"
compressed = brotlicffi.compress(payload)
decompressor = brotlicffi.Decompressor()
assert decompressor.decompress(compressed) + decompressor.finish() == payload