buffers.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* buffers.c */
  5. #include "or.h"
  6. extern or_options_t options; /* command-line and config-file options */
  7. /* Create a new buf of size MAX_BUF_SIZE. Write a pointer to it
  8. * into *buf, write MAX_BUF_SIZE into *buflen, and initialize
  9. * *buf_datalen to 0. Return 0 if success, or -1 if malloc fails.
  10. */
  11. int buf_new(char **buf, int *buflen, int *buf_datalen) {
  12. assert(buf && buflen && buf_datalen);
  13. *buf = (char *)malloc(MAX_BUF_SIZE);
  14. if(!*buf)
  15. return -1;
  16. // memset(*buf,0,MAX_BUF_SIZE);
  17. *buflen = MAX_BUF_SIZE;
  18. *buf_datalen = 0;
  19. return 0;
  20. }
  21. void buf_free(char *buf) {
  22. free(buf);
  23. }
  24. /* read from socket s, writing onto buf+buf_datalen. If at_most is >= 0 then
  25. * read at most 'at_most' bytes, and in any case don't read more than will fit based on buflen.
  26. * If read() returns 0, set *reached_eof to 1 and return 0. If you want to tear
  27. * down the connection return -1, else return the number of bytes read.
  28. */
  29. int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof) {
  30. int read_result;
  31. assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0));
  32. /* this is the point where you would grow the buffer, if you want to */
  33. if(at_most < 0 || *buflen - *buf_datalen < at_most)
  34. at_most = *buflen - *buf_datalen; /* take the min of the two */
  35. /* (note that this only modifies at_most inside this function) */
  36. if(at_most == 0)
  37. return 0; /* we shouldn't read anything */
  38. if(!options.LinkPadding && at_most > 10*sizeof(cell_t)) {
  39. /* if no linkpadding: do a rudimentary round-robin so one
  40. * connection can't hog a thickpipe
  41. */
  42. at_most = 10*(CELL_PAYLOAD_SIZE - TOPIC_HEADER_SIZE);
  43. /* XXX this still isn't perfect. now we read 10 data payloads per read --
  44. * but if we're reading from a connection that speaks cells, we always
  45. * read a partial cell from the network and can't process it yet. Good
  46. * enough for now though. (And maybe best, to stress our code more.)
  47. */
  48. }
  49. // log(LOG_DEBUG,"read_to_buf(): reading at most %d bytes.",at_most);
  50. read_result = read(s, *buf+*buf_datalen, at_most);
  51. if (read_result < 0) {
  52. if(errno!=EAGAIN) { /* it's a real error */
  53. return -1;
  54. }
  55. return 0;
  56. } else if (read_result == 0) {
  57. log(LOG_DEBUG,"read_to_buf(): Encountered eof");
  58. *reached_eof = 1;
  59. return 0;
  60. } else { /* we read some bytes */
  61. *buf_datalen += read_result;
  62. // log(LOG_DEBUG,"read_to_buf(): Read %d bytes. %d on inbuf.",read_result, *buf_datalen);
  63. return read_result;
  64. }
  65. }
  66. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen) {
  67. /* push from buf onto s
  68. * then memmove to front of buf
  69. * return -1 or how many bytes remain to be flushed */
  70. int write_result;
  71. assert(buf && *buf && buflen && buf_flushlen && buf_datalen && (s>=0) && (*buf_flushlen <= *buf_datalen));
  72. if(*buf_flushlen == 0) /* nothing to flush */
  73. return 0;
  74. /* this is the point where you would grow the buffer, if you want to */
  75. write_result = write(s, *buf, *buf_flushlen);
  76. if (write_result < 0) {
  77. if(errno!=EAGAIN) { /* it's a real error */
  78. return -1;
  79. }
  80. log(LOG_DEBUG,"flush_buf(): write() would block, returning.");
  81. return 0;
  82. } else {
  83. *buf_datalen -= write_result;
  84. *buf_flushlen -= write_result;
  85. memmove(*buf, *buf+write_result, *buf_datalen);
  86. // log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d ready to flush, %d remain.",
  87. // write_result,*buf_flushlen,*buf_datalen);
  88. return *buf_flushlen;
  89. }
  90. }
  91. int write_to_buf(char *string, int string_len,
  92. char **buf, int *buflen, int *buf_datalen) {
  93. /* append string to buf (growing as needed, return -1 if "too big")
  94. * return total number of bytes on the buf
  95. */
  96. assert(string && buf && *buf && buflen && buf_datalen);
  97. /* this is the point where you would grow the buffer, if you want to */
  98. if (string_len + *buf_datalen > *buflen) { /* we're out of luck */
  99. log(LOG_DEBUG, "write_to_buf(): buflen too small. Time to implement growing dynamic bufs.");
  100. return -1;
  101. }
  102. memcpy(*buf+*buf_datalen, string, string_len);
  103. *buf_datalen += string_len;
  104. // log(LOG_DEBUG,"write_to_buf(): added %d bytes to buf (now %d total).",string_len, *buf_datalen);
  105. return *buf_datalen;
  106. }
  107. z_stream *zstream_new(int compression)
  108. {
  109. z_stream* stream;
  110. stream = malloc(sizeof(z_stream));
  111. if (!stream)
  112. return NULL;
  113. memset(stream, 0, sizeof(z_stream));
  114. if (compression) {
  115. if (deflateInit(stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
  116. log(LOG_ERR, "Error initializing zlib: %s", stream->msg);
  117. free(stream);
  118. return NULL;
  119. }
  120. } else {
  121. if (inflateInit(stream) != Z_OK) {
  122. log(LOG_ERR, "Error initializing zlib: %s", stream->msg);
  123. free(stream);
  124. return NULL;
  125. }
  126. }
  127. return stream;
  128. }
  129. z_compression *compression_new()
  130. {
  131. return (z_compression *) zstream_new(1);
  132. }
  133. z_decompression *decompression_new()
  134. {
  135. return (z_compression *) zstream_new(0);
  136. }
  137. void compression_free(z_stream *stream)
  138. {
  139. int r;
  140. r = deflateEnd(stream);
  141. if (r != Z_OK)
  142. log(LOG_ERR, "while closing compression: %d (%s)", r, stream->msg);
  143. free(stream);
  144. }
  145. void decompression_free(z_stream *stream)
  146. {
  147. int r;
  148. r = inflateEnd(stream);
  149. if (r != Z_OK)
  150. log(LOG_ERR, "while closing decompression: %d (%s)", r, stream->msg);
  151. free(stream);
  152. }
  153. int compress_from_buf(char *string, int string_len,
  154. char **buf_in, int *buflen_in, int *buf_datalen_in,
  155. z_stream *zstream, int flush) {
  156. int err;
  157. if (!*buf_datalen_in)
  158. return 0;
  159. zstream->next_in = *buf_in;
  160. zstream->avail_in = *buf_datalen_in;
  161. zstream->next_out = string;
  162. zstream->avail_out = string_len;
  163. err = deflate(zstream, flush);
  164. switch (err)
  165. {
  166. case Z_OK:
  167. case Z_STREAM_END:
  168. log(LOG_DEBUG, "Compressed (%d/%d); filled (%d/%d).",
  169. *buf_datalen_in-zstream->avail_in, *buf_datalen_in,
  170. string_len-zstream->avail_out, string_len);
  171. memmove(*buf_in, zstream->next_in, zstream->avail_in);
  172. *buf_datalen_in = zstream->avail_in;
  173. return string_len - zstream->avail_out;
  174. case Z_STREAM_ERROR:
  175. case Z_BUF_ERROR:
  176. log(LOG_ERR, "Error processing compression: %s", zstream->msg);
  177. return -1;
  178. default:
  179. log(LOG_ERR, "Unknown return value from deflate: %d", err);
  180. return -1;
  181. }
  182. }
  183. int decompress_buf_to_buf(char **buf_in, int *buflen_in, int *buf_datalen_in,
  184. char **buf_out, int *buflen_out, int *buf_datalen_out,
  185. z_stream *zstream, int flush)
  186. {
  187. int err;
  188. zstream->next_in = *buf_in;
  189. zstream->avail_in = *buf_datalen_in;
  190. zstream->next_out = *buf_out + *buf_datalen_out;
  191. zstream->avail_out = *buflen_out - *buf_datalen_out;
  192. if (!zstream->avail_in && !zstream->avail_out)
  193. return 0;
  194. err = inflate(zstream, flush);
  195. switch (err)
  196. {
  197. case Z_OK:
  198. case Z_STREAM_END:
  199. log(LOG_DEBUG, "Uncompressed (%d/%d); filled (%d/%d)",
  200. *buf_datalen_in-zstream->avail_in, *buf_datalen_in,
  201. (*buflen_out-*buf_datalen_out)-zstream->avail_out,
  202. (*buflen_out-*buf_datalen_out) );
  203. memmove(*buf_in, zstream->next_in, zstream->avail_in);
  204. *buf_datalen_in = zstream->avail_in;
  205. *buf_datalen_out = *buflen_out - zstream->avail_out;
  206. return 1;
  207. case Z_STREAM_ERROR:
  208. case Z_BUF_ERROR:
  209. log(LOG_ERR, "Error processing compression: %s", zstream->msg);
  210. return 1;
  211. default:
  212. log(LOG_ERR, "Unknown return value from deflate: %d", err);
  213. return -1;
  214. }
  215. }
  216. int fetch_from_buf(char *string, int string_len,
  217. char **buf, int *buflen, int *buf_datalen) {
  218. /* if there are string_len bytes in buf, write them onto string,
  219. * then memmove buf back (that is, remove them from buf).
  220. *
  221. * If there are not enough bytes on the buffer to fill string, return -1.
  222. *
  223. * Return the number of bytes still on the buffer. */
  224. assert(string && buf && *buf && buflen && buf_datalen);
  225. /* this is the point where you would grow the buffer, if you want to */
  226. if(string_len > *buf_datalen) /* we want too much. sorry. */
  227. return -1;
  228. memcpy(string,*buf,string_len);
  229. *buf_datalen -= string_len;
  230. memmove(*buf, *buf+string_len, *buf_datalen);
  231. return *buf_datalen;
  232. }
  233. int find_on_inbuf(char *string, int string_len,
  234. char *buf, int buf_datalen) {
  235. /* find first instance of needle 'string' on haystack 'buf'. return how
  236. * many bytes from the beginning of buf to the end of string.
  237. * If it's not there, return -1.
  238. */
  239. char *location;
  240. char *last_possible = buf + buf_datalen - string_len;
  241. assert(string && string_len > 0 && buf);
  242. if(buf_datalen < string_len)
  243. return -1;
  244. for(location = buf; location <= last_possible; location++)
  245. if((*location == *string) && !memcmp(location+1, string+1, string_len-1))
  246. return location-buf+string_len;
  247. return -1;
  248. }
  249. /*
  250. Local Variables:
  251. mode:c
  252. indent-tabs-mode:nil
  253. c-basic-offset:2
  254. End:
  255. */