size_t nextOffset;
size_t headerFooterPos;
CHUNK_STATE state;
- char* lenBuffer;
+ char lenBuffer[11];
} rdg_http_encoding_chunked_context;
typedef struct
TRANSFER_ENCODING httpTransferEncoding;
union _context
{
- rdg_http_encoding_chunked_context* chunked;
+ rdg_http_encoding_chunked_context chunked;
} context;
} rdg_http_encoding_context;
assert(encodingContext != NULL);
while (TRUE)
{
- switch (encodingContext->context.chunked->state)
+ switch (encodingContext->context.chunked.state)
{
case ChunkStateData:
{
status = BIO_read(bio, pBuffer,
- (size > encodingContext->context.chunked->nextOffset
- ? encodingContext->context.chunked->nextOffset
+ (size > encodingContext->context.chunked.nextOffset
+ ? encodingContext->context.chunked.nextOffset
: size));
if (status <= 0)
return (effectiveDataLen > 0 ? effectiveDataLen : status);
- encodingContext->context.chunked->nextOffset -= status;
- if (encodingContext->context.chunked->nextOffset == 0)
+ encodingContext->context.chunked.nextOffset -= status;
+ if (encodingContext->context.chunked.nextOffset == 0)
{
- encodingContext->context.chunked->state = ChunkStateFooter;
- encodingContext->context.chunked->headerFooterPos = 0;
+ encodingContext->context.chunked.state = ChunkStateFooter;
+ encodingContext->context.chunked.headerFooterPos = 0;
}
effectiveDataLen += status;
- if (status == size)
+ if ((size_t)status == size)
return effectiveDataLen;
pBuffer += status;
case ChunkStateFooter:
{
char _dummy[2];
- assert(encodingContext->context.chunked->nextOffset == 0);
- assert(encodingContext->context.chunked->headerFooterPos < 2);
+ assert(encodingContext->context.chunked.nextOffset == 0);
+ assert(encodingContext->context.chunked.headerFooterPos < 2);
status =
- BIO_read(bio, _dummy, 2 - encodingContext->context.chunked->headerFooterPos);
+ BIO_read(bio, _dummy, 2 - encodingContext->context.chunked.headerFooterPos);
if (status >= 0)
{
- encodingContext->context.chunked->headerFooterPos += status;
- if (encodingContext->context.chunked->headerFooterPos == 2)
+ encodingContext->context.chunked.headerFooterPos += status;
+ if (encodingContext->context.chunked.headerFooterPos == 2)
{
- encodingContext->context.chunked->state = ChunkStateLenghHeader;
- encodingContext->context.chunked->headerFooterPos = 0;
+ encodingContext->context.chunked.state = ChunkStateLenghHeader;
+ encodingContext->context.chunked.headerFooterPos = 0;
}
}
else
{
BOOL _haveNewLine = FALSE;
size_t tmp;
- assert(encodingContext->context.chunked->nextOffset == 0);
- while (encodingContext->context.chunked->headerFooterPos < 10 && !_haveNewLine)
+ char* dst = &encodingContext->context.chunked
+ .lenBuffer[encodingContext->context.chunked.headerFooterPos];
+ assert(encodingContext->context.chunked.nextOffset == 0);
+ while (encodingContext->context.chunked.headerFooterPos < 10 && !_haveNewLine)
{
- status = BIO_read(bio,
- encodingContext->context.chunked->lenBuffer +
- encodingContext->context.chunked->headerFooterPos,
- 1);
+ status = BIO_read(bio, dst, 1);
if (status >= 0)
{
- if (encodingContext->context.chunked
- ->lenBuffer[encodingContext->context.chunked->headerFooterPos] ==
- '\n')
+ if (*dst == '\n')
_haveNewLine = TRUE;
- encodingContext->context.chunked->headerFooterPos += status;
+ encodingContext->context.chunked.headerFooterPos += status;
+ dst += status;
}
else
return (effectiveDataLen > 0 ? effectiveDataLen : status);
}
- encodingContext->context.chunked
- ->lenBuffer[encodingContext->context.chunked->headerFooterPos] = '\0';
+
+ *dst = '\0';
+
/* strtoul is tricky, error are reported via errno, we also need
* to ensure the result does not overflow */
errno = 0;
- tmp = strtoul(encodingContext->context.chunked->lenBuffer, NULL, 16);
+ tmp = strtoul(encodingContext->context.chunked.lenBuffer, NULL, 16);
if ((errno != 0) || (tmp > SIZE_MAX))
return -1;
- encodingContext->context.chunked->nextOffset = tmp;
- encodingContext->context.chunked->state = ChunkStateData;
+ encodingContext->context.chunked.nextOffset = tmp;
+ encodingContext->context.chunked.state = ChunkStateData;
- if (encodingContext->context.chunked->nextOffset == 0)
+ if (encodingContext->context.chunked.nextOffset == 0)
{ // end of stream
int fd = BIO_get_fd(bio, NULL);
if (fd >= 0)
close(fd);
WLog_WARN(TAG, "cunked encoding end of stream received");
- encodingContext->context.chunked->headerFooterPos = 0;
- encodingContext->context.chunked->state = ChunkStateFooter;
+ encodingContext->context.chunked.headerFooterPos = 0;
+ encodingContext->context.chunked.state = ChunkStateFooter;
}
}
break;
static BOOL rdg_read_all(rdpTls* tls, BYTE* buffer, size_t size,
rdg_http_encoding_context* transferEncoding)
{
- int status;
- int readCount = 0;
+ size_t readCount = 0;
BYTE* pBuffer = buffer;
while (readCount < size)
{
- status = rdg_socket_read(tls->bio, pBuffer, size - readCount, transferEncoding);
+ int status = rdg_socket_read(tls->bio, pBuffer, size - readCount, transferEncoding);
if (status <= 0)
{
if (encoding == TransferEncodingChunked)
{
rdg->transferEncoding.httpTransferEncoding = TransferEncodingChunked;
- rdg->transferEncoding.context.chunked = (rdg_http_encoding_chunked_context*)calloc(
- 1, sizeof(rdg_http_encoding_chunked_context));
- rdg->transferEncoding.context.chunked->nextOffset = 0;
- rdg->transferEncoding.context.chunked->headerFooterPos = 0;
- rdg->transferEncoding.context.chunked->state = ChunkStateLenghHeader;
- rdg->transferEncoding.context.chunked->lenBuffer = (char*)calloc(11, sizeof(char));
+ rdg->transferEncoding.context.chunked.nextOffset = 0;
+ rdg->transferEncoding.context.chunked.headerFooterPos = 0;
+ rdg->transferEncoding.context.chunked.state = ChunkStateLenghHeader;
}
if (!rdg_skip_seed_payload(tls, bodyLength, &rdg->transferEncoding))
{
DeleteCriticalSection(&rdg->writeSection);
- if (rdg->transferEncoding.httpTransferEncoding == TransferEncodingChunked)
- {
- free(rdg->transferEncoding.context.chunked->lenBuffer);
- free(rdg->transferEncoding.context.chunked);
- }
-
free(rdg);
}