From 8a80c85e7ee3606e746e27387ce80e066bf31575 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Thu, 5 Nov 2009 23:33:40 +0000 Subject: [PATCH] * don't use sprintf while doing urlescappend; do it in place. --- libcitadel/lib/stringbuf.c | 10 +++++++++- libcitadel/tests/stringbuf_test.c | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 89d93fa88..8f37fada7 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -777,7 +777,15 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) } } if (c == 1) { - sprintf(pt,"%%%02X", *pch); + char ch; + ch = *pch; + *pt = '%'; + *(pt + 1) = ((ch & 0xF0) > 0x90) ? + ('A' + ((ch & 0xF0) >> 4) - 0x0A) : + ('0' + ((ch & 0xF0) >> 4)) ; + *(pt + 2) = ((ch & 0x0F) > 0x09) ? + ('A' + (ch & 0x0F) - 0x0A) : + ('0' + (ch & 0x0F)) ; pt += 3; OutBuf->BufUsed += 3; pch ++; diff --git a/libcitadel/tests/stringbuf_test.c b/libcitadel/tests/stringbuf_test.c index 772cd9a70..1c094f406 100644 --- a/libcitadel/tests/stringbuf_test.c +++ b/libcitadel/tests/stringbuf_test.c @@ -298,6 +298,20 @@ static void TestStrBufRemove_token_NotThere(void) FreeStrBuf(&Test); } + +static void TestStrBufUrlescAppend(void) +{ + const char *expect = "%20%2B%23%26%3B%60%27%7C%2A%3F%2D%7E%3C%3E%5E%28%29%5B%5D%7B%7D%2F%24%22%5C"; + StrBuf *In = NewStrBufPlain(HKEY( " +#&;`'|*?-~<>^()[]{}/$\"\\")); + StrBuf *Out = NewStrBuf(); + + StrBufUrlescAppend (Out, In, NULL); + printf ("%s<\n%s<\n%s\n", ChrPtr(In), ChrPtr(Out), expect); + CU_ASSERT_STRING_EQUAL(ChrPtr(Out), expect); + FreeStrBuf(&In); + FreeStrBuf(&Out); +} + /* Some samples from the original... CU_ASSERT_EQUAL(10, 10); @@ -360,6 +374,8 @@ static void AddStrBufSimlpeTests(void) pGroup = CU_add_suite("TestStrBufRemove_token", NULL, NULL); pTest = CU_add_test(pGroup, "TestStrBufRemove_token_NotThere", TestStrBufRemove_token_NotThere); + pGroup = CU_add_suite("TestStrBuf_escapers", NULL, NULL); + pTest = CU_add_test(pGroup, "TestStrBufUrlescAppend", TestStrBufUrlescAppend); } -- 2.39.2