ossp-pkg/xds/xdr-encode-octetstream.c
1.2
/*
XDS - OSSP Extensible Data Serialization Library
Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
This file is part of OSSP XDS, an extensible data serialization
library which can be found at http://www.ossp.com/pkg/xds/.
Permission to use, copy, modify, and distribute this software for
any purpose with or without fee is hereby granted, provided that
the above copyright notice and this permission notice appear in all
copies.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include "internal.h"
int xdr_encode_octetstream(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
u_int8_t* p;
size_t p_len;
size_t padding;
/* Consistency checks. */
assert(xds != NULL);
assert(buffer != NULL);
assert(buffer_size != 0);
assert(args != NULL);
if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
return XDS_ERR_INVALID_ARG;
/* Get value from stack and calculate the correct amount of
padding. */
p = (u_int8_t*)va_arg(*args, void*);
assert(p != NULL);
p_len = va_arg(*args, size_t);
padding = (4 - (p_len & 0x03)) & 0x03;
assert((p_len + padding) % 4 == 0);
/* Format the values into the buffer. */
if (buffer_size >= 4 + p_len + padding)
{
((u_int8_t*)buffer)[0] = (p_len >> 24) & 0x000000ff;
((u_int8_t*)buffer)[1] = (p_len >> 16) & 0x000000ff;
((u_int8_t*)buffer)[2] = (p_len >> 8) & 0x000000ff;
((u_int8_t*)buffer)[3] = (p_len >> 0) & 0x000000ff;
memmove((u_int8_t*)buffer+4, p, p_len);
memset((u_int8_t*)buffer+4+p_len, 0, padding);
}
/* Done. */
return 4 + p_len + padding;
}