stream-reader.hh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. Copyright (c) 2019 - 2020, Syoyo Fujita.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the Syoyo Fujita nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. //
  26. // Simple byte stream reader. Consider endianness when reading 2, 4, 8 bytes data.
  27. //
  28. #include <cstdint>
  29. #include <cstdlib>
  30. #include <cstring>
  31. #include <memory>
  32. namespace tinyusdz {
  33. namespace {
  34. static inline void swap2(unsigned short *val) {
  35. unsigned short tmp = *val;
  36. uint8_t *dst = reinterpret_cast<uint8_t *>(val);
  37. uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
  38. dst[0] = src[1];
  39. dst[1] = src[0];
  40. }
  41. static inline void swap4(uint32_t *val) {
  42. uint32_t tmp = *val;
  43. uint8_t *dst = reinterpret_cast<uint8_t *>(val);
  44. uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
  45. dst[0] = src[3];
  46. dst[1] = src[2];
  47. dst[2] = src[1];
  48. dst[3] = src[0];
  49. }
  50. static inline void swap4(int *val) {
  51. int tmp = *val;
  52. uint8_t *dst = reinterpret_cast<uint8_t *>(val);
  53. uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
  54. dst[0] = src[3];
  55. dst[1] = src[2];
  56. dst[2] = src[1];
  57. dst[3] = src[0];
  58. }
  59. static inline void swap8(uint64_t *val) {
  60. uint64_t tmp = (*val);
  61. uint8_t *dst = reinterpret_cast<uint8_t *>(val);
  62. uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
  63. dst[0] = src[7];
  64. dst[1] = src[6];
  65. dst[2] = src[5];
  66. dst[3] = src[4];
  67. dst[4] = src[3];
  68. dst[5] = src[2];
  69. dst[6] = src[1];
  70. dst[7] = src[0];
  71. }
  72. static inline void swap8(int64_t *val) {
  73. int64_t tmp = (*val);
  74. uint8_t *dst = reinterpret_cast<uint8_t *>(val);
  75. uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
  76. dst[0] = src[7];
  77. dst[1] = src[6];
  78. dst[2] = src[5];
  79. dst[3] = src[4];
  80. dst[4] = src[3];
  81. dst[5] = src[2];
  82. dst[6] = src[1];
  83. dst[7] = src[0];
  84. }
  85. } // namespace
  86. ///
  87. /// Simple stream reader
  88. ///
  89. class StreamReader {
  90. public:
  91. explicit StreamReader(const uint8_t *binary, const size_t length,
  92. const bool swap_endian)
  93. : binary_(binary), length_(length), swap_endian_(swap_endian), idx_(0) {
  94. (void)pad_;
  95. }
  96. bool seek_set(const uint64_t offset) const {
  97. if (offset > length_) {
  98. return false;
  99. }
  100. idx_ = offset;
  101. return true;
  102. }
  103. bool seek_from_currect(const int64_t offset) const {
  104. if ((int64_t(idx_) + offset) < 0) {
  105. return false;
  106. }
  107. if (size_t((int64_t(idx_) + offset)) > length_) {
  108. return false;
  109. }
  110. idx_ = size_t(int64_t(idx_) + offset);
  111. return true;
  112. }
  113. size_t read(const size_t n, const uint64_t dst_len, uint8_t *dst) const {
  114. size_t len = n;
  115. if ((idx_ + len) > length_) {
  116. len = length_ - size_t(idx_);
  117. }
  118. if (len > 0) {
  119. if (dst_len < len) {
  120. // dst does not have enough space. return 0 for a while.
  121. return 0;
  122. }
  123. memcpy(dst, &binary_[idx_], len);
  124. idx_ += len;
  125. return len;
  126. } else {
  127. return 0;
  128. }
  129. }
  130. bool read1(uint8_t *ret) const {
  131. if ((idx_ + 1) > length_) {
  132. return false;
  133. }
  134. const uint8_t val = binary_[idx_];
  135. (*ret) = val;
  136. idx_ += 1;
  137. return true;
  138. }
  139. bool read_bool(bool *ret) const {
  140. if ((idx_ + 1) > length_) {
  141. return false;
  142. }
  143. const char val = static_cast<const char>(binary_[idx_]);
  144. (*ret) = bool(val);
  145. idx_ += 1;
  146. return true;
  147. }
  148. bool read1(char *ret) const {
  149. if ((idx_ + 1) > length_) {
  150. return false;
  151. }
  152. const char val = static_cast<const char>(binary_[idx_]);
  153. (*ret) = val;
  154. idx_ += 1;
  155. return true;
  156. }
  157. bool read2(unsigned short *ret) const {
  158. if ((idx_ + 2) > length_) {
  159. return false;
  160. }
  161. unsigned short val =
  162. *(reinterpret_cast<const unsigned short *>(&binary_[idx_]));
  163. if (swap_endian_) {
  164. swap2(&val);
  165. }
  166. (*ret) = val;
  167. idx_ += 2;
  168. return true;
  169. }
  170. bool read4(uint32_t *ret) const {
  171. if ((idx_ + 4) > length_) {
  172. return false;
  173. }
  174. uint32_t val = *(reinterpret_cast<const uint32_t *>(&binary_[idx_]));
  175. if (swap_endian_) {
  176. swap4(&val);
  177. }
  178. (*ret) = val;
  179. idx_ += 4;
  180. return true;
  181. }
  182. bool read4(int *ret) const {
  183. if ((idx_ + 4) > length_) {
  184. return false;
  185. }
  186. int val = *(reinterpret_cast<const int *>(&binary_[idx_]));
  187. if (swap_endian_) {
  188. swap4(&val);
  189. }
  190. (*ret) = val;
  191. idx_ += 4;
  192. return true;
  193. }
  194. bool read8(uint64_t *ret) const {
  195. if ((idx_ + 8) > length_) {
  196. return false;
  197. }
  198. uint64_t val = *(reinterpret_cast<const uint64_t *>(&binary_[idx_]));
  199. if (swap_endian_) {
  200. swap8(&val);
  201. }
  202. (*ret) = val;
  203. idx_ += 8;
  204. return true;
  205. }
  206. bool read8(int64_t *ret) const {
  207. if ((idx_ + 8) > length_) {
  208. return false;
  209. }
  210. int64_t val = *(reinterpret_cast<const int64_t *>(&binary_[idx_]));
  211. if (swap_endian_) {
  212. swap8(&val);
  213. }
  214. (*ret) = val;
  215. idx_ += 8;
  216. return true;
  217. }
  218. bool read_float(float *ret) const {
  219. if (!ret) {
  220. return false;
  221. }
  222. float value;
  223. if (!read4(reinterpret_cast<int *>(&value))) {
  224. return false;
  225. }
  226. (*ret) = value;
  227. return true;
  228. }
  229. bool read_double(double *ret) const {
  230. if (!ret) {
  231. return false;
  232. }
  233. double value;
  234. if (!read8(reinterpret_cast<uint64_t *>(&value))) {
  235. return false;
  236. }
  237. (*ret) = value;
  238. return true;
  239. }
  240. #if 0
  241. bool read_value(Value *inout) {
  242. if (!inout) {
  243. return false;
  244. }
  245. if (inout->Type() == VALUE_TYPE_FLOAT) {
  246. float value;
  247. if (!read_float(&value)) {
  248. return false;
  249. }
  250. (*inout) = Value(value);
  251. } else if (inout->Type() == VALUE_TYPE_INT) {
  252. int value;
  253. if (!read4(&value)) {
  254. return false;
  255. }
  256. (*inout) = Value(value);
  257. } else {
  258. TINYVDBIO_ASSERT(0);
  259. return false;
  260. }
  261. return true;
  262. }
  263. #endif
  264. size_t tell() const { return size_t(idx_); }
  265. const uint8_t *data() const { return binary_; }
  266. bool swap_endian() const { return swap_endian_; }
  267. size_t size() const { return length_; }
  268. private:
  269. const uint8_t *binary_;
  270. const size_t length_;
  271. bool swap_endian_;
  272. char pad_[7];
  273. mutable uint64_t idx_;
  274. };
  275. } // namespace tinyusdz