Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1195)

Unified Diff: third_party/brotli/enc/streams.cc

Issue 1956893002: Added brotli enc/ and tools/ directories. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated to most recent build tools Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/brotli/enc/streams.h ('k') | third_party/brotli/enc/transform.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/brotli/enc/streams.cc
diff --git a/third_party/brotli/enc/streams.cc b/third_party/brotli/enc/streams.cc
new file mode 100644
index 0000000000000000000000000000000000000000..17eda2d588a232a09d16b977110c9d83c57aa321
--- /dev/null
+++ b/third_party/brotli/enc/streams.cc
@@ -0,0 +1,114 @@
+/* Copyright 2009 Google Inc. All Rights Reserved.
+
+ Distributed under MIT license.
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+// Convience routines to make Brotli I/O classes from some memory containers and
+// files.
+
+#include "./streams.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+namespace brotli {
+
+BrotliMemOut::BrotliMemOut(void* buf, size_t len)
+ : buf_(buf),
+ len_(len),
+ pos_(0) {}
+
+void BrotliMemOut::Reset(void* buf, size_t len) {
+ buf_ = buf;
+ len_ = len;
+ pos_ = 0;
+}
+
+// Brotli output routine: copy n bytes to the output buffer.
+bool BrotliMemOut::Write(const void *buf, size_t n) {
+ if (n + pos_ > len_)
+ return false;
+ char* p = reinterpret_cast<char*>(buf_) + pos_;
+ memcpy(p, buf, n);
+ pos_ += n;
+ return true;
+}
+
+BrotliStringOut::BrotliStringOut(std::string* buf, size_t max_size)
+ : buf_(buf),
+ max_size_(max_size) {
+ assert(buf->empty());
+}
+
+void BrotliStringOut::Reset(std::string* buf, size_t max_size) {
+ buf_ = buf;
+ max_size_ = max_size;
+}
+
+// Brotli output routine: add n bytes to a string.
+bool BrotliStringOut::Write(const void *buf, size_t n) {
+ if (buf_->size() + n > max_size_)
+ return false;
+ buf_->append(static_cast<const char*>(buf), n);
+ return true;
+}
+
+BrotliMemIn::BrotliMemIn(const void* buf, size_t len)
+ : buf_(buf),
+ len_(len),
+ pos_(0) {}
+
+void BrotliMemIn::Reset(const void* buf, size_t len) {
+ buf_ = buf;
+ len_ = len;
+ pos_ = 0;
+}
+
+// Brotli input routine: read the next chunk of memory.
+const void* BrotliMemIn::Read(size_t n, size_t* output) {
+ if (pos_ == len_) {
+ return NULL;
+ }
+ if (n > len_ - pos_)
+ n = len_ - pos_;
+ const char* p = reinterpret_cast<const char*>(buf_) + pos_;
+ pos_ += n;
+ *output = n;
+ return p;
+}
+
+BrotliFileIn::BrotliFileIn(FILE* f, size_t max_read_size)
+ : f_(f),
+ buf_(new char[max_read_size]),
+ buf_size_(max_read_size) { }
+
+BrotliFileIn::~BrotliFileIn(void) {
+ delete[] buf_;
+}
+
+const void* BrotliFileIn::Read(size_t n, size_t* bytes_read) {
+ if (n > buf_size_) {
+ n = buf_size_;
+ } else if (n == 0) {
+ return feof(f_) ? NULL : buf_;
+ }
+ *bytes_read = fread(buf_, 1, n, f_);
+ if (*bytes_read == 0) {
+ return NULL;
+ } else {
+ return buf_;
+ }
+}
+
+BrotliFileOut::BrotliFileOut(FILE* f) : f_(f) {}
+
+bool BrotliFileOut::Write(const void* buf, size_t n) {
+ if (fwrite(buf, n, 1, f_) != 1) {
+ return false;
+ }
+ return true;
+}
+
+} // namespace brotli
« no previous file with comments | « third_party/brotli/enc/streams.h ('k') | third_party/brotli/enc/transform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698