char *ret;
unsigned char *d;
const unsigned char *s;
+ int length = 0;
if (!str) {
return NULL;
}
- ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */
+ /* First, compute the space needed for the escaped string.
+ * This could be tweaked a bit for '\b', '\n'... These characters
+ * should not be common, so do not bother. */
+ s = (const unsigned char *)str;
+ for (; *s; ++s) {
+ if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
+ length += 4; /* for '\\' + c2x() */
+ }
+ else {
+ length++;
+ }
+ }
+
+ ret = apr_palloc(p, length + 1);
d = (unsigned char *)ret;
s = (const unsigned char *)str;
for (; *s; ++s) {