blob: 1c8ef65b04168efd091f4049d3261b80213d459c [file] [log] [blame]
Zoltan Szabadka30625ba2013-12-16 14:45:57 +01001/* 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*/
Zoltan Szabadka1571db32013-11-15 19:02:17 +010017
18#ifndef BROTLI_DEC_STREAMS_H_
19#define BROTLI_DEC_STREAMS_H_
20
21#include <stdio.h>
22#include "./types.h"
23
24#if defined(__cplusplus) || defined(c_plusplus)
25extern "C" {
26#endif
27
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010028/* Function pointer type used to read len bytes into buf. Returns the */
29/* number of bytes read or -1 on error. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010030typedef int (*BrotliInputFunction)(void* data, uint8_t* buf, size_t len);
31
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010032/* Input callback function with associated data. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010033typedef struct {
34 BrotliInputFunction cb_;
35 void* data_;
36} BrotliInput;
37
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010038/* Reads len bytes into buf, using the in callback. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010039static BROTLI_INLINE int BrotliRead(BrotliInput in, uint8_t* buf, size_t len) {
40 return in.cb_(in.data_, buf, len);
41}
42
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010043/* Function pointer type used to write len bytes into buf. Returns the */
44/* number of bytes written or -1 on error. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010045typedef int (*BrotliOutputFunction)(void* data, const uint8_t* buf, size_t len);
46
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010047/* Output callback function with associated data. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010048typedef struct {
49 BrotliOutputFunction cb_;
50 void* data_;
51} BrotliOutput;
52
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010053/* Writes len bytes into buf, using the out callback. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010054static BROTLI_INLINE int BrotliWrite(BrotliOutput out,
55 const uint8_t* buf, size_t len) {
56 return out.cb_(out.data_, buf, len);
57}
58
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010059/* Memory region with position. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010060typedef struct {
61 const uint8_t* buffer;
62 size_t length;
63 size_t pos;
64} BrotliMemInput;
65
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010066/* Input callback where *data is a BrotliMemInput struct. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010067int BrotliMemInputFunction(void* data, uint8_t* buf, size_t count);
68
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010069/* Returns an input callback that wraps the given memory region. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010070BrotliInput BrotliInitMemInput(const uint8_t* buffer, size_t length,
71 BrotliMemInput* mem_input);
72
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010073/* Output buffer with position. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010074typedef struct {
75 uint8_t* buffer;
76 size_t length;
77 size_t pos;
78} BrotliMemOutput;
79
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010080/* Output callback where *data is a BrotliMemOutput struct. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010081int BrotliMemOutputFunction(void* data, const uint8_t* buf, size_t count);
82
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010083/* Returns an output callback that wraps the given memory region. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010084BrotliOutput BrotliInitMemOutput(uint8_t* buffer, size_t length,
85 BrotliMemOutput* mem_output);
86
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010087/* Input callback that reads from standard input. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010088int BrotliStdinInputFunction(void* data, uint8_t* buf, size_t count);
89BrotliInput BrotliStdinInput();
90
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010091/* Output callback that writes to standard output. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010092int BrotliStdoutOutputFunction(void* data, const uint8_t* buf, size_t count);
93BrotliOutput BrotliStdoutOutput();
94
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010095/* Output callback that writes to a file. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +010096int BrotliFileOutputFunction(void* data, const uint8_t* buf, size_t count);
97BrotliOutput BrotliFileOutput(FILE* f);
98
99#if defined(__cplusplus) || defined(c_plusplus)
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100100} /* extern "C" */
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100101#endif
102
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100103#endif /* BROTLI_DEC_STREAMS_H_ */