[MacViews] Show comboboxes with a native NSMenu
[chromium-blink-merge.git] / third_party / brotli / dec / streams.c
blobb33f7a46e9e26ad20f51fb1b2f40bdbc6b0a9b45
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 size_t limit = output->length - output->pos;
55 if (count > limit) {
56 count = limit;
58 memcpy(output->buffer + output->pos, buf, count);
59 output->pos += count;
60 return (int)count;
63 BrotliOutput BrotliInitMemOutput(uint8_t* buffer, size_t length,
64 BrotliMemOutput* mem_output) {
65 BrotliOutput output;
66 mem_output->buffer = buffer;
67 mem_output->length = length;
68 mem_output->pos = 0;
69 output.cb_ = &BrotliMemOutputFunction;
70 output.data_ = mem_output;
71 return output;
74 int BrotliStdinInputFunction(void* data, uint8_t* buf, size_t count) {
75 (void) data; /* Shut up LLVM */
76 #ifndef _WIN32
77 return (int)read(STDIN_FILENO, buf, count);
78 #else
79 return -1;
80 #endif
83 BrotliInput BrotliStdinInput() {
84 BrotliInput in;
85 in.cb_ = BrotliStdinInputFunction;
86 in.data_ = NULL;
87 return in;
90 int BrotliStdoutOutputFunction(void* data, const uint8_t* buf, size_t count) {
91 (void) data; /* Shut up LLVM */
92 #ifndef _WIN32
93 return (int)write(STDOUT_FILENO, buf, count);
94 #else
95 return -1;
96 #endif
99 BrotliOutput BrotliStdoutOutput() {
100 BrotliOutput out;
101 out.cb_ = BrotliStdoutOutputFunction;
102 out.data_ = NULL;
103 return out;
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) {
111 BrotliInput in;
112 in.cb_ = BrotliFileInputFunction;
113 in.data_ = f;
114 return in;
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) {
122 BrotliOutput out;
123 out.cb_ = BrotliFileOutputFunction;
124 out.data_ = f;
125 return out;
129 #if defined(__cplusplus) || defined(c_plusplus)
130 } /* extern "C" */
131 #endif