From 0337c71cf45e9b646c1cd4cb39426073623267f2 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 11 Dec 2022 09:46:27 +0100 Subject: [PATCH] Backport from xs. --- xs.h | 18 ++++++++++++++++++ xs_httpd.h | 8 +++----- xs_version.h | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/xs.h b/xs.h index 63342db..3758c8d 100644 --- a/xs.h +++ b/xs.h @@ -90,6 +90,7 @@ d_char *xs_number_new(double f); double xs_number_get(const char *v); const char *xs_number_str(const char *v); +void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); #ifdef XS_IMPLEMENTATION @@ -907,6 +908,23 @@ const char *xs_number_str(const char *v) } +void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size) +/* clone of memmem */ +{ + char *p, *r = NULL; + int offset = 0; + + while (!r && h_size - offset > n_size && (p = strchr(haystack + offset, *needle))) { + if (memcmp(p, needle, n_size) == 0) + r = p; + else + offset = p - haystack + 1; + } + + return r; +} + + #endif /* XS_IMPLEMENTATION */ #endif /* _XS_H */ diff --git a/xs_httpd.h b/xs_httpd.h index 89ab57a..454d786 100644 --- a/xs_httpd.h +++ b/xs_httpd.h @@ -69,8 +69,6 @@ d_char *xs_url_vars(char *str) } -void *memmem(const void *, size_t, const void *, size_t); - d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) /* parses a multipart/form-data payload */ { @@ -94,7 +92,7 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) d_char *p_vars = xs_dict_new(); /* iterate searching the boundaries */ - while ((p = memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) { + while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) { xs *s1 = NULL; xs *l1 = NULL; char *vn = NULL; @@ -133,13 +131,13 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) } /* find the start of the part content */ - if ((p = memmem(p, p_size - offset, "\r\n\r\n", 4)) == NULL) + if ((p = xs_memmem(p, p_size - offset, "\r\n\r\n", 4)) == NULL) break; p += 4; /* find the next boundary */ - if ((q = memmem(p, p_size - offset, boundary, bsz)) == NULL) + if ((q = xs_memmem(p, p_size - offset, boundary, bsz)) == NULL) break; po = p - payload; diff --git a/xs_version.h b/xs_version.h index a579266..5b9a4cf 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* ad1c7ba748725abdecc1f1124d697f9130c49e87 */ +/* 3aa82bc4fc310ec95194602bed88a9767e100350 */