From: Wilfried Göesgens Date: Sun, 10 Jan 2010 21:12:04 +0000 (+0000) Subject: * add function to append strings encoding them in hex digits; this is basicaly a... X-Git-Tag: v7.86~517 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=ed3a7a1add3b86e9a966e1765bd0e406904bdb88;p=citadel.git * add function to append strings encoding them in hex digits; this is basicaly a stripped down copy of the url escaper. --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 48ba7b589..e43359457 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -321,6 +321,7 @@ int StrBufSanitizeAscii(StrBuf *Buf, const char Mute); #define RB (2) #define QU (3) void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn); +void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn); long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks); long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn); long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 6c068c580..f0da3c55a 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1563,6 +1563,53 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) *pt = '\0'; } +/** + * @ingroup StrBuf_DeEnCoder + * @brief append a string in hex encoding to the buffer + * @param OutBuf the output buffer + * @param In Buffer to encode + * @param PlainIn way in from plain old c strings + */ +void StrBufHexescAppend(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 - 3; /**< 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 - 3; /**< we max append 3 chars at once plus the \0 */ + pt = OutBuf->buf + OutBuf->BufUsed; + } + + *pt = HexList[(unsigned char)*pch][0]; + pt ++; + *pt = HexList[(unsigned char)*pch][1]; + pt ++; pch ++; OutBuf->BufUsed += 2; + } + *pt = '\0'; +} + /** * @ingroup StrBuf_DeEnCoder * @brief Append a string, escaping characters which have meaning in HTML.