blob: edb17c3b749e30bb910209062f37ffdc0a7a87de [file] [log] [blame]
Zoltan Szabadka6a530332015-04-23 14:29:01 +02001// Copyright 2009 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// Convience routines to make Brotli I/O classes from some memory containers and
16// files.
17
18#include "./streams.h"
19
20#include <assert.h>
Zoltan Szabadka7de70db2015-08-11 11:09:04 +020021#include <stdlib.h>
Zoltan Szabadka6a530332015-04-23 14:29:01 +020022#include <string.h>
23
24namespace brotli {
25
26BrotliMemOut::BrotliMemOut(void* buf, int len)
27 : buf_(buf),
28 len_(len),
29 pos_(0) {}
30
31void BrotliMemOut::Reset(void* buf, int len) {
32 buf_ = buf;
33 len_ = len;
34 pos_ = 0;
35}
36
37// Brotli output routine: copy n bytes to the output buffer.
38bool BrotliMemOut::Write(const void *buf, size_t n) {
39 if (n + pos_ > len_)
40 return false;
41 char* p = reinterpret_cast<char*>(buf_) + pos_;
42 memcpy(p, buf, n);
43 pos_ += n;
44 return true;
45}
46
47BrotliStringOut::BrotliStringOut(std::string* buf, int max_size)
48 : buf_(buf),
49 max_size_(max_size) {
50 assert(buf->empty());
51}
52
53void BrotliStringOut::Reset(std::string* buf, int max_size) {
54 buf_ = buf;
55 max_size_ = max_size;
56}
57
58// Brotli output routine: add n bytes to a string.
59bool BrotliStringOut::Write(const void *buf, size_t n) {
60 if (buf_->size() + n > max_size_)
61 return false;
62 buf_->append(static_cast<const char*>(buf), n);
63 return true;
64}
65
66BrotliMemIn::BrotliMemIn(const void* buf, int len)
67 : buf_(buf),
68 len_(len),
69 pos_(0) {}
70
71void BrotliMemIn::Reset(const void* buf, int len) {
72 buf_ = buf;
73 len_ = len;
74 pos_ = 0;
75}
76
77// Brotli input routine: read the next chunk of memory.
78const void* BrotliMemIn::Read(size_t n, size_t* output) {
79 if (pos_ == len_) {
80 return NULL;
81 }
82 if (n > len_ - pos_)
83 n = len_ - pos_;
84 const char* p = reinterpret_cast<const char*>(buf_) + pos_;
85 pos_ += n;
86 *output = n;
87 return p;
88}
89
90BrotliFileIn::BrotliFileIn(FILE* f, size_t max_read_size)
91 : f_(f),
92 buf_(malloc(max_read_size)),
93 buf_size_(max_read_size) {}
94
95BrotliFileIn::~BrotliFileIn() {
96 if (buf_) free(buf_);
97}
98
99const void* BrotliFileIn::Read(size_t n, size_t* bytes_read) {
100 if (buf_ == NULL) {
101 *bytes_read = 0;
102 return NULL;
103 }
104 if (n > buf_size_) {
105 n = buf_size_;
106 } else if (n == 0) {
107 return feof(f_) ? NULL : buf_;
108 }
109 *bytes_read = fread(buf_, 1, n, f_);
110 if (*bytes_read == 0) {
111 return NULL;
112 } else {
113 return buf_;
114 }
115}
116
117BrotliFileOut::BrotliFileOut(FILE* f) : f_(f) {}
118
119bool BrotliFileOut::Write(const void* buf, size_t n) {
120 if (fwrite(buf, n, 1, f_) != 1) {
121 return false;
122 }
123 return true;
124}
125
126
127} // namespace brotli