diff --git a/client/renderers/EGL/shader.c b/client/renderers/EGL/shader.c index 16c2dd57..6983bc64 100644 --- a/client/renderers/EGL/shader.c +++ b/client/renderers/EGL/shader.c @@ -20,6 +20,7 @@ #include "shader.h" #include "common/debug.h" +#include "common/stringutils.h" #include "util.h" #include @@ -219,18 +220,22 @@ bool egl_shaderCompile(EGL_Shader * this, const char * vertex_code, if (useDMA) { - const char * search = "sampler2D"; - const char * replace = "samplerExternalOES"; + const char search[] = "sampler2D"; + const char replace[] = "samplerExternalOES"; - const char * src = fragment_code; + const char * offset = NULL; int instances = 0; - while((src = strstr(src, search))) + + while((offset = memsearch( + fragment_code, fragment_size, + search , sizeof(search)-1, + offset))) { ++instances; - src += strlen(search); + offset += sizeof(search)-1; } - const int diff = (strlen(replace) - strlen(search)) * instances; + const int diff = (sizeof(replace) - sizeof(search)) * instances; const int newLen = fragment_size + diff; newCode = malloc(newLen + 1); if (!newCode) @@ -239,7 +244,7 @@ bool egl_shaderCompile(EGL_Shader * this, const char * vertex_code, goto exit; } - src = fragment_code; + const char * src = fragment_code; char * dst = newCode; for(int i = 0; i < instances; ++i) { @@ -248,10 +253,10 @@ bool egl_shaderCompile(EGL_Shader * this, const char * vertex_code, memcpy(dst, src, offset); dst += offset; - src = pos + strlen(search); + src = pos + sizeof(search)-1; - memcpy(dst, replace, strlen(replace)); - dst += strlen(replace); + memcpy(dst, replace, sizeof(replace)-1); + dst += sizeof(replace)-1; } const int final = fragment_size - (src - fragment_code); diff --git a/common/include/common/stringutils.h b/common/include/common/stringutils.h index 3afa61fb..e7929837 100644 --- a/common/include/common/stringutils.h +++ b/common/include/common/stringutils.h @@ -37,4 +37,10 @@ bool str_containsValue(const char * list, char delimiter, const char * value); // Local implementation of strdup char * lg_strdup(const char *s); +// search a non null terminated buffer for a value +const char * memsearch( + const char * haystack, size_t haystackSize, + const char * needle , size_t needleSize , + const char * offset); + #endif diff --git a/common/src/stringutils.c b/common/src/stringutils.c index 8e11d760..52aba65a 100644 --- a/common/src/stringutils.c +++ b/common/src/stringutils.c @@ -26,6 +26,7 @@ #include #include "common/stringutils.h" +#include "common/debug.h" int valloc_sprintf(char ** str, const char * format, va_list ap) { @@ -110,3 +111,24 @@ char * lg_strdup(const char *s) memcpy(out, s, len); return out; } + +const char * memsearch( + const char * haystack, size_t haystackSize, + const char * needle , size_t needleSize , + const char * offset) +{ + int i = 0; + if (offset) + { + DEBUG_ASSERT(offset >= haystack); + DEBUG_ASSERT(offset < haystack + haystackSize); + i = offset - haystack; + } + + const int searchSize = haystackSize - needleSize + 1; + for(; i < searchSize; ++i) + if (memcmp(haystack + i, needle, needleSize) == 0) + return haystack + i; + + return NULL; +}