Added writeUnsignedIntBigEndianBlob.
diff --git a/ndn-cpp/c/encoding/BinaryXMLEncoder.c b/ndn-cpp/c/encoding/BinaryXMLEncoder.c
index ad7a5d1..6111c8c 100644
--- a/ndn-cpp/c/encoding/BinaryXMLEncoder.c
+++ b/ndn-cpp/c/encoding/BinaryXMLEncoder.c
@@ -61,18 +61,17 @@
}
/**
- * Reverse the length bytes in array starting at offset.
+ * Reverse the length bytes in array.
* @param array
- * @param offset
* @param length
*/
-static void reverse(unsigned char *array, unsigned int offset, unsigned int length)
+static void reverse(unsigned char *array, unsigned int length)
{
if (length == 0)
return;
- unsigned char *left = array + offset;
- unsigned char *right = array + offset + length - 1;
+ unsigned char *left = array;
+ unsigned char *right = array + length - 1;
while (left < right) {
// Swap.
unsigned char temp = *left;
@@ -109,7 +108,57 @@
}
// Now reverse.
- reverse(self->output.array, startOffset, self->offset - startOffset);
+ reverse(self->output.array + startOffset, self->offset - startOffset);
+ return 0;
+}
+
+/**
+ * Like memcpy, copy length bytes from source to dest, assuming that the buffers can overlap and that
+ * we are shifting the buffer right.
+ * Don't use memcpy to shift because its behavior is not guaranteed when the buffers overlap.
+ * @param dest
+ * @param source
+ * @param length
+ */
+static void copyBufferRight(unsigned char *dest, unsigned char *source, unsigned int length)
+{
+ if (length == 0)
+ return;
+
+ // We are shifting right, so start from the end of the buffer.
+ unsigned char *from = source + length - 1;
+ unsigned char *to = dest + length - 1;
+ while (from >= source)
+ *(to--) = *(from--);
+}
+
+/**
+ * Shift a buffer in self->output.array right by the amount needed to prefix a header with type, then encode the header
+ * at startOffset.
+ * The buffer to shift right begins at startOffset and self->offset is at the end.
+ * @param self pointer to the ndn_BinaryXMLEncoder struct
+ * @param startOffset the offset in self->output.array of the start of the buffer to shift right
+ * @param type the header type
+ * @return 0 for success, else an error code
+ */
+static ndn_Error insertHeader
+ (struct ndn_BinaryXMLEncoder *self, unsigned int startOffset, unsigned int type)
+{
+ unsigned int nBufferBytes = self->offset - startOffset;
+ unsigned int nHeaderBytes = getNHeaderEncodingBytes(nBufferBytes);
+ ndn_Error error;
+ if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + nHeaderBytes))
+ return error;
+
+ copyBufferRight(self->output.array + startOffset + nHeaderBytes, self->output.array + startOffset, nBufferBytes);
+
+ // Override the offset to force encodeTypeAndValue to encode at startOffset, then fix the offset.
+ self->offset = startOffset;
+ if (error = ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_UDATA, nBufferBytes))
+ // We don't really expect to get an error, since we have already ensured the length.
+ return error;
+ self->offset = startOffset + nHeaderBytes + nBufferBytes;
+
return 0;
}
@@ -195,26 +244,9 @@
if (error = encodeUnsignedDecimalInt(self, value))
return error;
- unsigned int nIntegerBytes = self->offset - startOffset;
- unsigned int nHeaderBytes = getNHeaderEncodingBytes(nIntegerBytes);
- if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + nHeaderBytes))
+ if (error = insertHeader(self, startOffset, ndn_BinaryXML_UDATA))
return error;
- // Don't use memcpy to shift because its behavior is not guaranteed when the buffers overlap.
- // We are shifting forward, so start from the end of the buffer.
- unsigned char *source = self->output.array + startOffset + nIntegerBytes - 1;
- unsigned char *dest = source + nHeaderBytes;
- unsigned char *sourceFinal = self->output.array + startOffset;
- while (source >= sourceFinal)
- *(dest--) = *(source--);
-
- // Override the offset to force encodeTypeAndValue to encode at startOffset, then fix the offset.
- self->offset = startOffset;
- if (error = ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_UDATA, nIntegerBytes))
- // We don't really expect to get an error, since we have already ensured the length.
- return error;
- self->offset = startOffset + nHeaderBytes + nIntegerBytes;
-
return 0;
}
@@ -232,3 +264,25 @@
return 0;
}
+
+ndn_Error ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(struct ndn_BinaryXMLEncoder *self, unsigned int value)
+{
+ // First encode the big endian backwards, then reverse it.
+ unsigned int startOffset = self->offset;
+ ndn_Error error;
+ while (value != 0) {
+ if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + 1))
+ return error;
+
+ self->output.array[self->offset++] = (unsigned char)(value & 0xff);
+ value >>= 8;
+ }
+
+ unsigned int bigEndianLength = self->offset - startOffset;
+ reverse(self->output.array + startOffset, bigEndianLength);
+
+ if (error = insertHeader(self, startOffset, ndn_BinaryXML_BLOB))
+ return error;
+
+ return 0;
+}