buffer.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $TOG: buffer.C /main/9 1998/04/17 11:50:56 mgreess $
  25. *
  26. * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved.
  27. * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
  28. * States. Use of a copyright notice is precautionary only and does not
  29. * imply publication or disclosure.
  30. *
  31. * This software contains confidential information and trade secrets of HaL
  32. * Computer Systems, Inc. Use, disclosure, or reproduction is prohibited
  33. * without the prior express written permission of HaL Computer Systems, Inc.
  34. *
  35. * RESTRICTED RIGHTS LEGEND
  36. * Use, duplication, or disclosure by the Government is subject to
  37. * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
  38. * Technical Data and Computer Software clause at DFARS 252.227-7013.
  39. * HaL Computer Systems, Inc.
  40. * 1315 Dell Avenue, Campbell, CA 95008
  41. *
  42. */
  43. #include "utility/buffer.h"
  44. /***********************************************************/
  45. // Constructor
  46. /***********************************************************/
  47. /*
  48. buffer::buffer(const int sz)
  49. {
  50. _alloc(sz);
  51. }
  52. */
  53. buffer::buffer(buffer& original): v_swap_order(original.v_swap_order)
  54. {
  55. _alloc(original.v_bufsz);
  56. memcpy(v_aptr, original.v_aptr, original.v_bufsz);
  57. }
  58. void buffer::_alloc(const int sz)
  59. {
  60. int void_ptr_size = sizeof(voidPtr);
  61. v_bufsz = sz;
  62. if ( sz > 0 ) {
  63. v_base = ::new char[void_ptr_size+sz];
  64. memset(v_base, (char)0, void_ptr_size+sz);
  65. v_align_offset = int(void_ptr_size - long(v_base) % void_ptr_size);
  66. if (v_align_offset != void_ptr_size)
  67. v_base += v_align_offset;
  68. else
  69. v_align_offset = 0;
  70. v_allocated = 1;
  71. } else {
  72. v_base = 0;
  73. v_allocated = 0;
  74. }
  75. v_aptr = v_eptr = v_base;
  76. }
  77. /***********************************************************/
  78. // Destructor
  79. /***********************************************************/
  80. buffer::~buffer()
  81. {
  82. if ( v_allocated == 1 ) {
  83. free((char*)(v_base-v_align_offset));
  84. }
  85. }
  86. void buffer::reset()
  87. {
  88. v_eptr = v_base;
  89. v_aptr = v_base;
  90. memset(v_base, (char)0, v_bufsz);
  91. }
  92. /*
  93. void buffer::set_chunk(char* ptr, const int sz)
  94. {
  95. bufsz = sz;
  96. eptr = aptr = base = ptr;
  97. allocated = 0;
  98. }
  99. void buffer::set_content_sz(const int sz)
  100. {
  101. eptr = base + sz;
  102. }
  103. */
  104. /***********************************************************/
  105. // Expand buffer chunk to newsz.
  106. /***********************************************************/
  107. int buffer::expand_chunk(const int newsz)
  108. {
  109. if ( v_allocated == 0 )
  110. throw(stringException("expand a buffer with reference memory"));
  111. int cursz = buf_sz();
  112. if ( newsz <= cursz ) {
  113. v_bufsz = newsz;
  114. return 0;
  115. }
  116. int void_ptr_size = sizeof(voidPtr);
  117. //MESSAGE(cerr, "real expand");
  118. //char* x = (char*)malloc(void_ptr_size+newsz);
  119. char* x = ::new char [void_ptr_size+newsz];
  120. int delta = int(void_ptr_size - long(x) % void_ptr_size);
  121. x += delta ;
  122. memcpy(x, v_base, cursz);
  123. memset(x+cursz, char(0), newsz - cursz);
  124. v_aptr -= long(v_base);
  125. v_eptr -= long(v_base);
  126. //free((char*)(v_base-v_align_offset));
  127. delete (char*)(v_base-v_align_offset);
  128. v_align_offset = delta;
  129. v_base = x;
  130. v_aptr += long(v_base);
  131. v_eptr += long(v_base);
  132. v_bufsz = newsz;
  133. return 0;
  134. }
  135. #define CASTBNDEXCEPT
  136. /***********************************************************/
  137. // Get sz chars to the array x. x is supposed allocated
  138. /***********************************************************/
  139. buffer& buffer::get(char *x, int sz)
  140. {
  141. if ( sz + v_aptr > v_eptr ) {
  142. MESSAGE(cerr, "buffer::get(): underflow");
  143. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sz + v_aptr) ));
  144. }
  145. memcpy(x, v_aptr, sz);
  146. v_aptr += sz;
  147. return *this;
  148. }
  149. /***********************************************************/
  150. // skip i chars.
  151. /***********************************************************/
  152. buffer& buffer::skip(int i)
  153. {
  154. if ( i + v_aptr > v_eptr )
  155. MESSAGE(cerr, "buffer::skip(): underflow");
  156. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(i + v_aptr) ));
  157. v_aptr += i;
  158. return *this;
  159. }
  160. /***********************************************************/
  161. // Get a char to y.
  162. /***********************************************************/
  163. buffer& buffer::get(char& y)
  164. {
  165. if ( v_aptr > v_eptr) {
  166. MESSAGE(cerr, "buffer::get(char&): underflow");
  167. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(1+v_aptr)));
  168. }
  169. y = *v_aptr;
  170. v_aptr++;
  171. return *this;
  172. }
  173. buffer& buffer::getusc(int& y)
  174. {
  175. y = *(unsigned char*)v_aptr;
  176. v_aptr++;
  177. return *this;
  178. }
  179. /***********************************************************/
  180. // Get an integer to y. option can be ASCII or BINARY.
  181. /***********************************************************/
  182. buffer& buffer::get(int& y)
  183. {
  184. unsigned int x;
  185. get(x);
  186. y = (int)x;
  187. return *this;
  188. }
  189. buffer& buffer::get(unsigned int& y)
  190. {
  191. if ( v_aptr + sizeof(y) > v_eptr ) {
  192. MESSAGE(cerr, "buffer::get(int&): underflow");
  193. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(unsigned)+v_aptr)));
  194. }
  195. get((char*)&y, sizeof(y));
  196. #ifdef PORTABLE_DB
  197. if ( v_swap_order == true )
  198. ORDER_SWAP_UINT(y);
  199. #endif
  200. return *this;
  201. }
  202. /***********************************************************/
  203. // Get a long to y. option can be ASCII or BINARY.
  204. /***********************************************************/
  205. buffer& buffer::get(long& y)
  206. {
  207. //MESSAGE(cerr, "WARNING: buffer::get(long& y) +++++++++++++++++++++++");
  208. if ( v_aptr + sizeof(y) > v_eptr ) {
  209. MESSAGE(cerr, "buffer::get(long&): underflow");
  210. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(long)+v_aptr)));
  211. }
  212. get((char*)&y, sizeof(y));
  213. #ifdef PORTABLE_DB
  214. if ( v_swap_order == true )
  215. ORDER_SWAP_LONG(y);
  216. #endif
  217. return *this;
  218. }
  219. /***********************************************************/
  220. // Get a float to y.
  221. /***********************************************************/
  222. buffer& buffer::get(float& y)
  223. {
  224. if ( v_aptr + sizeof(y) > v_eptr ) {
  225. MESSAGE(cerr, "buffer::get(float &): underflow");
  226. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
  227. }
  228. get((char*)&y, sizeof(y));
  229. #ifdef PORTABLE_DB
  230. if ( v_swap_order == true )
  231. ORDER_SWAP_FLOAT(y);
  232. #endif
  233. return *this;
  234. }
  235. buffer& buffer::get(unsigned short& y)
  236. {
  237. if ( v_aptr + sizeof(y) > v_eptr ) {
  238. MESSAGE(cerr, "buffer::get(float &): underflow");
  239. throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
  240. }
  241. get((char*)&y, sizeof(y));
  242. #ifdef PORTABLE_DB
  243. if ( v_swap_order == true )
  244. ORDER_SWAP_USHORT(y);
  245. #endif
  246. return *this;
  247. }
  248. /***********************************************************/
  249. // Put a char to buffer.
  250. /***********************************************************/
  251. buffer& buffer::put(const char content, Boolean exp_buf)
  252. {
  253. //return put((char*)&content, sizeof(content));
  254. if ( (int) v_bufsz == content_sz() )
  255. {
  256. if ( exp_buf == true )
  257. expand_chunk(v_bufsz + 10);
  258. else {
  259. MESSAGE( cerr, "buffer::put(const char): overflow");
  260. throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, 1) );
  261. }
  262. }
  263. *v_eptr = content;
  264. v_eptr++;
  265. return *this;
  266. }
  267. buffer& buffer::put(const unsigned char content, Boolean exp_buf)
  268. {
  269. return put((char*)&content, sizeof(content), exp_buf);
  270. }
  271. /***********************************************************/
  272. // Put a unsigned int to buffer.
  273. /***********************************************************/
  274. buffer& buffer::put(const int content, Boolean exp_buf)
  275. {
  276. return put((unsigned int)content, exp_buf);
  277. }
  278. buffer& buffer::put(const unsigned int content, Boolean exp_buf)
  279. {
  280. #ifdef PORTABLE_DB
  281. if ( v_swap_order == true )
  282. ORDER_SWAP_UINT(content);
  283. #endif
  284. return put((char*)&content, sizeof(content), exp_buf);
  285. }
  286. /***********************************************************/
  287. // Put a long to buffer.
  288. /***********************************************************/
  289. buffer& buffer::put(const long content, Boolean exp_buf)
  290. {
  291. //MESSAGE(cerr, "WARNING: buffer::put(long& y) =====================");
  292. #ifdef PORTABLE_DB
  293. if ( v_swap_order == true )
  294. ORDER_SWAP_LONG(content);
  295. #endif
  296. return put((char*)&content, sizeof(content), exp_buf);
  297. }
  298. /***********************************************************/
  299. // Put a unsigned short to buffer.
  300. /***********************************************************/
  301. buffer& buffer::put(const unsigned short content, Boolean exp_buf)
  302. {
  303. #ifdef PORTABLE_DB
  304. if ( v_swap_order == true )
  305. ORDER_SWAP_USHORT(content);
  306. #endif
  307. return put((char*)&content, sizeof(content), exp_buf);
  308. }
  309. /***********************************************************/
  310. // Put a float to buffer.
  311. /***********************************************************/
  312. buffer& buffer::put(const float content, Boolean exp_buf)
  313. {
  314. #ifdef PORTABLE_DB
  315. if ( v_swap_order == true )
  316. ORDER_SWAP_FLOAT(content);
  317. #endif
  318. return put((char*)&content, sizeof(content), exp_buf);
  319. }
  320. /***********************************************************/
  321. // Put sz chars to buffer.
  322. /***********************************************************/
  323. buffer& buffer::put(const char* content, int sz, Boolean exp_buf)
  324. {
  325. if ( sz > (int)(v_bufsz - content_sz()) ) {
  326. if ( exp_buf == true )
  327. expand_chunk(v_bufsz + sz);
  328. else {
  329. MESSAGE( cerr, "buffer::put(char*, int): overflow");
  330. throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, sz) );
  331. }
  332. }
  333. //memcpy(v_eptr, content, sz);
  334. //debug(cerr, int(v_base));
  335. //debug(cerr, int(v_eptr));
  336. for ( int i=0; i<sz; i++ ) {
  337. v_eptr[i] = content[i];
  338. //debug(cerr, int(v_eptr[i]));
  339. }
  340. v_eptr += sz;
  341. return *this;
  342. }
  343. Boolean operator ==(buffer&x, buffer& y)
  344. {
  345. if ( x.content_sz() != y.content_sz() ) {
  346. debug(cerr, x.content_sz());
  347. debug(cerr, y.content_sz());
  348. }
  349. unsigned char* x_buf = (unsigned char*)x.get_base();
  350. unsigned char* y_buf = (unsigned char*)y.get_base();
  351. for ( int i=0; i<x.content_sz(); i++ ) {
  352. if ( x_buf[i] != y_buf[i] ) {
  353. debug(cerr, i);
  354. debug(cerr, x_buf[i]);
  355. debug(cerr, y_buf[i]);
  356. //return false;
  357. }
  358. }
  359. return true;
  360. }
  361. /***********************************************************/
  362. // show content
  363. /***********************************************************/
  364. ostream& operator<<(ostream& s, buffer& b)
  365. {
  366. //debug(s, b.bufsz);
  367. //debug(s, b.content_sz());
  368. //debug(s, b.base);
  369. //debug(s, b.eptr);
  370. //debug(s, b.aptr);
  371. int x = b.v_eptr - b.v_base ;
  372. int i;
  373. for ( i = 0; i < x; i++ ) {
  374. s << b.v_base[i];
  375. /*
  376. if ( isprint(b.v_base[i]) )
  377. cout << b.v_base[i];
  378. else
  379. cout << int(b.v_base[i]);
  380. */
  381. }
  382. MESSAGE(cerr, "buffer=");
  383. for ( i = 0; i < x; i++ ) {
  384. cout << int(b.v_base[i]) << " ";
  385. }
  386. cout << endl;
  387. return s;
  388. }
  389. /*
  390. void buffer::cdrIn(buffer& buf)
  391. {
  392. buf.get(v_bufsz);
  393. buf.get(v_base, v_bufsz);
  394. }
  395. void buffer::cdrOut(buffer& buf)
  396. {
  397. buf.put(v_bufsz);
  398. buf.put(v_base, v_bufsz);
  399. }
  400. */