BufferOperation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #ifndef CBufferOperation_h
  2. #define CBufferOperation_h
  3. #include "SimpleString.h"
  4. #include "AutoArray.h"
  5. #include "Blob.h"
  6. namespace Chromium {
  7. #define IOBUF_T_I8 0
  8. #define IOBUF_T_I4 1
  9. #define IOBUF_T_I2 2
  10. #define IOBUF_T_I1 3
  11. #define IOBUF_T_STR 4
  12. #define IOBUF_T_BUF 5
  13. #define IOBUF_T_WSTR 6
  14. #define IOBUF_T_7BIT 7
  15. /////////////////// Read Buf Start ///////////////////////
  16. static void buffer_read_buf(const char* iobuf, char* buf, int n, int* rpos)
  17. {
  18. // assert(content_size(iobuf) >= n);
  19. memcpy(buf, iobuf + *rpos, n);
  20. *rpos += n;
  21. }
  22. static void buffer_read_7bit_int(const char* iobuf, int* v, int* rpos)
  23. {
  24. int count = 0;
  25. int shift = 0;
  26. unsigned char b;
  27. do {
  28. if (shift == 5 * 7) // corrupted stream
  29. assert(0);
  30. b = iobuf[*rpos];
  31. *rpos += 1;
  32. count |= (b & 0x7F) << shift;
  33. shift += 7;
  34. } while (b & 0x80); // most left bit is flag
  35. *v = count;
  36. }
  37. static void buffer_read(const char* iobuf, int t, void* v, int* n, int* rpos) {
  38. int len = 0;
  39. switch (t)
  40. {
  41. case IOBUF_T_I8:
  42. len += 8;
  43. buffer_read_buf(iobuf, (char*)v, len, rpos);
  44. break;
  45. case IOBUF_T_I4:
  46. len += 4;
  47. buffer_read_buf(iobuf, (char*)v, len, rpos);
  48. break;
  49. case IOBUF_T_I2:
  50. len += 2;
  51. buffer_read_buf(iobuf, (char*)v, len, rpos);
  52. break;
  53. case IOBUF_T_I1:
  54. len += 1;
  55. buffer_read_buf(iobuf, (char*)v, len, rpos);
  56. break;
  57. case IOBUF_T_STR:
  58. if (v) {
  59. int old_rpos = *rpos;
  60. buffer_read_7bit_int(iobuf, &len, rpos);
  61. if (!n || len < *n) {
  62. buffer_read_buf(iobuf, (char*)v, len, rpos);
  63. *((char*)v + len) = 0;
  64. if (n)
  65. *n = len;
  66. }
  67. else {
  68. *rpos = old_rpos;
  69. *n = len;
  70. }
  71. }
  72. else {
  73. if (n) {
  74. int old_rpos = *rpos;
  75. buffer_read_7bit_int(iobuf, &len, rpos);
  76. *rpos = old_rpos;
  77. *n = len;
  78. }
  79. else {
  80. assert(0);
  81. }
  82. }
  83. break;
  84. case IOBUF_T_WSTR:
  85. if (v) {
  86. int old_rpos = *rpos;
  87. buffer_read_7bit_int(iobuf, &len, rpos);
  88. if (!n || len < *n) {
  89. buffer_read_buf(iobuf, (char*)v, len, rpos);
  90. #if (defined _WIN32 || defined _WIN64)
  91. * ((char*)v + len) = 0; // wchar termination char is 2 byte
  92. *((char*)v + len + 1) = 0;
  93. #else
  94. memset((void*)((char*)v + len), 0, sizeof(wchar_t));
  95. #endif
  96. if (n)
  97. *n = len;
  98. }
  99. else {
  100. *rpos = old_rpos;
  101. *n = len;
  102. }
  103. }
  104. else {
  105. if (n) {
  106. int old_rpos = *rpos;
  107. buffer_read_7bit_int(iobuf, &len, rpos);
  108. *rpos = old_rpos;
  109. *n = len;
  110. }
  111. else {
  112. assert(0);
  113. }
  114. }
  115. break;
  116. case IOBUF_T_BUF:
  117. if (*n > 0)
  118. buffer_read_buf(iobuf, (char*)v, *n, rpos);
  119. break;
  120. case IOBUF_T_7BIT:
  121. buffer_read_7bit_int(iobuf, (int*)v, rpos);
  122. break;
  123. default:
  124. assert(0);
  125. }
  126. }
  127. void buffer_format_readv(const char* iobuf, int* rpos, const char* fmt, va_list arg)
  128. {
  129. if (fmt) {
  130. const char* p = fmt;
  131. while (*p) {
  132. switch (*p) {
  133. case '8':
  134. buffer_read(iobuf, IOBUF_T_I8, va_arg(arg, void*), NULL, rpos);
  135. break;
  136. case '7': // 7bit int code
  137. buffer_read(iobuf, IOBUF_T_7BIT, va_arg(arg, void*), NULL, rpos);
  138. break;
  139. case '4':
  140. buffer_read(iobuf, IOBUF_T_I4, va_arg(arg, void*), 0, rpos);
  141. break;
  142. case '2':
  143. buffer_read(iobuf, IOBUF_T_I2, va_arg(arg, void*), 0, rpos);
  144. break;
  145. case '1':
  146. buffer_read(iobuf, IOBUF_T_I1, va_arg(arg, void*), 0, rpos);
  147. break;
  148. case 's':case 'S':
  149. {
  150. char** pstr = va_arg(arg, char**);
  151. int slen;
  152. buffer_read(iobuf, IOBUF_T_STR, NULL, &slen, rpos);
  153. //*pstr = (char*)malloc(slen+1);
  154. *pstr = new char[slen + 1];
  155. buffer_read(iobuf, IOBUF_T_STR, *pstr, 0, rpos);
  156. }
  157. break;
  158. case 'w': case 'W':
  159. {
  160. wchar_t** pstr = va_arg(arg, wchar_t**);
  161. int slen;
  162. buffer_read(iobuf, IOBUF_T_WSTR, NULL, &slen, rpos);
  163. #if (defined _WIN32 || defined _WIN64)
  164. * pstr = new wchar_t[slen + 2];
  165. #else
  166. * pstr = new wchar_t[slen + 1];
  167. #endif
  168. buffer_read(iobuf, IOBUF_T_WSTR, *pstr, 0, rpos);
  169. }
  170. break;
  171. case 'b':case 'B':
  172. {
  173. void* buf = va_arg(arg, void*);
  174. int size = va_arg(arg, int);
  175. buffer_read(iobuf, IOBUF_T_BUF, buf, &size, rpos);
  176. }
  177. break;
  178. default:
  179. assert(0);
  180. break;
  181. }
  182. p++;
  183. }
  184. }
  185. }
  186. class BufferFormatRead {
  187. public:
  188. static void buffer_format_read(const char* iobuf, int* rpos, const char* fmt, ...)
  189. {
  190. va_list arg;
  191. va_start(arg, fmt);
  192. buffer_format_readv(iobuf, rpos, fmt, arg);
  193. va_end(arg);
  194. }
  195. };
  196. template<class T>
  197. static inline void ReadT(const char* iobuf, T& t, int* rpos) {
  198. int size = sizeof(t);
  199. buffer_read(iobuf, IOBUF_T_BUF, &t, &size, rpos);
  200. }
  201. static inline void ReadT(const char* iobuf, void* data, int& len, int* rpos) {
  202. buffer_read(iobuf, IOBUF_T_BUF, data, &len, rpos);
  203. }
  204. template<class T>
  205. static inline void ReadArrayT(const char* iobuf, CAutoArray<T>& t, int* rpos) {
  206. int nArrayNum(0);
  207. buffer_read(iobuf, IOBUF_T_I4, &nArrayNum, NULL, rpos);
  208. if (nArrayNum > 0)
  209. {
  210. t.Init(nArrayNum);
  211. for (int i = 0; i < nArrayNum; i++)
  212. {
  213. ReadT(iobuf, t[i], rpos);
  214. }
  215. }
  216. }
  217. static inline void ReadT(const char* iobuf, CSimpleStringA& t, int* rpos) {
  218. #if (defined _WIN32 || defined _WIN64)
  219. wchar_t* wt = NULL;
  220. #else
  221. SPCHAR* wt = NULL;
  222. #endif
  223. BufferFormatRead::buffer_format_read(iobuf, rpos, "w", &wt); // string is unicode so we can compatible with silverlight
  224. CSimpleStringW wStr = wt;
  225. free(wt);
  226. t = CSimpleStringW2A(wStr);
  227. }
  228. static inline void ReadT(const char* iobuf, CSimpleStringW& t, int* rpos) {
  229. #if (defined _WIN32 || defined _WIN64)
  230. wchar_t* wt = NULL;
  231. #else
  232. SPCHAR* wt = NULL;
  233. #endif
  234. BufferFormatRead::buffer_format_read(iobuf, rpos, "w", &wt); // string is unicode so we can compatible with silverlight
  235. t = wt;
  236. free(wt);
  237. }
  238. /////////////////// Read Buf End///////////////////////
  239. ////////////////// Write Buffer Start /////////////////////
  240. static void expand(char* iobuf, int head_capacity, int newcapacity,
  241. int* capacity, int* hcapacity, int* rpos, int* wpos)
  242. {
  243. if (head_capacity == -1) {
  244. int t = *capacity;
  245. while (newcapacity > t)
  246. t = t * 2;
  247. if (t > *capacity) {
  248. iobuf = (char*)realloc(iobuf, t);
  249. *capacity = t;
  250. }
  251. }
  252. else if (newcapacity == -1) {
  253. int t = *hcapacity;
  254. while (head_capacity > t)
  255. t = t * 2;
  256. if (t > *rpos) {
  257. char* new_data;
  258. int new_rpos;
  259. int new_wpos;
  260. newcapacity = *capacity + (t - *hcapacity);
  261. // new_data = (char*)malloc(newcapacity);
  262. new_data = new char[newcapacity];
  263. new_rpos = t;
  264. new_wpos = new_rpos + (*wpos - *rpos);
  265. memcpy(new_data + new_rpos, iobuf + *rpos, *wpos - *rpos);
  266. free(iobuf);
  267. iobuf = new_data;
  268. *rpos = new_rpos;
  269. *wpos = new_wpos;
  270. *hcapacity = t;
  271. *capacity = newcapacity;
  272. }
  273. }
  274. else {
  275. int t = *capacity;
  276. // char *new_data = (char*)malloc(t);
  277. char* new_data = new char[t];
  278. int new_rpos = head_capacity;
  279. int new_wpos = new_rpos + (*wpos - *rpos);
  280. while (newcapacity > t)
  281. t = t * 2;
  282. memcpy(new_data + new_rpos, iobuf + *rpos, *wpos - *rpos);
  283. free(iobuf);
  284. iobuf = new_data;
  285. *rpos = new_rpos;
  286. *wpos = new_wpos;
  287. *capacity = t;
  288. }
  289. }
  290. static void buffer_write_buf(char* iobuf, const char* buf, int n, int* wpos, int* capacity)
  291. {
  292. int rpos = 0;
  293. int hcapacity = 0;
  294. if (n + (*wpos) > *capacity)
  295. expand(iobuf, -1, n + *wpos, capacity, &hcapacity, &rpos, wpos);
  296. memcpy(iobuf + *wpos, buf, n);
  297. *wpos += n;
  298. }
  299. static int write_7bit_int(unsigned char buf[8], unsigned int v)
  300. {
  301. int i = 0;
  302. while (v >= 0x80) {
  303. buf[i++] = (unsigned char)(v | 0x80); // set most left bit == 1
  304. v >>= 7;
  305. }
  306. buf[i++] = (unsigned char)v;
  307. return i;
  308. }
  309. static void buffer_write_7bit_int(char* iobuf, unsigned int v, int* wpos, int* capacity)
  310. {
  311. char buf[8];
  312. int n = write_7bit_int((unsigned char*)buf, v);
  313. buffer_write_buf(iobuf, buf, n, wpos, capacity);
  314. }
  315. void buffer_write(char* iobuf, int t, const void* v, int n, int* wpos, int* capacity)
  316. {
  317. int len = 0;
  318. switch (t) {
  319. case IOBUF_T_I8:
  320. len += 8;
  321. buffer_write_buf(iobuf, (const char*)v, len, wpos, capacity);
  322. break;
  323. case IOBUF_T_I4:
  324. len += 4;
  325. buffer_write_buf(iobuf, (const char*)v, len, wpos, capacity);
  326. break;
  327. case IOBUF_T_I2:
  328. len += 2;
  329. buffer_write_buf(iobuf, (const char*)v, len, wpos, capacity);
  330. break;
  331. case IOBUF_T_I1:
  332. len += 1;
  333. buffer_write_buf(iobuf, (const char*)v, len, wpos, capacity);
  334. break;
  335. case IOBUF_T_STR:
  336. if (!v) {
  337. n = 0;
  338. }
  339. else {
  340. if (n == -1)
  341. n = strlen((const char*)v);
  342. }
  343. buffer_write_7bit_int(iobuf, (unsigned int)n, wpos, capacity);
  344. if (n > 0)
  345. buffer_write_buf(iobuf, (const char*)v, n, wpos, capacity);
  346. break;
  347. case IOBUF_T_WSTR:
  348. if (!v) {
  349. n = 0;
  350. }
  351. else {
  352. if (n == -1)
  353. #if (defined _WIN32 || defined _WIN64)
  354. n = wcslen((const wchar_t*)v) << 1;
  355. #else
  356. n = wcslen((const wchar_t*)v) << 2;
  357. #endif
  358. }
  359. buffer_write_7bit_int(iobuf, n, wpos, capacity);
  360. if (n > 0)
  361. buffer_write_buf(iobuf, (const char*)v, n, wpos, capacity);
  362. break;
  363. case IOBUF_T_BUF:
  364. if (n > 0)
  365. buffer_write_buf(iobuf, (const char*)v, n, wpos, capacity);
  366. break;
  367. case IOBUF_T_7BIT:
  368. buffer_write_7bit_int(iobuf, *(unsigned int*)v, wpos, capacity);
  369. break;
  370. default:
  371. assert(0);
  372. break;
  373. }
  374. }
  375. template<class T>
  376. static inline void WriteT(char* iobuf, T& t, int* wpos, int* capacity)
  377. {
  378. buffer_write(iobuf, IOBUF_T_BUF, &t, sizeof(t), wpos, capacity);
  379. }
  380. template<class T>
  381. static inline void WriteArrayT(char* iobuf, CAutoArray<T>& t, int* wpos, int* capacity)
  382. {
  383. int nArrayNum = t.GetCount();
  384. buffer_write(iobuf, IOBUF_T_I4, &nArrayNum, 0, wpos, capacity);
  385. for (int i = 0; i < nArrayNum; i++)
  386. {
  387. WriteT(iobuf, t[i], wpos, capacity);
  388. }
  389. }
  390. template<class T>
  391. static inline void WriteArrayT(char* iobuf, const CAutoArray<T>& t, int* wpos, int* capacity)
  392. {
  393. int nArrayNum = t.GetCount();
  394. buffer_write(iobuf, IOBUF_T_I4, &nArrayNum, 0, wpos, capacity);
  395. for (int i = 0; i < nArrayNum; i++)
  396. {
  397. WriteT(iobuf, t[i], wpos, capacity);
  398. }
  399. }
  400. static inline void WriteT(char* iobuf, CSimpleStringA& t, int* wpos, int* capacity)
  401. {
  402. CSimpleStringW wt = CSimpleStringA2W(t); // string is unicode so we can compatible with silverlight
  403. buffer_write(iobuf, IOBUF_T_WSTR, wt.GetData(), -1, wpos, capacity);
  404. }
  405. static inline void WriteT(char* iobuf, CSimpleStringW& t, int* wpos, int* capacity)
  406. {
  407. buffer_write(iobuf, IOBUF_T_WSTR, t.GetData(), -1, wpos, capacity);
  408. }
  409. static inline void WriteT(char* iobuf, const CSimpleStringA& t, int* wpos, int* capacity)
  410. {
  411. CSimpleStringW wt = CSimpleStringA2W(t); // string is unicode so we can compatible with silverlight
  412. buffer_write(iobuf, IOBUF_T_WSTR, wt.GetData(), -1, wpos, capacity);
  413. }
  414. static inline void WriteT(char* iobuf, const CSimpleStringW& t, int* wpos, int* capacity)
  415. {
  416. buffer_write(iobuf, IOBUF_T_WSTR, t.GetData(), -1, wpos, capacity);
  417. }
  418. static inline void WriteT(char* iobuf, const char* t, int* wpos, int* capacity, int len)
  419. {
  420. buffer_write(iobuf, IOBUF_T_BUF, t, len, wpos, capacity);
  421. }
  422. // static inline void WriteT(char *iobuf, CBlob &t)
  423. // {
  424. // // iobuffer_write(iobuf, IOBUF_T_I4, &t.m_iLength, 0);
  425. // // iobuffer_write(iobuf, IOBUF_T_BUF, t.m_pData, t.m_iLength);
  426. // }
  427. //
  428. //
  429. // static inline void WriteT(char *iobuf, const CBlob &t)
  430. // {
  431. // // iobuffer_write(iobuf, IOBUF_T_I4, &t.m_iLength, 0);
  432. // // iobuffer_write(iobuf, IOBUF_T_BUF, t.m_pData, t.m_iLength);
  433. // }
  434. ///////////////// Write Buffer End //////////////////////
  435. }
  436. #endif