libwinpr-utils: started replacement stream utils

This commit is contained in:
Marc-André Moreau 2012-05-25 03:24:42 -04:00
parent b70683af9e
commit 6379cc628b
7 changed files with 407 additions and 10 deletions

205
include/winpr/stream.h Normal file
View File

@ -0,0 +1,205 @@
/*
* 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.
*/
#ifndef WINPR_UTILS_STREAM_H
#define WINPR_UTILS_STREAM_H
#include <winpr/winpr.h>
#include <winpr/wtypes.h>
struct stream
{
BYTE* p;
BYTE* end;
BYTE* data;
size_t size;
};
typedef struct stream Stream;
typedef struct stream* PStream;
WINPR_API PStream PStreamAlloc(size_t size);
WINPR_API void StreamAlloc(PStream s, size_t size);
WINPR_API void StreamReAlloc(PStream s, size_t size);
WINPR_API PStream PStreamAllocAttach(BYTE* data, size_t size);
WINPR_API void StreamAllocAttach(PStream s, BYTE* data, size_t size);
WINPR_API void PStreamFree(PStream s);
WINPR_API void StreamFree(PStream s);
WINPR_API void PStreamFreeDetach(PStream s);
WINPR_API void StreamFreeDetach(PStream s);
WINPR_API void StreamAttach(PStream s, BYTE* data, size_t size);
WINPR_API void StreamDetach(PStream s);
#define StreamRead_UINT8(_s, _v) do { _v = \
*_s->p++; } while (0)
#define StreamRead_UINT16(_s, _v) do { _v = \
(UINT16)(*_s->p) + \
(((UINT16)(*(_s->p + 1))) << 8); \
_s->p += 2; } while (0)
#define StreamRead_UINT16_BE(_s, _v) do { _v = \
(((UINT16)(*_s->p)) << 8) + \
(UINT16)(*(_s->p + 1)); \
_s->p += 2; } while (0)
#define StreamRead_UINT32(_s, _v) do { _v = \
(UINT32)(*_s->p) + \
(((UINT32)(*(_s->p + 1))) << 8) + \
(((UINT32)(*(_s->p + 2))) << 16) + \
(((UINT32)(*(_s->p + 3))) << 24); \
_s->p += 4; } while (0)
#define StreamRead_UINT32_BE(_s, _v) do { _v = \
(((uint32)(*(_s->p))) << 24) + \
(((uint32)(*(_s->p + 1))) << 16) + \
(((uint32)(*(_s->p + 2))) << 8) + \
(((uint32)(*(_s->p + 3)))); \
_s->p += 4; } while (0)
#define StreamRead_UINT64(_s, _v) do { _v = \
(UINT64)(*_s->p) + \
(((UINT64)(*(_s->p + 1))) << 8) + \
(((UINT64)(*(_s->p + 2))) << 16) + \
(((UINT64)(*(_s->p + 3))) << 24) + \
(((UINT64)(*(_s->p + 4))) << 32) + \
(((UINT64)(*(_s->p + 5))) << 40) + \
(((UINT64)(*(_s->p + 6))) << 48) + \
(((UINT64)(*(_s->p + 7))) << 56); \
_s->p += 8; } while (0)
#define StreamRead(_s, _b, _n) do { \
memcpy(_b, (_s->p), (_n)); \
_s->p += (_n); \
} while (0)
#define StreamWrite_UINT8(_s, _v) do { \
*_s->p++ = (uint8)(_v); } while (0)
#define StreamWrite_UINT16(_s, _v) do { \
*_s->p++ = (_v) & 0xFF; \
*_s->p++ = ((_v) >> 8) & 0xFF; } while (0)
#define StreamWrite_UINT16_BE(_s, _v) do { \
*_s->p++ = ((_v) >> 8) & 0xFF; \
*_s->p++ = (_v) & 0xFF; } while (0)
#define StreamWrite_UINT32(_s, _v) do { \
*_s->p++ = (_v) & 0xFF; \
*_s->p++ = ((_v) >> 8) & 0xFF; \
*_s->p++ = ((_v) >> 16) & 0xFF; \
*_s->p++ = ((_v) >> 24) & 0xFF; } while (0)
#define StreamWrite_UINT32_BE(_s, _v) do { \
StreamWrite_UINT16_BE(_s, ((_v) >> 16 & 0xFFFF)); \
StreamWrite_UINT16_BE(_s, ((_v) & 0xFFFF)); \
} while (0)
#define StreamWrite_UINT64(_s, _v) do { \
*_s->p++ = (UINT64)(_v) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 8) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 16) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 24) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 32) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 40) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 48) & 0xFF; \
*_s->p++ = ((UINT64)(_v) >> 56) & 0xFF; } while (0)
#define StreamWrite(_s, _b, _n) do { \
memcpy(_s->p, (_b), (_n)); \
_s->p += (_n); \
} while (0)
#define StreamPeek_UINT8(_s, _v) do { _v = \
*_s->p; } while (0)
#define StreamPeek_UINT16(_s, _v) do { _v = \
(UINT16)(*_s->p) + \
(((UINT16)(*(_s->p + 1))) << 8); \
} while (0)
#define StreamPeek_UINT32(_s, _v) do { _v = \
(UINT32)(*_s->p) + \
(((UINT32)(*(_s->p + 1))) << 8) + \
(((UINT32)(*(_s->p + 2))) << 16) + \
(((UINT32)(*(_s->p + 3))) << 24); \
} while (0)
#define StreamPeek_UINT64(_s, _v) do { _v = \
(UINT64)(*_s->p) + \
(((UINT64)(*(_s->p + 1))) << 8) + \
(((UINT64)(*(_s->p + 2))) << 16) + \
(((UINT64)(*(_s->p + 3))) << 24) + \
(((UINT64)(*(_s->p + 4))) << 32) + \
(((UINT64)(*(_s->p + 5))) << 40) + \
(((UINT64)(*(_s->p + 6))) << 48) + \
(((UINT64)(*(_s->p + 7))) << 56); \
} while (0)
#define StreamPeek(_s, _b, _n) do { \
memcpy(_b, (_s->p), (_n)); \
} while (0)
#define StreamSeek(_s,_offset) _s->p += (_offset)
#define StreamRewind(_s,_offset) _s->p -= (_offset)
#define StreamSeek_UINT8(_s) StreamSeek(_s, 1)
#define StreamSeek_UINT16(_s) StreamSeek(_s, 2)
#define StreamSeek_UINT32(_s) StreamSeek(_s, 4)
#define StreamSeek_UINT64(_s) StreamSeek(_s, 8)
#define StreamRewind_UINT8(_s) StreamRewind(_s, 1)
#define StreamRewind_UINT16(_s) StreamRewind(_s, 2)
#define StreamRewind_UINT32(_s) StreamRewind(_s, 4)
#define StreamRewind_UINT64(_s) StreamRewind(_s, 8)
#define StreamZero(_s, _n) do { \
memset(_s->p, '\0', (_n)); \
_s->p += (_n); \
} while (0)
#define StreamFill(_s, _v, _n) do { \
memset(_s->p, _v, (_n)); \
_s->p += (_n); \
} while (0)
#define StreamCopy(_dst, _src, _n) do { \
memcpy(_dst->p, _src->p, _n); \
_dst->p += _n; \
_src->p += _n; \
} while (0)
#define StreamGetOffset(_s) (_s->p - _s->data)
#define StreamSetOffset(_s, _m) _s->p = _s->data + (_m)
#define StreamSeal(_s) _s->end = (_s->p - _s->data)
#define StreamGetMark(_s, _m) _m = _s->p
#define StreamSetMark(_s, _m) _s->p = _m
#define StreamGetData(_s) _s->data
#define StreamGetPointer(_s) _s->p
#define StreamGetEnd(_s) _s->end
#define StreamGetSize(_s) _s->size
#define StreamSize(_s) (_s->p - _s->data)
#define StreamSealedSize(_s) (_s->end - _s->data)
#define StreamRemainingSize(_s) (_s->size - (_s->p - _s->data))
#endif /* WINPR_UTILS_STREAM_H */

View File

@ -43,7 +43,7 @@ if(WITH_REGISTRY_XML)
target_link_libraries(winpr-registry ${LIBXML2_LIBRARIES})
endif()
target_link_libraries(winpr-registry freerdp-utils)
target_link_libraries(winpr-registry winpr-utils)
install(TARGETS winpr-registry DESTINATION ${CMAKE_INSTALL_LIBDIR})

View File

@ -66,7 +66,6 @@ if (NOT WIN32)
endif()
target_link_libraries(winpr-sspi winpr-utils)
target_link_libraries(winpr-sspi freerdp-utils)
target_link_libraries(winpr-sspi freerdp-crypto)
target_link_libraries(winpr-sspi ${ZLIB_LIBRARIES})

View File

@ -23,8 +23,6 @@
#include <winpr/sspi.h>
#include <freerdp/crypto/crypto.h>
#include <freerdp/utils/unicode.h>
#include "../sspi.h"
enum _NTLM_STATE
@ -77,12 +75,12 @@ typedef enum _AV_ID AV_ID;
struct _NTLM_CONTEXT
{
boolean server;
boolean ntlm_v2;
BOOL server;
BOOL ntlm_v2;
NTLM_STATE state;
int SendSeqNum;
int RecvSeqNum;
boolean confidentiality;
BOOL confidentiality;
CryptoRc4 SendRc4Seal;
CryptoRc4 RecvRc4Seal;
BYTE* SendSigningKey;

View File

@ -21,13 +21,12 @@
#define WINPR_SSPI_SCHANNEL_PRIVATE_H
#include <winpr/sspi.h>
#include <freerdp/crypto/crypto.h>
#include "../sspi.h"
struct _SCHANNEL_CONTEXT
{
boolean server;
BOOL server;
};
typedef struct _SCHANNEL_CONTEXT SCHANNEL_CONTEXT;

View File

@ -18,7 +18,8 @@
# limitations under the License.
set(WINPR_UTILS_SRCS
print.c)
print.c
stream.c)
add_library(winpr-utils ${WINPR_UTILS_SRCS})

195
winpr/utils/stream.c Normal file
View File

@ -0,0 +1,195 @@
/*
* 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)
{
s->data = (BYTE*) malloc(size);
s->p = s->data;
s->end = s->p;
s->size = size;
}
else
{
ZeroMemory(s, sizeof(Stream));
}
}
return s;
}
void StreamAlloc(PStream s, size_t size)
{
if (s != NULL)
{
if (size > 0)
{
s->data = (BYTE*) malloc(size);
s->p = s->data;
s->end = s->p;
s->size = size;
}
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;
size_t offset_end;
old_size = s->size;
offset_p = s->p - s->data;
offset_end = s->end - s->data;
s->data = (BYTE*) realloc(s->data, size);
s->size = size;
if (old_size <= size)
{
s->p = s->data + offset_p;
s->end = s->data + offset_end;
}
else
{
s->p = s->data;
s->end = s->p;
}
}
else
{
if (s->size > 0)
free(s->data);
ZeroMemory(s, sizeof(Stream));
}
}
}
PStream PStreamAllocAttach(BYTE* data, size_t size)
{
PStream s;
s = malloc(sizeof(Stream));
if (s != NULL)
{
s->data = data;
s->p = s->data;
s->end = s->p;
s->size = size;
}
return 0;
}
void StreamAllocAttach(PStream s, BYTE* data, size_t size)
{
if (s != NULL)
{
s->data = data;
s->p = s->data;
s->end = s->p;
s->size = size;
}
}
void PStreamFree(PStream s)
{
if (s != NULL)
{
if (s->data != NULL)
free(s->data);
free(s);
}
}
void StreamFree(PStream s)
{
if (s != NULL)
{
if (s->data != NULL)
free(s->data);
}
}
void PStreamFreeDetach(PStream s)
{
if (s != NULL)
{
s->data = NULL;
s->p = s->data;
s->end = s->p;
s->size = 0;
free(s);
}
}
void StreamFreeDetach(PStream s)
{
if (s != NULL)
{
s->data = NULL;
s->p = s->data;
s->end = s->p;
s->size = 0;
}
}
void StreamAttach(PStream s, BYTE* data, size_t size)
{
if (s != NULL)
{
s->data = data;
s->p = s->data;
s->end = s->p;
s->size = size;
}
}
void StreamDetach(PStream s)
{
if (s != NULL)
{
s->data = NULL;
s->p = s->data;
s->end = s->p;
s->size = 0;
}
}