2014-05-21 00:39:21 +04:00
|
|
|
/**
|
2014-05-21 20:44:49 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
*
|
2014-05-22 23:56:47 +04:00
|
|
|
* Copyright 2014 Thincast Technologies GmbH
|
|
|
|
* Copyright 2014 Hardening <contact@hardening-consulting.com>
|
2014-05-21 00:39:21 +04:00
|
|
|
*
|
2014-05-21 20:44:49 +04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-21 00:39:21 +04:00
|
|
|
*
|
2014-05-21 20:44:49 +04:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-05-21 00:39:21 +04:00
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2015-02-03 17:28:05 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
#include <freerdp/utils/ringbuffer.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2014-05-21 00:39:21 +04:00
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <freerdp/log.h>
|
2014-05-21 00:39:21 +04:00
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
#define TAG FREERDP_TAG("utils.ringbuffer")
|
|
|
|
|
2015-02-03 17:28:05 +03:00
|
|
|
#ifdef WITH_DEBUG_RINGBUFFER
|
2016-10-07 15:04:40 +03:00
|
|
|
#define DEBUG_RINGBUFFER(...) WLog_DBG(TAG, __VA_ARGS__)
|
2015-02-03 17:28:05 +03:00
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
#define DEBUG_RINGBUFFER(...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
} while (0)
|
2015-02-03 17:28:05 +03:00
|
|
|
#endif
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
BOOL ringbuffer_init(RingBuffer* rb, size_t initialSize)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
|
|
|
rb->buffer = malloc(initialSize);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (!rb->buffer)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
rb->readPtr = rb->writePtr = 0;
|
|
|
|
rb->initialSize = rb->size = rb->freeSize = initialSize;
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_init(%p)", (void*)rb);
|
2014-05-21 00:39:21 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
size_t ringbuffer_used(const RingBuffer* rb)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2015-01-14 21:15:57 +03:00
|
|
|
return rb->size - rb->freeSize;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
size_t ringbuffer_capacity(const RingBuffer* rb)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2015-01-14 21:15:57 +03:00
|
|
|
return rb->size;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
void ringbuffer_destroy(RingBuffer* rb)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_destroy(%p)", (void*)rb);
|
2015-01-14 21:15:57 +03:00
|
|
|
free(rb->buffer);
|
|
|
|
rb->buffer = NULL;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
static BOOL ringbuffer_realloc(RingBuffer* rb, size_t targetSize)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2015-01-14 21:15:57 +03:00
|
|
|
BYTE* newData;
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_realloc(%p): targetSize: %" PRIdz "", (void*)rb, targetSize);
|
2014-05-21 00:39:21 +04:00
|
|
|
|
|
|
|
if (rb->writePtr == rb->readPtr)
|
|
|
|
{
|
|
|
|
/* when no size is used we can realloc() and set the heads at the
|
|
|
|
* beginning of the buffer
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
newData = (BYTE*)realloc(rb->buffer, targetSize);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (!newData)
|
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
rb->readPtr = rb->writePtr = 0;
|
2014-05-21 20:32:59 +04:00
|
|
|
rb->buffer = newData;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
else if ((rb->writePtr >= rb->readPtr) && (rb->writePtr < targetSize))
|
|
|
|
{
|
|
|
|
/* we reallocate only if we're in that case, realloc don't touch read
|
|
|
|
* and write heads
|
|
|
|
*
|
|
|
|
* readPtr writePtr
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* [............|XXXXXXXXXXXXXX|..........]
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
newData = (BYTE*)realloc(rb->buffer, targetSize);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (!newData)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
rb->buffer = newData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* in case of malloc the read head is moved at the beginning of the new buffer
|
|
|
|
* and the write head is set accordingly
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
newData = (BYTE*)malloc(targetSize);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (!newData)
|
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (rb->readPtr < rb->writePtr)
|
|
|
|
{
|
|
|
|
/* readPtr writePtr
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* [............|XXXXXXXXXXXXXX|..........]
|
|
|
|
*/
|
|
|
|
memcpy(newData, rb->buffer + rb->readPtr, ringbuffer_used(rb));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* writePtr readPtr
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* [XXXXXXXXXXXX|..............|XXXXXXXXXX]
|
|
|
|
*/
|
2015-01-14 21:15:57 +03:00
|
|
|
BYTE* dst = newData;
|
2014-05-21 00:39:21 +04:00
|
|
|
memcpy(dst, rb->buffer + rb->readPtr, rb->size - rb->readPtr);
|
|
|
|
dst += (rb->size - rb->readPtr);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (rb->writePtr)
|
|
|
|
memcpy(dst, rb->buffer, rb->writePtr);
|
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
rb->writePtr = rb->size - rb->freeSize;
|
|
|
|
rb->readPtr = 0;
|
2014-05-21 20:32:59 +04:00
|
|
|
free(rb->buffer);
|
2014-05-21 00:39:21 +04:00
|
|
|
rb->buffer = newData;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb->freeSize += (targetSize - rb->size);
|
|
|
|
rb->size = targetSize;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-28 00:10:49 +03:00
|
|
|
* Write to a ringbuffer
|
2014-05-21 00:39:21 +04:00
|
|
|
*
|
2022-04-28 00:10:49 +03:00
|
|
|
* @param rb A pointer to the ringbuffer
|
|
|
|
* @param ptr A pointer to the data to write
|
|
|
|
* @param sz The number of bytes to write
|
|
|
|
*
|
|
|
|
* @return \b TRUE for success, \b FALSE for failure
|
2014-05-21 00:39:21 +04:00
|
|
|
*/
|
2015-01-14 21:15:57 +03:00
|
|
|
BOOL ringbuffer_write(RingBuffer* rb, const BYTE* ptr, size_t sz)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2014-05-21 20:17:39 +04:00
|
|
|
size_t toWrite;
|
|
|
|
size_t remaining;
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_write(%p): sz: %" PRIdz "", (void*)rb, sz);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if ((rb->freeSize <= sz) && !ringbuffer_realloc(rb, rb->size + sz))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* the write could be split in two
|
|
|
|
* readHead writeHead
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* [ ################ ]
|
|
|
|
*/
|
2014-05-21 20:17:39 +04:00
|
|
|
toWrite = sz;
|
|
|
|
remaining = sz;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (rb->size - rb->writePtr < sz)
|
|
|
|
toWrite = rb->size - rb->writePtr;
|
|
|
|
|
|
|
|
if (toWrite)
|
|
|
|
{
|
|
|
|
memcpy(rb->buffer + rb->writePtr, ptr, toWrite);
|
|
|
|
remaining -= toWrite;
|
|
|
|
ptr += toWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining)
|
|
|
|
memcpy(rb->buffer, ptr, remaining);
|
|
|
|
|
|
|
|
rb->writePtr = (rb->writePtr + sz) % rb->size;
|
|
|
|
rb->freeSize -= sz;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
BYTE* ringbuffer_ensure_linear_write(RingBuffer* rb, size_t sz)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_ensure_linear_write(%p): sz: %" PRIdz "", (void*)rb, sz);
|
2015-02-03 17:28:05 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (rb->freeSize < sz)
|
|
|
|
{
|
|
|
|
if (!ringbuffer_realloc(rb, rb->size + sz - rb->freeSize + 32))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb->writePtr == rb->readPtr)
|
|
|
|
{
|
2015-01-16 02:41:57 +03:00
|
|
|
rb->writePtr = rb->readPtr = 0;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rb->writePtr + sz < rb->size)
|
|
|
|
return rb->buffer + rb->writePtr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* to add: .......
|
|
|
|
* [ XXXXXXXXX ]
|
|
|
|
*
|
|
|
|
* result:
|
|
|
|
* [XXXXXXXXX....... ]
|
|
|
|
*/
|
|
|
|
memmove(rb->buffer, rb->buffer + rb->readPtr, rb->writePtr - rb->readPtr);
|
|
|
|
rb->readPtr = 0;
|
|
|
|
rb->writePtr = rb->size - rb->freeSize;
|
|
|
|
return rb->buffer + rb->writePtr;
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
BOOL ringbuffer_commit_written_bytes(RingBuffer* rb, size_t sz)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_commit_written_bytes(%p): sz: %" PRIdz "", (void*)rb, sz);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
if (sz < 1)
|
|
|
|
return TRUE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
if (rb->writePtr + sz > rb->size)
|
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-21 00:39:21 +04:00
|
|
|
rb->writePtr = (rb->writePtr + sz) % rb->size;
|
|
|
|
rb->freeSize -= sz;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
int ringbuffer_peek(const RingBuffer* rb, DataChunk chunks[2], size_t sz)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
|
|
|
size_t remaining = sz;
|
|
|
|
size_t toRead;
|
|
|
|
int chunkIndex = 0;
|
2015-01-14 21:15:57 +03:00
|
|
|
int status = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_peek(%p): sz: %" PRIdz "", (void*)rb, sz);
|
2015-01-14 21:15:57 +03:00
|
|
|
|
|
|
|
if (sz < 1)
|
|
|
|
return 0;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
if ((rb->size - rb->freeSize) < sz)
|
2014-05-21 00:39:21 +04:00
|
|
|
remaining = rb->size - rb->freeSize;
|
|
|
|
|
|
|
|
toRead = remaining;
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
if ((rb->readPtr + remaining) > rb->size)
|
2014-05-21 00:39:21 +04:00
|
|
|
toRead = rb->size - rb->readPtr;
|
|
|
|
|
|
|
|
if (toRead)
|
|
|
|
{
|
|
|
|
chunks[0].data = rb->buffer + rb->readPtr;
|
|
|
|
chunks[0].size = toRead;
|
|
|
|
remaining -= toRead;
|
|
|
|
chunkIndex++;
|
2015-01-14 21:15:57 +03:00
|
|
|
status++;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining)
|
|
|
|
{
|
|
|
|
chunks[chunkIndex].data = rb->buffer;
|
|
|
|
chunks[chunkIndex].size = remaining;
|
2015-01-14 21:15:57 +03:00
|
|
|
status++;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
return status;
|
2014-05-21 00:39:21 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
void ringbuffer_commit_read_bytes(RingBuffer* rb, size_t sz)
|
2014-05-21 00:39:21 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_RINGBUFFER("ringbuffer_commit_read_bytes(%p): sz: %" PRIdz "", (void*)rb, sz);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-01-14 21:15:57 +03:00
|
|
|
if (sz < 1)
|
|
|
|
return;
|
2014-05-21 00:39:21 +04:00
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(rb->size - rb->freeSize >= sz);
|
2014-05-21 00:39:21 +04:00
|
|
|
rb->readPtr = (rb->readPtr + sz) % rb->size;
|
|
|
|
rb->freeSize += sz;
|
|
|
|
|
|
|
|
/* when we reach a reasonable free size, we can go back to the original size */
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((rb->size != rb->initialSize) && (ringbuffer_used(rb) < rb->initialSize / 2))
|
2014-05-21 00:39:21 +04:00
|
|
|
ringbuffer_realloc(rb, rb->initialSize);
|
|
|
|
}
|