--- xdr-encode-octetstream.c 2001/07/23 15:33:42 1.4
+++ xdr-encode-octetstream.c 2001/08/01 11:26:56 1.4.2.1
@@ -29,7 +29,9 @@
#include <string.h>
#include "internal.h"
-int xdr_encode_octetstream(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
+int xdr_encode_octetstream(xds_t* xds, void* engine_context,
+ void* buffer, size_t buffer_size, size_t* used_buffer_size,
+ va_list* args)
{
xds_uint8_t* p;
size_t p_len;
@@ -37,12 +39,11 @@
/* 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;
+ xds_check_parameter(xds != NULL);
+ xds_check_parameter(buffer != NULL);
+ xds_check_parameter(buffer_size != 0);
+ xds_check_parameter(used_buffer_size != NULL);
+ xds_check_parameter(args != NULL);
/* Get value from stack and calculate the correct amount of
padding. */
@@ -53,19 +54,24 @@
padding = (4 - (p_len & 0x03)) & 0x03;
assert((p_len + padding) % 4 == 0);
+ /* We need (4 + p_len + padding) bytes in the buffer to format our
+ parameter. If we don't have them, return an underflow error. */
+
+ if (buffer_size < 4 + p_len + padding)
+ return XDS_ERR_UNDERFLOW;
+ else
+ *used_buffer_size = 4 + p_len + padding;
+
/* Format the values into the buffer. */
- if (buffer_size >= 4 + p_len + padding)
- {
- ((xds_uint8_t*)buffer)[0] = (p_len >> 24) & 0x000000ff;
- ((xds_uint8_t*)buffer)[1] = (p_len >> 16) & 0x000000ff;
- ((xds_uint8_t*)buffer)[2] = (p_len >> 8) & 0x000000ff;
- ((xds_uint8_t*)buffer)[3] = (p_len >> 0) & 0x000000ff;
- memmove((xds_uint8_t*)buffer+4, p, p_len);
- memset((xds_uint8_t*)buffer+4+p_len, 0, padding);
- }
+ ((xds_uint8_t*)buffer)[0] = (p_len >> 24) & 0x000000ff;
+ ((xds_uint8_t*)buffer)[1] = (p_len >> 16) & 0x000000ff;
+ ((xds_uint8_t*)buffer)[2] = (p_len >> 8) & 0x000000ff;
+ ((xds_uint8_t*)buffer)[3] = (p_len >> 0) & 0x000000ff;
+ memmove((xds_uint8_t*)buffer+4, p, p_len);
+ memset((xds_uint8_t*)buffer+4+p_len, 0, padding);
/* Done. */
- return 4 + p_len + padding;
+ return XDS_OK;
}
|