/*********************************************************************************************************************************** Compress data ***********************************************************************************************************************************/ static Buffer * testCompress(IoFilter *compress, Buffer *decompressed, size_t inputSize, size_t outputSize) { Buffer *compressed = bufNew(1024 * 1024); size_t inputTotal = 0; ioBufferSizeSet(outputSize); IoFilterGroup *filterGroup = ioFilterGroupNew(); ioFilterGroupAdd(filterGroup, compress); IoWrite *write = ioBufferWriteNew(compressed); ioWriteFilterGroupSet(write, filterGroup); ioWriteOpen(write); // Compress input data while (inputTotal < bufSize(decompressed)) { // Generate the input buffer based on input size. This breaks the data up into chunks as it would be in a real scenario. Buffer *input = bufNewC( bufPtr(decompressed) + inputTotal, inputSize > bufSize(decompressed) - inputTotal ? bufSize(decompressed) - inputTotal : inputSize); ioWrite(write, input); inputTotal += bufUsed(input); bufFree(input); } ioWriteClose(write); memContextFree(((GzipCompress *)ioFilterDriver(compress))->memContext); return compressed; }
/*********************************************************************************************************************************** Read all IO into a buffer ***********************************************************************************************************************************/ Buffer * ioReadBuf(IoRead *read) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(IO_READ, read); FUNCTION_TEST_END(); ASSERT(read != NULL); Buffer *result = NULL; MEM_CONTEXT_TEMP_BEGIN() { // Read IO into the buffer result = bufNew(0); do { bufResize(result, bufSize(result) + ioBufferSize()); ioRead(read, result); } while (!ioReadEof(read)); // Resize the buffer and move to calling context bufResize(result, bufUsed(result)); bufMove(result, MEM_CONTEXT_OLD()); } MEM_CONTEXT_TEMP_END(); FUNCTION_TEST_RETURN(result); }