zstream.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #ifndef __higinet_zistream_h__
  2. #define __higinet_zistream_h__
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <vector>
  6. #include <string>
  7. #include <stdexcept>
  8. struct stream_exception: public std::logic_error
  9. {
  10. explicit stream_exception(const char*e)
  11. :std::logic_error(e)
  12. {
  13. }
  14. explicit stream_exception(const std::string&e)
  15. :std::logic_error(e)
  16. {
  17. }
  18. };
  19. namespace endian
  20. {
  21. struct flag
  22. {
  23. #ifndef LITTLE_ENDIAN
  24. static const bool little=true;
  25. #else
  26. static const bool little=false;
  27. #endif
  28. };
  29. inline void cp_uint8_r(void*d,const void*s,size_t c)
  30. {
  31. uint8_t*di=(uint8_t*)d;
  32. const uint8_t*si=(const uint8_t*)s;
  33. while(c-->0)
  34. *di++=*--si;
  35. }
  36. inline void cp_uint8(void*d,const void*s,size_t c)
  37. {
  38. memcpy(d,s,c);
  39. }
  40. template<bool little> struct copy_impl
  41. {
  42. void operator()(void*d,const void*s,size_t)const
  43. {
  44. }
  45. };
  46. template<> struct copy_impl<true>
  47. {
  48. void operator()(void*d,const void*s,size_t count)const
  49. {
  50. cp_uint8 ((uint8_t*)d,(const uint8_t*)s,count);
  51. }
  52. };
  53. template<> struct copy_impl<false>
  54. {
  55. void operator()(void*d,const void*s,size_t count)const
  56. {
  57. cp_uint8_r (d, (const uint8_t*)s+count, count);
  58. }
  59. };
  60. inline void copy(void*d,const void*s,size_t count)
  61. {
  62. copy_impl<endian::flag::little>()(d,s,count);
  63. }
  64. }
  65. struct skip
  66. {
  67. int m_count;
  68. skip(int count):m_count(count){}
  69. };
  70. template<typename T,typename F>
  71. struct as
  72. {
  73. T t;
  74. F&f;
  75. as(F&f_):f(f_){}
  76. };
  77. struct npc
  78. {
  79. int m_count;
  80. npc(int c=-1):m_count(c) {}
  81. void reset(int count=-1){m_count=count;}
  82. operator int()const {return m_count;}
  83. };
  84. struct zistream
  85. {
  86. private:
  87. const uint8_t* m_buff;
  88. size_t m_pos,m_size;
  89. bool m_owner_buff;
  90. npc m_np;
  91. void assert_buff_size(size_t len)
  92. {
  93. if(len > m_size)
  94. {
  95. char buf[256];
  96. sprintf(buf, "无更多的数据:%s(size=%d,pos=%d,fetch=%d).", __FUNCTION__,(int)m_size,(int)m_pos,(int)len);
  97. throw stream_exception(buf);
  98. }
  99. }
  100. public:
  101. const uint8_t*buff()const{return m_buff;}
  102. const uint8_t*cur_data()const{return m_buff+m_pos;}
  103. size_t pos()const{return m_pos;}
  104. void load(void*d,size_t len,size_t pos)
  105. {
  106. assert_buff_size(pos+len);
  107. endian::copy(d,m_buff+pos,len);
  108. }
  109. void load(void*d,size_t len)
  110. {
  111. load(d,len,m_pos);
  112. m_pos+=len;
  113. }
  114. int32_t load_int8 (size_t pos){int8_t rc;load(&rc,sizeof(rc),pos);return rc;}
  115. int32_t load_int16(size_t pos){int16_t rc;load(&rc,sizeof(rc),pos);return rc;}
  116. int32_t load_int32(size_t pos){int32_t rc;load(&rc,sizeof(rc),pos);return rc;}
  117. int64_t load_int64(size_t pos){int64_t rc;load(&rc,sizeof(rc),pos);return rc;}
  118. int32_t load_int8 (){int8_t rc; load(&rc,sizeof(rc)); return rc;}
  119. int32_t load_int16(){int16_t rc; load(&rc,sizeof(rc)); return rc;}
  120. int32_t load_int32(){int32_t rc; load(&rc,sizeof(rc)); return rc;}
  121. int64_t load_int64(){int64_t rc; load(&rc,sizeof(rc)); return rc;}
  122. uint32_t load_uint8 (size_t pos){uint8_t rc;load(&rc,sizeof(rc),pos);return rc;}
  123. uint32_t load_uint16(size_t pos){uint16_t rc;load(&rc,sizeof(rc),pos);return rc;}
  124. uint32_t load_uint32(size_t pos){uint32_t rc;load(&rc,sizeof(rc),pos);return rc;}
  125. uint64_t load_uint64(size_t pos){uint64_t rc;load(&rc,sizeof(rc),pos);return rc;}
  126. uint32_t load_uint8 (){uint8_t rc; load(&rc,sizeof(rc)); return rc;}
  127. uint32_t load_uint16(){uint16_t rc; load(&rc,sizeof(rc)); return rc;}
  128. uint32_t load_uint32(){uint32_t rc; load(&rc,sizeof(rc)); return rc;}
  129. uint64_t load_uint64(){uint64_t rc; load(&rc,sizeof(rc)); return rc;}
  130. std::vector<uint8_t> load_bytes(size_t count)
  131. {
  132. std::vector<uint8_t> rc(count,0);
  133. load(&*rc.begin(), count);
  134. return std::move(rc);
  135. }
  136. std::string load_string(size_t count)
  137. {
  138. std::string rc(count,' ');
  139. load(&*rc.begin(), count);
  140. return std::move(rc);
  141. }
  142. bool eof() const {return m_pos >= m_size; }
  143. size_t size()const {return m_size;}
  144. void set_size(uint32_t size){m_size=size;}
  145. public:
  146. zistream(const void*pbuf,size_t buf_size,bool owner_buff=false)
  147. :m_buff((const uint8_t*)pbuf)
  148. ,m_pos(0)
  149. ,m_size(buf_size)
  150. ,m_owner_buff(owner_buff)
  151. {
  152. }
  153. ~zistream()
  154. {
  155. if(m_owner_buff && m_buff)
  156. {
  157. ::free((void*)m_buff);
  158. }
  159. }
  160. friend zistream& operator>>(zistream&is, uint8_t &i) { i=is.load_uint8 ();return is; }
  161. friend zistream& operator>>(zistream&is, uint16_t&i) { i=is.load_uint16();return is; }
  162. friend zistream& operator>>(zistream&is, uint32_t&i) { i=is.load_uint32();return is; }
  163. friend zistream& operator>>(zistream&is, uint64_t&i) { i=is.load_uint64();return is; }
  164. friend zistream& operator>>(zistream&is, int8_t &i) { i=is.load_int8 ();return is; }
  165. friend zistream& operator>>(zistream&is, int16_t&i) { i=is.load_int16();return is; }
  166. friend zistream& operator>>(zistream&is, int32_t&i) { i=is.load_int32();return is; }
  167. friend zistream& operator>>(zistream&is, int64_t&i) { i=is.load_int64();return is; }
  168. friend zistream& operator>>(zistream&is, const npc&i) { is.m_np.reset(i);return is;}
  169. friend zistream& operator>>(zistream&is, void*x)
  170. {
  171. if(is.m_np <= 0)
  172. return is;
  173. is.load(x,is.m_np);
  174. is.m_np.reset();
  175. return is;
  176. }
  177. friend zistream& operator>>(zistream&is, const skip&s)
  178. {
  179. is.assert_buff_size(s.m_count);
  180. is.m_pos+=s.m_count;
  181. return is;
  182. }
  183. };
  184. class zostream
  185. {
  186. private:
  187. uint8_t* m_buff;
  188. size_t m_pos,m_size;
  189. int m_owner_buff;
  190. void grow(size_t nsize)
  191. {
  192. if(m_buff!=nullptr && nsize<m_size)
  193. return;
  194. while(m_size<nsize)
  195. m_size<<=1;
  196. m_buff=(uint8_t*) realloc(m_buff,m_size);
  197. m_owner_buff=1;
  198. }
  199. public:
  200. zostream& save(const void*pbuf,int len,size_t pos)
  201. {
  202. grow(len+pos);
  203. endian::copy(m_buff+pos,pbuf,len);
  204. if(len+pos>m_pos)
  205. m_pos=len+pos;
  206. return *this;
  207. }
  208. zostream& save(const void*pbuf,int len)
  209. {
  210. grow(len+m_pos);
  211. endian::copy(m_buff+m_pos,pbuf,len);
  212. m_pos+=len;
  213. return *this;
  214. }
  215. zostream& save(int8_t i,size_t pos){return save(&i,sizeof(i),pos);}
  216. zostream& save(int16_t i,size_t pos){return save(&i,sizeof(i),pos);}
  217. zostream& save(int32_t i,size_t pos){return save(&i,sizeof(i),pos);}
  218. zostream& save(int64_t i,size_t pos){return save(&i,sizeof(i),pos);}
  219. zostream& save(int8_t i) {return save(&i,sizeof(i));}
  220. zostream& save(int16_t i) {return save(&i,sizeof(i));}
  221. zostream& save(int32_t i) {return save(&i,sizeof(i));}
  222. zostream& save(int64_t i) {return save(&i,sizeof(i));}
  223. zostream& save(uint8_t i,size_t pos){return save(&i,sizeof(i),pos);}
  224. zostream& save(uint16_t i,size_t pos){return save(&i,sizeof(i),pos);}
  225. zostream& save(uint32_t i,size_t pos){return save(&i,sizeof(i),pos);}
  226. zostream& save(uint64_t i,size_t pos){return save(&i,sizeof(i),pos);}
  227. zostream& save(uint8_t i) {return save(&i,sizeof(i));}
  228. zostream& save(uint16_t i) {return save(&i,sizeof(i));}
  229. zostream& save(uint32_t i) {return save(&i,sizeof(i));}
  230. zostream& save(uint64_t i) {return save(&i,sizeof(i));}
  231. zostream& save(const std::string&s)
  232. {
  233. return save(s.c_str(), s.size());
  234. }
  235. zostream& save(const char*s)
  236. {
  237. return save(s, strlen(s));
  238. }
  239. friend zostream& operator<<(zostream&os, uint8_t i) { return os.save(i);}
  240. friend zostream& operator<<(zostream&os, uint16_t i) { return os.save(i);}
  241. friend zostream& operator<<(zostream&os, uint32_t i) { return os.save(i);}
  242. friend zostream& operator<<(zostream&os, uint64_t i) { return os.save(i);}
  243. friend zostream& operator<<(zostream&os, int8_t i) { return os.save(i);}
  244. friend zostream& operator<<(zostream&os, int16_t i) { return os.save(i);}
  245. friend zostream& operator<<(zostream&os, int32_t i) { return os.save(i);}
  246. friend zostream& operator<<(zostream&os, int64_t i) { return os.save(i);}
  247. friend zostream& operator<<(zostream&os, const std::string &i) { return os.save(i);}
  248. friend zostream& operator<<(zostream&os, const char*i) { return os.save(i);}
  249. public:
  250. zostream(size_t buf_size,void*buf=nullptr)
  251. :m_buff((uint8_t*)buf)
  252. ,m_pos(0)
  253. ,m_size(buf_size)
  254. {
  255. if(m_buff)
  256. {
  257. m_owner_buff=0;
  258. }
  259. }
  260. ~zostream()
  261. {
  262. if(m_owner_buff && m_buff)
  263. free(m_buff);
  264. }
  265. const uint8_t*data()const{return m_buff;}const
  266. uint8_t *data(){return m_buff;}
  267. size_t size()const {return m_pos;}
  268. };
  269. #endif