Use ndn_memcpy and ndn_memset based on HAVE_MEMCPY and HAVE_MEMSET
diff --git a/Makefile.am b/Makefile.am
index facc5cd..b51d0c4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,7 +31,8 @@
ndn-cpp/encoding/BinaryXMLWireFormat.cpp ndn-cpp/encoding/BinaryXMLWireFormat.hpp \
ndn-cpp/encoding/WireFormat.cpp ndn-cpp/encoding/WireFormat.hpp \
ndn-cpp/encoding/base.h \
- ndn-cpp/encoding/ccnb.cc ndn-cpp/encoding/ccnb.h
+ ndn-cpp/encoding/ccnb.cc ndn-cpp/encoding/ccnb.h \
+ ndn-cpp/util/ndn_memory.c ndn-cpp/util/ndn_memory.h
test_encode_decode_interest_SOURCES = test/test-encode-decode-interest.cpp
test_encode_decode_interest_LDADD = ndn-cpp.a
diff --git a/Makefile.in b/Makefile.in
index 4f62b8c..7ca10a4 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -145,7 +145,8 @@
ndn-cpp/encoding/BinaryXMLStructureDecoder.$(OBJEXT) \
ndn-cpp/encoding/BinaryXMLWireFormat.$(OBJEXT) \
ndn-cpp/encoding/WireFormat.$(OBJEXT) \
- ndn-cpp/encoding/ccnb.$(OBJEXT)
+ ndn-cpp/encoding/ccnb.$(OBJEXT) \
+ ndn-cpp/util/ndn_memory.$(OBJEXT)
ndn_cpp_a_OBJECTS = $(am_ndn_cpp_a_OBJECTS)
PROGRAMS = $(bin_PROGRAMS)
am_test_encode_decode_interest_OBJECTS = \
@@ -403,7 +404,8 @@
ndn-cpp/encoding/BinaryXMLWireFormat.cpp ndn-cpp/encoding/BinaryXMLWireFormat.hpp \
ndn-cpp/encoding/WireFormat.cpp ndn-cpp/encoding/WireFormat.hpp \
ndn-cpp/encoding/base.h \
- ndn-cpp/encoding/ccnb.cc ndn-cpp/encoding/ccnb.h
+ ndn-cpp/encoding/ccnb.cc ndn-cpp/encoding/ccnb.h \
+ ndn-cpp/util/ndn_memory.c ndn-cpp/util/ndn_memory.h
test_encode_decode_interest_SOURCES = test/test-encode-decode-interest.cpp
test_encode_decode_interest_LDADD = ndn-cpp.a
@@ -545,6 +547,14 @@
ndn-cpp/encoding/$(DEPDIR)/$(am__dirstamp)
ndn-cpp/encoding/ccnb.$(OBJEXT): ndn-cpp/encoding/$(am__dirstamp) \
ndn-cpp/encoding/$(DEPDIR)/$(am__dirstamp)
+ndn-cpp/util/$(am__dirstamp):
+ @$(MKDIR_P) ndn-cpp/util
+ @: > ndn-cpp/util/$(am__dirstamp)
+ndn-cpp/util/$(DEPDIR)/$(am__dirstamp):
+ @$(MKDIR_P) ndn-cpp/util/$(DEPDIR)
+ @: > ndn-cpp/util/$(DEPDIR)/$(am__dirstamp)
+ndn-cpp/util/ndn_memory.$(OBJEXT): ndn-cpp/util/$(am__dirstamp) \
+ ndn-cpp/util/$(DEPDIR)/$(am__dirstamp)
ndn-cpp.a: $(ndn_cpp_a_OBJECTS) $(ndn_cpp_a_DEPENDENCIES) $(EXTRA_ndn_cpp_a_DEPENDENCIES)
$(AM_V_at)-rm -f ndn-cpp.a
@@ -610,6 +620,7 @@
-rm -f ndn-cpp/*.$(OBJEXT)
-rm -f ndn-cpp/encoding/*.$(OBJEXT)
-rm -f ndn-cpp/fields/*.$(OBJEXT)
+ -rm -f ndn-cpp/util/*.$(OBJEXT)
-rm -f test/*.$(OBJEXT)
distclean-compile:
@@ -629,6 +640,7 @@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/fields/$(DEPDIR)/name-component.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/fields/$(DEPDIR)/name.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/fields/$(DEPDIR)/signature.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/util/$(DEPDIR)/ndn_memory.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@test/$(DEPDIR)/test-encode-decode-interest.Po@am__quote@
.c.o:
@@ -933,6 +945,8 @@
-rm -f ndn-cpp/encoding/$(am__dirstamp)
-rm -f ndn-cpp/fields/$(DEPDIR)/$(am__dirstamp)
-rm -f ndn-cpp/fields/$(am__dirstamp)
+ -rm -f ndn-cpp/util/$(DEPDIR)/$(am__dirstamp)
+ -rm -f ndn-cpp/util/$(am__dirstamp)
-rm -f test/$(DEPDIR)/$(am__dirstamp)
-rm -f test/$(am__dirstamp)
@@ -946,7 +960,7 @@
distclean: distclean-am
-rm -f $(am__CONFIG_DISTCLEAN_FILES)
- -rm -rf ndn-cpp/$(DEPDIR) ndn-cpp/encoding/$(DEPDIR) ndn-cpp/fields/$(DEPDIR) test/$(DEPDIR)
+ -rm -rf ndn-cpp/$(DEPDIR) ndn-cpp/encoding/$(DEPDIR) ndn-cpp/fields/$(DEPDIR) ndn-cpp/util/$(DEPDIR) test/$(DEPDIR)
-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic \
distclean-hdr distclean-tags
@@ -994,7 +1008,7 @@
maintainer-clean: maintainer-clean-am
-rm -f $(am__CONFIG_DISTCLEAN_FILES)
-rm -rf $(top_srcdir)/autom4te.cache
- -rm -rf ndn-cpp/$(DEPDIR) ndn-cpp/encoding/$(DEPDIR) ndn-cpp/fields/$(DEPDIR) test/$(DEPDIR)
+ -rm -rf ndn-cpp/$(DEPDIR) ndn-cpp/encoding/$(DEPDIR) ndn-cpp/fields/$(DEPDIR) ndn-cpp/util/$(DEPDIR) test/$(DEPDIR)
-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic
diff --git a/ndn-cpp/encoding/BinaryXMLStructureDecoder.c b/ndn-cpp/encoding/BinaryXMLStructureDecoder.c
index 0d53a3b..e29515d 100644
--- a/ndn-cpp/encoding/BinaryXMLStructureDecoder.c
+++ b/ndn-cpp/encoding/BinaryXMLStructureDecoder.c
@@ -4,7 +4,7 @@
* BSD license, See the LICENSE file for more information.
*/
-#include <memory.h>
+#include "../util/ndn_memory.h"
#include "BinaryXML.h"
#include "BinaryXMLDecoder.h"
#include "BinaryXMLStructureDecoder.h"
@@ -30,12 +30,12 @@
self->state = ndn_BinaryXMLStructureDecoder_READ_HEADER_OR_CLOSE;
}
-const char *ndn_BinaryXMLStructureDecoder_findElementEnd
- (struct ndn_BinaryXMLStructureDecoder *self, const unsigned char *input, unsigned int inputLength)
+char *ndn_BinaryXMLStructureDecoder_findElementEnd
+ (struct ndn_BinaryXMLStructureDecoder *self, unsigned char *input, unsigned int inputLength)
{
if (self->gotElementEnd)
// Someone is calling when we already got the end.
- return (const char *)0;
+ return (char *)0;
struct ndn_BinaryXMLDecoder decoder;
ndn_BinaryXMLDecoder_init(&decoder, input, inputLength);
@@ -43,7 +43,7 @@
while (1) {
if (self->offset >= inputLength)
// All the cases assume we have some input. Return and wait for more.
- return (const char *)0;
+ return (char *)0;
if (self->state == ndn_BinaryXMLStructureDecoder_READ_HEADER_OR_CLOSE) {
// First check for CLOSE.
@@ -54,7 +54,7 @@
if (self->level == 0) {
// Finished.
self->gotElementEnd = 1;
- return (const char *)0;
+ return (char *)0;
}
if (self->level < 0)
return "ndn_BinaryXMLStructureDecoder_findElementEnd: Unexpected close tag";
@@ -72,9 +72,9 @@
return "ndn_BinaryXMLStructureDecoder_findElementEnd: Can't store more header bytes than the size of headerBuffer";
self->useHeaderBuffer = 1;
unsigned int nNewBytes = self->headerLength - startingHeaderLength;
- memcpy(self->headerBuffer + startingHeaderLength, input + (self->offset - nNewBytes), nNewBytes);
+ ndn_memcpy(self->headerBuffer + startingHeaderLength, input + (self->offset - nNewBytes), nNewBytes);
- return (const char *)0;
+ return (char *)0;
}
unsigned int headerByte = (unsigned int)input[self->offset++];
++self->headerLength;
@@ -90,7 +90,7 @@
if (self->headerLength > sizeof(self->headerBuffer))
return "ndn_BinaryXMLStructureDecoder_findElementEnd: Can't store more header bytes than the size of headerBuffer";
unsigned int nNewBytes = self->headerLength - startingHeaderLength;
- memcpy(self->headerBuffer + startingHeaderLength, input + (self->offset - nNewBytes), nNewBytes);
+ ndn_memcpy(self->headerBuffer + startingHeaderLength, input + (self->offset - nNewBytes), nNewBytes);
// Use a local decoder just for the headerBuffer.
struct ndn_BinaryXMLDecoder bufferDecoder;
@@ -137,7 +137,7 @@
// Need more.
self->offset += nRemainingBytes;
self->nBytesToRead -= nRemainingBytes;
- return (const char *)0;
+ return (char *)0;
}
// Got the bytes. Read a new header or close.
self->offset += self->nBytesToRead;
diff --git a/ndn-cpp/util/ndn_memory.c b/ndn-cpp/util/ndn_memory.c
new file mode 100644
index 0000000..5863d44
--- /dev/null
+++ b/ndn-cpp/util/ndn_memory.c
@@ -0,0 +1,27 @@
+/*
+ * Author: Jeff Thompson
+ *
+ * BSD license, See the LICENSE file for more information.
+ */
+
+#include "ndn_memory.h"
+
+#if !HAVE_MEMCPY
+void ndn_memcpy(unsigned char *dest, unsigned char *src, unsigned int len)
+{
+ unsigned int i;
+
+ for (i = 0; i < len; i++)
+ dest[i] = src[i];
+}
+#endif
+
+#if !HAVE_MEMSET
+void ndn_memset(unsigned char *dest, int val, unsigned int len)
+{
+ unsigned int i;
+
+ for (i = 0; i < len; i++)
+ dest[i] = (unsigned char)val;
+}
+#endif
\ No newline at end of file
diff --git a/ndn-cpp/util/ndn_memory.h b/ndn-cpp/util/ndn_memory.h
new file mode 100644
index 0000000..07bd24c
--- /dev/null
+++ b/ndn-cpp/util/ndn_memory.h
@@ -0,0 +1,51 @@
+/*
+ * Author: Jeff Thompson
+ *
+ * BSD license, See the LICENSE file for more information.
+ */
+
+/*
+ * Based on HAVE_MEMCPY and HAVE_MEMSET in config.h, use the library version or a local implementation of memcpy and memset.
+ */
+
+#ifndef NDN_MEMORY_H
+#define NDN_MEMORY_H
+
+#include "../../config.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if HAVE_MEMCPY
+#include <memory.h>
+/**
+ * Use the library version of memcpy.
+ */
+static inline void ndn_memcpy(unsigned char *dest, unsigned char *src, unsigned int len) { memcpy(dest, src, len); }
+#else
+/**
+ * Use a local implementation of memcpy instead of the library version.
+ */
+void ndn_memcpy(unsigned char *dest, unsigned char *src, unsigned int len);
+#endif
+
+#if HAVE_MEMSET
+#include <memory.h>
+/**
+ * Use the library version of memset.
+ */
+static inline void ndn_memset(unsigned char *dest, int val, unsigned int len) { memset(dest, val, len); }
+#else
+/**
+ * Use a local implementation of memset instead of the library version.
+ */
+void ndn_memset(unsigned char *dest, int val, unsigned int len);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+