From e90125f01e582e17532248aa9bf680f8950a0b45 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Fri, 3 May 2019 09:31:11 +0930 Subject: [PATCH] A more sane way of writing sub-int32 arrays, even if it isn't XDR. --- ez-blob-xdrn.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/ez-blob-xdrn.c b/ez-blob-xdrn.c index 057ff49..4c87819 100644 --- a/ez-blob-xdrn.c +++ b/ez-blob-xdrn.c @@ -27,7 +27,7 @@ single singlep vector vectorp list listp cstring ?null 1 ?null ?null - int8 x x 2 x 3 + int8 x x 2 x 3 int16 x x 2 int32 x x int64 x x @@ -37,9 +37,9 @@ 1. Nullable fields are optional and must be properly annotated. They are written as Optional-Data. - 2. These are written as Variable-Length Opaque Data rather than - Variable-Length Array. The size is the byte size not - the element count. + 2. These aren't XDR compatible, it's written as a Variable-Length Array + but with 2-byte elements and end-padding. Well int8 is identical + to Opaque Data. 3. These are 8-bit strings (e.g. UTF-8), not necessarily ASCII as specified by String. @@ -63,16 +63,13 @@ static __inline__ size_t roundup(size_t v) { } /* - Arrays of byte and short are encoded as 'opquate data' for - compactness, the count is the number of bytes. - - Arrays of int32/int64/float/double are written as arrays, - the count is the number of elements. + Arrays of byte and short are encoded as if they were Variable-Length + Array but with natural sized elements, followed by padding. */ static void xdrio_writev(struct ez_blobio *io, const ez_blob *data, size_t elshift) { size_t size = data->eb_size << elshift; - int32_t count = elshift <= 1 ? size : data->eb_size; + int32_t count = data->eb_size; void *v; blobio_write32(io, count); @@ -85,7 +82,7 @@ static void xdrio_writev(struct ez_blobio *io, const ez_blob *data, size_t elshi static void xdrio_readv(struct ez_blobio *io, ez_blob *data, size_t elshift) { int32_t count = blobio_readi32(io); - size_t size = elshift <= 1 ? count : count << elshift; + size_t size = count << elshift; void *src = blobio_take(io, size); if (src) { @@ -94,7 +91,7 @@ static void xdrio_readv(struct ez_blobio *io, ez_blob *data, size_t elshift) { if (mem) { memcpy(mem, src, size); data->eb_data = mem; - data->eb_size = size >> elshift; + data->eb_size = count; blobio_read_align(io, 4); } else { io->error = ENOMEM; -- 2.39.5