libwinpr-utils: simplify stream utils

This commit is contained in:
Marc-André Moreau 2012-12-10 14:23:27 -05:00
parent c4e06eb0a7
commit 6e07570dda
4 changed files with 25 additions and 131 deletions

View File

@ -23,6 +23,7 @@
#define FREERDP_CORE_RPC_H
#include <winpr/wtypes.h>
#include <winpr/stream.h>
#include <winpr/collections.h>
#include <winpr/interlocked.h>

View File

@ -35,17 +35,11 @@ struct stream
typedef struct stream Stream;
typedef struct stream* PStream;
WINPR_API PStream PStream_Alloc(size_t size);
WINPR_API void Stream_Alloc(PStream s, size_t size);
WINPR_API void Stream_Realloc(PStream s, size_t size);
WINPR_API PStream PStream_AllocAttach(BYTE* buffer, size_t size);
WINPR_API void Stream_AllocAttach(PStream s, BYTE* buffer, size_t size);
WINPR_API void PStream_Free(PStream s);
WINPR_API PStream Stream_Alloc(size_t size);
WINPR_API PStream Stream_AllocAttach(BYTE* buffer, size_t size);
WINPR_API void Stream_Free(PStream s);
WINPR_API void PStream_FreeDetach(PStream s);
WINPR_API void Stream_FreeDetach(PStream s);
WINPR_API void Stream_Attach(PStream s, BYTE* buffer, size_t size);
WINPR_API void Stream_Detach(PStream s);
#define Stream_Read_UINT8(_s, _v) do { _v = \
*_s->pointer++; } while (0)

View File

@ -195,13 +195,13 @@ SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buf
NTLM_NEGOTIATE_MESSAGE message;
ZeroMemory(&message, sizeof(message));
s = PStream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
s = Stream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
ntlm_read_message_header(s, (NTLM_MESSAGE_HEADER*) &message);
if (!ntlm_validate_message_header(s, (NTLM_MESSAGE_HEADER*) &message, MESSAGE_TYPE_NEGOTIATE))
{
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_E_INVALID_TOKEN;
}
@ -212,7 +212,7 @@ SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buf
(message.NegotiateFlags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) &&
(message.NegotiateFlags & NTLMSSP_NEGOTIATE_UNICODE)))
{
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_E_INVALID_TOKEN;
}
@ -251,7 +251,7 @@ SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buf
context->state = NTLM_STATE_CHALLENGE;
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_I_CONTINUE_NEEDED;
}
@ -263,7 +263,7 @@ SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer bu
NTLM_NEGOTIATE_MESSAGE message;
ZeroMemory(&message, sizeof(message));
s = PStream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
s = Stream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
ntlm_populate_message_header((NTLM_MESSAGE_HEADER*) &message, MESSAGE_TYPE_NEGOTIATE);
@ -331,7 +331,7 @@ SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer bu
context->state = NTLM_STATE_CHALLENGE;
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_I_CONTINUE_NEEDED;
}
@ -348,7 +348,7 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf
ntlm_generate_client_challenge(context);
ZeroMemory(&message, sizeof(message));
s = PStream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
s = Stream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
StartOffset = Stream_Pointer(s);
@ -356,7 +356,7 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf
if (!ntlm_validate_message_header(s, (NTLM_MESSAGE_HEADER*) &message, MESSAGE_TYPE_CHALLENGE))
{
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_E_INVALID_TOKEN;
}
@ -517,7 +517,7 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf
ntlm_free_message_fields_buffer(&(message.TargetName));
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_I_CONTINUE_NEEDED;
}
@ -530,7 +530,7 @@ SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer bu
NTLM_CHALLENGE_MESSAGE message;
ZeroMemory(&message, sizeof(message));
s = PStream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
s = Stream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
/* Version */
ntlm_get_version_info(&(message.Version));
@ -620,7 +620,7 @@ SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer bu
context->state = NTLM_STATE_AUTHENTICATE;
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_I_CONTINUE_NEEDED;
}
@ -641,13 +641,13 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer
AvFlags = NULL;
ZeroMemory(&message, sizeof(message));
s = PStream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
s = Stream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
ntlm_read_message_header(s, (NTLM_MESSAGE_HEADER*) &message);
if (!ntlm_validate_message_header(s, (NTLM_MESSAGE_HEADER*) &message, MESSAGE_TYPE_AUTHENTICATE))
{
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_E_INVALID_TOKEN;
}
@ -697,9 +697,9 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer
if (message.NtChallengeResponse.Len > 0)
{
PStream s = PStream_AllocAttach(message.NtChallengeResponse.Buffer, message.NtChallengeResponse.Len);
PStream s = Stream_AllocAttach(message.NtChallengeResponse.Buffer, message.NtChallengeResponse.Len);
ntlm_read_ntlm_v2_response(s, &response);
PStream_FreeDetach(s);
Stream_FreeDetach(s);
context->NtChallengeResponse.pvBuffer = message.NtChallengeResponse.Buffer;
context->NtChallengeResponse.cbBuffer = message.NtChallengeResponse.Len;
@ -864,7 +864,7 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer
context->state = NTLM_STATE_FINAL;
PStream_FreeDetach(s);
Stream_FreeDetach(s);
ntlm_free_message_fields_buffer(&(message.DomainName));
ntlm_free_message_fields_buffer(&(message.UserName));
@ -892,7 +892,7 @@ SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer
NTLM_AUTHENTICATE_MESSAGE message;
ZeroMemory(&message, sizeof(message));
s = PStream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
s = Stream_AllocAttach(buffer->pvBuffer, buffer->cbBuffer);
if (context->NTLMv2)
{
@ -1066,7 +1066,7 @@ SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer
context->state = NTLM_STATE_FINAL;
PStream_FreeDetach(s);
Stream_FreeDetach(s);
return SEC_I_COMPLETE_NEEDED;
}

View File

@ -25,7 +25,7 @@
#include <winpr/crt.h>
#include <winpr/stream.h>
PStream PStream_Alloc(size_t size)
PStream Stream_Alloc(size_t size)
{
PStream s;
@ -49,55 +49,7 @@ PStream PStream_Alloc(size_t size)
return s;
}
void Stream_Alloc(PStream s, size_t size)
{
if (s != NULL)
{
if (size > 0)
{
s->buffer = (BYTE*) malloc(size);
s->pointer = s->buffer;
s->capacity = size;
s->length = size;
}
else
{
ZeroMemory(s, sizeof(Stream));
}
}
}
void Stream_Realloc(PStream s, size_t size)
{
if (s != NULL)
{
if (size > 0)
{
size_t old_size;
size_t offset_p;
old_size = s->capacity;
offset_p = s->pointer - s->buffer;
s->buffer = (BYTE*) realloc(s->buffer, size);
s->capacity = size;
if (old_size <= size)
s->pointer = s->buffer + offset_p;
else
s->pointer = s->buffer;
}
else
{
if (s->capacity > 0)
free(s->buffer);
ZeroMemory(s, sizeof(Stream));
}
}
}
PStream PStream_AllocAttach(BYTE* buffer, size_t size)
PStream Stream_AllocAttach(BYTE* buffer, size_t size)
{
PStream s;
@ -114,45 +66,13 @@ PStream PStream_AllocAttach(BYTE* buffer, size_t size)
return s;
}
void Stream_AllocAttach(PStream s, BYTE* buffer, size_t size)
{
if (s != NULL)
{
s->buffer = buffer;
s->pointer = s->buffer;
s->capacity = size;
s->length = size;
}
}
void PStream_Free(PStream s)
{
if (s != NULL)
{
if (s->buffer != NULL)
free(s->buffer);
free(s);
}
}
void Stream_Free(PStream s)
{
if (s != NULL)
{
if (s->buffer != NULL)
free(s->buffer);
}
}
void PStream_FreeDetach(PStream s)
{
if (s != NULL)
{
s->buffer = NULL;
s->pointer = s->buffer;
s->capacity = 0;
s->length = 0;
free(s);
}
}
@ -165,27 +85,6 @@ void Stream_FreeDetach(PStream s)
s->pointer = s->buffer;
s->capacity = 0;
s->length = 0;
}
}
void Stream_Attach(PStream s, BYTE* buffer, size_t size)
{
if (s != NULL)
{
s->buffer = buffer;
s->pointer = s->buffer;
s->capacity = size;
s->length = size;
}
}
void Stream_Detach(PStream s)
{
if (s != NULL)
{
s->buffer = NULL;
s->pointer = s->buffer;
s->capacity = 0;
s->length = 0;
free(s);
}
}