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 size_t limit
= output
->length
- output
->pos
;
58 memcpy(output
->buffer
+ output
->pos
, buf
, count
);
63 BrotliOutput
BrotliInitMemOutput(uint8_t* buffer
, size_t length
,
64 BrotliMemOutput
* mem_output
) {
66 mem_output
->buffer
= buffer
;
67 mem_output
->length
= length
;
69 output
.cb_
= &BrotliMemOutputFunction
;
70 output
.data_
= mem_output
;
74 int BrotliStdinInputFunction(void* data
, uint8_t* buf
, size_t count
) {
75 (void) data
; /* Shut up LLVM */
77 return (int)read(STDIN_FILENO
, buf
, count
);
83 BrotliInput
BrotliStdinInput() {
85 in
.cb_
= BrotliStdinInputFunction
;
90 int BrotliStdoutOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
91 (void) data
; /* Shut up LLVM */
93 return (int)write(STDOUT_FILENO
, buf
, count
);
99 BrotliOutput
BrotliStdoutOutput() {
101 out
.cb_
= BrotliStdoutOutputFunction
;
106 int BrotliFileInputFunction(void* data
, uint8_t* buf
, size_t count
) {
107 return (int)fread(buf
, 1, count
, (FILE*)data
);
110 BrotliInput
BrotliFileInput(FILE* f
) {
112 in
.cb_
= BrotliFileInputFunction
;
117 int BrotliFileOutputFunction(void* data
, const uint8_t* buf
, size_t count
) {
118 return (int)fwrite(buf
, 1, count
, (FILE*)data
);
121 BrotliOutput
BrotliFileOutput(FILE* f
) {
123 out
.cb_
= BrotliFileOutputFunction
;
129 #if defined(__cplusplus) || defined(c_plusplus)