- Timestamp:
- 05/17/05 18:04:37 (17 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- a3dc71c
- Parents:
- dca0810
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/trace.c
rd2d527f r356fa40 168 168 union { 169 169 int fd; 170 #if HAVE_ZLIB 171 gzFile *file; 172 #else 170 173 FILE *file; 174 #endif 171 175 #if HAVE_PCAP 172 176 pcap_t *pcap; … … 193 197 double last_ts; 194 198 double start_ts; 195 struct {196 #define COMP_IBUF_SIZE 65536197 void *ibuf;198 void *cbuf;199 200 uint64_t ibuf_pos;201 uint64_t cbuf_max;202 uint64_t cbuf_pos;203 struct fifo_t *cfifo;204 } comp;205 199 }; 206 200 … … 233 227 uint8_t pad[3]; 234 228 }; 235 236 237 /* forward declarations for local functions */238 239 int trace_uncompress(void * dest, size_t destLen, const void *source, size_t sourceLen);240 int trace_comp_read(struct libtrace_t *trace, void *buffer, size_t len);241 242 229 243 230 #define RP_BUFSIZE 65536 … … 402 389 //(*libtrace)->packet.buffer = 0; 403 390 //(*libtrace)->packet.size = 0; 404 405 (*libtrace)->comp.ibuf = 0;406 (*libtrace)->comp.cbuf = 0;407 (*libtrace)->comp.cfifo = create_fifo(1048576);408 391 409 392 return 1; … … 483 466 { 484 467 #endif 485 // We just open files now, because we 486 // do the decompression internally 468 #if HAVE_ZLIB 469 libtrace->input.file = gzopen(libtrace->conn_info.path, "r"); 470 #else 487 471 libtrace->input.file = fopen(libtrace->conn_info.path, "r"); 472 #endif 488 473 } 489 474 break; … … 589 574 #endif 590 575 } else { 576 #if HAVE_ZLIB 577 gzclose(libtrace->input.file); 578 #else 591 579 fclose(libtrace->input.file); 580 #endif 592 581 } 593 582 // need to free things! 594 583 destroy_fifo(libtrace->fifo); 595 destroy_fifo(libtrace->comp.cfifo);596 597 if (libtrace->comp.ibuf) {598 free(libtrace->comp.ibuf);599 free(libtrace->comp.cbuf);600 }601 602 584 free(libtrace); 603 585 } … … 665 647 default: 666 648 #if HAVE_ZLIB 667 if ((numbytes=trace_comp_read(libtrace, 668 //if ((numbytes=gzread(libtrace->input.file, 649 if ((numbytes=gzread(libtrace->input.file, 669 650 buffer, 670 651 len)) == -1) { 671 //perror("gzread");652 perror("gzread"); 672 653 return -1; 673 654 } … … 777 758 int rlen; 778 759 // read in the trace header 779 if ((numbytes=trace_comp_read(libtrace, 780 //if ((numbytes=gzread(libtrace->input.file, 760 if ((numbytes=gzread(libtrace->input.file, 781 761 buffer, 782 762 dag_record_size)) == -1) { 783 //perror("gzread");763 perror("gzread"); 784 764 return -1; 785 765 } … … 793 773 794 774 // read in the rest of the packet 795 if ((numbytes=trace_comp_read(libtrace, 796 //if ((numbytes=gzread(libtrace->input.file, 775 if ((numbytes=gzread(libtrace->input.file, 797 776 buffer2, 798 777 size)) == -1) { 799 //perror("gzread");778 perror("gzread"); 800 779 return -1; 801 780 } … … 2055 2034 } 2056 2035 2057 //#ifdef HAVE_ZLIB2058 /* uncompr.c -- decompress a memory buffer2059 * Copyright (C) 1995-2003 Jean-loup Gailly.2060 * For conditions of distribution and use, see copyright notice in zlib.h2061 */2062 2063 2064 /* ===========================================================================2065 Decompresses the source buffer into the destination buffer. sourceLen is2066 the byte length of the source buffer. Upon entry, destLen is the total2067 size of the destination buffer, which must be large enough to hold the2068 entire uncompressed data. (The size of the uncompressed data must have2069 been saved previously by the compressor and transmitted to the decompressor2070 by some mechanism outside the scope of this compression library.)2071 Upon exit, destLen is the actual size of the compressed buffer.2072 This function can be used to decompress a whole file at once if the2073 input file is mmap'ed.2074 2075 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not2076 enough memory, Z_BUF_ERROR if there was not enough room in the output2077 buffer, or Z_DATA_ERROR if the input data was corrupted.2078 */2079 int trace_uncompress (void *dest, size_t destLen, const void *source, size_t sourceLen)2080 {2081 z_stream stream;2082 int err;2083 2084 stream.next_in = (Bytef*)source;2085 stream.avail_in = (uInt)sourceLen;2086 /* Check for source > 64K on 16-bit machine: */2087 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;2088 2089 stream.next_out = dest;2090 stream.avail_out = destLen;2091 if ((uLong)stream.avail_out != destLen) return Z_BUF_ERROR;2092 2093 stream.zalloc = (alloc_func)0;2094 stream.zfree = (free_func)0;2095 2096 err = inflateInit(&stream);2097 if (err != Z_OK) return err;2098 2099 err = inflate(&stream, Z_FINISH);2100 if (err != Z_STREAM_END) {2101 inflateEnd(&stream);2102 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))2103 return Z_DATA_ERROR;2104 return err;2105 }2106 destLen = stream.total_out;2107 2108 err = inflateEnd(&stream);2109 return err;2110 }2111 2112 2113 int trace_comp_read(struct libtrace_t *trace, void *buffer, size_t len) {2114 //we want to read len bytes out of the compressed file stored in *fh.2115 //store this in buffer, and return the number of bytes read.2116 2117 size_t numbytes = 0;2118 size_t ret = 0;2119 2120 if (trace->comp.ibuf == 0) {2121 trace->comp.ibuf = malloc(COMP_IBUF_SIZE);2122 trace->comp.cbuf_max = COMP_IBUF_SIZE * 3;2123 trace->comp.cbuf = malloc(trace->comp.cbuf_max);2124 }2125 2126 if (fifo_out_available(trace->comp.cfifo) < len) {2127 2128 if ((numbytes = fread(trace->comp.ibuf,2129 COMP_IBUF_SIZE,2130 1,2131 trace->input.file)) == 0 ) {2132 if (feof(trace->input.file)) {2133 return 0;2134 }2135 if (ferror(trace->input.file)) {2136 perror("fread");2137 return -1;2138 }2139 }2140 2141 // attempt to uncompress. This is wrapped up so2142 // that if the buffer is too small, it resizes2143 // and loops until it isn't too small2144 do {2145 trace->comp.cbuf_pos = trace->comp.cbuf_max;2146 // trace->comp.cbuf_pos is the size of the compression buffer2147 ret = trace_incompress(trace->comp.cbuf,2148 trace->comp.cbuf_pos,2149 trace->comp.ibuf,2150 COMP_IBUF_SIZE);2151 // trace->comp.cbuf_pos is NOW how much of the compression buffer is used;2152 if (ret == Z_BUF_ERROR) {2153 trace->comp.cbuf_max *= 2;2154 trace->comp.cbuf = realloc(trace->comp.cbuf,trace->comp.cbuf_max);2155 }2156 } while(ret == Z_BUF_ERROR);2157 2158 // bad. Exit.2159 if (ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {2160 return -1;2161 }2162 2163 fifo_write(trace->comp.cfifo,trace->comp.cbuf,trace->comp.cbuf_pos);2164 }2165 2166 if ((numbytes = fifo_out_read(trace->comp.cfifo, buffer, len)) == 0 ) {2167 fprintf(stderr,"Couldn't read from compressed file, bailing\n");2168 return -1;2169 }2170 2171 return len;2172 }2173 //#endif
Note: See TracChangeset
for help on using the changeset viewer.