Add ability to hex encode binary strings
authorWilfried Goesgens <dothebart@citadel.org>
Sun, 3 Jul 2011 13:47:55 +0000 (13:47 +0000)
committerWilfried Goesgens <dothebart@citadel.org>
Sun, 3 Jul 2011 13:47:55 +0000 (13:47 +0000)
 - when encoding plain md5 binary buffers we have to pass the length into the hex encoder, add parameter
 - add wrapper providing old schematic
 - fix documentation of StrBufSipLine()

libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 936f9117c85328f92e870664a81812307e2ebb46..88ab8d72b4940253bc17179b5184fcc97bc40006 100644 (file)
@@ -332,6 +332,7 @@ int StrBufSanitizeAscii(StrBuf *Buf, const char Mute);
 #define QU                     (3)
 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn);
 void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn);
+void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen);
 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 4e4328916edb2bb86e54740f3ce278e84d4afaed..9318ba66ca3a77fbd2fe91e0d80eb0eccb65977f 100644 (file)
@@ -1803,22 +1803,26 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
  * @param OutBuf the output buffer
  * @param In Buffer to encode
  * @param PlainIn way in from plain old c strings
+ * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
  */
-void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
+void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
 {
-       const char *pch, *pche;
+       const unsigned char *pch, *pche;
        char *pt, *pte;
        int len;
        
        if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
                return;
        if (PlainIn != NULL) {
-               len = strlen(PlainIn);
+               if (PlainInLen < 0)
+                       len = strlen((const char*)PlainIn);
+               else
+                       len = PlainInLen;
                pch = PlainIn;
                pche = pch + len;
        }
        else {
-               pch = In->buf;
+               pch = (const unsigned char*)In->buf;
                pche = pch + In->BufUsed;
                len = In->BufUsed;
        }
@@ -1836,14 +1840,26 @@ void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
                        pt = OutBuf->buf + OutBuf->BufUsed;
                }
 
-               *pt = HexList[(unsigned char)*pch][0];
+               *pt = HexList[*pch][0];
                pt ++;
-               *pt = HexList[(unsigned char)*pch][1];
+               *pt = HexList[*pch][1];
                pt ++; pch ++; OutBuf->BufUsed += 2;
        }
        *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)
+{
+       StrBufHexEscAppend(OutBuf, In, (const unsigned char*) PlainIn, -1);
+}
+
 /**
  * @ingroup StrBuf_DeEnCoder
  * @brief Append a string, escaping characters which have meaning in HTML.  
@@ -4225,7 +4241,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
  * @param Buf BLOB with lines of text...
  * @param Ptr moved arround to keep the next-line across several iterations
  *        has to be &NULL on start; will be &NotNULL on end of buffer
- * @returns size of copied buffer
+ * @returns size of remaining buffer
  */
 int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr)
 {