1 /* Copyright 2013 Google Inc. All Rights Reserved.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
15 Functions for streaming input and output.
22 #include "./streams.h"
24 #if defined(__cplusplus) || defined(c_plusplus)
28 int BrotliMemInputFunction(void* data
, uint8_t* buf
, size_t count
) {
29 BrotliMemInput
* input
= (BrotliMemInput
*)data
;
30 if (input
->pos
> input
->length
) {
33 if (input
->pos
+ count
> input
->length
) {
34 count
= input
->length
- input
->pos
;
36 memcpy(buf
, input
->buffer
+ input
->pos
, count
);
41 BrotliInput
BrotliInitMemInput(const uint8_t* buffer
, size_t length
,
42 BrotliMemInput
* mem_input
) {
44 mem_input
->buffer
= buffer
;
45 mem_input
->length
= length
;
47 input
.cb_
= &BrotliMemInputFunction
;
48 input
.data_
= mem_input
;
52 int BrotliMemOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
53 BrotliMemOutput
* output
= (BrotliMemOutput
*)data
;
54 if (output
->pos
+ count
> output
->length
) {
57 memcpy(output
->buffer
+ output
->pos
, buf
, count
);
62 BrotliOutput
BrotliInitMemOutput(uint8_t* buffer
, size_t length
,
63 BrotliMemOutput
* mem_output
) {
65 mem_output
->buffer
= buffer
;
66 mem_output
->length
= length
;
68 output
.cb_
= &BrotliMemOutputFunction
;
69 output
.data_
= mem_output
;
73 int BrotliStdinInputFunction(void* data
, uint8_t* buf
, size_t count
) {
75 return (int)read(STDIN_FILENO
, buf
, count
);
81 BrotliInput
BrotliStdinInput() {
83 in
.cb_
= BrotliStdinInputFunction
;
88 int BrotliStdoutOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
90 return (int)write(STDOUT_FILENO
, buf
, count
);
96 BrotliOutput
BrotliStdoutOutput() {
98 out
.cb_
= BrotliStdoutOutputFunction
;
103 int BrotliFileInputFunction(void* data
, uint8_t* buf
, size_t count
) {
104 return (int)fread(buf
, 1, count
, (FILE*)data
);
107 BrotliInput
BrotliFileInput(FILE* f
) {
109 in
.cb_
= BrotliFileInputFunction
;
114 int BrotliFileOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
115 return (int)fwrite(buf
, 1, count
, (FILE*)data
);
118 BrotliOutput
BrotliFileOutput(FILE* f
) {
120 out
.cb_
= BrotliFileOutputFunction
;
126 #if defined(__cplusplus) || defined(c_plusplus)