MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / xs_deflate_push

Function xs_deflate_push

modules/data/zlib/deflate.c:100–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

98}
99
100void xs_deflate_push(xsMachine *the)
101{
102 uint8_t *output = xsmcGetHostDataValidate(xsThis, xs_deflate_destructor);
103 z_stream *zlib = (z_stream *)(kOutputBufferSize + (char *)output);
104 int inputOffset = 0;
105 xsUnsignedValue inputRemaining;
106 void *input;
107 int inputEnd = (xsmcArgc > 1) ? xsmcTest(xsArg(1)) : 0;
108 int status = Z_OK;
109 uint8_t isString = xsStringType == xsmcTypeOf(xsArg(0));
110
111 if (isString)
112 inputRemaining = c_strlen(xsmcToString(xsArg(0)));
113 else
114 xsmcGetBufferReadable(xsArg(0), &input, &inputRemaining);
115
116 while ((0 != inputRemaining) || (Z_OK == status)) {
117 zlib->next_out = output;
118 zlib->avail_out = kOutputBufferSize;
119 zlib->total_out = 0;
120
121 if (isString)
122 zlib->next_in = inputOffset + (uint8_t *)xsmcToString(xsArg(0));
123 else {
124 xsUnsignedValue ignore;
125 xsmcGetBufferReadable(xsArg(0), &input, &ignore);
126 zlib->next_in = inputOffset + (uint8_t *)input;
127 }
128 zlib->avail_in = inputRemaining;
129 zlib->total_in = 0;
130
131 status = deflate(zlib, (inputEnd ? Z_FINISH : Z_NO_FLUSH));
132 if (Z_BUF_ERROR == status) {
133 status = Z_OK;
134 goto bail;
135 }
136 if ((Z_OK != status) && (Z_STREAM_END != status)) {
137 xs_deflate_close(the);
138
139 xsmcSetInteger(xsResult, status);
140 xsCall1(xsThis, xsID_onEnd, xsResult);
141 goto bail;
142 }
143
144 if (zlib->total_out) {
145 xsmcSetArrayBuffer(xsResult, output, zlib->total_out);
146 xsCall1(xsThis, xsID_onData, xsResult);
147 }
148
149 inputOffset += zlib->total_in;
150 inputRemaining -= zlib->total_in;
151 }
152
153 if (inputEnd || (status == Z_STREAM_END)) {
154 xs_deflate_close(the);
155
156 xsmcSetInteger(xsResult, (Z_STREAM_END == status) ? Z_OK : status);
157 xsCall1(xsThis, xsID_onEnd, xsResult);

Callers

nothing calls this directly

Calls 2

deflateFunction · 0.85
xs_deflate_closeFunction · 0.85

Tested by

no test coverage detected