--- xdr-encode-octetstream.c 2001/07/23 15:33:42 1.4
+++ xdr-encode-octetstream.c 2001/08/01 15:25:47 1.5
@@ -25,47 +25,45 @@
SUCH DAMAGE.
*/
-#include <assert.h>
#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;
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;
+ xds_init_encoding_engine(4);
/* Get value from stack and calculate the correct amount of
padding. */
p = (xds_uint8_t*)va_arg(*args, void*);
- assert(p != NULL);
+ xds_check_parameter(p != NULL);
p_len = va_arg(*args, size_t);
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. */
+
+ *used_buffer_size = 4 + p_len + padding;
+ if (buffer_size < *used_buffer_size)
+ return XDS_ERR_OVERFLOW;
+
/* 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;
}
|