| OLD | NEW |
| (Empty) |
| 1 /* Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 | |
| 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 | |
| 6 | |
| 7 http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | |
| 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. | |
| 14 | |
| 15 Functions for streaming input and output. | |
| 16 */ | |
| 17 | |
| 18 #include <string.h> | |
| 19 #ifndef _WIN32 | |
| 20 #include <unistd.h> | |
| 21 #endif | |
| 22 #include "./streams.h" | |
| 23 | |
| 24 #if defined(__cplusplus) || defined(c_plusplus) | |
| 25 extern "C" { | |
| 26 #endif | |
| 27 | |
| 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; | |
| 32 } | |
| 33 if (input->pos + count > input->length) { | |
| 34 count = input->length - input->pos; | |
| 35 } | |
| 36 memcpy(buf, input->buffer + input->pos, count); | |
| 37 input->pos += count; | |
| 38 return (int)count; | |
| 39 } | |
| 40 | |
| 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; | |
| 50 } | |
| 51 | |
| 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; | |
| 56 } | |
| 57 memcpy(output->buffer + output->pos, buf, count); | |
| 58 output->pos += count; | |
| 59 return (int)count; | |
| 60 } | |
| 61 | |
| 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; | |
| 71 } | |
| 72 | |
| 73 int BrotliStdinInputFunction(void* data, uint8_t* buf, size_t count) { | |
| 74 #ifndef _WIN32 | |
| 75 return (int)read(STDIN_FILENO, buf, count); | |
| 76 #else | |
| 77 return -1; | |
| 78 #endif | |
| 79 } | |
| 80 | |
| 81 BrotliInput BrotliStdinInput() { | |
| 82 BrotliInput in; | |
| 83 in.cb_ = BrotliStdinInputFunction; | |
| 84 in.data_ = NULL; | |
| 85 return in; | |
| 86 } | |
| 87 | |
| 88 int BrotliStdoutOutputFunction(void* data, const uint8_t* buf, size_t count) { | |
| 89 #ifndef _WIN32 | |
| 90 return (int)write(STDOUT_FILENO, buf, count); | |
| 91 #else | |
| 92 return -1; | |
| 93 #endif | |
| 94 } | |
| 95 | |
| 96 BrotliOutput BrotliStdoutOutput() { | |
| 97 BrotliOutput out; | |
| 98 out.cb_ = BrotliStdoutOutputFunction; | |
| 99 out.data_ = NULL; | |
| 100 return out; | |
| 101 } | |
| 102 | |
| 103 int BrotliFileInputFunction(void* data, uint8_t* buf, size_t count) { | |
| 104 return (int)fread(buf, 1, count, (FILE*)data); | |
| 105 } | |
| 106 | |
| 107 BrotliInput BrotliFileInput(FILE* f) { | |
| 108 BrotliInput in; | |
| 109 in.cb_ = BrotliFileInputFunction; | |
| 110 in.data_ = f; | |
| 111 return in; | |
| 112 } | |
| 113 | |
| 114 int BrotliFileOutputFunction(void* data, const uint8_t* buf, size_t count) { | |
| 115 return (int)fwrite(buf, 1, count, (FILE*)data); | |
| 116 } | |
| 117 | |
| 118 BrotliOutput BrotliFileOutput(FILE* f) { | |
| 119 BrotliOutput out; | |
| 120 out.cb_ = BrotliFileOutputFunction; | |
| 121 out.data_ = f; | |
| 122 return out; | |
| 123 } | |
| 124 | |
| 125 | |
| 126 #if defined(__cplusplus) || defined(c_plusplus) | |
| 127 } /* extern "C" */ | |
| 128 #endif | |
| OLD | NEW |