* add function to append strings encoding them in hex digits; this is basicaly a...
authorWilfried Göesgens <willi@citadel.org>
Sun, 10 Jan 2010 21:12:04 +0000 (21:12 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 10 Jan 2010 21:12:04 +0000 (21:12 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 48ba7b5895370e28e53d633b949584c3e8c018b1..e433594570f6f4cd49e07ed29b90dbc71257ae38 100644 (file)
@@ -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);
index 6c068c580d84a936019f45ea82f0b1c16d5cc639..f0da3c55ad411be838e5ac9617cc7da2fe99f229 100644 (file)
@@ -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.