2012-05-25 11:24:42 +04:00
|
|
|
/*
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Stream Utils
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/stream.h>
|
|
|
|
|
|
|
|
PStream PStreamAlloc(size_t size)
|
|
|
|
{
|
|
|
|
PStream s;
|
|
|
|
|
|
|
|
s = malloc(sizeof(Stream));
|
|
|
|
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
|
|
|
if (size > 0)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = (BYTE*) malloc(size);
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = size;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = size;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ZeroMemory(s, sizeof(Stream));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamAlloc(PStream s, size_t size)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
|
|
|
if (size > 0)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = (BYTE*) malloc(size);
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = size;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = size;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ZeroMemory(s, sizeof(Stream));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamReAlloc(PStream s, size_t size)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
size_t old_size;
|
|
|
|
size_t offset_p;
|
|
|
|
|
|
|
|
old_size = s->size;
|
2012-07-05 02:33:49 +04:00
|
|
|
offset_p = s->pointer - s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = (BYTE*) realloc(s->buffer, size);
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = size;
|
|
|
|
|
|
|
|
if (old_size <= size)
|
2012-07-05 02:33:49 +04:00
|
|
|
s->pointer = s->buffer + offset_p;
|
2012-05-25 11:24:42 +04:00
|
|
|
else
|
2012-07-05 02:33:49 +04:00
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (s->size > 0)
|
2012-07-05 02:33:49 +04:00
|
|
|
free(s->buffer);
|
2012-05-25 11:24:42 +04:00
|
|
|
|
|
|
|
ZeroMemory(s, sizeof(Stream));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-05 02:33:49 +04:00
|
|
|
PStream PStreamAllocAttach(BYTE* buffer, size_t size)
|
2012-05-25 11:24:42 +04:00
|
|
|
{
|
|
|
|
PStream s;
|
|
|
|
|
|
|
|
s = malloc(sizeof(Stream));
|
|
|
|
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = buffer;
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = size;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = size;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
|
2012-05-25 22:03:56 +04:00
|
|
|
return s;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
|
2012-07-05 02:33:49 +04:00
|
|
|
void StreamAllocAttach(PStream s, BYTE* buffer, size_t size)
|
2012-05-25 11:24:42 +04:00
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = buffer;
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = size;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = size;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PStreamFree(PStream s)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
if (s->buffer != NULL)
|
|
|
|
free(s->buffer);
|
2012-05-25 11:24:42 +04:00
|
|
|
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamFree(PStream s)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
if (s->buffer != NULL)
|
|
|
|
free(s->buffer);
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PStreamFreeDetach(PStream s)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = NULL;
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = 0;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = 0;
|
2012-05-25 11:24:42 +04:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamFreeDetach(PStream s)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = NULL;
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = 0;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = 0;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-05 02:33:49 +04:00
|
|
|
void StreamAttach(PStream s, BYTE* buffer, size_t size)
|
2012-05-25 11:24:42 +04:00
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = buffer;
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = size;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = size;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamDetach(PStream s)
|
|
|
|
{
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
2012-07-05 02:33:49 +04:00
|
|
|
s->buffer = NULL;
|
|
|
|
s->pointer = s->buffer;
|
2012-05-25 11:24:42 +04:00
|
|
|
s->size = 0;
|
2012-07-05 02:33:49 +04:00
|
|
|
s->length = 0;
|
2012-05-25 11:24:42 +04:00
|
|
|
}
|
|
|
|
}
|