int get_packet_data(struct packet *p) { size_t length; assert(p != NULL); assert(p->ndata != NULL); assert_packet_type(p->header.type); assert_valid_pdata(p); length = get_packet_data_length(p->header.type); if (!p->pdata || (p->pdata_len > 0 && p->pdata_len < length)) { if (!grow_buffer(&p->pdata, length)) return 0; p->pdata_len = length; } #define PACKET_STRUCT(name, id, s) \ if (p->header.type == id) \ return sio_frombuf_##s(p->pdata, p->ndata); #include "packet.def.h" #undef PACKET_STRUCT /* Should never be reached */ return 0; }
void TMAVectorBase::pack(void) { il_vsize = (il_count / ii_ngrow); if ((il_count % ii_ngrow)>0) il_vsize++; il_vsize*=ii_ngrow; if (il_vsize<il_initsize) il_vsize=il_initsize; grow_buffer(false); }
static void wbxml_start_element(void *ctx, WBXMLTag *element, WBXMLAttribute **atts, WB_BOOL empty) { NWbxmlDecoder* t = (NWbxmlDecoder*)ctx; char* tag = (char*)wbxml_tag_get_xml_name(element); int s, s2; s = nbk_strlen(tag); if (t->dstPos + s + 4 > t->dstMax) grow_buffer((uint8**)&t->dst, &t->dstMax, s, t->page); t->dst[t->dstPos++] = '<'; nbk_strcpy(&t->dst[t->dstPos], tag); t->dstPos += s; if (atts) { int i = 0; char *name, *value; while (atts[i]) { name = (char*)wbxml_attribute_get_xml_name(atts[i]); value = (char*)wbxml_attribute_get_xml_value(atts[i]); i++; s = nbk_strlen(name); s2 = nbk_strlen(value); if (t->dstPos + s + s2 + 4 > t->dstMax) grow_buffer((uint8**)&t->dst, &t->dstMax, s + s2, t->page); t->dst[t->dstPos++] = ' '; nbk_strcpy(&t->dst[t->dstPos], name); t->dstPos += s; t->dst[t->dstPos++] = '='; t->dst[t->dstPos++] = '"'; nbk_strcpy(&t->dst[t->dstPos], value); t->dstPos += s2; t->dst[t->dstPos++] = '"'; } } if (empty) t->dst[t->dstPos++] = '/'; t->dst[t->dstPos++] = '>'; }
static bool append_number(char **buffer, int *len, int *size, int number) { if (! try_append_number(*buffer, len, *size, number)) { if (! grow_buffer(buffer, size)) return false; return try_append_number(*buffer, len, *size, number); } else { return true; } }
static void wbxml_characters(void *ctx, WB_UTINY *ch, WB_ULONG start, WB_ULONG length) { NWbxmlDecoder* t = (NWbxmlDecoder*)ctx; int i; if (t->dstPos + (length - start) > t->dstMax) grow_buffer((uint8**)&t->dst, &t->dstMax, length - start, t->page); for (i=start; i < length; i++) t->dst[t->dstPos++] = ch[i]; }
static char *prepare_cmd(lua_State *L, buffer *b, enum cmd c, int pos, int len) { size_t oldlen = b->len; char *newstr = NULL; if (c == cmd_assign) return lb_realloc(L, b, pos + len); else if (c != cmd_insert) return grow_buffer(L, b, pos + len); else if ((newstr = lb_realloc(L, b, b->len+len)) != NULL) memmove(&b->str[pos+len], &b->str[pos], oldlen-pos); return newstr; }
long TMAVectorBase::add_element(PMAObject item,long index) { if ( index < 0 ) return -1; // Error. if ( il_count == il_vsize ) grow_buffer(); memmove( &CItems[index+1], &CItems[index], (il_count-index)*sizeof(PMAObject) ); il_count++; CItems[index] = item; return index; }
static PyObject * decompress(Decompressor *d, uint8_t *data, size_t len) { size_t data_size = 0; PyObject *result; result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); if (result == NULL) return NULL; d->lzs.next_in = data; d->lzs.avail_in = len; d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); d->lzs.avail_out = PyBytes_GET_SIZE(result); for (;;) { lzma_ret lzret; Py_BEGIN_ALLOW_THREADS lzret = lzma_code(&d->lzs, LZMA_RUN); data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result); Py_END_ALLOW_THREADS if (catch_lzma_error(lzret)) goto error; if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK) d->check = lzma_get_check(&d->lzs); if (lzret == LZMA_STREAM_END) { d->eof = 1; if (d->lzs.avail_in > 0) { Py_CLEAR(d->unused_data); d->unused_data = PyBytes_FromStringAndSize( (char *)d->lzs.next_in, d->lzs.avail_in); if (d->unused_data == NULL) goto error; } break; } else if (d->lzs.avail_in == 0) { break; } else if (d->lzs.avail_out == 0) { if (grow_buffer(&result) == -1) goto error; d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; } } if (data_size != (size_t)PyBytes_GET_SIZE(result)) if (_PyBytes_Resize(&result, data_size) == -1) goto error; return result; error: Py_XDECREF(result); return NULL; }
static char* read_entire_file( FILE* fp, size_t* out_size ) { size_t size = 0; size_t max = 1024; char* buffer = calloc( 1, max ); *out_size = 0; while ( fread( &buffer[size], 1, 1, fp ) ) { ++size; if ( size >= max ) { buffer = grow_buffer( buffer, max, 2*max ); max *= 2; } } *out_size = size; return grow_buffer( buffer, max, size+1 ); }
void wbxmlDecoder_addSourceData(NWbxmlDecoder* decoder, uint8* data, int length) { #if DEBUG_DECODER dump_char(decoder->page, "add data", -1); dump_int(decoder->page, length); dump_return(decoder->page); #endif if (decoder->srcMax - decoder->srcPos < length) grow_buffer(&decoder->src, &decoder->srcMax, length, decoder->page); NBK_memcpy(&decoder->src[decoder->srcPos], data, length); decoder->srcPos += length; }
static HRESULT write_output_buffer_mode(output_buffer *buffer, output_mode mode, const WCHAR *data, int len) { int length; char *ptr; if (mode & (OutputBuffer_Encoded | OutputBuffer_Both)) { if (buffer->code_page == CP_UTF8) { length = WideCharToMultiByte(buffer->code_page, 0, data, len, NULL, 0, NULL, NULL); grow_buffer(&buffer->encoded, length); ptr = buffer->encoded.data + buffer->encoded.written; length = WideCharToMultiByte(buffer->code_page, 0, data, len, ptr, length, NULL, NULL); buffer->encoded.written += len == -1 ? length-1 : length; } } if (mode & (OutputBuffer_Native | OutputBuffer_Both)) { /* WCHAR data just copied */ length = len == -1 ? strlenW(data) : len; if (length) { length *= sizeof(WCHAR); grow_buffer(&buffer->utf16, length); ptr = buffer->utf16.data + buffer->utf16.written; memcpy(ptr, data, length); buffer->utf16.written += length; ptr += length; /* null termination */ memset(ptr, 0, sizeof(WCHAR)); } } return S_OK; }
static void wbxml_end_element(void *ctx, WBXMLTag *element, WB_BOOL empty) { if (!empty) { NWbxmlDecoder* t = (NWbxmlDecoder*)ctx; char* tag = (char*)wbxml_tag_get_xml_name(element); int s = nbk_strlen(tag); if (t->dstPos + s + 4 > t->dstMax) grow_buffer((uint8**)&t->dst, &t->dstMax, s, t->page); t->dst[t->dstPos++] = '<'; t->dst[t->dstPos++] = '/'; nbk_strcpy(&t->dst[t->dstPos], tag); t->dstPos += s; t->dst[t->dstPos++] = '>'; } }
static int lb_move(lua_State *L) { buffer *b = lb_checkbuffer(L, 1); int dst = luaL_checkint(L, 2); size_t len = b->len, pos = real_range(L, 3, &len); size_t oldlen = b->len; if (dst > 0) dst -= 1; if (dst < 0) dst += b->len; if (dst < 0) dst = 0; if (grow_buffer(L, b, dst + len)) memmove(&b->str[dst], &b->str[pos], len); if (dst > oldlen) memset(&b->str[oldlen], 0, dst - oldlen); lua_settop(L, 1); return 1; }
static PyObject * compress(Compressor *c, uint8_t *data, size_t len, lzma_action action) { size_t data_size = 0; PyObject *result; result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); if (result == NULL) return NULL; c->lzs.next_in = data; c->lzs.avail_in = len; c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); c->lzs.avail_out = PyBytes_GET_SIZE(result); for (;;) { lzma_ret lzret; Py_BEGIN_ALLOW_THREADS lzret = lzma_code(&c->lzs, action); data_size = (char *)c->lzs.next_out - PyBytes_AS_STRING(result); Py_END_ALLOW_THREADS if (catch_lzma_error(lzret)) goto error; if ((action == LZMA_RUN && c->lzs.avail_in == 0) || (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) { break; } else if (c->lzs.avail_out == 0) { if (grow_buffer(&result) == -1) goto error; c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; } } if (data_size != (size_t)PyBytes_GET_SIZE(result)) if (_PyBytes_Resize(&result, data_size) == -1) goto error; return result; error: Py_XDECREF(result); return NULL; }
int MiniConsole::grow(int glines, int gbuffer) { /* * grow of a specified amount of lines or bytes the * current line and text buffers; i.e.: make room * to support glines more lines and gbuffer more characters. * * grow values can be negative. in this case, the current * buffers will be shrunk of the specified amounts. * * consistency will be preserved by deleting a certain amount * of lines: the older ones. * * a zero amount for a value imply the line or buffer arrays * to remain untouched. * */ /* FINISH ME AND TEST ME */ int gb,gl; gb=grow_buffer(gbuffer); gl=grow_lines (glines); return !( gb==0 && 0==gl );// 0 if both 0 }
static int make_stream_space(parser_t *self, size_t nbytes) { int i, status, cap; void *orig_ptr; // Can we fit potentially nbytes tokens (+ null terminators) in the stream? /* TRACE(("maybe growing buffers\n")); */ /* TOKEN STREAM */ orig_ptr = (void *) self->stream; self->stream = (char*) grow_buffer((void *) self->stream, self->stream_len, &self->stream_cap, nbytes * 2, sizeof(char), &status); if (status != 0) { return PARSER_OUT_OF_MEMORY; } // realloc sets errno when moving buffer? if (self->stream != orig_ptr) { // uff /* TRACE(("Moving word pointers\n")) */ self->pword_start = self->stream + self->word_start; for (i = 0; i < self->words_len; ++i) { self->words[i] = self->stream + self->word_starts[i]; } } /* WORD VECTORS */ cap = self->words_cap; self->words = (char**) grow_buffer((void *) self->words, self->words_len, &self->words_cap, nbytes, sizeof(char*), &status); if (status != 0) { return PARSER_OUT_OF_MEMORY; } // realloc took place if (cap != self->words_cap) { self->word_starts = (int*) safe_realloc((void *) self->word_starts, sizeof(int) * self->words_cap); if (self->word_starts == NULL) { return PARSER_OUT_OF_MEMORY; } } /* LINE VECTORS */ /* printf("Line_start: "); for (j = 0; j < self->lines + 1; ++j) { printf("%d ", self->line_fields[j]); } printf("\n"); printf("lines_cap: %d\n", self->lines_cap); */ cap = self->lines_cap; self->line_start = (int*) grow_buffer((void *) self->line_start, self->lines + 1, &self->lines_cap, nbytes, sizeof(int), &status); if (status != 0) { return PARSER_OUT_OF_MEMORY; } // realloc took place if (cap != self->lines_cap) { self->line_fields = (int*) safe_realloc((void *) self->line_fields, sizeof(int) * self->lines_cap); if (self->line_fields == NULL) { return PARSER_OUT_OF_MEMORY; } } /* TRACE(("finished growing buffers\n")); */ return 0; }
/* * Read and return the next packet from the savefile. Return the header * in hdr and a pointer to the contents in data. Return 0 on success, 1 * if there were no more packets, and -1 on an error. */ static int pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) { struct pcap_sf *ps = p->priv; struct pcap_sf_patched_pkthdr sf_hdr; FILE *fp = p->rfile; size_t amt_read; bpf_u_int32 t; /* * Read the packet header; the structure we use as a buffer * is the longer structure for files generated by the patched * libpcap, but if the file has the magic number for an * unpatched libpcap we only read as many bytes as the regular * header has. */ amt_read = fread(&sf_hdr, 1, ps->hdrsize, fp); if (amt_read != ps->hdrsize) { if (ferror(fp)) { pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, errno, "error reading dump file"); return (-1); } else { if (amt_read != 0) { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %lu header bytes, only got %lu", (unsigned long)ps->hdrsize, (unsigned long)amt_read); return (-1); } /* EOF */ return (1); } } if (p->swapped) { /* these were written in opposite byte order */ hdr->caplen = SWAPLONG(sf_hdr.caplen); hdr->len = SWAPLONG(sf_hdr.len); hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec); hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec); } else { hdr->caplen = sf_hdr.caplen; hdr->len = sf_hdr.len; hdr->ts.tv_sec = sf_hdr.ts.tv_sec; hdr->ts.tv_usec = sf_hdr.ts.tv_usec; } switch (ps->scale_type) { case PASS_THROUGH: /* * Just pass the time stamp through. */ break; case SCALE_UP: /* * File has microseconds, user wants nanoseconds; convert * it. */ hdr->ts.tv_usec = hdr->ts.tv_usec * 1000; break; case SCALE_DOWN: /* * File has nanoseconds, user wants microseconds; convert * it. */ hdr->ts.tv_usec = hdr->ts.tv_usec / 1000; break; } /* Swap the caplen and len fields, if necessary. */ switch (ps->lengths_swapped) { case NOT_SWAPPED: break; case MAYBE_SWAPPED: if (hdr->caplen <= hdr->len) { /* * The captured length is <= the actual length, * so presumably they weren't swapped. */ break; } /* FALLTHROUGH */ case SWAPPED: t = hdr->caplen; hdr->caplen = hdr->len; hdr->len = t; break; } /* * Is the packet bigger than we consider sane? */ if (hdr->caplen > max_snaplen_for_dlt(p->linktype)) { /* * Yes. This may be a damaged or fuzzed file. * * Is it bigger than the snapshot length? * (We don't treat that as an error if it's not * bigger than the maximum we consider sane; see * below.) */ if (hdr->caplen > (bpf_u_int32)p->snapshot) { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "invalid packet capture length %u, bigger than " "snaplen of %d", hdr->caplen, p->snapshot); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "invalid packet capture length %u, bigger than " "maximum of %u", hdr->caplen, max_snaplen_for_dlt(p->linktype)); } return (-1); } if (hdr->caplen > (bpf_u_int32)p->snapshot) { /* * The packet is bigger than the snapshot length * for this file. * * This can happen due to Solaris 2.3 systems tripping * over the BUFMOD problem and not setting the snapshot * length correctly in the savefile header. * * libpcap 0.4 and later on Solaris 2.3 should set the * snapshot length correctly in the pcap file header, * even though they don't set a snapshot length in bufmod * (the buggy bufmod chops off the *beginning* of the * packet if a snapshot length is specified); they should * also reduce the captured length, as supplied to the * per-packet callback, to the snapshot length if it's * greater than the snapshot length, so the code using * libpcap should see the packet cut off at the snapshot * length, even though the full packet is copied up to * userland. * * However, perhaps some versions of libpcap failed to * set the snapshot length currectly in the file header * or the per-packet header, or perhaps this is a * corrupted safefile or a savefile built/modified by a * fuzz tester, so we check anyway. */ size_t bytes_to_discard; size_t bytes_to_read, bytes_read; char discard_buf[4096]; if (hdr->caplen > p->bufsize) { /* * Grow the buffer to the snapshot length. */ if (!grow_buffer(p, p->snapshot)) return (-1); } /* * Read the first p->bufsize bytes into the buffer. */ amt_read = fread(p->buffer, 1, p->bufsize, fp); if (amt_read != p->bufsize) { if (ferror(fp)) { pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, errno, "error reading dump file"); } else { /* * Yes, this uses hdr->caplen; technically, * it's true, because we would try to read * and discard the rest of those bytes, and * that would fail because we got EOF before * the read finished. */ pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %u captured bytes, only got %lu", hdr->caplen, (unsigned long)amt_read); } return (-1); } /* * Now read and discard what's left. */ bytes_to_discard = hdr->caplen - p->bufsize; bytes_read = amt_read; while (bytes_to_discard != 0) { bytes_to_read = bytes_to_discard; if (bytes_to_read > sizeof (discard_buf)) bytes_to_read = sizeof (discard_buf); amt_read = fread(discard_buf, 1, bytes_to_read, fp); bytes_read += amt_read; if (amt_read != bytes_to_read) { if (ferror(fp)) { pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, errno, "error reading dump file"); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %u captured bytes, only got %lu", hdr->caplen, (unsigned long)bytes_read); } return (-1); } bytes_to_discard -= amt_read; } /* * Adjust caplen accordingly, so we don't get confused later * as to how many bytes we have to play with. */ hdr->caplen = p->bufsize; } else { if (hdr->caplen > p->bufsize) { /* * Grow the buffer to the next power of 2, or * the snaplen, whichever is lower. */ u_int new_bufsize; new_bufsize = hdr->caplen; /* * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */ new_bufsize--; new_bufsize |= new_bufsize >> 1; new_bufsize |= new_bufsize >> 2; new_bufsize |= new_bufsize >> 4; new_bufsize |= new_bufsize >> 8; new_bufsize |= new_bufsize >> 16; new_bufsize++; if (new_bufsize > (u_int)p->snapshot) new_bufsize = p->snapshot; if (!grow_buffer(p, new_bufsize)) return (-1); } /* read the packet itself */ amt_read = fread(p->buffer, 1, hdr->caplen, fp); if (amt_read != hdr->caplen) { if (ferror(fp)) { pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, errno, "error reading dump file"); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %u captured bytes, only got %lu", hdr->caplen, (unsigned long)amt_read); } return (-1); } }