* don't use sprintf while doing urlescappend; do it in place.
authorWilfried Göesgens <willi@citadel.org>
Thu, 5 Nov 2009 23:33:40 +0000 (23:33 +0000)
committerWilfried Göesgens <willi@citadel.org>
Thu, 5 Nov 2009 23:33:40 +0000 (23:33 +0000)
libcitadel/lib/stringbuf.c
libcitadel/tests/stringbuf_test.c

index 89d93fa881e72143a8f745993913d04ff0525532..8f37fada7d726a9c41532c3bc70da70b096d2d19 100644 (file)
@@ -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 ++;
index 772cd9a704bbf29242ead5feaba423f3fbed0846..1c094f406f6d7e39f568b07468e65d9438c62cf7 100644 (file)
@@ -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);
 }