xref: /aosp_15_r20/external/brotli/python/brotli.py (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker# Copyright 2016 The Brotli Authors. All rights reserved.
2*f4ee7fbaSAndroid Build Coastguard Worker#
3*f4ee7fbaSAndroid Build Coastguard Worker# Distributed under MIT license.
4*f4ee7fbaSAndroid Build Coastguard Worker# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5*f4ee7fbaSAndroid Build Coastguard Worker
6*f4ee7fbaSAndroid Build Coastguard Worker"""Functions to compress and decompress data using the Brotli library."""
7*f4ee7fbaSAndroid Build Coastguard Worker
8*f4ee7fbaSAndroid Build Coastguard Workerimport _brotli
9*f4ee7fbaSAndroid Build Coastguard Worker
10*f4ee7fbaSAndroid Build Coastguard Worker
11*f4ee7fbaSAndroid Build Coastguard Worker# The library version.
12*f4ee7fbaSAndroid Build Coastguard Worker__version__ = _brotli.__version__
13*f4ee7fbaSAndroid Build Coastguard Worker
14*f4ee7fbaSAndroid Build Coastguard Worker# The compression mode.
15*f4ee7fbaSAndroid Build Coastguard WorkerMODE_GENERIC = _brotli.MODE_GENERIC
16*f4ee7fbaSAndroid Build Coastguard WorkerMODE_TEXT = _brotli.MODE_TEXT
17*f4ee7fbaSAndroid Build Coastguard WorkerMODE_FONT = _brotli.MODE_FONT
18*f4ee7fbaSAndroid Build Coastguard Worker
19*f4ee7fbaSAndroid Build Coastguard Worker# The Compressor object.
20*f4ee7fbaSAndroid Build Coastguard WorkerCompressor = _brotli.Compressor
21*f4ee7fbaSAndroid Build Coastguard Worker
22*f4ee7fbaSAndroid Build Coastguard Worker# The Decompressor object.
23*f4ee7fbaSAndroid Build Coastguard WorkerDecompressor = _brotli.Decompressor
24*f4ee7fbaSAndroid Build Coastguard Worker
25*f4ee7fbaSAndroid Build Coastguard Worker# Compress a byte string.
26*f4ee7fbaSAndroid Build Coastguard Workerdef compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0):
27*f4ee7fbaSAndroid Build Coastguard Worker    """Compress a byte string.
28*f4ee7fbaSAndroid Build Coastguard Worker
29*f4ee7fbaSAndroid Build Coastguard Worker    Args:
30*f4ee7fbaSAndroid Build Coastguard Worker      string (bytes): The input data.
31*f4ee7fbaSAndroid Build Coastguard Worker      mode (int, optional): The compression mode can be MODE_GENERIC (default),
32*f4ee7fbaSAndroid Build Coastguard Worker        MODE_TEXT (for UTF-8 format text input) or MODE_FONT (for WOFF 2.0).
33*f4ee7fbaSAndroid Build Coastguard Worker      quality (int, optional): Controls the compression-speed vs compression-
34*f4ee7fbaSAndroid Build Coastguard Worker        density tradeoff. The higher the quality, the slower the compression.
35*f4ee7fbaSAndroid Build Coastguard Worker        Range is 0 to 11. Defaults to 11.
36*f4ee7fbaSAndroid Build Coastguard Worker      lgwin (int, optional): Base 2 logarithm of the sliding window size. Range
37*f4ee7fbaSAndroid Build Coastguard Worker        is 10 to 24. Defaults to 22.
38*f4ee7fbaSAndroid Build Coastguard Worker      lgblock (int, optional): Base 2 logarithm of the maximum input block size.
39*f4ee7fbaSAndroid Build Coastguard Worker        Range is 16 to 24. If set to 0, the value will be set based on the
40*f4ee7fbaSAndroid Build Coastguard Worker        quality. Defaults to 0.
41*f4ee7fbaSAndroid Build Coastguard Worker
42*f4ee7fbaSAndroid Build Coastguard Worker    Returns:
43*f4ee7fbaSAndroid Build Coastguard Worker      The compressed byte string.
44*f4ee7fbaSAndroid Build Coastguard Worker
45*f4ee7fbaSAndroid Build Coastguard Worker    Raises:
46*f4ee7fbaSAndroid Build Coastguard Worker      brotli.error: If arguments are invalid, or compressor fails.
47*f4ee7fbaSAndroid Build Coastguard Worker    """
48*f4ee7fbaSAndroid Build Coastguard Worker    compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin,
49*f4ee7fbaSAndroid Build Coastguard Worker                            lgblock=lgblock)
50*f4ee7fbaSAndroid Build Coastguard Worker    return compressor.process(string) + compressor.finish()
51*f4ee7fbaSAndroid Build Coastguard Worker
52*f4ee7fbaSAndroid Build Coastguard Worker# Decompress a compressed byte string.
53*f4ee7fbaSAndroid Build Coastguard Workerdecompress = _brotli.decompress
54*f4ee7fbaSAndroid Build Coastguard Worker
55*f4ee7fbaSAndroid Build Coastguard Worker# Raised if compression or decompression fails.
56*f4ee7fbaSAndroid Build Coastguard Workererror = _brotli.error
57