42 |
|
|
43 |
void Stream_write(Stream * stream, const void * p, size_t size) { |
void Stream_write(Stream * stream, const void * p, size_t size) { |
44 |
size_t stream_length = stream->length; |
size_t stream_length = stream->length; |
45 |
|
size_t stream_capacity = stream->capacity; |
46 |
|
if (stream_capacity - stream_length < size) { |
47 |
|
if (SIZE_MAX - size < stream_length) { |
48 |
|
fatal("out of memory"); |
49 |
|
} |
50 |
|
|
51 |
if (SIZE_MAX - size < stream_length) { |
if (SIZE_MAX / 2 < stream_capacity) { |
52 |
fatal("out of memory"); |
stream_capacity = SIZE_MAX; |
|
} |
|
|
|
|
|
size_t new_length = stream_length + size; |
|
|
size_t new_capacity = stream->capacity; |
|
|
if (new_capacity < new_length) { |
|
|
if (SIZE_MAX / 2 < new_capacity) { |
|
|
new_capacity = SIZE_MAX; |
|
53 |
} |
} |
54 |
else { |
else { |
55 |
new_capacity *= 2; |
stream_capacity *= 2; |
56 |
} |
} |
57 |
|
|
58 |
if (new_capacity < new_length) { |
size_t new_length = stream_length + size; |
59 |
new_capacity = new_length; |
if (stream_capacity < new_length) { |
60 |
|
stream_capacity = new_length; |
61 |
} |
} |
62 |
|
|
63 |
stream->data = xrealloc(stream->data, new_capacity); |
stream->data = xrealloc(stream->data, stream_capacity); |
64 |
stream->capacity = new_capacity; |
stream->capacity = stream_capacity; |
65 |
|
memcpy(stream->data + stream_length, p, size); |
66 |
|
stream->length = new_length; |
67 |
|
} |
68 |
|
else { |
69 |
|
memcpy(stream->data + stream_length, p, size); |
70 |
|
stream->length = stream_length + size; |
71 |
} |
} |
|
|
|
|
memcpy(stream->data + stream_length, p, size); |
|
|
stream->length = new_length; |
|
72 |
} |
} |
73 |
|
|
74 |
void Stream_write_string(Stream * stream, const char * s) { |
void Stream_write_string(Stream * stream, const char * s) { |