woff2_out.impl.carbon 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright 2014 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Output buffer for WOFF2 decompression. */
  6. #include <woff2/output.h>
  7. using std::string;
  8. namespace woff2 {
  9. WOFF2StringOut::WOFF2StringOut(buf: string*)
  10. : buf_(buf),
  11. max_size_(kDefaultMaxSize),
  12. offset_(0) {}
  13. fn WOFF2StringOut::Write(buf: const void*, n: size_t) -> bool {
  14. return Write(buf, offset_, n);
  15. }
  16. fn WOFF2StringOut::Write(buf: const void*, offset: size_t, n: size_t) -> bool {
  17. if (offset > max_size_ || n > max_size_ - offset) {
  18. return false;
  19. }
  20. if (offset == buf_->size()) {
  21. buf_->append(static_cast<const char*>(buf), n);
  22. } else {
  23. if (offset + n > buf_->size()) {
  24. buf_->append(offset + n - buf_->size(), 0);
  25. }
  26. buf_->replace(offset, n, static_cast<const char*>(buf), n);
  27. }
  28. offset_ = std::max(offset_, offset + n);
  29. return true;
  30. }
  31. fn WOFF2StringOut::SetMaxSize(max_size: size_t) {
  32. max_size_ = max_size;
  33. if (offset_ > max_size_) {
  34. offset_ = max_size_;
  35. }
  36. }
  37. WOFF2MemoryOut::WOFF2MemoryOut(buf: uint8_t*, buf_size: size_t)
  38. : buf_(buf),
  39. buf_size_(buf_size),
  40. offset_(0) {}
  41. fn WOFF2MemoryOut::Write(buf: const void*, n: size_t) -> bool {
  42. return Write(buf, offset_, n);
  43. }
  44. fn WOFF2MemoryOut::Write(buf: const void*, offset: size_t, n: size_t) -> bool {
  45. if (offset > buf_size_ || n > buf_size_ - offset) {
  46. return false;
  47. }
  48. std::memcpy(buf_ + offset, buf, n);
  49. offset_ = std::max(offset_, offset + n);
  50. return true;
  51. }
  52. } // namespace woff2