Bstream.C
上传用户:zbbssh
上传日期:2007-01-08
资源大小:196k
文件大小:8k
源码类别:

CA认证

开发平台:

C/C++

  1. /*
  2. ------------------------------------------------------------------
  3.   Copyright
  4.   Sun Microsystems, Inc.
  5.   Copyright (C) 1994, 1995, 1996 Sun Microsystems, Inc.  All Rights
  6.   Reserved.
  7.   Permission is hereby granted, free of charge, to any person
  8.   obtaining a copy of this software and associated documentation
  9.   files (the "Software"), to deal in the Software without
  10.   restriction, including without limitation the rights to use,
  11.   copy, modify, merge, publish, distribute, sublicense, and/or sell
  12.   copies of the Software or derivatives of the Software, and to 
  13.   permit persons to whom the Software or its derivatives is furnished 
  14.   to do so, subject to the following conditions:
  15.   The above copyright notice and this permission notice shall be
  16.   included in all copies or substantial portions of the Software.
  17.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18.   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19.   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20.   NONINFRINGEMENT.  IN NO EVENT SHALL SUN MICROSYSTEMS, INC., BE LIABLE
  21.   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22.   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23.   CONNECTION WITH THE SOFTWARE OR DERIVATES OF THIS SOFTWARE OR 
  24.   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.   Except as contained in this notice, the name of Sun Microsystems, Inc.
  26.   shall not be used in advertising or otherwise to promote
  27.   the sale, use or other dealings in this Software or its derivatives 
  28.   without prior written authorization from Sun Microsystems, Inc.
  29. */
  30. #pragma ident "@(#)Bstream.C 1.5 96/01/29 Sun Microsystems"
  31. #include <sys/types.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include "Bstream.h"
  37. Bstream::Bstream()
  38. {
  39.   len = 0; peekindex = 0;
  40. deletethis = datap = NULL;
  41. }
  42. Bstream::Bstream(int al, byte *adp)
  43. {
  44. len = al;
  45. deletethis = datap = new byte[len];
  46. (void) bcopy(adp, datap, al);
  47. peekindex = 0;
  48. return;
  49. }
  50. Bstream::Bstream(const char *str)
  51. {
  52. len = strlen(str);
  53. deletethis = datap = new byte[len];
  54. (void) bcopy((void *)str, datap, len);
  55. peekindex = 0;
  56. return;
  57. }
  58. Bstream::Bstream(const Bstream& a)
  59. {
  60. deletethis = datap = new byte[a.len];
  61. len = a.len;
  62. peekindex = 0;
  63. (void) bcopy(a.datap, datap, len);
  64. return;
  65. }
  66. Bstream::Bstream(const String& a)
  67. {
  68. deletethis = datap = new byte[a.length()];
  69. len = a.length();
  70. peekindex = 0;
  71. (void) bcopy((byte *)((const char *)a), datap, len);
  72. return;
  73. }
  74. Bstream&
  75. Bstream:: operator =(const Bstream& a)
  76. {
  77. if (deletethis != a.deletethis) { // Check for a = a;
  78. if (deletethis != NULL)
  79. delete(deletethis);
  80. deletethis = datap = new byte[a.len];
  81. len = a.len;
  82. peekindex = 0;
  83. (void) bcopy(a.datap, datap, len);
  84. }
  85. return (*this);
  86. }
  87. Boolean operator ==(const Bstream& a, const Bstream& b)
  88. {
  89. Boolean retval;
  90. int  ret;
  91. if (a.len != b.len)
  92. return (BOOL_FALSE);
  93. ret = bcmp(a.datap, b.datap, a.len);
  94. if (ret == 0) retval = BOOL_TRUE;
  95. else retval = BOOL_FALSE;
  96. return (retval);
  97. }
  98. Boolean operator !=(const Bstream& a, const Bstream& b)
  99. {
  100. if (a == b)
  101. return (BOOL_FALSE);
  102. else
  103. return (BOOL_TRUE);
  104. }
  105. Bstream operator +(const Bstream& a, const Bstream& b)
  106. {
  107. Bstream sumstr;
  108. int sumlen = a.len + b.len;
  109. byte *sumdatap = new byte[sumlen];
  110. (void) bcopy(a.datap, sumdatap, a.len);
  111. (void) bcopy(b.datap, sumdatap+a.len, b.len);
  112. sumstr = Bstream(sumlen, sumdatap);
  113. delete sumdatap;
  114. return (sumstr);
  115. }
  116. Bstream&
  117. Bstream::operator +=(const Bstream& b)
  118. {
  119. int sumlen = len + b.len;
  120. byte *sumdatap = new byte[sumlen];
  121. (void) bcopy(datap, sumdatap, len);
  122. (void) bcopy(b.datap, sumdatap + len, b.len);
  123. if (deletethis != NULL) {
  124. delete (deletethis);
  125. } else
  126. printf("deletethis was NULLn");
  127. deletethis = datap = sumdatap;
  128. len = sumlen;
  129. peekindex = 0;
  130. return (*this);
  131. }
  132. Bstream::~Bstream()
  133. {
  134. if (deletethis != NULL)
  135. delete(deletethis);
  136. }
  137. Bstream::operator const char*() const
  138. {
  139. return ((char *)datap);
  140. }
  141. #ifdef ndef
  142. Bstream::operator String() const
  143. {
  144. return (String((char *)datap, len));
  145. }
  146. #endif ndef
  147. String
  148. Bstream::getstr() const
  149. {
  150. return (String((char *)datap, len));
  151. }
  152. int Bstream::consume(int l)
  153. {
  154. if (l > len)
  155. return (FAILURE);
  156. len -= l;
  157. datap += l;
  158. peekindex = 0;
  159. return (SUCCESS);
  160. }
  161. int Bstream::truncate(int l)
  162. {
  163. if (l > len)
  164. return (FAILURE);
  165. len -= l;
  166. peekindex = 0;
  167. return (SUCCESS);
  168. }
  169. int Bstream::getlength() const
  170. {
  171. return (len);
  172. }
  173. int Bstream::fetchbyte(byte& val)
  174. {
  175. if (len == 0)
  176. return (FAILURE);
  177. val = *datap++;
  178. len -= 1;
  179. peekindex = 0;
  180. return (SUCCESS);
  181. }
  182. int Bstream::peekbyte(byte& val)
  183. {
  184. if (len == 0 || peekindex > len)
  185. return (FAILURE);
  186. val = datap[peekindex++];
  187. return (SUCCESS);
  188. }
  189. byte *
  190. Bstream::getdatap() const
  191. {
  192. return (datap);
  193. }
  194. Bstream
  195. Bstream::replace(const Bstream& from, const Bstream& to) const
  196. {
  197. int i, fromlen, tolen;
  198. Bstream scan = *this; // XXX
  199. fromlen = from.getlength();
  200. tolen =  to.getlength();
  201. for (i = 0; i < scan.len; i++) {
  202. if (bcmp(&(scan.datap[i]), from.datap, fromlen) == 0) {
  203. Bstream before(i, scan.datap);
  204. scan.consume(i + fromlen);
  205. return (before + to + scan.replace(from, to));
  206. }
  207. }
  208. return (scan);
  209. }
  210. byte
  211. Bstream::last() const
  212. {
  213. if (len > 0)
  214. return (datap[len - 1]);
  215. else
  216. return (0); // XXX
  217. }
  218. int
  219. Bstream::store(const char *filename) const
  220. {
  221. int fd = ::open(filename, O_RDWR | O_TRUNC | O_CREAT, 0644);
  222. if (fd < 0)
  223. return (fd);
  224. int ret = ::write(fd, (char *)datap, len);
  225. ::close(fd);
  226. return (ret);
  227. }
  228. int
  229. Bstream::write(FILE *f) const
  230. {
  231. int ret = fwrite((char *)datap, 1, len, f);
  232. return (ret);
  233. }
  234. void 
  235. Bstream::print() const
  236. {
  237. const int LINEWIDTH = 20;
  238. for (int i=0; i<len; ) {
  239. for(int j=0; j<LINEWIDTH && ((i+j) < len); j++)
  240. printf("%.2x ", datap[i+j]);
  241. printf("n");
  242. i += LINEWIDTH;
  243. }
  244. }
  245. void 
  246. Bstream::prints() const
  247. {
  248. for (int i=0; i<len; i++) {
  249. printf("%c", (char )datap[i]);
  250. }
  251. }
  252. void 
  253. Bstream::printhexint() const
  254. {
  255. for (int i=0; i<len; i++) {
  256. printf("%.2x", datap[i]);
  257. }
  258. }
  259. void 
  260. Bstream::printdecint() const
  261. {
  262. for (int i=0; i<len; i++) {
  263. // XXX We need to make this a decimal mp int print
  264. printf("%.2x", datap[i]);
  265. }
  266. }
  267. String
  268. Bstream::gethexstr() const
  269. {
  270. char *buf = new char[len*2+1];
  271. bzero(buf, sizeof(buf));
  272. for (int i=0; i<len; i++) {
  273. sprintf(buf+i*2, "%.2x", datap[i]);
  274. }
  275. String hexstr = buf;
  276. delete buf;
  277. return (hexstr);
  278. }
  279. String
  280. Bstream::getdecstr() const
  281. {
  282. // XXX This should be converted to
  283. // output as a decimal integer. Right
  284. // now it just outputs as a hex mp int.
  285. char *buf = new char[len*2];
  286. bzero(buf, sizeof(buf));
  287. for (int i=0; i<len; i++) {
  288. sprintf(buf+i*2, "%.2x", datap[i]);
  289. }
  290. String hexstr = buf;
  291. delete buf;
  292. return (hexstr);
  293. }
  294. Bstream
  295. File_to_Bstr(const char *filename)
  296. {
  297. Bstream retval;
  298. FILE *fc;
  299. fc = fopen(filename, "r");
  300. if (fc == NULL) {
  301. #ifdef ndef
  302. printf("unable to open %sn", filename);
  303. perror("File_to_Bstr:");
  304. #endif ndef
  305. return (retval);
  306. }
  307. fseek(fc, 0, 2);
  308. int len = (int)ftell(fc);
  309. rewind(fc);
  310. byte *buf = new byte[len];
  311. int ret = fread((char *)buf, 1, len, fc);
  312. if (ret != len) {
  313. printf("File_to_Bstr: ret != len");
  314. return (retval);
  315. }
  316. retval = Bstream(len, buf);
  317. delete buf;
  318. fclose(fc);
  319. return retval;
  320. }
  321. String
  322. File_to_String(const char *filename)
  323. {
  324. String null;
  325. FILE *fc;
  326. fc = fopen(filename, "r");
  327. if (fc == NULL) {
  328. fprintf(stderr, "unable to open %sn", filename);
  329. perror("File_to_String:");
  330. return (null);
  331. }
  332. fseek(fc, 0, 2);
  333. int len = (int)ftell(fc);
  334. rewind(fc);
  335. char *buf = new char[len];
  336. int ret = fread((char *)buf, 1, len, fc);
  337. if (ret != len) {
  338. fprintf(stderr, "File_to_String: ret != len");
  339. return (null);
  340. }
  341. String str(buf, len);
  342. delete buf;
  343. fclose(fc);
  344. return str;
  345. }