From: hw.claudio@gmail.com
To: Luiz Capitulino <lcapitulino@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: Claudio Fontana <claudio.fontana@huawei.com>,
Peter Maydell <peter.maydell@linaro.org>,
Gonglei <arei.gonglei@huawei.com>,
qemu-devel@nongnu.org
Subject: [Qemu-devel] [RFC v6 1/2] util: add memmem replacement function
Date: Mon, 18 May 2015 13:22:17 +0200 [thread overview]
Message-ID: <1431948138-14238-2-git-send-email-hw.claudio@gmail.com> (raw)
In-Reply-To: <1431948138-14238-1-git-send-email-hw.claudio@gmail.com>
From: Claudio Fontana <claudio.fontana@huawei.com>
if the memmem function is missing, provide a trivial replacement.
Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
---
configure | 15 +++++++++++++
include/qemu/osdep.h | 4 ++++
util/Makefile.objs | 1 +
util/memmem.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 82 insertions(+)
create mode 100644 util/memmem.c
diff --git a/configure b/configure
index 1f0f485..feb55b1 100755
--- a/configure
+++ b/configure
@@ -3078,6 +3078,17 @@ if compile_prog "" "" ; then
fi
##########################################
+# memmem probe
+cat > $TMPC <<EOF
+#include <string.h>
+int main(int argc, char *argv[]) { return memmem(argv[0], 0, argv[0], 0) != argv[0]; }
+EOF
+memmem=no
+if compile_prog "" "" ; then
+ memmem=yes
+fi
+
+##########################################
# fdt probe
# fdt support is mandatory for at least some target architectures,
# so insist on it if we're building those system emulators.
@@ -4431,6 +4442,7 @@ echo "RDMA support $rdma"
echo "TCG interpreter $tcg_interpreter"
echo "fdt support $fdt"
echo "preadv support $preadv"
+echo "memmem support $memmem"
echo "fdatasync $fdatasync"
echo "madvise $madvise"
echo "posix_madvise $posix_madvise"
@@ -4780,6 +4792,9 @@ fi
if test "$preadv" = "yes" ; then
echo "CONFIG_PREADV=y" >> $config_host_mak
fi
+if test "$memmem" = "yes" ; then
+ echo "CONFIG_MEMMEM=y" >> $config_host_mak
+fi
if test "$fdt" = "yes" ; then
echo "CONFIG_FDT=y" >> $config_host_mak
fi
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index b3300cc..d74ddff 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -201,6 +201,10 @@ ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
#include <sys/uio.h>
#endif
+#ifndef CONFIG_MEMMEM
+void *memmem(const void *hay, size_t hay_len, const void *s, size_t s_len);
+#endif /* !CONFIG_MEMMEM */
+
#ifdef _WIN32
static inline void qemu_timersub(const struct timeval *val1,
const struct timeval *val2,
diff --git a/util/Makefile.objs b/util/Makefile.objs
index ceaba30..628242f 100644
--- a/util/Makefile.objs
+++ b/util/Makefile.objs
@@ -1,6 +1,7 @@
util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o
util-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o event_notifier-win32.o
util-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o event_notifier-posix.o qemu-openpty.o
+util-obj-$(call lnot,$(CONFIG_MEMMEM)) += memmem.o
util-obj-y += envlist.o path.o module.o
util-obj-$(call lnot,$(CONFIG_INT128)) += host-utils.o
util-obj-y += bitmap.o bitops.o hbitmap.o
diff --git a/util/memmem.c b/util/memmem.c
new file mode 100644
index 0000000..32b58ba
--- /dev/null
+++ b/util/memmem.c
@@ -0,0 +1,62 @@
+/*
+ * memmem replacement function
+ *
+ * Copyright (C) 2015 Huawei Technologies Duesseldorf GmbH
+ * Written by Claudio Fontana <claudio.fontana@huawei.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <qemu-common.h>
+
+/*
+ * Search for the first occurrence of a binary string ("needle")
+ * in a memory region ("haystack").
+ *
+ * If needle length is 0, it returns the pointer to the haystack.
+ * Otherwise it returns the pointer to the first character of the first
+ * occurrence of the needle in the haystack, or NULL if none are found.
+ *
+ */
+void *memmem(const void *haystack, size_t hay_len, const void *needle, size_t s_len)
+{
+ const unsigned char *hay = (const unsigned char *)haystack;
+ const unsigned char *s = (const unsigned char *)needle;
+ const unsigned char *last = hay + (hay_len - s_len);
+
+ if (s_len == 0) {
+ return (void *)hay;
+ }
+
+ if (hay_len < s_len) {
+ return NULL;
+ }
+
+ if (s_len == 1) {
+ return memchr(hay, s[0], hay_len);
+ }
+
+ for (; hay <= last; hay++) {
+ if (hay[0] == s[0] && memcmp(hay, s, s_len) == 0) {
+ return (void *)hay;
+ }
+ }
+
+ return NULL;
+}
--
1.8.5.3
next prev parent reply other threads:[~2015-05-18 11:21 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-18 11:22 [Qemu-devel] [RFC v6 0/2] monitor: add memory search commands s, sp hw.claudio
2015-05-18 11:22 ` hw.claudio [this message]
2015-05-18 14:47 ` [Qemu-devel] [RFC v6 1/2] util: add memmem replacement function Eric Blake
2015-05-18 11:22 ` [Qemu-devel] [RFC v6 2/2] monitor: add memory search commands s, sp hw.claudio
2015-05-28 20:18 ` [Qemu-devel] [RFC v6 0/2] " Luiz Capitulino
2015-06-11 17:53 ` Luiz Capitulino
2015-06-12 6:21 ` Markus Armbruster
2015-06-12 8:35 ` Claudio Fontana
2015-06-12 12:57 ` Markus Armbruster
2015-06-12 13:31 ` Claudio Fontana
2015-06-12 22:31 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1431948138-14238-2-git-send-email-hw.claudio@gmail.com \
--to=hw.claudio@gmail.com \
--cc=arei.gonglei@huawei.com \
--cc=claudio.fontana@huawei.com \
--cc=lcapitulino@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).