vector_streambuf.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (c) 2016-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/utils/exceptions.hpp>
  24. #include <detail/serialization/vector_streambuf.hpp>
  25. namespace xlnt {
  26. namespace detail {
  27. vector_istreambuf::vector_istreambuf(const std::vector<std::uint8_t> &data)
  28. : data_(data),
  29. position_(0)
  30. {
  31. }
  32. vector_istreambuf::int_type vector_istreambuf::underflow()
  33. {
  34. if (position_ == data_.size())
  35. {
  36. return traits_type::eof();
  37. }
  38. return traits_type::to_int_type(static_cast<char>(data_[position_]));
  39. }
  40. vector_istreambuf::int_type vector_istreambuf::uflow()
  41. {
  42. if (position_ == data_.size())
  43. {
  44. return traits_type::eof();
  45. }
  46. return traits_type::to_int_type(static_cast<char>(data_[position_++]));
  47. }
  48. std::streamsize vector_istreambuf::showmanyc()
  49. {
  50. if (position_ == data_.size())
  51. {
  52. return static_cast<std::streamsize>(-1);
  53. }
  54. return static_cast<std::streamsize>(data_.size() - position_);
  55. }
  56. std::streampos vector_istreambuf::seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode)
  57. {
  58. if (way == std::ios_base::beg)
  59. {
  60. position_ = 0;
  61. }
  62. else if (way == std::ios_base::end)
  63. {
  64. position_ = data_.size();
  65. }
  66. if (off < 0)
  67. {
  68. if (static_cast<std::size_t>(-off) > position_)
  69. {
  70. position_ = 0;
  71. return static_cast<std::ptrdiff_t>(-1);
  72. }
  73. else
  74. {
  75. position_ -= static_cast<std::size_t>(-off);
  76. }
  77. }
  78. else if (off > 0)
  79. {
  80. if (static_cast<std::size_t>(off) + position_ > data_.size())
  81. {
  82. position_ = data_.size();
  83. return static_cast<std::ptrdiff_t>(-1);
  84. }
  85. else
  86. {
  87. position_ += static_cast<std::size_t>(off);
  88. }
  89. }
  90. return static_cast<std::ptrdiff_t>(position_);
  91. }
  92. std::streampos vector_istreambuf::seekpos(std::streampos sp, std::ios_base::openmode)
  93. {
  94. if (sp < 0)
  95. {
  96. position_ = 0;
  97. }
  98. else if (static_cast<std::size_t>(sp) > data_.size())
  99. {
  100. position_ = data_.size();
  101. }
  102. else
  103. {
  104. position_ = static_cast<std::size_t>(sp);
  105. }
  106. return static_cast<std::ptrdiff_t>(position_);
  107. }
  108. vector_ostreambuf::vector_ostreambuf(std::vector<std::uint8_t> &data)
  109. : data_(data),
  110. position_(0)
  111. {
  112. }
  113. vector_ostreambuf::int_type vector_ostreambuf::overflow(int_type c)
  114. {
  115. if (c != traits_type::eof())
  116. {
  117. data_.push_back(static_cast<std::uint8_t>(c));
  118. position_ = data_.size() - 1;
  119. }
  120. return traits_type::to_int_type(static_cast<char>(data_[position_]));
  121. }
  122. std::streamsize vector_ostreambuf::xsputn(const char *s, std::streamsize n)
  123. {
  124. if (data_.empty())
  125. {
  126. data_.resize(static_cast<std::size_t>(n));
  127. }
  128. else
  129. {
  130. auto position_size = data_.size();
  131. auto required_size = static_cast<std::size_t>(position_ + static_cast<std::size_t>(n));
  132. data_.resize(std::max(position_size, required_size));
  133. }
  134. std::copy(s, s + n, data_.begin() + static_cast<std::ptrdiff_t>(position_));
  135. position_ += static_cast<std::size_t>(n);
  136. return n;
  137. }
  138. std::streampos vector_ostreambuf::seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode)
  139. {
  140. if (way == std::ios_base::beg)
  141. {
  142. position_ = 0;
  143. }
  144. else if (way == std::ios_base::end)
  145. {
  146. position_ = data_.size();
  147. }
  148. if (off < 0)
  149. {
  150. if (static_cast<std::size_t>(-off) > position_)
  151. {
  152. position_ = 0;
  153. return static_cast<std::ptrdiff_t>(-1);
  154. }
  155. else
  156. {
  157. position_ -= static_cast<std::size_t>(-off);
  158. }
  159. }
  160. else if (off > 0)
  161. {
  162. if (static_cast<std::size_t>(off) + position_ > data_.size())
  163. {
  164. position_ = data_.size();
  165. return static_cast<std::ptrdiff_t>(-1);
  166. }
  167. else
  168. {
  169. position_ += static_cast<std::size_t>(off);
  170. }
  171. }
  172. return static_cast<std::ptrdiff_t>(position_);
  173. }
  174. std::streampos vector_ostreambuf::seekpos(std::streampos sp, std::ios_base::openmode)
  175. {
  176. if (sp < 0)
  177. {
  178. position_ = 0;
  179. }
  180. else if (static_cast<std::size_t>(sp) > data_.size())
  181. {
  182. position_ = data_.size();
  183. }
  184. else
  185. {
  186. position_ = static_cast<std::size_t>(sp);
  187. }
  188. return static_cast<std::ptrdiff_t>(position_);
  189. }
  190. XLNT_API std::vector<std::uint8_t> to_vector(std::istream &in_stream)
  191. {
  192. if (!in_stream)
  193. {
  194. throw xlnt::exception("bad stream");
  195. }
  196. return std::vector<std::uint8_t>(
  197. std::istreambuf_iterator<char>(in_stream),
  198. std::istreambuf_iterator<char>());
  199. }
  200. XLNT_API void to_stream(const std::vector<std::uint8_t> &bytes, std::ostream &out_stream)
  201. {
  202. if (!out_stream)
  203. {
  204. throw xlnt::exception("bad stream");
  205. }
  206. out_stream.write(reinterpret_cast<const char *>(bytes.data()), static_cast<std::ptrdiff_t>(bytes.size()));
  207. }
  208. XLNT_API std::ostream &operator<<(std::ostream &out_stream, const std::vector<std::uint8_t> &bytes)
  209. {
  210. to_stream(bytes, out_stream);
  211. return out_stream;
  212. }
  213. } // namespace detail
  214. } // namespace xlnt