X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=libcitadel%2Flib%2Fstringbuf.c;fp=libcitadel%2Flib%2Fstringbuf.c;h=31e99b52f06c15a477990a726314595cdda34956;hp=892963152b0c653c3f18449c9f51e247f96aef11;hb=0daf8cf6c657b725c2d53776ba766d632456a891;hpb=950fb0db5169e42074bf351f4d2b6ff86d424fca diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 892963152..31e99b52f 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -29,6 +29,7 @@ #include #define SHOW_ME_VAPPEND_PRINTF #include + #ifndef LINUX_SENDFILE #include #endif @@ -1823,6 +1824,66 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) *pt = '\0'; } +/** + * @ingroup StrBuf_DeEnCoder + * @brief Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer + * @param OutBuf the output buffer + * @param In Buffer to encode + * @param PlainIn way in from plain old c strings + */ +void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) +{ + const char *pch, *pche; + char *pt, *pte; + int len; + + if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) ) + return; + if (PlainIn != NULL) { + len = strlen(PlainIn); + pch = PlainIn; + pche = pch + len; + } + else { + pch = In->buf; + pche = pch + In->BufUsed; + len = In->BufUsed; + } + + if (len == 0) + return; + + pt = OutBuf->buf + OutBuf->BufUsed; + pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */ + + while (pch < pche) { + if (pt >= pte) { + IncreaseBuf(OutBuf, 1, -1); + pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */ + pt = OutBuf->buf + OutBuf->BufUsed; + } + + if((*pch >= 'a' && *pch <= 'z') || + (*pch >= 'A' && *pch <= 'Z') || /* A-Z */ + (*pch >= '0' && *pch <= ':') || /* 0-9 : */ + (*pch == '!') || (*pch == '_') || + (*pch == ',') || (*pch == '.')) + { + *(pt++) = *(pch++); + OutBuf->BufUsed++; + } + else { + *pt = '%'; + *(pt + 1) = HexList[(unsigned char)*pch][0]; + *(pt + 2) = HexList[(unsigned char)*pch][1]; + pt += 3; + OutBuf->BufUsed += 3; + pch ++; + } + } + *pt = '\0'; +} + /** * @ingroup StrBuf_DeEnCoder * @brief append a string in hex encoding to the buffer