8 #include <sys/select.h>
10 #include <sys/types.h>
11 #define SHOW_ME_VAPPEND_PRINTF
13 #include "libcitadel.h"
25 int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
26 const Bytef * source, uLong sourceLen, int level);
28 int BaseStrBufSize = 64;
30 const char *StrBufNOTNULL = ((char*) NULL) - 1;
33 * @defgroup StrBuf Stringbuffer, A class for manipulating strings with dynamic buffers
34 * StrBuf is a versatile class, aiding the handling of dynamic strings
35 * * reduce de/reallocations
36 * * reduce the need to remeasure it
37 * * reduce scanning over the string (in @ref StrBuf_NextTokenizer "Tokenizers")
38 * * allow asyncroneous IO for line and Blob based operations
39 * * reduce the use of memove in those
40 * * Quick filling in several operations with append functions
44 * @defgroup StrBuf_DeConstructors Create/Destroy StrBufs
49 * @defgroup StrBuf_Cast Cast operators to interact with char* based code
51 * use these operators to interfere with code demanding char*;
52 * if you need to own the content, smash me. Avoid, since we loose the length information.
56 * @defgroup StrBuf_Filler Create/Replace/Append Content into a StrBuf
58 * operations to get your Strings into a StrBuf, manipulating them, or appending
61 * @defgroup StrBuf_NextTokenizer Fast tokenizer to pull tokens in sequence
63 * Quick tokenizer; demands of the user to pull its tokens in sequence
67 * @defgroup StrBuf_Tokenizer tokenizer Functions; Slow ones.
69 * versatile tokenizer; random access to tokens, but slower; Prefer the @ref StrBuf_NextTokenizer "Next Tokenizer"
73 * @defgroup StrBuf_BufferedIO Buffered IO with Asynchroneous reads and no unneeded memmoves (the fast ones)
75 * File IO to fill StrBufs; Works with work-buffer shared across several calls;
76 * External Cursor to maintain the current read position inside of the buffer
77 * the non-fast ones will use memove to keep the start of the buffer the read buffer (which is slower)
81 * @defgroup StrBuf_IO FileIO; Prefer @ref StrBuf_BufferedIO
87 * @defgroup StrBuf_DeEnCoder functions to translate the contents of a buffer
89 * these functions translate the content of a buffer into another representation;
90 * some are combined Fillers and encoders
94 * Private Structure for the Stringbuffer
97 char *buf; /**< the pointer to the dynamic buffer */
98 long BufSize; /**< how many spcae do we optain */
99 long BufUsed; /**< StNumber of Chars used excluding the trailing \\0 */
100 int ConstBuf; /**< are we just a wrapper arround a static buffer and musn't we be changed? */
102 long nIncreases; /**< for profiling; cound how many times we needed more */
103 char bt [SIZ]; /**< Stacktrace of last increase */
104 char bt_lastinc [SIZ]; /**< How much did we increase last time? */
109 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE);
110 static inline int Ctdl_IsUtf8SequenceStart(const char Char);
113 #ifdef HAVE_BACKTRACE
114 static void StrBufBacktrace(StrBuf *Buf, int which)
118 void *stack_frames[50];
123 pstart = pch = Buf->bt;
125 pstart = pch = Buf->bt_lastinc;
126 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
127 strings = backtrace_symbols(stack_frames, size);
128 for (i = 0; i < size; i++) {
130 n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
132 n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
143 * @ingroup StrBuf_Cast
144 * @brief Cast operator to Plain String
145 * @note if the buffer is altered by StrBuf operations, this pointer may become
146 * invalid. So don't lean on it after altering the buffer!
147 * Since this operation is considered cheap, rather call it often than risking
148 * your pointer to become invalid!
149 * @param Str the string we want to get the c-string representation for
150 * @returns the Pointer to the Content. Don't mess with it!
152 inline const char *ChrPtr(const StrBuf *Str)
160 * @ingroup StrBuf_Cast
161 * @brief since we know strlen()'s result, provide it here.
162 * @param Str the string to return the length to
163 * @returns contentlength of the buffer
165 inline int StrLength(const StrBuf *Str)
167 return (Str != NULL) ? Str->BufUsed : 0;
171 * @ingroup StrBuf_DeConstructors
172 * @brief local utility function to resize the buffer
173 * @param Buf the buffer whichs storage we should increase
174 * @param KeepOriginal should we copy the original buffer or just start over with a new one
175 * @param DestSize what should fit in after?
177 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
180 size_t NewSize = Buf->BufSize * 2;
186 while (NewSize <= DestSize)
189 NewBuf= (char*) malloc(NewSize);
193 if (KeepOriginal && (Buf->BufUsed > 0))
195 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
204 Buf->BufSize = NewSize;
207 #ifdef HAVE_BACKTRACE
208 StrBufBacktrace(Buf, 1);
215 * @ingroup StrBuf_DeConstructors
216 * @brief shrink an _EMPTY_ buffer if its Buffer superseeds threshhold to NewSize. Buffercontent is thoroughly ignored and flushed.
217 * @param Buf Buffer to shrink (has to be empty)
218 * @param ThreshHold if the buffer is bigger then this, its readjusted
219 * @param NewSize if we Shrink it, how big are we going to be afterwards?
221 void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize)
223 if (Buf->BufUsed > ThreshHold) {
225 Buf->buf = (char*) malloc(NewSize);
227 Buf->BufSize = NewSize;
232 * @ingroup StrBuf_DeConstructors
233 * @brief shrink long term buffers to their real size so they don't waste memory
234 * @param Buf buffer to shrink
235 * @param Force if not set, will just executed if the buffer is much to big; set for lifetime strings
236 * @returns physical size of the buffer
238 long StrBufShrinkToFit(StrBuf *Buf, int Force)
241 (Buf->BufUsed + (Buf->BufUsed / 3) > Buf->BufSize))
243 char *TmpBuf = (char*) malloc(Buf->BufUsed + 1);
244 memcpy (TmpBuf, Buf->buf, Buf->BufUsed + 1);
245 Buf->BufSize = Buf->BufUsed + 1;
253 * @ingroup StrBuf_DeConstructors
254 * @brief Allocate a new buffer with default buffer size
255 * @returns the new stringbuffer
257 StrBuf* NewStrBuf(void)
261 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
262 NewBuf->buf = (char*) malloc(BaseStrBufSize);
263 NewBuf->buf[0] = '\0';
264 NewBuf->BufSize = BaseStrBufSize;
266 NewBuf->ConstBuf = 0;
268 NewBuf->nIncreases = 0;
269 NewBuf->bt[0] = '\0';
270 NewBuf->bt_lastinc[0] = '\0';
271 #ifdef HAVE_BACKTRACE
272 StrBufBacktrace(NewBuf, 0);
279 * @ingroup StrBuf_DeConstructors
280 * @brief Copy Constructor; returns a duplicate of CopyMe
281 * @param CopyMe Buffer to faxmilate
282 * @returns the new stringbuffer
284 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
291 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
292 NewBuf->buf = (char*) malloc(CopyMe->BufSize);
293 memcpy(NewBuf->buf, CopyMe->buf, CopyMe->BufUsed + 1);
294 NewBuf->BufUsed = CopyMe->BufUsed;
295 NewBuf->BufSize = CopyMe->BufSize;
296 NewBuf->ConstBuf = 0;
298 NewBuf->nIncreases = 0;
299 NewBuf->bt[0] = '\0';
300 NewBuf->bt_lastinc[0] = '\0';
301 #ifdef HAVE_BACKTRACE
302 StrBufBacktrace(NewBuf, 0);
309 * @ingroup StrBuf_DeConstructors
310 * @brief create a new Buffer using an existing c-string
311 * this function should also be used if you want to pre-suggest
312 * the buffer size to allocate in conjunction with ptr == NULL
313 * @param ptr the c-string to copy; may be NULL to create a blank instance
314 * @param nChars How many chars should we copy; -1 if we should measure the length ourselves
315 * @returns the new stringbuffer
317 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
320 size_t Siz = BaseStrBufSize;
323 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
325 CopySize = strlen((ptr != NULL)?ptr:"");
329 while (Siz <= CopySize)
332 NewBuf->buf = (char*) malloc(Siz);
333 NewBuf->BufSize = Siz;
335 memcpy(NewBuf->buf, ptr, CopySize);
336 NewBuf->buf[CopySize] = '\0';
337 NewBuf->BufUsed = CopySize;
340 NewBuf->buf[0] = '\0';
343 NewBuf->ConstBuf = 0;
345 NewBuf->nIncreases = 0;
346 NewBuf->bt[0] = '\0';
347 NewBuf->bt_lastinc[0] = '\0';
348 #ifdef HAVE_BACKTRACE
349 StrBufBacktrace(NewBuf, 0);
356 * @ingroup StrBuf_DeConstructors
357 * @brief Set an existing buffer from a c-string
358 * @param Buf buffer to load
359 * @param ptr c-string to put into
360 * @param nChars set to -1 if we should work 0-terminated
361 * @returns the new length of the string
363 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
365 size_t Siz = Buf->BufSize;
369 CopySize = strlen(ptr);
373 while (Siz <= CopySize)
376 if (Siz != Buf->BufSize)
377 IncreaseBuf(Buf, 0, Siz);
378 memcpy(Buf->buf, ptr, CopySize);
379 Buf->buf[CopySize] = '\0';
380 Buf->BufUsed = CopySize;
387 * @ingroup StrBuf_DeConstructors
388 * @brief use strbuf as wrapper for a string constant for easy handling
389 * @param StringConstant a string to wrap
390 * @param SizeOfStrConstant should be sizeof(StringConstant)-1
392 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
396 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
397 NewBuf->buf = (char*) StringConstant;
398 NewBuf->BufSize = SizeOfStrConstant;
399 NewBuf->BufUsed = SizeOfStrConstant;
400 NewBuf->ConstBuf = 1;
402 NewBuf->nIncreases = 0;
403 NewBuf->bt[0] = '\0';
404 NewBuf->bt_lastinc[0] = '\0';
411 * @ingroup StrBuf_DeConstructors
412 * @brief flush the content of a Buf; keep its struct
413 * @param buf Buffer to flush
415 int FlushStrBuf(StrBuf *buf)
427 * @ingroup StrBuf_DeConstructors
428 * @brief wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
429 * @param buf Buffer to wipe
431 int FLUSHStrBuf(StrBuf *buf)
437 if (buf->BufUsed > 0) {
438 memset(buf->buf, 0, buf->BufUsed);
445 int hFreeDbglog = -1;
448 * @ingroup StrBuf_DeConstructors
449 * @brief Release a Buffer
450 * Its a double pointer, so it can NULL your pointer
451 * so fancy SIG11 appear instead of random results
452 * @param FreeMe Pointer Pointer to the buffer to free
454 void FreeStrBuf (StrBuf **FreeMe)
459 if (hFreeDbglog == -1){
460 pid_t pid = getpid();
462 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
463 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
465 if ((*FreeMe)->nIncreases > 0)
469 n = snprintf(buf, SIZ * 3, "+|%ld|%ld|%ld|%s|%s|\n",
470 (*FreeMe)->nIncreases,
474 (*FreeMe)->bt_lastinc);
475 n = write(hFreeDbglog, buf, n);
481 n = snprintf(buf, 128, "_|0|%ld%ld|\n",
484 n = write(hFreeDbglog, buf, n);
487 if (!(*FreeMe)->ConstBuf)
488 free((*FreeMe)->buf);
494 * @ingroup StrBuf_DeConstructors
495 * @brief flatten a Buffer to the Char * we return
496 * Its a double pointer, so it can NULL your pointer
497 * so fancy SIG11 appear instead of random results
498 * The Callee then owns the buffer and is responsible for freeing it.
499 * @param SmashMe Pointer Pointer to the buffer to release Buf from and free
500 * @returns the pointer of the buffer; Callee owns the memory thereafter.
502 char *SmashStrBuf (StrBuf **SmashMe)
506 if (*SmashMe == NULL)
509 if (hFreeDbglog == -1){
510 pid_t pid = getpid();
512 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
513 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
515 if ((*SmashMe)->nIncreases > 0)
519 n = snprintf(buf, SIZ * 3, "S+|%ld|%ld|%ld|%s|%s|\n",
520 (*SmashMe)->nIncreases,
524 (*SmashMe)->bt_lastinc);
525 n = write(hFreeDbglog, buf, n);
531 n = snprintf(buf, 128, "S_|0|%ld%ld|\n",
533 (*SmashMe)->BufSize);
534 n = write(hFreeDbglog, buf, n);
537 Ret = (*SmashMe)->buf;
544 * @ingroup StrBuf_DeConstructors
545 * @brief Release the buffer
546 * If you want put your StrBuf into a Hash, use this as Destructor.
547 * @param VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
549 void HFreeStrBuf (void *VFreeMe)
551 StrBuf *FreeMe = (StrBuf*)VFreeMe;
555 if (hFreeDbglog == -1){
556 pid_t pid = getpid();
558 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
559 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
561 if (FreeMe->nIncreases > 0)
565 n = snprintf(buf, SIZ * 3, "+|%ld|%ld|%ld|%s|%s|\n",
571 write(hFreeDbglog, buf, n);
577 n = snprintf(buf, 128, "_|%ld|%ld%ld|\n",
583 if (!FreeMe->ConstBuf)
590 * @brief Wrapper around atol
592 long StrTol(const StrBuf *Buf)
597 return atol(Buf->buf);
604 * @brief Wrapper around atoi
606 int StrToi(const StrBuf *Buf)
610 if (Buf->BufUsed > 0)
611 return atoi(Buf->buf);
618 * @brief Checks to see if the string is a pure number
620 int StrBufIsNumber(const StrBuf *Buf) {
625 strtoll(Buf->buf, &pEnd, 10);
626 if (pEnd == NULL && ((Buf->buf)-pEnd) != 0) {
633 * @brief modifies a Single char of the Buf
634 * You can point to it via char* or a zero-based integer
635 * @param Buf The buffer to manipulate
636 * @param ptr char* to zero; use NULL if unused
637 * @param nThChar zero based pointer into the string; use -1 if unused
638 * @param PeekValue The Character to place into the position
640 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
645 nThChar = ptr - Buf->buf;
646 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
648 Buf->buf[nThChar] = PeekValue;
654 * @brief Append a StringBuffer to the buffer
655 * @param Buf Buffer to modify
656 * @param AppendBuf Buffer to copy at the end of our buffer
657 * @param Offset Should we start copying from an offset?
659 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
661 if ((AppendBuf == NULL) || (Buf == NULL) || (AppendBuf->buf == NULL))
664 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1)
667 AppendBuf->BufUsed + Buf->BufUsed);
669 memcpy(Buf->buf + Buf->BufUsed,
670 AppendBuf->buf + Offset,
671 AppendBuf->BufUsed - Offset);
672 Buf->BufUsed += AppendBuf->BufUsed - Offset;
673 Buf->buf[Buf->BufUsed] = '\0';
679 * @brief Append a C-String to the buffer
680 * @param Buf Buffer to modify
681 * @param AppendBuf Buffer to copy at the end of our buffer
682 * @param AppendSize number of bytes to copy; set to -1 if we should count it in advance
683 * @param Offset Should we start copying from an offset?
685 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset)
688 long BufSizeRequired;
690 if ((AppendBuf == NULL) || (Buf == NULL))
694 aps = strlen(AppendBuf + Offset);
696 aps = AppendSize - Offset;
698 BufSizeRequired = Buf->BufUsed + aps + 1;
699 if (Buf->BufSize <= BufSizeRequired)
700 IncreaseBuf(Buf, (Buf->BufUsed > 0), BufSizeRequired);
702 memcpy(Buf->buf + Buf->BufUsed,
706 Buf->buf[Buf->BufUsed] = '\0';
711 * @brief Callback for cURL to append the webserver reply to a buffer
712 * @param ptr pre-defined by the cURL API; see man 3 curl for mre info
713 * @param size pre-defined by the cURL API; see man 3 curl for mre info
714 * @param nmemb pre-defined by the cURL API; see man 3 curl for mre info
715 * @param stream pre-defined by the cURL API; see man 3 curl for mre info
717 size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
726 StrBufAppendBufPlain(Target, ptr, size * nmemb, 0);
732 * @ingroup StrBuf_DeEnCoder
733 * @brief Escape a string for feeding out as a URL while appending it to a Buffer
734 * @param OutBuf the output buffer
735 * @param In Buffer to encode
736 * @param PlainIn way in from plain old c strings
738 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
740 const char *pch, *pche;
743 const char ec[] = " +#&;`'|*?-~<>^()[]{}/$\"\\";
744 int eclen = sizeof(ec) -1;
746 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
748 if (PlainIn != NULL) {
749 len = strlen(PlainIn);
755 pche = pch + In->BufUsed;
762 pt = OutBuf->buf + OutBuf->BufUsed;
763 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
767 IncreaseBuf(OutBuf, 1, -1);
768 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
769 pt = OutBuf->buf + OutBuf->BufUsed;
773 for (b = 0; b < eclen; ++b) {
780 sprintf(pt,"%%%02X", *pch);
782 OutBuf->BufUsed += 3;
794 * @ingroup StrBuf_DeEnCoder
795 * @brief Append a string, escaping characters which have meaning in HTML.
797 * @param Target target buffer
798 * @param Source source buffer; set to NULL if you just have a C-String
799 * @param PlainIn Plain-C string to append; set to NULL if unused
800 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
801 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
802 * if set to 2, linebreaks are replaced by <br/>
804 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
806 const char *aptr, *eiptr;
810 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
813 if (PlainIn != NULL) {
815 len = strlen(PlainIn);
820 eiptr = aptr + Source->BufUsed;
821 len = Source->BufUsed;
827 bptr = Target->buf + Target->BufUsed;
828 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
830 while (aptr < eiptr){
832 IncreaseBuf(Target, 1, -1);
833 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
834 bptr = Target->buf + Target->BufUsed;
837 memcpy(bptr, "<", 4);
839 Target->BufUsed += 4;
841 else if (*aptr == '>') {
842 memcpy(bptr, ">", 4);
844 Target->BufUsed += 4;
846 else if (*aptr == '&') {
847 memcpy(bptr, "&", 5);
849 Target->BufUsed += 5;
851 else if (*aptr == '"') {
852 memcpy(bptr, """, 6);
854 Target->BufUsed += 6;
856 else if (*aptr == '\'') {
857 memcpy(bptr, "'", 5);
859 Target->BufUsed += 5;
861 else if (*aptr == LB) {
866 else if (*aptr == RB) {
871 else if (*aptr == QU) {
876 else if ((*aptr == 32) && (nbsp == 1)) {
877 memcpy(bptr, " ", 6);
879 Target->BufUsed += 6;
881 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
882 *bptr='\0'; /* nothing */
884 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
885 memcpy(bptr, "<br/>", 11);
887 Target->BufUsed += 11;
891 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
892 *bptr='\0'; /* nothing */
902 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
904 return Target->BufUsed;
908 * @ingroup StrBuf_DeEnCoder
909 * @brief Append a string, escaping characters which have meaning in HTML.
910 * Converts linebreaks into blanks; escapes single quotes
911 * @param Target target buffer
912 * @param Source source buffer; set to NULL if you just have a C-String
913 * @param PlainIn Plain-C string to append; set to NULL if unused
915 void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
917 const char *aptr, *eiptr;
921 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
924 if (PlainIn != NULL) {
926 len = strlen(PlainIn);
931 eiptr = aptr + Source->BufUsed;
932 len = Source->BufUsed;
938 eptr = Target->buf + Target->BufSize - 8;
939 tptr = Target->buf + Target->BufUsed;
941 while (aptr < eiptr){
943 IncreaseBuf(Target, 1, -1);
944 eptr = Target->buf + Target->BufSize - 8;
945 tptr = Target->buf + Target->BufUsed;
952 else if (*aptr == '\r') {
956 else if (*aptr == '\'') {
962 Target->BufUsed += 5;
975 * @ingroup StrBuf_DeEnCoder
976 * @brief Append a string, escaping characters which have meaning in ICAL.
978 * @param Target target buffer
979 * @param Source source buffer; set to NULL if you just have a C-String
980 * @param PlainIn Plain-C string to append; set to NULL if unused
982 void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
984 const char *aptr, *eiptr;
988 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
991 if (PlainIn != NULL) {
993 len = strlen(PlainIn);
998 eiptr = aptr + Source->BufUsed;
999 len = Source->BufUsed;
1005 eptr = Target->buf + Target->BufSize - 8;
1006 tptr = Target->buf + Target->BufUsed;
1008 while (aptr < eiptr){
1009 if(tptr + 3 >= eptr) {
1010 IncreaseBuf(Target, 1, -1);
1011 eptr = Target->buf + Target->BufSize - 8;
1012 tptr = Target->buf + Target->BufUsed;
1015 if (*aptr == '\n') {
1022 else if (*aptr == '\r') {
1029 else if (*aptr == ',') {
1045 * @ingroup StrBuf_DeEnCoder
1046 * @brief Append a string, escaping characters which have meaning in JavaScript strings .
1048 * @param Target target buffer
1049 * @param Source source buffer; set to NULL if you just have a C-String
1050 * @param PlainIn Plain-C string to append; set to NULL if unused
1051 * @returns size of result or -1
1053 long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1055 const char *aptr, *eiptr;
1059 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1062 if (PlainIn != NULL) {
1064 len = strlen(PlainIn);
1069 eiptr = aptr + Source->BufUsed;
1070 len = Source->BufUsed;
1076 bptr = Target->buf + Target->BufUsed;
1077 eptr = Target->buf + Target->BufSize - 3; /* our biggest unit to put in... */
1079 while (aptr < eiptr){
1081 IncreaseBuf(Target, 1, -1);
1082 eptr = Target->buf + Target->BufSize - 3;
1083 bptr = Target->buf + Target->BufUsed;
1090 Target->BufUsed += 2;
1091 } else if (*aptr == '\\') {
1096 Target->BufUsed += 2;
1106 if ((bptr == eptr - 1 ) && !IsEmptyStr(aptr) )
1108 return Target->BufUsed;
1112 * @ingroup StrBuf_DeEnCoder
1113 * @brief Append a string, escaping characters which have meaning in HTML + json.
1115 * @param Target target buffer
1116 * @param Source source buffer; set to NULL if you just have a C-String
1117 * @param PlainIn Plain-C string to append; set to NULL if unused
1118 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
1119 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
1120 * if set to 2, linebreaks are replaced by <br/>
1122 long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
1124 const char *aptr, *eiptr;
1127 int IsUtf8Sequence = 0;
1129 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1132 if (PlainIn != NULL) {
1134 len = strlen(PlainIn);
1139 eiptr = aptr + Source->BufUsed;
1140 len = Source->BufUsed;
1146 bptr = Target->buf + Target->BufUsed;
1147 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1149 while (aptr < eiptr){
1151 IncreaseBuf(Target, 1, -1);
1152 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1153 bptr = Target->buf + Target->BufUsed;
1156 memcpy(bptr, "<", 4);
1158 Target->BufUsed += 4;
1160 else if (*aptr == '>') {
1161 memcpy(bptr, ">", 4);
1163 Target->BufUsed += 4;
1165 else if (*aptr == '&') {
1166 memcpy(bptr, "&", 5);
1168 Target->BufUsed += 5;
1170 else if (*aptr == LB) {
1175 else if (*aptr == RB) {
1180 else if ((*aptr == 32) && (nbsp == 1)) {
1181 memcpy(bptr, " ", 6);
1183 Target->BufUsed += 6;
1185 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
1186 *bptr='\0'; /* nothing */
1188 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
1189 memcpy(bptr, "<br/>", 11);
1191 Target->BufUsed += 11;
1194 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
1195 *bptr='\0'; /* nothing */
1198 else if ((*aptr == '"') || (*aptr == QU)) {
1203 Target->BufUsed += 2;
1204 } else if (*aptr == '\\') {
1209 Target->BufUsed += 2;
1212 if (IsUtf8Sequence != 0) {
1221 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
1233 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
1235 return Target->BufUsed;
1241 * @brief extracts a substring from Source into dest
1242 * @param dest buffer to place substring into
1243 * @param Source string to copy substring from
1244 * @param Offset chars to skip from start
1245 * @param nChars number of chars to copy
1246 * @returns the number of chars copied; may be different from nChars due to the size of Source
1248 int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
1250 size_t NCharsRemain;
1251 if (Offset > Source->BufUsed)
1256 if (Offset + nChars < Source->BufUsed)
1258 if (nChars >= dest->BufSize)
1259 IncreaseBuf(dest, 0, nChars + 1);
1260 memcpy(dest->buf, Source->buf + Offset, nChars);
1261 dest->BufUsed = nChars;
1262 dest->buf[dest->BufUsed] = '\0';
1265 NCharsRemain = Source->BufUsed - Offset;
1266 if (NCharsRemain >= dest->BufSize)
1267 IncreaseBuf(dest, 0, NCharsRemain + 1);
1268 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
1269 dest->BufUsed = NCharsRemain;
1270 dest->buf[dest->BufUsed] = '\0';
1271 return NCharsRemain;
1276 * @brief sprintf like function appending the formated string to the buffer
1277 * vsnprintf version to wrap into own calls
1278 * @param Buf Buffer to extend by format and Params
1279 * @param format printf alike format to add
1280 * @param ap va_list containing the items for format
1282 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
1290 if ((Buf == NULL) || (format == NULL))
1293 BufSize = Buf->BufSize;
1294 nWritten = Buf->BufSize + 1;
1295 Offset = Buf->BufUsed;
1296 newused = Offset + nWritten;
1298 while (newused >= BufSize) {
1300 nWritten = vsnprintf(Buf->buf + Offset,
1301 Buf->BufSize - Offset,
1304 newused = Offset + nWritten;
1305 if (newused >= Buf->BufSize) {
1306 IncreaseBuf(Buf, 1, newused);
1307 newused = Buf->BufSize + 1;
1310 Buf->BufUsed = Offset + nWritten;
1311 BufSize = Buf->BufSize;
1319 * @brief sprintf like function appending the formated string to the buffer
1320 * @param Buf Buffer to extend by format and Params
1321 * @param format printf alike format to add
1323 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
1331 if ((Buf == NULL) || (format == NULL))
1334 BufSize = Buf->BufSize;
1335 nWritten = Buf->BufSize + 1;
1336 Offset = Buf->BufUsed;
1337 newused = Offset + nWritten;
1339 while (newused >= BufSize) {
1340 va_start(arg_ptr, format);
1341 nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
1342 Buf->BufSize - Buf->BufUsed,
1345 newused = Buf->BufUsed + nWritten;
1346 if (newused >= Buf->BufSize) {
1347 IncreaseBuf(Buf, 1, newused);
1348 newused = Buf->BufSize + 1;
1351 Buf->BufUsed += nWritten;
1352 BufSize = Buf->BufSize;
1360 * @brief sprintf like function putting the formated string into the buffer
1361 * @param Buf Buffer to extend by format and Parameters
1362 * @param format printf alike format to add
1364 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
1369 if ((Buf == NULL) || (format == NULL))
1372 nWritten = Buf->BufSize + 1;
1373 while (nWritten >= Buf->BufSize) {
1374 va_start(arg_ptr, format);
1375 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
1377 if (nWritten >= Buf->BufSize) {
1378 IncreaseBuf(Buf, 0, 0);
1379 nWritten = Buf->BufSize + 1;
1382 Buf->BufUsed = nWritten ;
1388 * @ingroup StrBuf_Tokenizer
1389 * @brief Counts the numbmer of tokens in a buffer
1390 * @param source String to count tokens in
1391 * @param tok Tokenizer char to count
1392 * @returns numbers of tokenizer chars found
1394 int StrBufNum_tokens(const StrBuf *source, char tok)
1398 return num_tokens(source->buf, tok);
1402 * remove_token() - a tokenizer that kills, maims, and destroys
1405 * @ingroup StrBuf_Tokenizer
1406 * @brief a string tokenizer
1407 * @param Source StringBuffer to read into
1408 * @param parmnum n'th Parameter to remove
1409 * @param separator tokenizer character
1410 * @returns -1 if not found, else length of token.
1412 int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
1415 char *d, *s, *end; /* dest, source */
1418 /* Find desired @parameter */
1419 end = Source->buf + Source->BufUsed;
1421 while ((count < parmnum) &&
1424 /* End of string, bail! */
1429 if (*d == separator) {
1434 if ((d == NULL) || (d >= end))
1435 return 0; /* @Parameter not found */
1437 /* Find next @parameter */
1439 while ((*s && *s != separator) &&
1444 if (*s == separator)
1448 /* Hack and slash */
1450 memmove(d, s, Source->BufUsed - (s - Source->buf));
1451 Source->BufUsed += ReducedBy;
1453 else if (d == Source->buf) {
1455 Source->BufUsed = 0;
1459 Source->BufUsed += ReducedBy;
1472 * @ingroup StrBuf_Tokenizer
1473 * @brief a string tokenizer
1474 * @param dest Destination StringBuffer
1475 * @param Source StringBuffer to read into
1476 * @param parmnum n'th Parameter to extract
1477 * @param separator tokenizer character
1478 * @returns -1 if not found, else length of token.
1480 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
1482 const char *s, *e; //* source * /
1483 int len = 0; //* running total length of extracted string * /
1484 int current_token = 0; //* token currently being processed * /
1487 dest->buf[0] = '\0';
1493 if ((Source == NULL) || (Source->BufUsed ==0)) {
1497 e = s + Source->BufUsed;
1500 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1502 while ((s<e) && !IsEmptyStr(s)) {
1503 if (*s == separator) {
1506 if (len >= dest->BufSize) {
1507 dest->BufUsed = len;
1508 if (IncreaseBuf(dest, 1, -1) < 0) {
1513 if ( (current_token == parmnum) &&
1514 (*s != separator)) {
1515 dest->buf[len] = *s;
1518 else if (current_token > parmnum) {
1524 dest->buf[len] = '\0';
1525 dest->BufUsed = len;
1527 if (current_token < parmnum) {
1528 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
1531 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
1540 * @ingroup StrBuf_Tokenizer
1541 * @brief a string tokenizer to fetch an integer
1542 * @param Source String containing tokens
1543 * @param parmnum n'th Parameter to extract
1544 * @param separator tokenizer character
1545 * @returns 0 if not found, else integer representation of the token
1547 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
1557 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1564 * @ingroup StrBuf_Tokenizer
1565 * @brief a string tokenizer to fetch a long integer
1566 * @param Source String containing tokens
1567 * @param parmnum n'th Parameter to extract
1568 * @param separator tokenizer character
1569 * @returns 0 if not found, else long integer representation of the token
1571 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
1581 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1589 * @ingroup StrBuf_Tokenizer
1590 * @brief a string tokenizer to fetch an unsigned long
1591 * @param Source String containing tokens
1592 * @param parmnum n'th Parameter to extract
1593 * @param separator tokenizer character
1594 * @returns 0 if not found, else unsigned long representation of the token
1596 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
1607 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0) {
1611 return (unsigned long) atol(pnum);
1620 * @ingroup StrBuf_NextTokenizer
1621 * @brief a string tokenizer; Bounds checker
1622 * function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
1623 * @param Source our tokenbuffer
1624 * @param pStart the token iterator pointer to inspect
1625 * @returns whether the revolving pointer is inside of the search range
1627 int StrBufHaveNextToken(const StrBuf *Source, const char **pStart)
1629 if ((Source == NULL) ||
1630 (*pStart == StrBufNOTNULL) ||
1631 (Source->BufUsed == 0))
1635 if (*pStart == NULL)
1639 else if (*pStart > Source->buf + Source->BufUsed)
1643 else if (*pStart <= Source->buf)
1652 * @ingroup StrBuf_NextTokenizer
1653 * @brief a string tokenizer
1654 * @param dest Destination StringBuffer
1655 * @param Source StringBuffer to read into
1656 * @param pStart pointer to the end of the last token. Feed with NULL on start.
1657 * @param separator tokenizer
1658 * @returns -1 if not found, else length of token.
1660 int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
1662 const char *s; /* source */
1663 const char *EndBuffer; /* end stop of source buffer */
1664 int current_token = 0; /* token currently being processed */
1665 int len = 0; /* running total length of extracted string */
1667 if ((Source == NULL) ||
1668 (Source->BufUsed == 0) )
1670 *pStart = StrBufNOTNULL;
1674 EndBuffer = Source->buf + Source->BufUsed;
1678 dest->buf[0] = '\0';
1683 *pStart = EndBuffer + 1;
1687 if (*pStart == NULL)
1689 *pStart = Source->buf; /* we're starting to examine this buffer. */
1691 else if ((*pStart < Source->buf) ||
1692 (*pStart > EndBuffer ) )
1694 return -1; /* no more tokens to find. */
1698 /* start to find the next token */
1699 while ((s <= EndBuffer) &&
1700 (current_token == 0) )
1702 if (*s == separator)
1704 /* we found the next token */
1708 if (len >= dest->BufSize)
1710 /* our Dest-buffer isn't big enough, increase it. */
1711 dest->BufUsed = len;
1713 if (IncreaseBuf(dest, 1, -1) < 0) {
1714 /* WHUT? no more mem? bail out. */
1721 if ( (current_token == 0 ) && /* are we in our target token? */
1722 (!IsEmptyStr(s) ) &&
1723 (separator != *s) ) /* don't copy the token itself */
1725 dest->buf[len] = *s; /* Copy the payload */
1726 ++len; /* remember the bigger size. */
1732 /* did we reach the end? */
1733 if ((s > EndBuffer)) {
1734 EndBuffer = StrBufNOTNULL;
1735 *pStart = EndBuffer;
1738 *pStart = s; /* remember the position for the next run */
1741 /* sanitize our extracted token */
1742 dest->buf[len] = '\0';
1743 dest->BufUsed = len;
1750 * @ingroup StrBuf_NextTokenizer
1751 * @brief a string tokenizer
1752 * @param Source StringBuffer to read from
1753 * @param pStart pointer to the end of the last token. Feed with NULL.
1754 * @param separator tokenizer character
1755 * @param nTokens number of tokens to fastforward over
1756 * @returns -1 if not found, else length of token.
1758 int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens)
1760 const char *s, *EndBuffer; //* source * /
1761 int len = 0; //* running total length of extracted string * /
1762 int current_token = 0; //* token currently being processed * /
1764 if ((Source == NULL) ||
1765 (Source->BufUsed ==0)) {
1769 return Source->BufUsed;
1771 if (*pStart == NULL)
1772 *pStart = Source->buf;
1774 EndBuffer = Source->buf + Source->BufUsed;
1776 if ((*pStart < Source->buf) ||
1777 (*pStart > EndBuffer)) {
1785 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1787 while ((s<EndBuffer) && !IsEmptyStr(s)) {
1788 if (*s == separator) {
1791 if (current_token >= nTokens) {
1803 * @ingroup StrBuf_NextTokenizer
1804 * @brief a string tokenizer to fetch an integer
1805 * @param Source StringBuffer to read from
1806 * @param pStart Cursor on the tokenstring
1807 * @param separator tokenizer character
1808 * @returns 0 if not found, else integer representation of the token
1810 int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator)
1820 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1827 * @ingroup StrBuf_NextTokenizer
1828 * @brief a string tokenizer to fetch a long integer
1829 * @param Source StringBuffer to read from
1830 * @param pStart Cursor on the tokenstring
1831 * @param separator tokenizer character
1832 * @returns 0 if not found, else long integer representation of the token
1834 long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator)
1844 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1852 * @ingroup StrBuf_NextTokenizer
1853 * @brief a string tokenizer to fetch an unsigned long
1854 * @param Source StringBuffer to read from
1855 * @param pStart Cursor on the tokenstring
1856 * @param separator tokenizer character
1857 * @returns 0 if not found, else unsigned long representation of the token
1859 unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator)
1870 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) {
1874 return (unsigned long) atol(pnum);
1883 * @ingroup StrBuf_IO
1884 * @brief Read a line from socket
1885 * flushes and closes the FD on error
1886 * @param buf the buffer to get the input to
1887 * @param fd pointer to the filedescriptor to read
1888 * @param append Append to an existing string or replace?
1889 * @param Error strerror() on error
1890 * @returns numbers of chars read
1892 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
1894 int len, rlen, slen;
1899 slen = len = buf->BufUsed;
1901 rlen = read(*fd, &buf->buf[len], 1);
1903 *Error = strerror(errno);
1910 if (buf->buf[len] == '\n')
1912 if (buf->buf[len] != '\r')
1914 if (len + 2 >= buf->BufSize) {
1916 buf->buf[len+1] = '\0';
1917 IncreaseBuf(buf, 1, -1);
1921 buf->buf[len] = '\0';
1926 * @ingroup StrBuf_BufferedIO
1927 * @brief Read a line from socket
1928 * flushes and closes the FD on error
1929 * @param Line the line to read from the fd / I/O Buffer
1930 * @param buf the buffer to get the input to
1931 * @param fd pointer to the filedescriptor to read
1932 * @param timeout number of successless selects until we bail out
1933 * @param selectresolution how long to wait on each select
1934 * @param Error strerror() on error
1935 * @returns numbers of chars read
1937 int StrBufTCP_read_buffered_line(StrBuf *Line,
1941 int selectresolution,
1945 int nSuccessLess = 0;
1952 if (buf->BufUsed > 0) {
1953 pch = strchr(buf->buf, '\n');
1956 len = pch - buf->buf;
1957 if (len > 0 && (*(pch - 1) == '\r') )
1959 StrBufSub(Line, buf, 0, len - rlen);
1960 StrBufCutLeft(buf, len + 1);
1965 if (buf->BufSize - buf->BufUsed < 10)
1966 IncreaseBuf(buf, 1, -1);
1968 fdflags = fcntl(*fd, F_GETFL);
1969 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
1971 while ((nSuccessLess < timeout) && (pch == NULL)) {
1973 tv.tv_sec = selectresolution;
1978 if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
1979 *Error = strerror(errno);
1985 if (IsNonBlock && ! FD_ISSET(*fd, &rfds)) {
1990 &buf->buf[buf->BufUsed],
1991 buf->BufSize - buf->BufUsed - 1);
1993 *Error = strerror(errno);
1998 else if (rlen > 0) {
2000 buf->BufUsed += rlen;
2001 buf->buf[buf->BufUsed] = '\0';
2002 if (buf->BufUsed + 10 > buf->BufSize) {
2003 IncreaseBuf(buf, 1, -1);
2005 pch = strchr(buf->buf, '\n');
2012 len = pch - buf->buf;
2013 if (len > 0 && (*(pch - 1) == '\r') )
2015 StrBufSub(Line, buf, 0, len - rlen);
2016 StrBufCutLeft(buf, len + 1);
2023 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
2024 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
2026 * @ingroup StrBuf_BufferedIO
2027 * @brief Read a line from socket
2028 * flushes and closes the FD on error
2029 * @param Line Line to read from the fd / I/O Buffer
2030 * @param IOBuf the buffer to get the input to
2031 * @param Pos pointer to the current read position, should be NULL initialized!
2032 * @param fd pointer to the filedescriptor to read
2033 * @param timeout number of successless selects until we bail out
2034 * @param selectresolution how long to wait on each select
2035 * @param Error strerror() on error
2036 * @returns numbers of chars read
2038 int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
2043 int selectresolution,
2046 const char *pche = NULL;
2047 const char *pos = NULL;
2049 int nSuccessLess = 0;
2051 const char *pch = NULL;
2057 if ((IOBuf->BufUsed > 0) &&
2059 (pos < IOBuf->buf + IOBuf->BufUsed))
2061 pche = IOBuf->buf + IOBuf->BufUsed;
2063 while ((pch < pche) && (*pch != '\n'))
2065 if ((pch >= pche) || (*pch == '\0'))
2067 if ((pch != NULL) &&
2072 if (len > 0 && (*(pch - 1) == '\r') )
2074 StrBufSub(Line, IOBuf, (pos - IOBuf->buf), len - rlen);
2084 StrBufCutLeft(IOBuf, (pos - IOBuf->buf));
2088 if (IOBuf->BufSize - IOBuf->BufUsed < 10) {
2089 IncreaseBuf(IOBuf, 1, -1);
2092 fdflags = fcntl(*fd, F_GETFL);
2093 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
2096 while ((nSuccessLess < timeout) &&
2106 if (select((*fd) + 1, &rfds, NULL, NULL, &tv) == -1) {
2107 *Error = strerror(errno);
2111 *Error = ErrRBLF_SelectFailed;
2114 if (! FD_ISSET(*fd, &rfds) != 0) {
2120 &IOBuf->buf[IOBuf->BufUsed],
2121 IOBuf->BufSize - IOBuf->BufUsed - 1);
2123 *Error = strerror(errno);
2128 else if (rlen > 0) {
2130 IOBuf->BufUsed += rlen;
2131 IOBuf->buf[IOBuf->BufUsed] = '\0';
2132 if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
2133 IncreaseBuf(IOBuf, 1, -1);
2136 pche = IOBuf->buf + IOBuf->BufUsed;
2138 while ((pch < pche) && (*pch != '\n'))
2140 if ((pch >= pche) || (*pch == '\0'))
2149 if (len > 0 && (*(pch - 1) == '\r') )
2151 StrBufSub(Line, IOBuf, 0, len - rlen);
2152 *Pos = pos + len + 1;
2155 *Error = ErrRBLF_NotEnoughSentFromServer;
2161 * @ingroup StrBuf_IO
2162 * @brief Input binary data from socket
2163 * flushes and closes the FD on error
2164 * @param Buf the buffer to get the input to
2165 * @param fd pointer to the filedescriptor to read
2166 * @param append Append to an existing string or replace?
2167 * @param nBytes the maximal number of bytes to read
2168 * @param Error strerror() on error
2169 * @returns numbers of chars read
2171 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
2174 int len, rlen, slen;
2181 if ((Buf == NULL) || (*fd == -1))
2185 if (Buf->BufUsed + nBytes >= Buf->BufSize)
2186 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
2188 ptr = Buf->buf + Buf->BufUsed;
2190 slen = len = Buf->BufUsed;
2192 fdflags = fcntl(*fd, F_GETFL);
2193 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
2195 while ((nRead < nBytes) &&
2205 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
2206 *Error = strerror(errno);
2210 *Error = ErrRBLF_SelectFailed;
2213 if (! FD_ISSET(*fd, &rfds) != 0) {
2219 if ((rlen = read(*fd,
2221 nBytes - nRead)) == -1) {
2224 *Error = strerror(errno);
2229 Buf->BufUsed += rlen;
2231 Buf->buf[Buf->BufUsed] = '\0';
2235 const char *ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting.";
2237 * @ingroup StrBuf_BufferedIO
2238 * @brief Input binary data from socket
2239 * flushes and closes the FD on error
2240 * @param Blob put binary thing here
2241 * @param IOBuf the buffer to get the input to
2242 * @param Pos offset inside of IOBuf
2243 * @param fd pointer to the filedescriptor to read
2244 * @param append Append to an existing string or replace?
2245 * @param nBytes the maximal number of bytes to read
2246 * @param check whether we should search for '000\n' terminators in case of timeouts
2247 * @param Error strerror() on error
2248 * @returns numbers of chars read
2250 int StrBufReadBLOBBuffered(StrBuf *Blob,
2267 int nAlreadyRead = 0;
2275 if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL))
2279 if (Blob->BufUsed + nBytes >= Blob->BufSize)
2280 IncreaseBuf(Blob, append, Blob->BufUsed + nBytes);
2285 len = pos - IOBuf->buf;
2286 rlen = IOBuf->BufUsed - len;
2289 if ((IOBuf->BufUsed > 0) &&
2291 (pos < IOBuf->buf + IOBuf->BufUsed))
2293 pche = IOBuf->buf + IOBuf->BufUsed;
2296 if (rlen < nBytes) {
2297 memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
2298 Blob->BufUsed += rlen;
2299 Blob->buf[Blob->BufUsed] = '\0';
2300 nAlreadyRead = nRead = rlen;
2303 if (rlen >= nBytes) {
2304 memcpy(Blob->buf + Blob->BufUsed, pos, nBytes);
2305 Blob->BufUsed += nBytes;
2306 Blob->buf[Blob->BufUsed] = '\0';
2307 if (rlen == nBytes) {
2318 if (IOBuf->BufSize < nBytes - nRead)
2319 IncreaseBuf(IOBuf, 0, nBytes - nRead);
2322 slen = len = Blob->BufUsed;
2324 fdflags = fcntl(*fd, F_GETFL);
2325 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
2330 while ((nRead < nBytes) &&
2339 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
2340 *Error = strerror(errno);
2344 *Error = ErrRBLF_SelectFailed;
2347 if (! FD_ISSET(*fd, &rfds) != 0) {
2359 *Error = strerror(errno);
2362 else if (rlen == 0){
2364 if ((check == NNN_TERM) &&
2366 (strncmp(IOBuf->buf + IOBuf->BufUsed - 5, "\n000\n", 5) == 0))
2368 StrBufPlain(Blob, HKEY("\n000\n"));
2369 StrBufCutRight(Blob, 5);
2370 return Blob->BufUsed;
2372 if (nSelects > 10) {
2374 *Error = ErrRBB_too_many_selects;
2378 else if (rlen > 0) {
2381 IOBuf->BufUsed += rlen;
2384 if (nRead > nBytes) {
2385 *Pos = IOBuf->buf + nBytes;
2387 Blob->buf[Blob->BufUsed] = '\0';
2388 StrBufAppendBufPlain(Blob, IOBuf->buf, nBytes, 0);
2392 return nRead + nAlreadyRead;
2397 * @brief Cut nChars from the start of the string
2398 * @param Buf Buffer to modify
2399 * @param nChars how many chars should be skipped?
2401 void StrBufCutLeft(StrBuf *Buf, int nChars)
2403 if (nChars >= Buf->BufUsed) {
2407 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
2408 Buf->BufUsed -= nChars;
2409 Buf->buf[Buf->BufUsed] = '\0';
2414 * @brief Cut the trailing n Chars from the string
2415 * @param Buf Buffer to modify
2416 * @param nChars how many chars should be trunkated?
2418 void StrBufCutRight(StrBuf *Buf, int nChars)
2420 if (nChars >= Buf->BufUsed) {
2424 Buf->BufUsed -= nChars;
2425 Buf->buf[Buf->BufUsed] = '\0';
2430 * @brief Cut the string after n Chars
2431 * @param Buf Buffer to modify
2432 * @param AfternChars after how many chars should we trunkate the string?
2433 * @param At if non-null and points inside of our string, cut it there.
2435 void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
2438 AfternChars = At - Buf->buf;
2441 if ((AfternChars < 0) || (AfternChars >= Buf->BufUsed))
2443 Buf->BufUsed = AfternChars;
2444 Buf->buf[Buf->BufUsed] = '\0';
2450 * @brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
2451 * @param Buf the string to modify
2453 void StrBufTrim(StrBuf *Buf)
2456 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
2458 while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
2461 if (delta > 0) StrBufCutLeft(Buf, delta);
2463 if (Buf->BufUsed == 0) return;
2464 while (isspace(Buf->buf[Buf->BufUsed - 1])){
2467 Buf->buf[Buf->BufUsed] = '\0';
2472 * @brief uppercase the contents of a buffer
2473 * @param Buf the buffer to translate
2475 void StrBufUpCase(StrBuf *Buf)
2480 pche = pch + Buf->BufUsed;
2481 while (pch < pche) {
2482 *pch = toupper(*pch);
2490 * @brief lowercase the contents of a buffer
2491 * @param Buf the buffer to translate
2493 void StrBufLowerCase(StrBuf *Buf)
2498 pche = pch + Buf->BufUsed;
2499 while (pch < pche) {
2500 *pch = tolower(*pch);
2507 * @brief removes double slashes from pathnames
2508 * @param Dir directory string to filter
2509 * @param RemoveTrailingSlash allows / disallows trailing slashes
2511 void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
2517 while (!IsEmptyStr(a)) {
2529 if ((RemoveTrailingSlash) && (*(b - 1) != '/')){
2534 Dir->BufUsed = b - Dir->buf;
2538 * @ingroup StrBuf_DeEnCoder
2539 * @brief unhide special chars hidden to the HTML escaper
2540 * @param target buffer to put the unescaped string in
2541 * @param source buffer to unescape
2543 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
2549 FlushStrBuf(target);
2551 if (source == NULL ||target == NULL)
2556 len = source->BufUsed;
2557 for (a = 0; a < len; ++a) {
2558 if (target->BufUsed >= target->BufSize)
2559 IncreaseBuf(target, 1, -1);
2561 if (source->buf[a] == '=') {
2562 hex[0] = source->buf[a + 1];
2563 hex[1] = source->buf[a + 2];
2566 sscanf(hex, "%02x", &b);
2567 target->buf[target->BufUsed] = b;
2568 target->buf[++target->BufUsed] = 0;
2572 target->buf[target->BufUsed] = source->buf[a];
2573 target->buf[++target->BufUsed] = 0;
2580 * @ingroup StrBuf_DeEnCoder
2581 * @brief hide special chars from the HTML escapers and friends
2582 * @param target buffer to put the escaped string in
2583 * @param source buffer to escape
2585 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
2590 FlushStrBuf(target);
2592 if (source == NULL ||target == NULL)
2597 len = source->BufUsed;
2598 for (i=0; i<len; ++i) {
2599 if (target->BufUsed + 4 >= target->BufSize)
2600 IncreaseBuf(target, 1, -1);
2601 if ( (isalnum(source->buf[i])) ||
2602 (source->buf[i]=='-') ||
2603 (source->buf[i]=='_') ) {
2604 target->buf[target->BufUsed++] = source->buf[i];
2607 sprintf(&target->buf[target->BufUsed],
2609 (0xFF &source->buf[i]));
2610 target->BufUsed += 3;
2613 target->buf[target->BufUsed + 1] = '\0';
2617 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
2618 #define OS_CODE 0x03 /*< unix */
2621 * @ingroup StrBuf_DeEnCoder
2622 * @brief uses the same calling syntax as compress2(), but it
2623 * creates a stream compatible with HTTP "Content-encoding: gzip"
2624 * @param dest compressed buffer
2625 * @param destLen length of the compresed data
2626 * @param source source to encode
2627 * @param sourceLen length of source to encode
2628 * @param level compression level
2630 int ZEXPORT compress_gzip(Bytef * dest,
2632 const Bytef * source,
2636 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
2638 /* write gzip header */
2639 snprintf((char *) dest, *destLen,
2640 "%c%c%c%c%c%c%c%c%c%c",
2641 gz_magic[0], gz_magic[1], Z_DEFLATED,
2642 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
2645 /* normal deflate */
2648 stream.next_in = (Bytef *) source;
2649 stream.avail_in = (uInt) sourceLen;
2650 stream.next_out = dest + 10L; // after header
2651 stream.avail_out = (uInt) * destLen;
2652 if ((uLong) stream.avail_out != *destLen)
2655 stream.zalloc = (alloc_func) 0;
2656 stream.zfree = (free_func) 0;
2657 stream.opaque = (voidpf) 0;
2659 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
2660 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
2664 err = deflate(&stream, Z_FINISH);
2665 if (err != Z_STREAM_END) {
2666 deflateEnd(&stream);
2667 return err == Z_OK ? Z_BUF_ERROR : err;
2669 *destLen = stream.total_out + 10L;
2671 /* write CRC and Length */
2672 uLong crc = crc32(0L, source, sourceLen);
2674 for (n = 0; n < 4; ++n, ++*destLen) {
2675 dest[*destLen] = (int) (crc & 0xff);
2678 uLong len = stream.total_in;
2679 for (n = 0; n < 4; ++n, ++*destLen) {
2680 dest[*destLen] = (int) (len & 0xff);
2683 err = deflateEnd(&stream);
2690 * @ingroup StrBuf_DeEnCoder
2691 * @brief compress the buffer with gzip
2692 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
2693 * @param Buf buffer whose content is to be gzipped
2695 int CompressBuffer(StrBuf *Buf)
2698 char *compressed_data = NULL;
2699 size_t compressed_len, bufsize;
2702 bufsize = compressed_len = Buf->BufUsed + (Buf->BufUsed / 100) + 100;
2703 compressed_data = malloc(compressed_len);
2705 if (compressed_data == NULL)
2707 /* Flush some space after the used payload so valgrind shuts up... */
2708 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
2709 Buf->buf[Buf->BufUsed + i++] = '\0';
2710 if (compress_gzip((Bytef *) compressed_data,
2713 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
2716 Buf->buf = compressed_data;
2717 Buf->BufUsed = compressed_len;
2718 Buf->BufSize = bufsize;
2719 /* Flush some space after the used payload so valgrind shuts up... */
2721 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
2722 Buf->buf[Buf->BufUsed + i++] = '\0';
2725 free(compressed_data);
2727 #endif /* HAVE_ZLIB */
2732 * @ingroup StrBuf_DeEnCoder
2733 * @brief decode a buffer from base 64 encoding; destroys original
2734 * @param Buf Buffor to transform
2736 int StrBufDecodeBase64(StrBuf *Buf)
2740 if (Buf == NULL) return -1;
2742 xferbuf = (char*) malloc(Buf->BufSize);
2743 siz = CtdlDecodeBase64(xferbuf,
2753 * @ingroup StrBuf_DeEnCoder
2754 * @brief decode a buffer from base 64 encoding; destroys original
2755 * @param Buf Buffor to transform
2757 int StrBufDecodeHex(StrBuf *Buf)
2760 char *pch, *pche, *pchi;
2762 if (Buf == NULL) return -1;
2764 pch = pchi = Buf->buf;
2765 pche = pch + Buf->BufUsed;
2767 while (pchi < pche){
2768 ch = decode_hex(pchi);
2775 Buf->BufUsed = pch - Buf->buf;
2776 return Buf->BufUsed;
2780 * @ingroup StrBuf_DeEnCoder
2781 * @brief replace all chars >0x20 && < 0x7F with Mute
2782 * @param Mute char to put over invalid chars
2783 * @param Buf Buffor to transform
2785 int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
2789 if (Buf == NULL) return -1;
2790 pch = (unsigned char *)Buf->buf;
2791 while (pch < (unsigned char *)Buf->buf + Buf->BufUsed) {
2792 if ((*pch < 0x20) || (*pch > 0x7F))
2796 return Buf->BufUsed;
2801 * @ingroup StrBuf_DeEnCoder
2802 * @brief remove escaped strings from i.e. the url string (like %20 for blanks)
2803 * @param Buf Buffer to translate
2804 * @param StripBlanks Reduce several blanks to one?
2806 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
2812 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
2813 Buf->buf[Buf->BufUsed - 1] = '\0';
2818 while (a < Buf->BufUsed) {
2819 if (Buf->buf[a] == '+')
2821 else if (Buf->buf[a] == '%') {
2822 /* don't let % chars through, rather truncate the input. */
2823 if (a + 2 > Buf->BufUsed) {
2828 hex[0] = Buf->buf[a + 1];
2829 hex[1] = Buf->buf[a + 2];
2832 sscanf(hex, "%02x", &b);
2833 Buf->buf[a] = (char) b;
2834 len = Buf->BufUsed - a - 2;
2836 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
2848 * @ingroup StrBuf_DeEnCoder
2849 * @brief RFC2047-encode a header field if necessary.
2850 * If no non-ASCII characters are found, the string
2851 * will be copied verbatim without encoding.
2853 * @param target Target buffer.
2854 * @param source Source string to be encoded.
2855 * @returns encoded length; -1 if non success.
2857 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
2859 const char headerStr[] = "=?UTF-8?Q?";
2860 int need_to_encode = 0;
2864 if ((source == NULL) ||
2868 while ((i < source->BufUsed) &&
2869 (!IsEmptyStr (&source->buf[i])) &&
2870 (need_to_encode == 0)) {
2871 if (((unsigned char) source->buf[i] < 32) ||
2872 ((unsigned char) source->buf[i] > 126)) {
2878 if (!need_to_encode) {
2879 if (*target == NULL) {
2880 *target = NewStrBufPlain(source->buf, source->BufUsed);
2883 FlushStrBuf(*target);
2884 StrBufAppendBuf(*target, source, 0);
2886 return (*target)->BufUsed;
2888 if (*target == NULL)
2889 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
2890 else if (sizeof(headerStr) + source->BufUsed >= (*target)->BufSize)
2891 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
2892 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
2893 (*target)->BufUsed = sizeof(headerStr) - 1;
2894 for (i=0; (i < source->BufUsed); ++i) {
2895 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2896 IncreaseBuf(*target, 1, 0);
2897 ch = (unsigned char) source->buf[i];
2898 if ((ch < 32) || (ch > 126) || (ch == 61)) {
2899 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
2900 (*target)->BufUsed += 3;
2903 (*target)->buf[(*target)->BufUsed] = ch;
2904 (*target)->BufUsed++;
2908 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2909 IncreaseBuf(*target, 1, 0);
2911 (*target)->buf[(*target)->BufUsed++] = '?';
2912 (*target)->buf[(*target)->BufUsed++] = '=';
2913 (*target)->buf[(*target)->BufUsed] = '\0';
2914 return (*target)->BufUsed;;
2919 * @brief replaces all occurances of 'search' by 'replace'
2920 * @param buf Buffer to modify
2921 * @param search character to search
2922 * @param replace character to replace search by
2924 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
2929 for (i=0; i<buf->BufUsed; i++)
2930 if (buf->buf[i] == search)
2931 buf->buf[i] = replace;
2938 * @ingroup StrBuf_DeEnCoder
2939 * @brief Wrapper around iconv_open()
2940 * Our version adds aliases for non-standard Microsoft charsets
2941 * such as 'MS950', aliasing them to names like 'CP950'
2943 * @param tocode Target encoding
2944 * @param fromcode Source encoding
2945 * @param pic anonimized pointer to iconv struct
2947 void ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
2950 iconv_t ic = (iconv_t)(-1) ;
2951 ic = iconv_open(tocode, fromcode);
2952 if (ic == (iconv_t)(-1) ) {
2953 char alias_fromcode[64];
2954 if ( (strlen(fromcode) == 5) && (!strncasecmp(fromcode, "MS", 2)) ) {
2955 safestrncpy(alias_fromcode, fromcode, sizeof alias_fromcode);
2956 alias_fromcode[0] = 'C';
2957 alias_fromcode[1] = 'P';
2958 ic = iconv_open(tocode, alias_fromcode);
2961 *(iconv_t *)pic = ic;
2967 * @ingroup StrBuf_DeEnCoder
2968 * @brief find one chunk of a RFC822 encoded string
2969 * @param Buffer where to search
2970 * @param bptr where to start searching
2971 * @returns found position, NULL if none.
2973 static inline char *FindNextEnd (const StrBuf *Buf, char *bptr)
2976 /* Find the next ?Q? */
2977 if (Buf->BufUsed - (bptr - Buf->buf) < 6)
2980 end = strchr(bptr + 2, '?');
2985 if ((Buf->BufUsed - (end - Buf->buf) > 3) &&
2986 ((*(end + 1) == 'B') || (*(end + 1) == 'Q')) &&
2987 (*(end + 2) == '?')) {
2988 /* skip on to the end of the cluster, the next ?= */
2989 end = strstr(end + 3, "?=");
2992 /* sort of half valid encoding, try to find an end. */
2993 end = strstr(bptr, "?=");
2999 * @brief swaps the contents of two StrBufs
3000 * this is to be used to have cheap switched between a work-buffer and a target buffer
3001 * @param A First one
3002 * @param B second one
3004 static inline void SwapBuffers(StrBuf *A, StrBuf *B)
3008 memcpy(&C, A, sizeof(*A));
3009 memcpy(A, B, sizeof(*B));
3010 memcpy(B, &C, sizeof(C));
3016 * @ingroup StrBuf_DeEnCoder
3017 * @brief convert one buffer according to the preselected iconv pointer PIC
3018 * @param ConvertBuf buffer we need to translate
3019 * @param TmpBuf To share a workbuffer over several iterations. prepare to have it filled with useless stuff afterwards.
3020 * @param pic Pointer to the iconv-session Object
3022 void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
3028 char *ibuf; /**< Buffer of characters to be converted */
3029 char *obuf; /**< Buffer for converted characters */
3030 size_t ibuflen; /**< Length of input buffer */
3031 size_t obuflen; /**< Length of output buffer */
3034 /* since we're converting to utf-8, one glyph may take up to 6 bytes */
3035 if (ConvertBuf->BufUsed * 6 >= TmpBuf->BufSize)
3036 IncreaseBuf(TmpBuf, 0, ConvertBuf->BufUsed * 6);
3038 ic = *(iconv_t*)pic;
3039 ibuf = ConvertBuf->buf;
3040 ibuflen = ConvertBuf->BufUsed;
3042 obuflen = TmpBuf->BufSize;
3044 siz = iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
3047 if (errno == E2BIG) {
3049 IncreaseBuf(TmpBuf, 0, 0);
3054 else if (errno == EILSEQ){
3055 /* hm, invalid utf8 sequence... what to do now? */
3056 /* An invalid multibyte sequence has been encountered in the input */
3058 else if (errno == EINVAL) {
3059 /* An incomplete multibyte sequence has been encountered in the input. */
3062 FlushStrBuf(TmpBuf);
3065 TmpBuf->BufUsed = TmpBuf->BufSize - obuflen;
3066 TmpBuf->buf[TmpBuf->BufUsed] = '\0';
3068 /* little card game: wheres the red lady? */
3069 SwapBuffers(ConvertBuf, TmpBuf);
3070 FlushStrBuf(TmpBuf);
3077 * @ingroup StrBuf_DeEnCoder
3078 * @brief catches one RFC822 encoded segment, and decodes it.
3079 * @param Target buffer to fill with result
3080 * @param DecodeMe buffer with stuff to process
3081 * @param SegmentStart points to our current segment in DecodeMe
3082 * @param SegmentEnd Points to the end of our current segment in DecodeMe
3083 * @param ConvertBuf Workbuffer shared between several iterations. Random content; needs to be valid
3084 * @param ConvertBuf2 Workbuffer shared between several iterations. Random content; needs to be valid
3085 * @param FoundCharset Characterset to default decoding to; if we find another we will overwrite it.
3087 inline static void DecodeSegment(StrBuf *Target,
3088 const StrBuf *DecodeMe,
3092 StrBuf *ConvertBuf2,
3093 StrBuf *FoundCharset)
3099 iconv_t ic = (iconv_t)(-1);
3103 /* Now we handle foreign character sets properly encoded
3104 * in RFC2047 format.
3106 StaticBuf.buf = SegmentStart;
3107 StaticBuf.BufUsed = SegmentEnd - SegmentStart;
3108 StaticBuf.BufSize = DecodeMe->BufSize - (SegmentStart - DecodeMe->buf);
3109 extract_token(charset, SegmentStart, 1, '?', sizeof charset);
3110 if (FoundCharset != NULL) {
3111 FlushStrBuf(FoundCharset);
3112 StrBufAppendBufPlain(FoundCharset, charset, -1, 0);
3114 extract_token(encoding, SegmentStart, 2, '?', sizeof encoding);
3115 StrBufExtract_token(ConvertBuf, &StaticBuf, 3, '?');
3117 *encoding = toupper(*encoding);
3118 if (*encoding == 'B') { /**< base64 */
3119 ConvertBuf2->BufUsed = CtdlDecodeBase64(ConvertBuf2->buf,
3121 ConvertBuf->BufUsed);
3123 else if (*encoding == 'Q') { /**< quoted-printable */
3127 while (pos < ConvertBuf->BufUsed)
3129 if (ConvertBuf->buf[pos] == '_')
3130 ConvertBuf->buf[pos] = ' ';
3134 ConvertBuf2->BufUsed = CtdlDecodeQuotedPrintable(
3137 ConvertBuf->BufUsed);
3140 StrBufAppendBuf(ConvertBuf2, ConvertBuf, 0);
3143 ctdl_iconv_open("UTF-8", charset, &ic);
3144 if (ic != (iconv_t)(-1) ) {
3146 StrBufConvert(ConvertBuf2, ConvertBuf, &ic);
3147 StrBufAppendBuf(Target, ConvertBuf2, 0);
3152 StrBufAppendBufPlain(Target, HKEY("(unreadable)"), 0);
3158 * @ingroup StrBuf_DeEnCoder
3159 * @brief Handle subjects with RFC2047 encoding such as:
3160 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
3161 * @param Target where to put the decoded string to
3162 * @param DecodeMe buffer with encoded string
3163 * @param DefaultCharset if we don't find one, which should we use?
3164 * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string,
3165 * put it here for later use where no string might be known.
3167 void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset)
3169 StrBuf *DecodedInvalidBuf = NULL;
3170 StrBuf *ConvertBuf, *ConvertBuf2;
3171 const StrBuf *DecodeMee = DecodeMe;
3172 char *start, *end, *next, *nextend, *ptr = NULL;
3174 iconv_t ic = (iconv_t)(-1) ;
3179 int illegal_non_rfc2047_encoding = 0;
3181 /* Sometimes, badly formed messages contain strings which were simply
3182 * written out directly in some foreign character set instead of
3183 * using RFC2047 encoding. This is illegal but we will attempt to
3184 * handle it anyway by converting from a user-specified default
3185 * charset to UTF-8 if we see any nonprintable characters.
3188 len = StrLength(DecodeMe);
3189 for (i=0; i<DecodeMe->BufUsed; ++i) {
3190 if ((DecodeMe->buf[i] < 32) || (DecodeMe->buf[i] > 126)) {
3191 illegal_non_rfc2047_encoding = 1;
3196 ConvertBuf = NewStrBufPlain(NULL, StrLength(DecodeMe));
3197 if ((illegal_non_rfc2047_encoding) &&
3198 (strcasecmp(ChrPtr(DefaultCharset), "UTF-8")) &&
3199 (strcasecmp(ChrPtr(DefaultCharset), "us-ascii")) )
3202 ctdl_iconv_open("UTF-8", ChrPtr(DefaultCharset), &ic);
3203 if (ic != (iconv_t)(-1) ) {
3204 DecodedInvalidBuf = NewStrBufDup(DecodeMe);
3205 StrBufConvert(DecodedInvalidBuf, ConvertBuf, &ic);///TODO: don't void const?
3206 DecodeMee = DecodedInvalidBuf;
3212 /* pre evaluate the first pair */
3213 nextend = end = NULL;
3214 len = StrLength(DecodeMee);
3215 start = strstr(DecodeMee->buf, "=?");
3216 eptr = DecodeMee->buf + DecodeMee->BufUsed;
3218 end = FindNextEnd (DecodeMee, start);
3220 StrBufAppendBuf(Target, DecodeMee, 0);
3221 FreeStrBuf(&ConvertBuf);
3222 FreeStrBuf(&DecodedInvalidBuf);
3226 ConvertBuf2 = NewStrBufPlain(NULL, StrLength(DecodeMee));
3228 if (start != DecodeMee->buf) {
3231 nFront = start - DecodeMee->buf;
3232 StrBufAppendBufPlain(Target, DecodeMee->buf, nFront, 0);
3236 * Since spammers will go to all sorts of absurd lengths to get their
3237 * messages through, there are LOTS of corrupt headers out there.
3238 * So, prevent a really badly formed RFC2047 header from throwing
3239 * this function into an infinite loop.
3241 while ((start != NULL) &&
3248 DecodeSegment(Target,
3256 next = strstr(end, "=?");
3258 if ((next != NULL) &&
3260 nextend = FindNextEnd(DecodeMee, next);
3261 if (nextend == NULL)
3264 /* did we find two partitions */
3265 if ((next != NULL) &&
3269 while ((ptr < next) &&
3275 /* did we find a gab just filled with blanks? */
3278 long gap = next - start;
3283 /* now terminate the gab at the end */
3284 delta = (next - end) - 2; ////TODO: const!
3285 ((StrBuf*)DecodeMee)->BufUsed -= delta;
3286 ((StrBuf*)DecodeMee)->buf[DecodeMee->BufUsed] = '\0';
3288 /* move next to its new location. */
3293 /* our next-pair is our new first pair now. */
3299 nextend = DecodeMee->buf + DecodeMee->BufUsed;
3300 if ((end != NULL) && (end < nextend)) {
3302 while ( (ptr < nextend) &&
3309 StrBufAppendBufPlain(Target, end, nextend - end, 0);
3311 FreeStrBuf(&ConvertBuf);
3312 FreeStrBuf(&ConvertBuf2);
3313 FreeStrBuf(&DecodedInvalidBuf);
3318 * @brief evaluate the length of an utf8 special character sequence
3319 * @param Char the character to examine
3320 * @returns width of utf8 chars in bytes
3322 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
3327 while ((n < 8) && ((test & *CharS) != 0)) {
3331 if ((n > 6) || ((CharE - CharS) > n))
3338 * @brief detect whether this char starts an utf-8 encoded char
3339 * @param Char character to inspect
3340 * @returns yes or no
3342 static inline int Ctdl_IsUtf8SequenceStart(const char Char)
3344 /** 11??.???? indicates an UTF8 Sequence. */
3345 return ((Char & 0xC0) != 0);
3350 * @brief measure the number of glyphs in an UTF8 string...
3351 * @param Buf string to measure
3352 * @returns the number of glyphs in Buf
3354 long StrBuf_Utf8StrLen(StrBuf *Buf)
3360 if ((Buf == NULL) || (Buf->BufUsed == 0))
3363 eptr = Buf->buf + Buf->BufUsed;
3364 while ((aptr < eptr) && (*aptr != '\0')) {
3365 if (Ctdl_IsUtf8SequenceStart(*aptr)){
3366 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
3367 while ((aptr < eptr) && (m-- > 0) && (*aptr++ != '\0'))
3381 * @brief cuts a string after maxlen glyphs
3382 * @param Buf string to cut to maxlen glyphs
3383 * @param maxlen how long may the string become?
3384 * @returns current length of the string
3386 long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
3392 eptr = Buf->buf + Buf->BufUsed;
3393 while ((aptr < eptr) && (*aptr != '\0')) {
3394 if (Ctdl_IsUtf8SequenceStart(*aptr)){
3395 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
3396 while ((m-- > 0) && (*aptr++ != '\0'))
3405 Buf->BufUsed = aptr - Buf->buf;
3406 return Buf->BufUsed;
3409 return Buf->BufUsed;
3416 * @brief extract a "next line" from Buf; Ptr to persist across several iterations
3417 * @param LineBuf your line will be copied here.
3418 * @param Buf BLOB with lines of text...
3419 * @param Ptr moved arround to keep the next-line across several iterations
3420 * has to be &NULL on start; will be &NotNULL on end of buffer
3421 * @returns size of copied buffer
3423 int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr)
3425 const char *aptr, *ptr, *eptr;
3428 if ((Buf == NULL) || (*Ptr == StrBufNOTNULL)) {
3429 *Ptr = StrBufNOTNULL;
3433 FlushStrBuf(LineBuf);
3435 ptr = aptr = Buf->buf;
3439 optr = LineBuf->buf;
3440 eptr = Buf->buf + Buf->BufUsed;
3441 xptr = LineBuf->buf + LineBuf->BufSize - 1;
3443 while ((ptr <= eptr) &&
3450 LineBuf->BufUsed = optr - LineBuf->buf;
3451 IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
3452 optr = LineBuf->buf + LineBuf->BufUsed;
3453 xptr = LineBuf->buf + LineBuf->BufSize - 1;
3457 if ((ptr >= eptr) && (optr > LineBuf->buf))
3459 LineBuf->BufUsed = optr - LineBuf->buf;
3461 if ((ptr <= eptr) && (*ptr == '\r'))
3463 if ((ptr <= eptr) && (*ptr == '\n'))
3470 *Ptr = StrBufNOTNULL;
3473 return Buf->BufUsed - (ptr - Buf->buf);