Allow only window values between kBrotliMinWindowBits and kBrotliMaxindowBits
[io-compress-brotli.git] / lib / IO / Compress / Brotli.pm
CommitLineData
f9995f31
MG
1package IO::Compress::Brotli;
2
3use 5.014000;
4use strict;
5use warnings;
6
09cf7186
QR
7use IO::Uncompress::Brotli;
8
9use parent qw/Exporter/;
10
11our @EXPORT = qw/bro/;
12our @EXPORT_OK = @EXPORT;
13
9d4f189a 14our $VERSION = '0.001001';
f9995f31 15
09cf7186
QR
16sub create {
17 my ($class) = @_;
18 my $state = BrotliEncoderCreateInstance();
19 bless \$state, $class
20}
21
22sub DESTROY {
23 my ($self) = @_;
24 BrotliEncoderDestroyInstance($$self)
25}
26
27sub quality {
28 my ($self, $quality) = @_;
29 BrotliEncoderSetQuality($$self, $quality)
30}
31
32sub window {
33 my ($self, $window) = @_;
34 BrotliEncoderSetWindow($$self, $window)
35}
36
37my %BROTLI_ENCODER_MODE = ( generic => 0, text => 1, font => 2 );
38sub mode {
39 my ($self, $mode) = @_;
40
41 die "Invalid encoder mode"
42 unless $BROTLI_ENCODER_MODE{$mode};
43
44 BrotliEncoderSetMode($$self, $mode)
45}
46
47use constant {
48 BROTLI_OPERATION_PROCESS => 0,
49 BROTLI_OPERATION_FLUSH => 1,
50 BROTLI_OPERATION_FINISH => 2
51};
52sub compress {
53 my ($self, $data) = @_;
54 BrotliEncoderCompressStream($$self, $data, BROTLI_OPERATION_PROCESS )
55}
56
57sub flush {
58 my ($self) = @_;
59 BrotliEncoderCompressStream($$self, '', BROTLI_OPERATION_FLUSH )
60}
61
62sub finish {
63 my ($self) = @_;
64 BrotliEncoderCompressStream($$self, '', BROTLI_OPERATION_FINISH )
65}
66
67# Untested, probably not working
68sub set_dictionary {
69 my ($self, $dict) = @_;
70 BrotliEncoderSetCustomDictionary($$self, $dict)
71}
72
f9995f31
MG
731;
74__END__
75
76=encoding utf-8
77
78=head1 NAME
79
80IO::Compress::Brotli - [Not yet implemented] Write Brotli buffers/streams
81
82=head1 SYNOPSIS
83
84 use IO::Compress::Brotli;
85
09cf7186
QR
86 # compress a buffer
87 my $encoded = bro $encoded;
88
89 # compress a stream
90 my $bro = IO::Compress::Brotli->create;
91 while(have_input()) {
92 my $block = get_input_block();
93 my $encoded_block = $bro->compress($block);
94 handle_output_block($encoded_block);
95 }
96 # Need to finish the steam
97 handle_output_block($bro->finish());
98
f9995f31
MG
99=head1 DESCRIPTION
100
09cf7186
QR
101IO::Compress::Brotli is a module that compressed Brotli buffers
102and streams. Despite its name, it is not a subclass of
103L<IO::Compress::Base> and does not implement its interface. This
104will be rectified in a future release.
105
106=head2 One-shot interface
107
108If you have the whole buffer in a Perl scalar use the B<bro>
109function.
110
111=over
112
113=item B<bro>(I<$input>)
114
115Takes a whole uncompressed buffer as input and returns the compressed
116data.
117
118Exported by default.
119
120=back
121
122=head2 Streaming interface
123
124If you want to process the data in blocks use the object oriented
125interface. The available methods are:
126
127=over
128
129=item IO::Compress::Brotli->B<create>
130
131Returns a IO::Compress::Brotli instance. Please note that a single
132instance cannot be used to decompress multiple streams.
133
134=item $bro->B<window>(I<$window>)
135
136Sets the window parameter on the brotli encoder.
137Defaults to BROTLI_DEFAULT_WINDOW (22).
138
139=item $bro->B<quality>(I<$quality>)
140
141Sets the quality paremeter on the brotli encoder.
142Defaults to BROTLI_DEFAULT_QUALITY (11).
143
144=item $bro->B<mode>(I<$mode>)
145
146Sets the brotli encoder mode, which can be any of "generic",
147"text" or "font". Defaults to "generic".
148
149=item $bro->B<compress>(I<$block>)
150
151Takes the a block of uncompressed data and returns a block of
152compressed data. Dies on error.
153
154=item $bro->B<flush>()
155
156Flushes any pending output from the encoder.
157
158=item $bro->B<finish>()
159
160Tells the encoder to start the finish operation, and flushes
161any remaining compressed output.
162
163Once finish is called, the encoder cannot be used to compress
164any more content.
165
166B<NOTE>: Calling finish is B<required>, or the output might
167remain unflushed, and the be missing termination marks.
168
169=back
170
171=head1 SEE ALSO
172
173Brotli Compressed Data Format Internet-Draft:
174L<https://www.ietf.org/id/draft-alakuijala-brotli-08.txt>
175
176Brotli source code: L<https://github.com/google/brotli/>
f9995f31
MG
177
178=head1 AUTHOR
179
180Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
181
182=head1 COPYRIGHT AND LICENSE
183
184Copyright (C) 2015 by Marius Gavrilescu
185
186This library is free software; you can redistribute it and/or modify
187it under the same terms as Perl itself, either Perl version 5.20.2 or,
188at your option, any later version of Perl 5 you may have available.
189
190
191=cut
This page took 0.020734 seconds and 4 git commands to generate.