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.
16 /* 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
) {
74 (void) data
; /* Shut up LLVM */
76 return (int)read(STDIN_FILENO
, buf
, count
);
82 BrotliInput
BrotliStdinInput() {
84 in
.cb_
= BrotliStdinInputFunction
;
89 int BrotliStdoutOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
90 (void) data
; /* Shut up LLVM */
92 return (int)write(STDOUT_FILENO
, buf
, count
);
98 BrotliOutput
BrotliStdoutOutput() {
100 out
.cb_
= BrotliStdoutOutputFunction
;
105 int BrotliFileInputFunction(void* data
, uint8_t* buf
, size_t count
) {
106 return (int)fread(buf
, 1, count
, (FILE*)data
);
109 BrotliInput
BrotliFileInput(FILE* f
) {
111 in
.cb_
= BrotliFileInputFunction
;
116 int BrotliFileOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
117 return (int)fwrite(buf
, 1, count
, (FILE*)data
);
120 BrotliOutput
BrotliFileOutput(FILE* f
) {
122 out
.cb_
= BrotliFileOutputFunction
;
128 #if defined(__cplusplus) || defined(c_plusplus)