| 88 | } |
| 89 | |
| 90 | void xs_inflate_push(xsMachine *the) |
| 91 | { |
| 92 | uint8_t *internalOutput = xsmcGetHostDataValidate(xsThis, xs_inflate_destructor); |
| 93 | z_stream *zlib = (z_stream *)(kOutputBufferSize + (char *)internalOutput); |
| 94 | int inputOffset = 0; |
| 95 | uint8_t *input, *output; |
| 96 | xsUnsignedValue inputRemaining, outputSize, ignore; |
| 97 | //@@int inputEnd = xsmcTest(xsArg(1)); |
| 98 | int inputEnd = 0; |
| 99 | int status = Z_OK; |
| 100 | int outputProvided = (xsmcArgc > 2) && xsmcTest(xsArg(2)); |
| 101 | |
| 102 | if (outputProvided) |
| 103 | xsmcGetBufferWritable(xsArg(2), (void **)&output, &outputSize); |
| 104 | else { |
| 105 | output = internalOutput; |
| 106 | outputSize = kOutputBufferSize; |
| 107 | } |
| 108 | |
| 109 | xsmcGetBufferReadable(xsArg(0), (void **)&input, &inputRemaining); |
| 110 | while (Z_OK == status) { |
| 111 | zlib->next_out = output; |
| 112 | zlib->avail_out = outputSize; |
| 113 | zlib->total_out = 0; |
| 114 | |
| 115 | xsmcGetBufferReadable(xsArg(0), (void **)&input, &ignore); |
| 116 | zlib->next_in = inputOffset + input; |
| 117 | zlib->avail_in = inputRemaining; |
| 118 | zlib->total_in = 0; |
| 119 | |
| 120 | status = inflate(zlib, (inputEnd ? Z_FINISH : MZ_NO_FLUSH)); |
| 121 | if ((Z_OK != status) && (Z_STREAM_END != status)) { |
| 122 | if (Z_DATA_ERROR == status) { |
| 123 | xs_inflate_close(the); |
| 124 | xsUnknownError("bad zlib data"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (zlib->total_out) { |
| 129 | if (outputProvided) { |
| 130 | xsResult = xsArg(2); |
| 131 | if (zlib->total_out != outputSize) |
| 132 | xsResult = xsCall2(xsResult, xsID_subarray, xsInteger(0), xsInteger(zlib->total_out)); |
| 133 | } |
| 134 | else |
| 135 | xsmcSetArrayBuffer(xsResult, output, zlib->total_out); |
| 136 | xsCall1(xsThis, xsID_onData, xsResult); |
| 137 | } |
| 138 | |
| 139 | inputOffset += zlib->total_in; |
| 140 | inputRemaining -= zlib->total_in; |
| 141 | |
| 142 | if ((Z_STREAM_END == status) || outputProvided) |
| 143 | break; |
| 144 | |
| 145 | if (Z_BUF_ERROR == status) { |
| 146 | if (!inputRemaining && !zlib->total_out) |
| 147 | break; |
nothing calls this directly
no test coverage detected