]>
Commit | Line | Data |
---|---|---|
f9995f31 MG |
1 | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | ||
3 | Distributed under MIT license. | |
4 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | |
5 | */ | |
6 | ||
7 | /* Functions for streaming input and output. */ | |
8 | ||
9 | #include <string.h> | |
10 | #ifndef _WIN32 | |
11 | #include <unistd.h> | |
12 | #endif | |
13 | #include "./streams.h" | |
14 | ||
15 | #if defined(__cplusplus) || defined(c_plusplus) | |
16 | extern "C" { | |
17 | #endif | |
18 | ||
19 | int BrotliMemInputFunction(void* data, uint8_t* buf, size_t count) { | |
20 | BrotliMemInput* input = (BrotliMemInput*)data; | |
21 | if (input->pos > input->length) { | |
22 | return -1; | |
23 | } | |
24 | if (input->pos + count > input->length) { | |
25 | count = input->length - input->pos; | |
26 | } | |
27 | memcpy(buf, input->buffer + input->pos, count); | |
28 | input->pos += count; | |
29 | return (int)count; | |
30 | } | |
31 | ||
32 | BrotliInput BrotliInitMemInput(const uint8_t* buffer, size_t length, | |
33 | BrotliMemInput* mem_input) { | |
34 | BrotliInput input; | |
35 | mem_input->buffer = buffer; | |
36 | mem_input->length = length; | |
37 | mem_input->pos = 0; | |
38 | input.cb_ = &BrotliMemInputFunction; | |
39 | input.data_ = mem_input; | |
40 | return input; | |
41 | } | |
42 | ||
43 | int BrotliMemOutputFunction(void* data, const uint8_t* buf, size_t count) { | |
44 | BrotliMemOutput* output = (BrotliMemOutput*)data; | |
45 | size_t limit = output->length - output->pos; | |
46 | if (count > limit) { | |
47 | count = limit; | |
48 | } | |
49 | memcpy(output->buffer + output->pos, buf, count); | |
50 | output->pos += count; | |
51 | return (int)count; | |
52 | } | |
53 | ||
54 | BrotliOutput BrotliInitMemOutput(uint8_t* buffer, size_t length, | |
55 | BrotliMemOutput* mem_output) { | |
56 | BrotliOutput output; | |
57 | mem_output->buffer = buffer; | |
58 | mem_output->length = length; | |
59 | mem_output->pos = 0; | |
60 | output.cb_ = &BrotliMemOutputFunction; | |
61 | output.data_ = mem_output; | |
62 | return output; | |
63 | } | |
64 | ||
65 | int BrotliFileInputFunction(void* data, uint8_t* buf, size_t count) { | |
66 | return (int)fread(buf, 1, count, (FILE*)data); | |
67 | } | |
68 | ||
69 | BrotliInput BrotliFileInput(FILE* f) { | |
70 | BrotliInput in; | |
71 | in.cb_ = BrotliFileInputFunction; | |
72 | in.data_ = f; | |
73 | return in; | |
74 | } | |
75 | ||
76 | int BrotliFileOutputFunction(void* data, const uint8_t* buf, size_t count) { | |
77 | return (int)fwrite(buf, 1, count, (FILE*)data); | |
78 | } | |
79 | ||
80 | BrotliOutput BrotliFileOutput(FILE* f) { | |
81 | BrotliOutput out; | |
82 | out.cb_ = BrotliFileOutputFunction; | |
83 | out.data_ = f; | |
84 | return out; | |
85 | } | |
86 | ||
87 | int BrotliNullOutputFunction(void* data , const uint8_t* buf, size_t count) { | |
88 | BROTLI_UNUSED(data); | |
89 | BROTLI_UNUSED(buf); | |
90 | return (int)count; | |
91 | } | |
92 | ||
93 | BrotliOutput BrotliNullOutput(void) { | |
94 | BrotliOutput out; | |
95 | out.cb_ = BrotliNullOutputFunction; | |
96 | out.data_ = NULL; | |
97 | return out; | |
98 | } | |
99 | ||
100 | #if defined(__cplusplus) || defined(c_plusplus) | |
101 | } /* extern "C" */ | |
102 | #endif |