Revert of ui: Clean up damaged rects and clear them after painting. (patchset #2...
[chromium-blink-merge.git] / third_party / brotli / dec / streams.c
blob623d417cf4c7100ff7d1303fb403944b4888784c
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. */
18 #include <string.h>
19 #ifndef _WIN32
20 #include <unistd.h>
21 #endif
22 #include "./streams.h"
24 #if defined(__cplusplus) || defined(c_plusplus)
25 extern "C" {
26 #endif
28 int BrotliMemInputFunction(void* data, uint8_t* buf, size_t count) {
29 BrotliMemInput* input = (BrotliMemInput*)data;
30 if (input->pos > input->length) {
31 return -1;
33 if (input->pos + count > input->length) {
34 count = input->length - input->pos;
36 memcpy(buf, input->buffer + input->pos, count);
37 input->pos += count;
38 return (int)count;
41 BrotliInput BrotliInitMemInput(const uint8_t* buffer, size_t length,
42 BrotliMemInput* mem_input) {
43 BrotliInput input;
44 mem_input->buffer = buffer;
45 mem_input->length = length;
46 mem_input->pos = 0;
47 input.cb_ = &BrotliMemInputFunction;
48 input.data_ = mem_input;
49 return 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) {
55 return -1;
57 memcpy(output->buffer + output->pos, buf, count);
58 output->pos += count;
59 return (int)count;
62 BrotliOutput BrotliInitMemOutput(uint8_t* buffer, size_t length,
63 BrotliMemOutput* mem_output) {
64 BrotliOutput output;
65 mem_output->buffer = buffer;
66 mem_output->length = length;
67 mem_output->pos = 0;
68 output.cb_ = &BrotliMemOutputFunction;
69 output.data_ = mem_output;
70 return output;
73 int BrotliStdinInputFunction(void* data, uint8_t* buf, size_t count) {
74 (void) data; /* Shut up LLVM */
75 #ifndef _WIN32
76 return (int)read(STDIN_FILENO, buf, count);
77 #else
78 return -1;
79 #endif
82 BrotliInput BrotliStdinInput() {
83 BrotliInput in;
84 in.cb_ = BrotliStdinInputFunction;
85 in.data_ = NULL;
86 return in;
89 int BrotliStdoutOutputFunction(void* data, const uint8_t* buf, size_t count) {
90 (void) data; /* Shut up LLVM */
91 #ifndef _WIN32
92 return (int)write(STDOUT_FILENO, buf, count);
93 #else
94 return -1;
95 #endif
98 BrotliOutput BrotliStdoutOutput() {
99 BrotliOutput out;
100 out.cb_ = BrotliStdoutOutputFunction;
101 out.data_ = NULL;
102 return out;
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) {
110 BrotliInput in;
111 in.cb_ = BrotliFileInputFunction;
112 in.data_ = f;
113 return in;
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) {
121 BrotliOutput out;
122 out.cb_ = BrotliFileOutputFunction;
123 out.data_ = f;
124 return out;
128 #if defined(__cplusplus) || defined(c_plusplus)
129 } /* extern "C" */
130 #endif