* StrBufDecodeBase64(): init output buffer so in case of an empty / illegal base64...
[citadel.git] / libcitadel / lib / stringbuf.c
1 #include "sysdep.h"
2 #include <ctype.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <sys/select.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #define SHOW_ME_VAPPEND_PRINTF
12 #include <stdarg.h>
13 #include "libcitadel.h"
14
15 #ifdef HAVE_ICONV
16 #include <iconv.h>
17 #endif
18
19 #ifdef HAVE_BACKTRACE
20 #include <execinfo.h>
21 #endif
22
23 #ifdef HAVE_ZLIB
24 #include <zlib.h>
25 int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
26                           const Bytef * source, uLong sourceLen, int level);
27 #endif
28 int BaseStrBufSize = 64;
29
30 const char *StrBufNOTNULL = ((char*) NULL) - 1;
31
32 const char HexList[256][3] = {
33         "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
34         "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
35         "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
36         "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
37         "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
38         "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
39         "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
40         "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
41         "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
42         "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
43         "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
44         "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
45         "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
46         "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
47         "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
48         "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
49
50 /**
51  * @defgroup StrBuf Stringbuffer, A class for manipulating strings with dynamic buffers
52  * StrBuf is a versatile class, aiding the handling of dynamic strings
53  *  * reduce de/reallocations
54  *  * reduce the need to remeasure it
55  *  * reduce scanning over the string (in @ref StrBuf_NextTokenizer "Tokenizers")
56  *  * allow asyncroneous IO for line and Blob based operations
57  *  * reduce the use of memove in those
58  *  * Quick filling in several operations with append functions
59  */
60
61 /**
62  * @defgroup StrBuf_DeConstructors Create/Destroy StrBufs
63  * @ingroup StrBuf
64  */
65
66 /**
67  * @defgroup StrBuf_Cast Cast operators to interact with char* based code
68  * @ingroup StrBuf
69  * use these operators to interfere with code demanding char*; 
70  * if you need to own the content, smash me. Avoid, since we loose the length information.
71  */
72
73 /**
74  * @defgroup StrBuf_Filler Create/Replace/Append Content into a StrBuf
75  * @ingroup StrBuf
76  * operations to get your Strings into a StrBuf, manipulating them, or appending
77  */
78 /**
79  * @defgroup StrBuf_NextTokenizer Fast tokenizer to pull tokens in sequence 
80  * @ingroup StrBuf
81  * Quick tokenizer; demands of the user to pull its tokens in sequence
82  */
83
84 /**
85  * @defgroup StrBuf_Tokenizer tokenizer Functions; Slow ones.
86  * @ingroup StrBuf
87  * versatile tokenizer; random access to tokens, but slower; Prefer the @ref StrBuf_NextTokenizer "Next Tokenizer"
88  */
89
90 /**
91  * @defgroup StrBuf_BufferedIO Buffered IO with Asynchroneous reads and no unneeded memmoves (the fast ones)
92  * @ingroup StrBuf
93  * File IO to fill StrBufs; Works with work-buffer shared across several calls;
94  * External Cursor to maintain the current read position inside of the buffer
95  * the non-fast ones will use memove to keep the start of the buffer the read buffer (which is slower) 
96  */
97
98 /**
99  * @defgroup StrBuf_IO FileIO; Prefer @ref StrBuf_BufferedIO
100  * @ingroup StrBuf
101  * Slow I/O; avoid.
102  */
103
104 /**
105  * @defgroup StrBuf_DeEnCoder functions to translate the contents of a buffer
106  * @ingroup StrBuf
107  * these functions translate the content of a buffer into another representation;
108  * some are combined Fillers and encoders
109  */
110
111 /**
112  * Private Structure for the Stringbuffer
113  */
114 struct StrBuf {
115         char *buf;         /**< the pointer to the dynamic buffer */
116         long BufSize;      /**< how many spcae do we optain */
117         long BufUsed;      /**< StNumber of Chars used excluding the trailing \\0 */
118         int ConstBuf;      /**< are we just a wrapper arround a static buffer and musn't we be changed? */
119 #ifdef SIZE_DEBUG
120         long nIncreases;   /**< for profiling; cound how many times we needed more */
121         char bt [SIZ];     /**< Stacktrace of last increase */
122         char bt_lastinc [SIZ]; /**< How much did we increase last time? */
123 #endif
124 };
125
126
127 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE);
128 static inline int Ctdl_IsUtf8SequenceStart(const char Char);
129
130 #ifdef SIZE_DEBUG
131 #ifdef HAVE_BACKTRACE
132 static void StrBufBacktrace(StrBuf *Buf, int which)
133 {
134         int n;
135         char *pstart, *pch;
136         void *stack_frames[50];
137         size_t size, i;
138         char **strings;
139
140         if (which)
141                 pstart = pch = Buf->bt;
142         else
143                 pstart = pch = Buf->bt_lastinc;
144         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
145         strings = backtrace_symbols(stack_frames, size);
146         for (i = 0; i < size; i++) {
147                 if (strings != NULL)
148                         n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
149                 else
150                         n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
151                 pch += n;
152         }
153         free(strings);
154
155
156 }
157 #endif
158
159 void dbg_FreeStrBuf(StrBuf *FreeMe, char *FromWhere)
160 {
161         if (hFreeDbglog == -1){
162                 pid_t pid = getpid();
163                 char path [SIZ];
164                 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
165                 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
166         }
167         if ((*FreeMe)->nIncreases > 0)
168         {
169                 char buf[SIZ * 3];
170                 long n;
171                 n = snprintf(buf, SIZ * 3, "%c+|%ld|%ld|%ld|%s|%s|\n",
172                              FromWhere,
173                              (*FreeMe)->nIncreases,
174                              (*FreeMe)->BufUsed,
175                              (*FreeMe)->BufSize,
176                              (*FreeMe)->bt,
177                              (*FreeMe)->bt_lastinc);
178                 n = write(hFreeDbglog, buf, n);
179         }
180         else
181         {
182                 char buf[128];
183                 long n;
184                 n = snprintf(buf, 128, "%c_|0|%ld%ld|\n",
185                              FromWhere,
186                              (*FreeMe)->BufUsed,
187                              (*FreeMe)->BufSize);
188                 n = write(hFreeDbglog, buf, n);
189         }
190 }
191
192 void dbg_IncreaseBuf(StrBuf *IncMe)
193 {
194         Buf->nIncreases++;
195 #ifdef HAVE_BACKTRACE
196         StrBufBacktrace(Buf, 1);
197 #endif
198 }
199
200 void dbg_Init(StrBuf *Buf)
201 {
202         Buf->nIncreases = 0;
203         Buf->bt[0] = '\0';
204         Buf->bt_lastinc[0] = '\0';
205 #ifdef HAVE_BACKTRACE
206         StrBufBacktrace(Buf, 0);
207 #endif
208 }
209
210 #else
211 /* void it... */
212 #define dbg_FreeStrBuf(a, b)
213 #define dbg_IncreaseBuf(a)
214 #define dbg_Init(a)
215
216 #endif
217
218 /** 
219  * @ingroup StrBuf_Cast
220  * @brief Cast operator to Plain String 
221  * @note if the buffer is altered by StrBuf operations, this pointer may become 
222  *  invalid. So don't lean on it after altering the buffer!
223  *  Since this operation is considered cheap, rather call it often than risking
224  *  your pointer to become invalid!
225  * @param Str the string we want to get the c-string representation for
226  * @returns the Pointer to the Content. Don't mess with it!
227  */
228 inline const char *ChrPtr(const StrBuf *Str)
229 {
230         if (Str == NULL)
231                 return "";
232         return Str->buf;
233 }
234
235 /**
236  * @ingroup StrBuf_Cast
237  * @brief since we know strlen()'s result, provide it here.
238  * @param Str the string to return the length to
239  * @returns contentlength of the buffer
240  */
241 inline int StrLength(const StrBuf *Str)
242 {
243         return (Str != NULL) ? Str->BufUsed : 0;
244 }
245
246 /**
247  * @ingroup StrBuf_DeConstructors
248  * @brief local utility function to resize the buffer
249  * @param Buf the buffer whichs storage we should increase
250  * @param KeepOriginal should we copy the original buffer or just start over with a new one
251  * @param DestSize what should fit in after?
252  */
253 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
254 {
255         char *NewBuf;
256         size_t NewSize = Buf->BufSize * 2;
257
258         if (Buf->ConstBuf)
259                 return -1;
260                 
261         if (DestSize > 0)
262                 while (NewSize <= DestSize)
263                         NewSize *= 2;
264
265         NewBuf= (char*) malloc(NewSize);
266         if (NewBuf == NULL)
267                 return -1;
268
269         if (KeepOriginal && (Buf->BufUsed > 0))
270         {
271                 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
272         }
273         else
274         {
275                 NewBuf[0] = '\0';
276                 Buf->BufUsed = 0;
277         }
278         free (Buf->buf);
279         Buf->buf = NewBuf;
280         Buf->BufSize = NewSize;
281
282         dbg_IncreaseBuf(Buf);
283
284         return Buf->BufSize;
285 }
286
287 /**
288  * @ingroup StrBuf_DeConstructors
289  * @brief shrink / increase an _EMPTY_ buffer to NewSize. Buffercontent is thoroughly ignored and flushed.
290  * @param Buf Buffer to shrink (has to be empty)
291  * @param ThreshHold if the buffer is bigger then this, its readjusted
292  * @param NewSize if we Shrink it, how big are we going to be afterwards?
293  */
294 void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize)
295 {
296         if ((Buf != NULL) && 
297             (Buf->BufUsed == 0) &&
298             (Buf->BufSize < ThreshHold)) {
299                 free(Buf->buf);
300                 Buf->buf = (char*) malloc(NewSize);
301                 Buf->BufUsed = 0;
302                 Buf->BufSize = NewSize;
303         }
304 }
305
306 /**
307  * @ingroup StrBuf_DeConstructors
308  * @brief shrink long term buffers to their real size so they don't waste memory
309  * @param Buf buffer to shrink
310  * @param Force if not set, will just executed if the buffer is much to big; set for lifetime strings
311  * @returns physical size of the buffer
312  */
313 long StrBufShrinkToFit(StrBuf *Buf, int Force)
314 {
315         if (Buf == NULL)
316                 return -1;
317         if (Force || 
318             (Buf->BufUsed + (Buf->BufUsed / 3) > Buf->BufSize))
319         {
320                 char *TmpBuf = (char*) malloc(Buf->BufUsed + 1);
321                 memcpy (TmpBuf, Buf->buf, Buf->BufUsed + 1);
322                 Buf->BufSize = Buf->BufUsed + 1;
323                 free(Buf->buf);
324                 Buf->buf = TmpBuf;
325         }
326         return Buf->BufUsed;
327 }
328
329 /**
330  * @ingroup StrBuf_DeConstructors
331  * @brief Allocate a new buffer with default buffer size
332  * @returns the new stringbuffer
333  */
334 StrBuf* NewStrBuf(void)
335 {
336         StrBuf *NewBuf;
337
338         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
339         NewBuf->buf = (char*) malloc(BaseStrBufSize);
340         NewBuf->buf[0] = '\0';
341         NewBuf->BufSize = BaseStrBufSize;
342         NewBuf->BufUsed = 0;
343         NewBuf->ConstBuf = 0;
344
345         dbg_Init (NewBuf);
346
347         return NewBuf;
348 }
349
350 /** 
351  * @ingroup StrBuf_DeConstructors
352  * @brief Copy Constructor; returns a duplicate of CopyMe
353  * @param CopyMe Buffer to faxmilate
354  * @returns the new stringbuffer
355  */
356 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
357 {
358         StrBuf *NewBuf;
359         
360         if (CopyMe == NULL)
361                 return NewStrBuf();
362
363         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
364         NewBuf->buf = (char*) malloc(CopyMe->BufSize);
365         memcpy(NewBuf->buf, CopyMe->buf, CopyMe->BufUsed + 1);
366         NewBuf->BufUsed = CopyMe->BufUsed;
367         NewBuf->BufSize = CopyMe->BufSize;
368         NewBuf->ConstBuf = 0;
369
370         dbg_Init(NewBuf);
371
372         return NewBuf;
373 }
374
375 /**
376  * @ingroup StrBuf_DeConstructors
377  * @brief create a new Buffer using an existing c-string
378  * this function should also be used if you want to pre-suggest
379  * the buffer size to allocate in conjunction with ptr == NULL
380  * @param ptr the c-string to copy; may be NULL to create a blank instance
381  * @param nChars How many chars should we copy; -1 if we should measure the length ourselves
382  * @returns the new stringbuffer
383  */
384 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
385 {
386         StrBuf *NewBuf;
387         size_t Siz = BaseStrBufSize;
388         size_t CopySize;
389
390         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
391         if (nChars < 0)
392                 CopySize = strlen((ptr != NULL)?ptr:"");
393         else
394                 CopySize = nChars;
395
396         while (Siz <= CopySize)
397                 Siz *= 2;
398
399         NewBuf->buf = (char*) malloc(Siz);
400         NewBuf->BufSize = Siz;
401         if (ptr != NULL) {
402                 memcpy(NewBuf->buf, ptr, CopySize);
403                 NewBuf->buf[CopySize] = '\0';
404                 NewBuf->BufUsed = CopySize;
405         }
406         else {
407                 NewBuf->buf[0] = '\0';
408                 NewBuf->BufUsed = 0;
409         }
410         NewBuf->ConstBuf = 0;
411
412         dbg_Init(NewBuf)
413
414                 return NewBuf;
415 }
416
417 /**
418  * @ingroup StrBuf_DeConstructors
419  * @brief Set an existing buffer from a c-string
420  * @param Buf buffer to load
421  * @param ptr c-string to put into 
422  * @param nChars set to -1 if we should work 0-terminated
423  * @returns the new length of the string
424  */
425 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
426 {
427         size_t Siz;
428         size_t CopySize;
429
430         if (Buf == NULL)
431                 return -1;
432         if (ptr == NULL) {
433                 FlushStrBuf(Buf);
434                 return -1;
435         }
436
437         Siz = Buf->BufSize;
438
439         if (nChars < 0)
440                 CopySize = strlen(ptr);
441         else
442                 CopySize = nChars;
443
444         while (Siz <= CopySize)
445                 Siz *= 2;
446
447         if (Siz != Buf->BufSize)
448                 IncreaseBuf(Buf, 0, Siz);
449         memcpy(Buf->buf, ptr, CopySize);
450         Buf->buf[CopySize] = '\0';
451         Buf->BufUsed = CopySize;
452         Buf->ConstBuf = 0;
453         return CopySize;
454 }
455
456
457 /**
458  * @ingroup StrBuf_DeConstructors
459  * @brief use strbuf as wrapper for a string constant for easy handling
460  * @param StringConstant a string to wrap
461  * @param SizeOfStrConstant should be sizeof(StringConstant)-1
462  */
463 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
464 {
465         StrBuf *NewBuf;
466
467         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
468         NewBuf->buf = (char*) StringConstant;
469         NewBuf->BufSize = SizeOfStrConstant;
470         NewBuf->BufUsed = SizeOfStrConstant;
471         NewBuf->ConstBuf = 1;
472
473         dbg_Init(NewBuf);
474
475         return NewBuf;
476 }
477
478
479 /**
480  * @ingroup StrBuf_DeConstructors
481  * @brief flush the content of a Buf; keep its struct
482  * @param buf Buffer to flush
483  */
484 int FlushStrBuf(StrBuf *buf)
485 {
486         if (buf == NULL)
487                 return -1;
488         if (buf->ConstBuf)
489                 return -1;       
490         buf->buf[0] ='\0';
491         buf->BufUsed = 0;
492         return 0;
493 }
494
495 /**
496  * @ingroup StrBuf_DeConstructors
497  * @brief wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
498  * @param buf Buffer to wipe
499  */
500 int FLUSHStrBuf(StrBuf *buf)
501 {
502         if (buf == NULL)
503                 return -1;
504         if (buf->ConstBuf)
505                 return -1;
506         if (buf->BufUsed > 0) {
507                 memset(buf->buf, 0, buf->BufUsed);
508                 buf->BufUsed = 0;
509         }
510         return 0;
511 }
512
513 #ifdef SIZE_DEBUG
514 int hFreeDbglog = -1;
515 #endif
516 /**
517  * @ingroup StrBuf_DeConstructors
518  * @brief Release a Buffer
519  * Its a double pointer, so it can NULL your pointer
520  * so fancy SIG11 appear instead of random results
521  * @param FreeMe Pointer Pointer to the buffer to free
522  */
523 void FreeStrBuf (StrBuf **FreeMe)
524 {
525         if (*FreeMe == NULL)
526                 return;
527
528         dbg_FreeStrBuf(FreeMe, 'F');
529
530         if (!(*FreeMe)->ConstBuf) 
531                 free((*FreeMe)->buf);
532         free(*FreeMe);
533         *FreeMe = NULL;
534 }
535
536 /**
537  * @ingroup StrBuf_DeConstructors
538  * @brief flatten a Buffer to the Char * we return 
539  * Its a double pointer, so it can NULL your pointer
540  * so fancy SIG11 appear instead of random results
541  * The Callee then owns the buffer and is responsible for freeing it.
542  * @param SmashMe Pointer Pointer to the buffer to release Buf from and free
543  * @returns the pointer of the buffer; Callee owns the memory thereafter.
544  */
545 char *SmashStrBuf (StrBuf **SmashMe)
546 {
547         char *Ret;
548
549         if ((SmashMe == NULL) || (*SmashMe == NULL))
550                 return NULL;
551         
552         dbg_FreeStrBuf(SmashMe, 'S');
553
554         Ret = (*SmashMe)->buf;
555         free(*SmashMe);
556         *SmashMe = NULL;
557         return Ret;
558 }
559
560 /**
561  * @ingroup StrBuf_DeConstructors
562  * @brief Release the buffer
563  * If you want put your StrBuf into a Hash, use this as Destructor.
564  * @param VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
565  */
566 void HFreeStrBuf (void *VFreeMe)
567 {
568         StrBuf *FreeMe = (StrBuf*)VFreeMe;
569         if (FreeMe == NULL)
570                 return;
571
572         dbg_FreeStrBuf(SmashMe, 'H');
573
574         if (!FreeMe->ConstBuf) 
575                 free(FreeMe->buf);
576         free(FreeMe);
577 }
578
579
580 /*******************************************************************************
581  *                      Simple string transformations                          *
582  *******************************************************************************/
583
584 /**
585  * @ingroup StrBuf
586  * @brief Wrapper around atol
587  */
588 long StrTol(const StrBuf *Buf)
589 {
590         if (Buf == NULL)
591                 return 0;
592         if(Buf->BufUsed > 0)
593                 return atol(Buf->buf);
594         else
595                 return 0;
596 }
597
598 /**
599  * @ingroup StrBuf
600  * @brief Wrapper around atoi
601  */
602 int StrToi(const StrBuf *Buf)
603 {
604         if (Buf == NULL)
605                 return 0;
606         if (Buf->BufUsed > 0)
607                 return atoi(Buf->buf);
608         else
609                 return 0;
610 }
611
612 /**
613  * @ingroup StrBuf
614  * @brief Checks to see if the string is a pure number 
615  * @param Buf The buffer to inspect
616  * @returns 1 if its a pure number, 0, if not.
617  */
618 int StrBufIsNumber(const StrBuf *Buf) {
619         char * pEnd;
620         if ((Buf == NULL) || (Buf->BufUsed == 0)) {
621                 return 0;
622         }
623         strtoll(Buf->buf, &pEnd, 10);
624         if (pEnd == Buf->buf)
625                 return 0;
626         if ((pEnd != NULL) && (pEnd == Buf->buf + Buf->BufUsed))
627                 return 1;
628         if (Buf->buf == pEnd)
629                 return 0;
630         return 0;
631
632
633 /**
634  * @ingroup StrBuf_Filler
635  * @brief modifies a Single char of the Buf
636  * You can point to it via char* or a zero-based integer
637  * @param Buf The buffer to manipulate
638  * @param ptr char* to zero; use NULL if unused
639  * @param nThChar zero based pointer into the string; use -1 if unused
640  * @param PeekValue The Character to place into the position
641  */
642 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
643 {
644         if (Buf == NULL)
645                 return -1;
646         if (ptr != NULL)
647                 nThChar = ptr - Buf->buf;
648         if ((nThChar < 0) || (nThChar > Buf->BufUsed))
649                 return -1;
650         Buf->buf[nThChar] = PeekValue;
651         return nThChar;
652 }
653
654 /**
655  * @ingroup StrBuf_Filler
656  * @brief modifies a range of chars of the Buf
657  * You can point to it via char* or a zero-based integer
658  * @param Buf The buffer to manipulate
659  * @param ptr char* to zero; use NULL if unused
660  * @param nThChar zero based pointer into the string; use -1 if unused
661  * @param nChars how many chars are to be flushed?
662  * @param PookValue The Character to place into that area
663  */
664 long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue)
665 {
666         if (Buf == NULL)
667                 return -1;
668         if (ptr != NULL)
669                 nThChar = ptr - Buf->buf;
670         if ((nThChar < 0) || (nThChar > Buf->BufUsed))
671                 return -1;
672         if (nThChar + nChars > Buf->BufUsed)
673                 nChars =  Buf->BufUsed - nThChar;
674
675         memset(Buf->buf + nThChar, PookValue, nChars);
676         /* just to be shure... */
677         Buf->buf[Buf->BufUsed] = 0;
678         return nChars;
679 }
680
681 /**
682  * @ingroup StrBuf_Filler
683  * @brief Append a StringBuffer to the buffer
684  * @param Buf Buffer to modify
685  * @param AppendBuf Buffer to copy at the end of our buffer
686  * @param Offset Should we start copying from an offset?
687  */
688 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
689 {
690         if ((AppendBuf == NULL) || (Buf == NULL) || (AppendBuf->buf == NULL))
691                 return;
692
693         if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1)
694                 IncreaseBuf(Buf, 
695                             (Buf->BufUsed > 0), 
696                             AppendBuf->BufUsed + Buf->BufUsed);
697
698         memcpy(Buf->buf + Buf->BufUsed, 
699                AppendBuf->buf + Offset, 
700                AppendBuf->BufUsed - Offset);
701         Buf->BufUsed += AppendBuf->BufUsed - Offset;
702         Buf->buf[Buf->BufUsed] = '\0';
703 }
704
705
706 /**
707  * @ingroup StrBuf_Filler
708  * @brief Append a C-String to the buffer
709  * @param Buf Buffer to modify
710  * @param AppendBuf Buffer to copy at the end of our buffer
711  * @param AppendSize number of bytes to copy; set to -1 if we should count it in advance
712  * @param Offset Should we start copying from an offset?
713  */
714 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset)
715 {
716         long aps;
717         long BufSizeRequired;
718
719         if ((AppendBuf == NULL) || (Buf == NULL))
720                 return;
721
722         if (AppendSize < 0 )
723                 aps = strlen(AppendBuf + Offset);
724         else
725                 aps = AppendSize - Offset;
726
727         BufSizeRequired = Buf->BufUsed + aps + 1;
728         if (Buf->BufSize <= BufSizeRequired)
729                 IncreaseBuf(Buf, (Buf->BufUsed > 0), BufSizeRequired);
730
731         memcpy(Buf->buf + Buf->BufUsed, 
732                AppendBuf + Offset, 
733                aps);
734         Buf->BufUsed += aps;
735         Buf->buf[Buf->BufUsed] = '\0';
736 }
737
738 /**
739  * @ingroup StrBuf_Filler
740  * @brief sprintf like function appending the formated string to the buffer
741  * vsnprintf version to wrap into own calls
742  * @param Buf Buffer to extend by format and Params
743  * @param format printf alike format to add
744  * @param ap va_list containing the items for format
745  */
746 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
747 {
748         va_list apl;
749         size_t BufSize;
750         size_t nWritten;
751         size_t Offset;
752         size_t newused;
753
754         if ((Buf == NULL)  || (format == NULL))
755                 return;
756
757         BufSize = Buf->BufSize;
758         nWritten = Buf->BufSize + 1;
759         Offset = Buf->BufUsed;
760         newused = Offset + nWritten;
761         
762         while (newused >= BufSize) {
763                 va_copy(apl, ap);
764                 nWritten = vsnprintf(Buf->buf + Offset, 
765                                      Buf->BufSize - Offset, 
766                                      format, apl);
767                 va_end(apl);
768                 newused = Offset + nWritten;
769                 if (newused >= Buf->BufSize) {
770                         IncreaseBuf(Buf, 1, newused);
771                         newused = Buf->BufSize + 1;
772                 }
773                 else {
774                         Buf->BufUsed = Offset + nWritten;
775                         BufSize = Buf->BufSize;
776                 }
777
778         }
779 }
780
781 /**
782  * @ingroup StrBuf_Filler
783  * @brief sprintf like function appending the formated string to the buffer
784  * @param Buf Buffer to extend by format and Params
785  * @param format printf alike format to add
786  */
787 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
788 {
789         size_t BufSize;
790         size_t nWritten;
791         size_t Offset;
792         size_t newused;
793         va_list arg_ptr;
794         
795         if ((Buf == NULL)  || (format == NULL))
796                 return;
797
798         BufSize = Buf->BufSize;
799         nWritten = Buf->BufSize + 1;
800         Offset = Buf->BufUsed;
801         newused = Offset + nWritten;
802
803         while (newused >= BufSize) {
804                 va_start(arg_ptr, format);
805                 nWritten = vsnprintf(Buf->buf + Buf->BufUsed, 
806                                      Buf->BufSize - Buf->BufUsed, 
807                                      format, arg_ptr);
808                 va_end(arg_ptr);
809                 newused = Buf->BufUsed + nWritten;
810                 if (newused >= Buf->BufSize) {
811                         IncreaseBuf(Buf, 1, newused);
812                         newused = Buf->BufSize + 1;
813                 }
814                 else {
815                         Buf->BufUsed += nWritten;
816                         BufSize = Buf->BufSize;
817                 }
818
819         }
820 }
821
822 /**
823  * @ingroup StrBuf_Filler
824  * @brief sprintf like function putting the formated string into the buffer
825  * @param Buf Buffer to extend by format and Parameters
826  * @param format printf alike format to add
827  */
828 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
829 {
830         size_t nWritten;
831         va_list arg_ptr;
832         
833         if ((Buf == NULL)  || (format == NULL))
834                 return;
835
836         nWritten = Buf->BufSize + 1;
837         while (nWritten >= Buf->BufSize) {
838                 va_start(arg_ptr, format);
839                 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
840                 va_end(arg_ptr);
841                 if (nWritten >= Buf->BufSize) {
842                         IncreaseBuf(Buf, 0, 0);
843                         nWritten = Buf->BufSize + 1;
844                         continue;
845                 }
846                 Buf->BufUsed = nWritten ;
847         }
848 }
849
850 /**
851  * @ingroup StrBuf_Filler
852  * @brief Callback for cURL to append the webserver reply to a buffer
853  * @param ptr pre-defined by the cURL API; see man 3 curl for mre info
854  * @param size pre-defined by the cURL API; see man 3 curl for mre info
855  * @param nmemb pre-defined by the cURL API; see man 3 curl for mre info
856  * @param stream pre-defined by the cURL API; see man 3 curl for mre info
857  */
858 size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
859 {
860
861         StrBuf *Target;
862
863         Target = stream;
864         if (ptr == NULL)
865                 return 0;
866
867         StrBufAppendBufPlain(Target, ptr, size * nmemb, 0);
868         return size * nmemb;
869 }
870
871
872 /**
873  * @ingroup StrBuf
874  * @brief extracts a substring from Source into dest
875  * @param dest buffer to place substring into
876  * @param Source string to copy substring from
877  * @param Offset chars to skip from start
878  * @param nChars number of chars to copy
879  * @returns the number of chars copied; may be different from nChars due to the size of Source
880  */
881 int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
882 {
883         size_t NCharsRemain;
884         if (Offset > Source->BufUsed)
885         {
886                 if (dest != NULL)
887                         FlushStrBuf(dest);
888                 return 0;
889         }
890         if (Offset + nChars < Source->BufUsed)
891         {
892                 if (nChars >= dest->BufSize)
893                         IncreaseBuf(dest, 0, nChars + 1);
894                 memcpy(dest->buf, Source->buf + Offset, nChars);
895                 dest->BufUsed = nChars;
896                 dest->buf[dest->BufUsed] = '\0';
897                 return nChars;
898         }
899         NCharsRemain = Source->BufUsed - Offset;
900         if (NCharsRemain  >= dest->BufSize)
901                 IncreaseBuf(dest, 0, NCharsRemain + 1);
902         memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
903         dest->BufUsed = NCharsRemain;
904         dest->buf[dest->BufUsed] = '\0';
905         return NCharsRemain;
906 }
907
908 /**
909  * @ingroup StrBuf
910  * @brief Cut nChars from the start of the string
911  * @param Buf Buffer to modify
912  * @param nChars how many chars should be skipped?
913  */
914 void StrBufCutLeft(StrBuf *Buf, int nChars)
915 {
916         if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
917         if (nChars >= Buf->BufUsed) {
918                 FlushStrBuf(Buf);
919                 return;
920         }
921         memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
922         Buf->BufUsed -= nChars;
923         Buf->buf[Buf->BufUsed] = '\0';
924 }
925
926 /**
927  * @ingroup StrBuf
928  * @brief Cut the trailing n Chars from the string
929  * @param Buf Buffer to modify
930  * @param nChars how many chars should be trunkated?
931  */
932 void StrBufCutRight(StrBuf *Buf, int nChars)
933 {
934         if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
935         if (nChars >= Buf->BufUsed) {
936                 FlushStrBuf(Buf);
937                 return;
938         }
939         Buf->BufUsed -= nChars;
940         Buf->buf[Buf->BufUsed] = '\0';
941 }
942
943 /**
944  * @ingroup StrBuf
945  * @brief Cut the string after n Chars
946  * @param Buf Buffer to modify
947  * @param AfternChars after how many chars should we trunkate the string?
948  * @param At if non-null and points inside of our string, cut it there.
949  */
950 void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
951 {
952         if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
953         if (At != NULL){
954                 AfternChars = At - Buf->buf;
955         }
956
957         if ((AfternChars < 0) || (AfternChars >= Buf->BufUsed))
958                 return;
959         Buf->BufUsed = AfternChars;
960         Buf->buf[Buf->BufUsed] = '\0';
961 }
962
963
964 /**
965  * @ingroup StrBuf
966  * @brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
967  * @param Buf the string to modify
968  */
969 void StrBufTrim(StrBuf *Buf)
970 {
971         int delta = 0;
972         if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
973
974         while ((Buf->BufUsed > 0) &&
975                isspace(Buf->buf[Buf->BufUsed - 1]))
976         {
977                 Buf->BufUsed --;
978         }
979         Buf->buf[Buf->BufUsed] = '\0';
980
981         if (Buf->BufUsed == 0) return;
982
983         while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
984                 delta ++;
985         }
986         if (delta > 0) StrBufCutLeft(Buf, delta);
987 }
988
989 void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
990 {
991         const char *pBuff;
992         const char *pLeft;
993         const char *pRight;
994
995         if (Buf == NULL)
996                 return;
997         pLeft = pBuff = Buf->buf;
998         while (pBuff != NULL) {
999                 pLeft = pBuff;
1000                 pBuff = strchr(pBuff, leftboundary);
1001                 if (pBuff != NULL)
1002                         pBuff++;
1003         }
1004                 
1005         if (pLeft != NULL)
1006                 pBuff = pLeft;
1007         else
1008                 pBuff = Buf->buf;
1009         pRight = strchr(pBuff, rightboundary);
1010         if (pRight != NULL)
1011                 StrBufCutAt(Buf, 0, pRight);
1012         if (pLeft != NULL)
1013                 StrBufCutLeft(Buf, pLeft - Buf->buf);
1014 }
1015
1016
1017 /**
1018  * @ingroup StrBuf_Filler
1019  * @brief uppercase the contents of a buffer
1020  * @param Buf the buffer to translate
1021  */
1022 void StrBufUpCase(StrBuf *Buf) 
1023 {
1024         char *pch, *pche;
1025
1026         if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1027
1028         pch = Buf->buf;
1029         pche = pch + Buf->BufUsed;
1030         while (pch < pche) {
1031                 *pch = toupper(*pch);
1032                 pch ++;
1033         }
1034 }
1035
1036
1037 /**
1038  * @ingroup StrBuf_Filler
1039  * @brief lowercase the contents of a buffer
1040  * @param Buf the buffer to translate
1041  */
1042 void StrBufLowerCase(StrBuf *Buf) 
1043 {
1044         char *pch, *pche;
1045
1046         if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1047
1048         pch = Buf->buf;
1049         pche = pch + Buf->BufUsed;
1050         while (pch < pche) {
1051                 *pch = tolower(*pch);
1052                 pch ++;
1053         }
1054 }
1055
1056
1057 /*******************************************************************************
1058  *           a tokenizer that kills, maims, and destroys                       *
1059  *******************************************************************************/
1060
1061 /**
1062  * @ingroup StrBuf_Tokenizer
1063  * @brief Replace a token at a given place with a given length by another token with given length
1064  * @param Buf String where to work on
1065  * @param where where inside of the Buf is the search-token
1066  * @param HowLong How long is the token to be replaced
1067  * @param Repl Token to insert at 'where'
1068  * @param ReplLen Length of repl
1069  * @returns -1 if fail else length of resulting Buf
1070  */
1071 int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, 
1072                        const char *Repl, long ReplLen)
1073 {
1074
1075         if ((Buf == NULL) || 
1076             (where > Buf->BufUsed) ||
1077             (where + HowLong > Buf->BufUsed))
1078                 return -1;
1079
1080         if (where + ReplLen - HowLong > Buf->BufSize)
1081                 if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0)
1082                         return -1;
1083
1084         memmove(Buf->buf + where + ReplLen, 
1085                 Buf->buf + where + HowLong,
1086                 Buf->BufUsed - where - HowLong);
1087                                                 
1088         memcpy(Buf->buf + where, 
1089                Repl, ReplLen);
1090
1091         Buf->BufUsed += ReplLen - HowLong;
1092
1093         return Buf->BufUsed;
1094 }
1095
1096 /**
1097  * @ingroup StrBuf_Tokenizer
1098  * @brief Counts the numbmer of tokens in a buffer
1099  * @param source String to count tokens in
1100  * @param tok    Tokenizer char to count
1101  * @returns numbers of tokenizer chars found
1102  */
1103 int StrBufNum_tokens(const StrBuf *source, char tok)
1104 {
1105         char *pch, *pche;
1106         long NTokens;
1107         if ((source == NULL) || (source->BufUsed == 0))
1108                 return 0;
1109         if ((source->BufUsed == 1) && (*source->buf == tok))
1110                 return 2;
1111         NTokens = 1;
1112         pch = source->buf;
1113         pche = pch + source->BufUsed;
1114         while (pch < pche)
1115         {
1116                 if (*pch == tok)
1117                         NTokens ++;
1118                 pch ++;
1119         }
1120         return NTokens;
1121 }
1122
1123 /**
1124  * @ingroup StrBuf_Tokenizer
1125  * @brief a string tokenizer
1126  * @param Source StringBuffer to read into
1127  * @param parmnum n'th Parameter to remove
1128  * @param separator tokenizer character
1129  * @returns -1 if not found, else length of token.
1130  */
1131 int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
1132 {
1133         int ReducedBy;
1134         char *d, *s, *end;              /* dest, source */
1135         int count = 0;
1136
1137         /* Find desired @parameter */
1138         end = Source->buf + Source->BufUsed;
1139         d = Source->buf;
1140         while ((d <= end) && 
1141                (count < parmnum))
1142         {
1143                 /* End of string, bail! */
1144                 if (!*d) {
1145                         d = NULL;
1146                         break;
1147                 }
1148                 if (*d == separator) {
1149                         count++;
1150                 }
1151                 d++;
1152         }
1153         if ((d == NULL) || (d >= end))
1154                 return 0;               /* @Parameter not found */
1155
1156         /* Find next @parameter */
1157         s = d;
1158         while ((s <= end) && 
1159                (*s && *s != separator))
1160         {
1161                 s++;
1162         }
1163         if (*s == separator)
1164                 s++;
1165         ReducedBy = d - s;
1166
1167         /* Hack and slash */
1168         if (s >= end) {
1169                 return 0;
1170         }
1171         else if (*s) {
1172                 memmove(d, s, Source->BufUsed - (s - Source->buf));
1173                 Source->BufUsed += ReducedBy;
1174                 Source->buf[Source->BufUsed] = '\0';
1175         }
1176         else if (d == Source->buf) {
1177                 *d = 0;
1178                 Source->BufUsed = 0;
1179         }
1180         else {
1181                 *--d = '\0';
1182                 Source->BufUsed += ReducedBy;
1183         }
1184         /*
1185         while (*s) {
1186                 *d++ = *s++;
1187         }
1188         *d = 0;
1189         */
1190         return ReducedBy;
1191 }
1192
1193
1194 /**
1195  * @ingroup StrBuf_Tokenizer
1196  * @brief a string tokenizer
1197  * @param dest Destination StringBuffer
1198  * @param Source StringBuffer to read into
1199  * @param parmnum n'th Parameter to extract
1200  * @param separator tokenizer character
1201  * @returns -1 if not found, else length of token.
1202  */
1203 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
1204 {
1205         const char *s, *e;              //* source * /
1206         int len = 0;                    //* running total length of extracted string * /
1207         int current_token = 0;          //* token currently being processed * /
1208          
1209         if (dest != NULL) {
1210                 dest->buf[0] = '\0';
1211                 dest->BufUsed = 0;
1212         }
1213         else
1214                 return(-1);
1215
1216         if ((Source == NULL) || (Source->BufUsed ==0)) {
1217                 return(-1);
1218         }
1219         s = Source->buf;
1220         e = s + Source->BufUsed;
1221
1222         //cit_backtrace();
1223         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1224
1225         while ((s<e) && !IsEmptyStr(s)) {
1226                 if (*s == separator) {
1227                         ++current_token;
1228                 }
1229                 if (len >= dest->BufSize) {
1230                         dest->BufUsed = len;
1231                         if (IncreaseBuf(dest, 1, -1) < 0) {
1232                                 dest->BufUsed --;
1233                                 break;
1234                         }
1235                 }
1236                 if ( (current_token == parmnum) && 
1237                      (*s != separator)) {
1238                         dest->buf[len] = *s;
1239                         ++len;
1240                 }
1241                 else if (current_token > parmnum) {
1242                         break;
1243                 }
1244                 ++s;
1245         }
1246         
1247         dest->buf[len] = '\0';
1248         dest->BufUsed = len;
1249                 
1250         if (current_token < parmnum) {
1251                 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
1252                 return(-1);
1253         }
1254         //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
1255         return(len);
1256 }
1257
1258
1259
1260
1261
1262 /**
1263  * @ingroup StrBuf_Tokenizer
1264  * @brief a string tokenizer to fetch an integer
1265  * @param Source String containing tokens
1266  * @param parmnum n'th Parameter to extract
1267  * @param separator tokenizer character
1268  * @returns 0 if not found, else integer representation of the token
1269  */
1270 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
1271 {
1272         StrBuf tmp;
1273         char buf[64];
1274         
1275         tmp.buf = buf;
1276         buf[0] = '\0';
1277         tmp.BufSize = 64;
1278         tmp.BufUsed = 0;
1279         tmp.ConstBuf = 1;
1280         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1281                 return(atoi(buf));
1282         else
1283                 return 0;
1284 }
1285
1286 /**
1287  * @ingroup StrBuf_Tokenizer
1288  * @brief a string tokenizer to fetch a long integer
1289  * @param Source String containing tokens
1290  * @param parmnum n'th Parameter to extract
1291  * @param separator tokenizer character
1292  * @returns 0 if not found, else long integer representation of the token
1293  */
1294 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
1295 {
1296         StrBuf tmp;
1297         char buf[64];
1298         
1299         tmp.buf = buf;
1300         buf[0] = '\0';
1301         tmp.BufSize = 64;
1302         tmp.BufUsed = 0;
1303         tmp.ConstBuf = 1;
1304         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1305                 return(atoi(buf));
1306         else
1307                 return 0;
1308 }
1309
1310
1311 /**
1312  * @ingroup StrBuf_Tokenizer
1313  * @brief a string tokenizer to fetch an unsigned long
1314  * @param Source String containing tokens
1315  * @param parmnum n'th Parameter to extract
1316  * @param separator tokenizer character
1317  * @returns 0 if not found, else unsigned long representation of the token
1318  */
1319 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
1320 {
1321         StrBuf tmp;
1322         char buf[64];
1323         char *pnum;
1324         
1325         tmp.buf = buf;
1326         buf[0] = '\0';
1327         tmp.BufSize = 64;
1328         tmp.BufUsed = 0;
1329         tmp.ConstBuf = 1;
1330         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0) {
1331                 pnum = &buf[0];
1332                 if (*pnum == '-')
1333                         pnum ++;
1334                 return (unsigned long) atol(pnum);
1335         }
1336         else 
1337                 return 0;
1338 }
1339
1340
1341
1342 /**
1343  * @ingroup StrBuf_NextTokenizer
1344  * @brief a string tokenizer; Bounds checker
1345  *  function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
1346  * @param Source our tokenbuffer
1347  * @param pStart the token iterator pointer to inspect
1348  * @returns whether the revolving pointer is inside of the search range
1349  */
1350 int StrBufHaveNextToken(const StrBuf *Source, const char **pStart)
1351 {
1352         if ((Source == NULL) || 
1353             (*pStart == StrBufNOTNULL) ||
1354             (Source->BufUsed == 0))
1355         {
1356                 return 0;
1357         }
1358         if (*pStart == NULL)
1359         {
1360                 return 1;
1361         }
1362         else if (*pStart > Source->buf + Source->BufUsed)
1363         {
1364                 return 0;
1365         }
1366         else if (*pStart <= Source->buf)
1367         {
1368                 return 0;
1369         }
1370
1371         return 1;
1372 }
1373
1374 /**
1375  * @ingroup StrBuf_NextTokenizer
1376  * @brief a string tokenizer
1377  * @param dest Destination StringBuffer
1378  * @param Source StringBuffer to read into
1379  * @param pStart pointer to the end of the last token. Feed with NULL on start.
1380  * @param separator tokenizer 
1381  * @returns -1 if not found, else length of token.
1382  */
1383 int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
1384 {
1385         const char *s;          /* source */
1386         const char *EndBuffer;  /* end stop of source buffer */
1387         int current_token = 0;  /* token currently being processed */
1388         int len = 0;            /* running total length of extracted string */
1389
1390         if ((Source          == NULL) || 
1391             (Source->BufUsed == 0)      ) 
1392         {
1393                 *pStart = StrBufNOTNULL;
1394                 return -1;
1395         }
1396          
1397         EndBuffer = Source->buf + Source->BufUsed;
1398
1399         if (dest != NULL) 
1400         {
1401                 dest->buf[0] = '\0';
1402                 dest->BufUsed = 0;
1403         }
1404         else
1405         {
1406                 *pStart = EndBuffer + 1;
1407                 return -1;
1408         }
1409
1410         if (*pStart == NULL)
1411         {
1412                 *pStart = Source->buf; /* we're starting to examine this buffer. */
1413         }
1414         else if ((*pStart < Source->buf) || 
1415                  (*pStart > EndBuffer  )   ) 
1416         {
1417                 return -1; /* no more tokens to find. */
1418         }
1419
1420         s = *pStart;
1421         /* start to find the next token */
1422         while ((s <= EndBuffer)      && 
1423                (current_token == 0) ) 
1424         {
1425                 if (*s == separator) 
1426                 {
1427                         /* we found the next token */
1428                         ++current_token;
1429                 }
1430
1431                 if (len >= dest->BufSize) 
1432                 {
1433                         /* our Dest-buffer isn't big enough, increase it. */
1434                         dest->BufUsed = len;
1435
1436                         if (IncreaseBuf(dest, 1, -1) < 0) {
1437                                 /* WHUT? no more mem? bail out. */
1438                                 s = EndBuffer;
1439                                 dest->BufUsed --;
1440                                 break;
1441                         }
1442                 }
1443
1444                 if ( (current_token == 0 ) &&   /* are we in our target token? */
1445                      (!IsEmptyStr(s)     ) &&
1446                      (separator     != *s)    ) /* don't copy the token itself */
1447                 {
1448                         dest->buf[len] = *s;    /* Copy the payload */
1449                         ++len;                  /* remember the bigger size. */
1450                 }
1451
1452                 ++s;
1453         }
1454
1455         /* did we reach the end? */
1456         if ((s > EndBuffer)) {
1457                 EndBuffer = StrBufNOTNULL;
1458                 *pStart = EndBuffer;
1459         }
1460         else {
1461                 *pStart = s;  /* remember the position for the next run */
1462         }
1463
1464         /* sanitize our extracted token */
1465         dest->buf[len] = '\0';
1466         dest->BufUsed  = len;
1467
1468         return (len);
1469 }
1470
1471
1472 /**
1473  * @ingroup StrBuf_NextTokenizer
1474  * @brief a string tokenizer
1475  * @param Source StringBuffer to read from
1476  * @param pStart pointer to the end of the last token. Feed with NULL.
1477  * @param separator tokenizer character
1478  * @param nTokens number of tokens to fastforward over
1479  * @returns -1 if not found, else length of token.
1480  */
1481 int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens)
1482 {
1483         const char *s, *EndBuffer;      //* source * /
1484         int len = 0;                    //* running total length of extracted string * /
1485         int current_token = 0;          //* token currently being processed * /
1486
1487         if ((Source == NULL) || 
1488             (Source->BufUsed ==0)) {
1489                 return(-1);
1490         }
1491         if (nTokens == 0)
1492                 return Source->BufUsed;
1493
1494         if (*pStart == NULL)
1495                 *pStart = Source->buf;
1496
1497         EndBuffer = Source->buf + Source->BufUsed;
1498
1499         if ((*pStart < Source->buf) || 
1500             (*pStart >  EndBuffer)) {
1501                 return (-1);
1502         }
1503
1504
1505         s = *pStart;
1506
1507         //cit_backtrace();
1508         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1509
1510         while ((s<EndBuffer) && !IsEmptyStr(s)) {
1511                 if (*s == separator) {
1512                         ++current_token;
1513                 }
1514                 if (current_token >= nTokens) {
1515                         break;
1516                 }
1517                 ++s;
1518         }
1519         *pStart = s;
1520         (*pStart) ++;
1521
1522         return(len);
1523 }
1524
1525 /**
1526  * @ingroup StrBuf_NextTokenizer
1527  * @brief a string tokenizer to fetch an integer
1528  * @param Source StringBuffer to read from
1529  * @param pStart Cursor on the tokenstring
1530  * @param separator tokenizer character
1531  * @returns 0 if not found, else integer representation of the token
1532  */
1533 int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator)
1534 {
1535         StrBuf tmp;
1536         char buf[64];
1537         
1538         tmp.buf = buf;
1539         buf[0] = '\0';
1540         tmp.BufSize = 64;
1541         tmp.BufUsed = 0;
1542         tmp.ConstBuf = 1;
1543         if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1544                 return(atoi(buf));
1545         else
1546                 return 0;
1547 }
1548
1549 /**
1550  * @ingroup StrBuf_NextTokenizer
1551  * @brief a string tokenizer to fetch a long integer
1552  * @param Source StringBuffer to read from
1553  * @param pStart Cursor on the tokenstring
1554  * @param separator tokenizer character
1555  * @returns 0 if not found, else long integer representation of the token
1556  */
1557 long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator)
1558 {
1559         StrBuf tmp;
1560         char buf[64];
1561         
1562         tmp.buf = buf;
1563         buf[0] = '\0';
1564         tmp.BufSize = 64;
1565         tmp.BufUsed = 0;
1566         tmp.ConstBuf = 1;
1567         if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1568                 return(atoi(buf));
1569         else
1570                 return 0;
1571 }
1572
1573
1574 /**
1575  * @ingroup StrBuf_NextTokenizer
1576  * @brief a string tokenizer to fetch an unsigned long
1577  * @param Source StringBuffer to read from
1578  * @param pStart Cursor on the tokenstring
1579  * @param separator tokenizer character
1580  * @returns 0 if not found, else unsigned long representation of the token
1581  */
1582 unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator)
1583 {
1584         StrBuf tmp;
1585         char buf[64];
1586         char *pnum;
1587         
1588         tmp.buf = buf;
1589         buf[0] = '\0';
1590         tmp.BufSize = 64;
1591         tmp.BufUsed = 0;
1592         tmp.ConstBuf = 1;
1593         if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) {
1594                 pnum = &buf[0];
1595                 if (*pnum == '-')
1596                         pnum ++;
1597                 return (unsigned long) atol(pnum);
1598         }
1599         else 
1600                 return 0;
1601 }
1602
1603
1604
1605
1606
1607 /*******************************************************************************
1608  *                             Escape Appending                                *
1609  *******************************************************************************/
1610
1611 /** 
1612  * @ingroup StrBuf_DeEnCoder
1613  * @brief Escape a string for feeding out as a URL while appending it to a Buffer
1614  * @param OutBuf the output buffer
1615  * @param In Buffer to encode
1616  * @param PlainIn way in from plain old c strings
1617  */
1618 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1619 {
1620         const char *pch, *pche;
1621         char *pt, *pte;
1622         int len;
1623         
1624         if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1625                 return;
1626         if (PlainIn != NULL) {
1627                 len = strlen(PlainIn);
1628                 pch = PlainIn;
1629                 pche = pch + len;
1630         }
1631         else {
1632                 pch = In->buf;
1633                 pche = pch + In->BufUsed;
1634                 len = In->BufUsed;
1635         }
1636
1637         if (len == 0) 
1638                 return;
1639
1640         pt = OutBuf->buf + OutBuf->BufUsed;
1641         pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1642
1643         while (pch < pche) {
1644                 if (pt >= pte) {
1645                         IncreaseBuf(OutBuf, 1, -1);
1646                         pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1647                         pt = OutBuf->buf + OutBuf->BufUsed;
1648                 }
1649
1650                 if((*pch >= 'a' && *pch <= 'z') ||
1651                    (*pch >= '@' && *pch <= 'Z') || /* @ A-Z */
1652                    (*pch >= '0' && *pch <= ':') || /* 0-9 : */
1653                    (*pch == '!') || (*pch == '_') || 
1654                    (*pch == ',') || (*pch == '.'))
1655                 {
1656                         *(pt++) = *(pch++);
1657                         OutBuf->BufUsed++;
1658                 }                       
1659                 else {
1660                         *pt = '%';
1661                         *(pt + 1) = HexList[(unsigned char)*pch][0];
1662                         *(pt + 2) = HexList[(unsigned char)*pch][1];
1663                         pt += 3;
1664                         OutBuf->BufUsed += 3;
1665                         pch ++;
1666                 }
1667         }
1668         *pt = '\0';
1669 }
1670
1671 /** 
1672  * @ingroup StrBuf_DeEnCoder
1673  * @brief append a string in hex encoding to the buffer
1674  * @param OutBuf the output buffer
1675  * @param In Buffer to encode
1676  * @param PlainIn way in from plain old c strings
1677  */
1678 void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1679 {
1680         const char *pch, *pche;
1681         char *pt, *pte;
1682         int len;
1683         
1684         if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1685                 return;
1686         if (PlainIn != NULL) {
1687                 len = strlen(PlainIn);
1688                 pch = PlainIn;
1689                 pche = pch + len;
1690         }
1691         else {
1692                 pch = In->buf;
1693                 pche = pch + In->BufUsed;
1694                 len = In->BufUsed;
1695         }
1696
1697         if (len == 0) 
1698                 return;
1699
1700         pt = OutBuf->buf + OutBuf->BufUsed;
1701         pte = OutBuf->buf + OutBuf->BufSize - 3; /**< we max append 3 chars at once plus the \0 */
1702
1703         while (pch < pche) {
1704                 if (pt >= pte) {
1705                         IncreaseBuf(OutBuf, 1, -1);
1706                         pte = OutBuf->buf + OutBuf->BufSize - 3; /**< we max append 3 chars at once plus the \0 */
1707                         pt = OutBuf->buf + OutBuf->BufUsed;
1708                 }
1709
1710                 *pt = HexList[(unsigned char)*pch][0];
1711                 pt ++;
1712                 *pt = HexList[(unsigned char)*pch][1];
1713                 pt ++; pch ++; OutBuf->BufUsed += 2;
1714         }
1715         *pt = '\0';
1716 }
1717
1718 /**
1719  * @ingroup StrBuf_DeEnCoder
1720  * @brief Append a string, escaping characters which have meaning in HTML.  
1721  *
1722  * @param Target        target buffer
1723  * @param Source        source buffer; set to NULL if you just have a C-String
1724  * @param PlainIn       Plain-C string to append; set to NULL if unused
1725  * @param nbsp          If nonzero, spaces are converted to non-breaking spaces.
1726  * @param nolinebreaks  if set to 1, linebreaks are removed from the string.
1727  *                      if set to 2, linebreaks are replaced by &ltbr/&gt
1728  */
1729 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
1730 {
1731         const char *aptr, *eiptr;
1732         char *bptr, *eptr;
1733         long len;
1734
1735         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1736                 return -1;
1737
1738         if (PlainIn != NULL) {
1739                 aptr = PlainIn;
1740                 len = strlen(PlainIn);
1741                 eiptr = aptr + len;
1742         }
1743         else {
1744                 aptr = Source->buf;
1745                 eiptr = aptr + Source->BufUsed;
1746                 len = Source->BufUsed;
1747         }
1748
1749         if (len == 0) 
1750                 return -1;
1751
1752         bptr = Target->buf + Target->BufUsed;
1753         eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in...  */
1754
1755         while (aptr < eiptr){
1756                 if(bptr >= eptr) {
1757                         IncreaseBuf(Target, 1, -1);
1758                         eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in...  */
1759                         bptr = Target->buf + Target->BufUsed;
1760                 }
1761                 if (*aptr == '<') {
1762                         memcpy(bptr, "&lt;", 4);
1763                         bptr += 4;
1764                         Target->BufUsed += 4;
1765                 }
1766                 else if (*aptr == '>') {
1767                         memcpy(bptr, "&gt;", 4);
1768                         bptr += 4;
1769                         Target->BufUsed += 4;
1770                 }
1771                 else if (*aptr == '&') {
1772                         memcpy(bptr, "&amp;", 5);
1773                         bptr += 5;
1774                         Target->BufUsed += 5;
1775                 }
1776                 else if (*aptr == '"') {
1777                         memcpy(bptr, "&quot;", 6);
1778                         bptr += 6;
1779                         Target->BufUsed += 6;
1780                 }
1781                 else if (*aptr == '\'') {
1782                         memcpy(bptr, "&#39;", 5);
1783                         bptr += 5;
1784                         Target->BufUsed += 5;
1785                 }
1786                 else if (*aptr == LB) {
1787                         *bptr = '<';
1788                         bptr ++;
1789                         Target->BufUsed ++;
1790                 }
1791                 else if (*aptr == RB) {
1792                         *bptr = '>';
1793                         bptr ++;
1794                         Target->BufUsed ++;
1795                 }
1796                 else if (*aptr == QU) {
1797                         *bptr ='"';
1798                         bptr ++;
1799                         Target->BufUsed ++;
1800                 }
1801                 else if ((*aptr == 32) && (nbsp == 1)) {
1802                         memcpy(bptr, "&nbsp;", 6);
1803                         bptr += 6;
1804                         Target->BufUsed += 6;
1805                 }
1806                 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
1807                         *bptr='\0';     /* nothing */
1808                 }
1809                 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
1810                         memcpy(bptr, "&lt;br/&gt;", 11);
1811                         bptr += 11;
1812                         Target->BufUsed += 11;
1813                 }
1814
1815
1816                 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
1817                         *bptr='\0';     /* nothing */
1818                 }
1819                 else{
1820                         *bptr = *aptr;
1821                         bptr++;
1822                         Target->BufUsed ++;
1823                 }
1824                 aptr ++;
1825         }
1826         *bptr = '\0';
1827         if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
1828                 return -1;
1829         return Target->BufUsed;
1830 }
1831
1832 /**
1833  * @ingroup StrBuf_DeEnCoder
1834  * @brief Append a string, escaping characters which have meaning in HTML.  
1835  * Converts linebreaks into blanks; escapes single quotes
1836  * @param Target        target buffer
1837  * @param Source        source buffer; set to NULL if you just have a C-String
1838  * @param PlainIn       Plain-C string to append; set to NULL if unused
1839  */
1840 void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1841 {
1842         const char *aptr, *eiptr;
1843         char *tptr, *eptr;
1844         long len;
1845
1846         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1847                 return ;
1848
1849         if (PlainIn != NULL) {
1850                 aptr = PlainIn;
1851                 len = strlen(PlainIn);
1852                 eiptr = aptr + len;
1853         }
1854         else {
1855                 aptr = Source->buf;
1856                 eiptr = aptr + Source->BufUsed;
1857                 len = Source->BufUsed;
1858         }
1859
1860         if (len == 0) 
1861                 return;
1862
1863         eptr = Target->buf + Target->BufSize - 8; 
1864         tptr = Target->buf + Target->BufUsed;
1865         
1866         while (aptr < eiptr){
1867                 if(tptr >= eptr) {
1868                         IncreaseBuf(Target, 1, -1);
1869                         eptr = Target->buf + Target->BufSize - 8; 
1870                         tptr = Target->buf + Target->BufUsed;
1871                 }
1872                
1873                 if (*aptr == '\n') {
1874                         *tptr = ' ';
1875                         Target->BufUsed++;
1876                 }
1877                 else if (*aptr == '\r') {
1878                         *tptr = ' ';
1879                         Target->BufUsed++;
1880                 }
1881                 else if (*aptr == '\'') {
1882                         *(tptr++) = '&';
1883                         *(tptr++) = '#';
1884                         *(tptr++) = '3';
1885                         *(tptr++) = '9';
1886                         *tptr = ';';
1887                         Target->BufUsed += 5;
1888                 } else {
1889                         *tptr = *aptr;
1890                         Target->BufUsed++;
1891                 }
1892                 tptr++; aptr++;
1893         }
1894         *tptr = '\0';
1895 }
1896
1897
1898
1899 /**
1900  * @ingroup StrBuf_DeEnCoder
1901  * @brief Append a string, escaping characters which have meaning in ICAL.  
1902  * [\n,] 
1903  * @param Target        target buffer
1904  * @param Source        source buffer; set to NULL if you just have a C-String
1905  * @param PlainIn       Plain-C string to append; set to NULL if unused
1906  */
1907 void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1908 {
1909         const char *aptr, *eiptr;
1910         char *tptr, *eptr;
1911         long len;
1912
1913         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1914                 return ;
1915
1916         if (PlainIn != NULL) {
1917                 aptr = PlainIn;
1918                 len = strlen(PlainIn);
1919                 eiptr = aptr + len;
1920         }
1921         else {
1922                 aptr = Source->buf;
1923                 eiptr = aptr + Source->BufUsed;
1924                 len = Source->BufUsed;
1925         }
1926
1927         if (len == 0) 
1928                 return;
1929
1930         eptr = Target->buf + Target->BufSize - 8; 
1931         tptr = Target->buf + Target->BufUsed;
1932         
1933         while (aptr < eiptr){
1934                 if(tptr + 3 >= eptr) {
1935                         IncreaseBuf(Target, 1, -1);
1936                         eptr = Target->buf + Target->BufSize - 8; 
1937                         tptr = Target->buf + Target->BufUsed;
1938                 }
1939                
1940                 if (*aptr == '\n') {
1941                         *tptr = '\\';
1942                         Target->BufUsed++;
1943                         tptr++;
1944                         *tptr = 'n';
1945                         Target->BufUsed++;
1946                 }
1947                 else if (*aptr == '\r') {
1948                         *tptr = '\\';
1949                         Target->BufUsed++;
1950                         tptr++;
1951                         *tptr = 'r';
1952                         Target->BufUsed++;
1953                 }
1954                 else if (*aptr == ',') {
1955                         *tptr = '\\';
1956                         Target->BufUsed++;
1957                         tptr++;
1958                         *tptr = ',';
1959                         Target->BufUsed++;
1960                 } else {
1961                         *tptr = *aptr;
1962                         Target->BufUsed++;
1963                 }
1964                 tptr++; aptr++;
1965         }
1966         *tptr = '\0';
1967 }
1968
1969 /**
1970  * @ingroup StrBuf_DeEnCoder
1971  * @brief Append a string, escaping characters which have meaning in JavaScript strings .  
1972  *
1973  * @param Target        target buffer
1974  * @param Source        source buffer; set to NULL if you just have a C-String
1975  * @param PlainIn       Plain-C string to append; set to NULL if unused
1976  * @returns size of result or -1
1977  */
1978 long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1979 {
1980         const char *aptr, *eiptr;
1981         char *bptr, *eptr;
1982         long len;
1983
1984         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1985                 return -1;
1986
1987         if (PlainIn != NULL) {
1988                 aptr = PlainIn;
1989                 len = strlen(PlainIn);
1990                 eiptr = aptr + len;
1991         }
1992         else {
1993                 aptr = Source->buf;
1994                 eiptr = aptr + Source->BufUsed;
1995                 len = Source->BufUsed;
1996         }
1997
1998         if (len == 0) 
1999                 return -1;
2000
2001         bptr = Target->buf + Target->BufUsed;
2002         eptr = Target->buf + Target->BufSize - 3; /* our biggest unit to put in...  */
2003
2004         while (aptr < eiptr){
2005                 if(bptr >= eptr) {
2006                         IncreaseBuf(Target, 1, -1);
2007                         eptr = Target->buf + Target->BufSize - 3; 
2008                         bptr = Target->buf + Target->BufUsed;
2009                 }
2010                 if (*aptr == '"') {
2011                         *bptr = '\\';
2012                         bptr ++;
2013                         *bptr = '"';
2014                         bptr ++;
2015                         Target->BufUsed += 2;
2016                 } else if (*aptr == '\\') {
2017                         *bptr = '\\';
2018                         bptr ++;
2019                         *bptr = '\\';
2020                         bptr ++;
2021                         Target->BufUsed += 2;
2022                 }
2023                 else{
2024                         *bptr = *aptr;
2025                         bptr++;
2026                         Target->BufUsed ++;
2027                 }
2028                 aptr ++;
2029         }
2030         *bptr = '\0';
2031         if ((bptr == eptr - 1 ) && !IsEmptyStr(aptr) )
2032                 return -1;
2033         return Target->BufUsed;
2034 }
2035
2036 /**
2037  * @ingroup StrBuf_DeEnCoder
2038  * @brief Append a string, escaping characters which have meaning in HTML + json.  
2039  *
2040  * @param Target        target buffer
2041  * @param Source        source buffer; set to NULL if you just have a C-String
2042  * @param PlainIn       Plain-C string to append; set to NULL if unused
2043  * @param nbsp          If nonzero, spaces are converted to non-breaking spaces.
2044  * @param nolinebreaks  if set to 1, linebreaks are removed from the string.
2045  *                      if set to 2, linebreaks are replaced by &ltbr/&gt
2046  */
2047 long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
2048 {
2049         const char *aptr, *eiptr;
2050         char *bptr, *eptr;
2051         long len;
2052         int IsUtf8Sequence = 0;
2053
2054         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2055                 return -1;
2056
2057         if (PlainIn != NULL) {
2058                 aptr = PlainIn;
2059                 len = strlen(PlainIn);
2060                 eiptr = aptr + len;
2061         }
2062         else {
2063                 aptr = Source->buf;
2064                 eiptr = aptr + Source->BufUsed;
2065                 len = Source->BufUsed;
2066         }
2067
2068         if (len == 0) 
2069                 return -1;
2070
2071         bptr = Target->buf + Target->BufUsed;
2072         eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in...  */
2073
2074         while (aptr < eiptr){
2075                 if(bptr >= eptr) {
2076                         IncreaseBuf(Target, 1, -1);
2077                         eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in...  */
2078                         bptr = Target->buf + Target->BufUsed;
2079                 }
2080                 if (*aptr == '<') {
2081                         memcpy(bptr, "&lt;", 4);
2082                         bptr += 4;
2083                         Target->BufUsed += 4;
2084                 }
2085                 else if (*aptr == '>') {
2086                         memcpy(bptr, "&gt;", 4);
2087                         bptr += 4;
2088                         Target->BufUsed += 4;
2089                 }
2090                 else if (*aptr == '&') {
2091                         memcpy(bptr, "&amp;", 5);
2092                         bptr += 5;
2093                         Target->BufUsed += 5;
2094                 }
2095                 else if (*aptr == LB) {
2096                         *bptr = '<';
2097                         bptr ++;
2098                         Target->BufUsed ++;
2099                 }
2100                 else if (*aptr == RB) {
2101                         *bptr = '>';
2102                         bptr ++;
2103                         Target->BufUsed ++;
2104                 }
2105                 else if ((*aptr == 32) && (nbsp == 1)) {
2106                         memcpy(bptr, "&nbsp;", 6);
2107                         bptr += 6;
2108                         Target->BufUsed += 6;
2109                 }
2110                 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
2111                         *bptr='\0';     /* nothing */
2112                 }
2113                 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
2114                         memcpy(bptr, "&lt;br/&gt;", 11);
2115                         bptr += 11;
2116                         Target->BufUsed += 11;
2117                 }
2118
2119                 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
2120                         *bptr='\0';     /* nothing */
2121                 }
2122
2123                 else if ((*aptr == '"') || (*aptr == QU)) {
2124                         *bptr = '\\';
2125                         bptr ++;
2126                         *bptr = '"';
2127                         bptr ++;
2128                         Target->BufUsed += 2;
2129                 } else if (*aptr == '\\') {
2130                         *bptr = '\\';
2131                         bptr ++;
2132                         *bptr = '\\';
2133                         bptr ++;
2134                         Target->BufUsed += 2;
2135                 }
2136                 else {
2137                         if (((unsigned char)*aptr) >= 0x20)
2138                         {
2139                                 IsUtf8Sequence =  Ctdl_GetUtf8SequenceLength(aptr, eiptr);
2140                                 
2141                                 *bptr = *aptr;
2142                                 Target->BufUsed ++;
2143                                 while (IsUtf8Sequence > 1){
2144                                         if(bptr + IsUtf8Sequence >= eptr) {
2145                                                 IncreaseBuf(Target, 1, -1);
2146                                                 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in...  */
2147                                                 bptr = Target->buf + Target->BufUsed - 1;
2148                                         }
2149                                         bptr++; aptr++;
2150                                         IsUtf8Sequence --;
2151                                         *bptr = *aptr;
2152                                         Target->BufUsed ++;
2153                                 }
2154                                 bptr++;
2155                         }
2156
2157                 }
2158                 aptr ++;
2159         }
2160         *bptr = '\0';
2161         if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
2162                 return -1;
2163         return Target->BufUsed;
2164 }
2165
2166 /**
2167  * @ingroup StrBuf_DeEnCoder
2168  * @brief unhide special chars hidden to the HTML escaper
2169  * @param target buffer to put the unescaped string in
2170  * @param source buffer to unescape
2171  */
2172 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source) 
2173 {
2174         int a, b, len;
2175         char hex[3];
2176
2177         if (target != NULL)
2178                 FlushStrBuf(target);
2179
2180         if (source == NULL ||target == NULL)
2181         {
2182                 return;
2183         }
2184
2185         len = source->BufUsed;
2186         for (a = 0; a < len; ++a) {
2187                 if (target->BufUsed >= target->BufSize)
2188                         IncreaseBuf(target, 1, -1);
2189
2190                 if (source->buf[a] == '=') {
2191                         hex[0] = source->buf[a + 1];
2192                         hex[1] = source->buf[a + 2];
2193                         hex[2] = 0;
2194                         b = 0;
2195                         sscanf(hex, "%02x", &b);
2196                         target->buf[target->BufUsed] = b;
2197                         target->buf[++target->BufUsed] = 0;
2198                         a += 2;
2199                 }
2200                 else {
2201                         target->buf[target->BufUsed] = source->buf[a];
2202                         target->buf[++target->BufUsed] = 0;
2203                 }
2204         }
2205 }
2206
2207
2208 /**
2209  * @ingroup StrBuf_DeEnCoder
2210  * @brief hide special chars from the HTML escapers and friends
2211  * @param target buffer to put the escaped string in
2212  * @param source buffer to escape
2213  */
2214 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source) 
2215 {
2216         int i, len;
2217
2218         if (target != NULL)
2219                 FlushStrBuf(target);
2220
2221         if (source == NULL ||target == NULL)
2222         {
2223                 return;
2224         }
2225
2226         len = source->BufUsed;
2227         for (i=0; i<len; ++i) {
2228                 if (target->BufUsed + 4 >= target->BufSize)
2229                         IncreaseBuf(target, 1, -1);
2230                 if ( (isalnum(source->buf[i])) || 
2231                      (source->buf[i]=='-') || 
2232                      (source->buf[i]=='_') ) {
2233                         target->buf[target->BufUsed++] = source->buf[i];
2234                 }
2235                 else {
2236                         sprintf(&target->buf[target->BufUsed], 
2237                                 "=%02X", 
2238                                 (0xFF &source->buf[i]));
2239                         target->BufUsed += 3;
2240                 }
2241         }
2242         target->buf[target->BufUsed + 1] = '\0';
2243 }
2244
2245
2246 /*******************************************************************************
2247  *                      Quoted Printable de/encoding                           *
2248  *******************************************************************************/
2249
2250 /**
2251  * @ingroup StrBuf_DeEnCoder
2252  * @brief decode a buffer from base 64 encoding; destroys original
2253  * @param Buf Buffor to transform
2254  */
2255 int StrBufDecodeBase64(StrBuf *Buf)
2256 {
2257         char *xferbuf;
2258         size_t siz;
2259         if (Buf == NULL) return -1;
2260
2261         xferbuf = (char*) malloc(Buf->BufSize);
2262         *xferbuf = '\0';
2263         siz = CtdlDecodeBase64(xferbuf,
2264                                Buf->buf,
2265                                Buf->BufUsed);
2266         free(Buf->buf);
2267         Buf->buf = xferbuf;
2268         Buf->BufUsed = siz;
2269         return siz;
2270 }
2271
2272 /**
2273  * @ingroup StrBuf_DeEnCoder
2274  * @brief decode a buffer from base 64 encoding; destroys original
2275  * @param Buf Buffor to transform
2276  */
2277 int StrBufDecodeHex(StrBuf *Buf)
2278 {
2279         unsigned int ch;
2280         char *pch, *pche, *pchi;
2281
2282         if (Buf == NULL) return -1;
2283
2284         pch = pchi = Buf->buf;
2285         pche = pch + Buf->BufUsed;
2286
2287         while (pchi < pche){
2288                 ch = decode_hex(pchi);
2289                 *pch = ch;
2290                 pch ++;
2291                 pchi += 2;
2292         }
2293
2294         *pch = '\0';
2295         Buf->BufUsed = pch - Buf->buf;
2296         return Buf->BufUsed;
2297 }
2298
2299 /**
2300  * @ingroup StrBuf_DeEnCoder
2301  * @brief replace all chars >0x20 && < 0x7F with Mute
2302  * @param Mute char to put over invalid chars
2303  * @param Buf Buffor to transform
2304  */
2305 int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
2306 {
2307         unsigned char *pch;
2308
2309         if (Buf == NULL) return -1;
2310         pch = (unsigned char *)Buf->buf;
2311         while (pch < (unsigned char *)Buf->buf + Buf->BufUsed) {
2312                 if ((*pch < 0x20) || (*pch > 0x7F))
2313                         *pch = Mute;
2314                 pch ++;
2315         }
2316         return Buf->BufUsed;
2317 }
2318
2319
2320 /**
2321  * @ingroup StrBuf_DeEnCoder
2322  * @brief remove escaped strings from i.e. the url string (like %20 for blanks)
2323  * @param Buf Buffer to translate
2324  * @param StripBlanks Reduce several blanks to one?
2325  */
2326 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
2327 {
2328         int a, b;
2329         char hex[3];
2330         long len;
2331
2332         if (Buf == NULL)
2333                 return -1;
2334
2335         while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
2336                 Buf->buf[Buf->BufUsed - 1] = '\0';
2337                 Buf->BufUsed --;
2338         }
2339
2340         a = 0; 
2341         while (a < Buf->BufUsed) {
2342                 if (Buf->buf[a] == '+')
2343                         Buf->buf[a] = ' ';
2344                 else if (Buf->buf[a] == '%') {
2345                         /* don't let % chars through, rather truncate the input. */
2346                         if (a + 2 > Buf->BufUsed) {
2347                                 Buf->buf[a] = '\0';
2348                                 Buf->BufUsed = a;
2349                         }
2350                         else {                  
2351                                 hex[0] = Buf->buf[a + 1];
2352                                 hex[1] = Buf->buf[a + 2];
2353                                 hex[2] = 0;
2354                                 b = 0;
2355                                 sscanf(hex, "%02x", &b);
2356                                 Buf->buf[a] = (char) b;
2357                                 len = Buf->BufUsed - a - 2;
2358                                 if (len > 0)
2359                                         memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
2360                         
2361                                 Buf->BufUsed -=2;
2362                         }
2363                 }
2364                 a++;
2365         }
2366         return a;
2367 }
2368
2369
2370 /**
2371  * @ingroup StrBuf_DeEnCoder
2372  * @brief       RFC2047-encode a header field if necessary.
2373  *              If no non-ASCII characters are found, the string
2374  *              will be copied verbatim without encoding.
2375  *
2376  * @param       target          Target buffer.
2377  * @param       source          Source string to be encoded.
2378  * @returns     encoded length; -1 if non success.
2379  */
2380 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
2381 {
2382         const char headerStr[] = "=?UTF-8?Q?";
2383         int need_to_encode = 0;
2384         int i = 0;
2385         unsigned char ch;
2386
2387         if ((source == NULL) || 
2388             (target == NULL))
2389             return -1;
2390
2391         while ((i < source->BufUsed) &&
2392                (!IsEmptyStr (&source->buf[i])) &&
2393                (need_to_encode == 0)) {
2394                 if (((unsigned char) source->buf[i] < 32) || 
2395                     ((unsigned char) source->buf[i] > 126)) {
2396                         need_to_encode = 1;
2397                 }
2398                 i++;
2399         }
2400
2401         if (!need_to_encode) {
2402                 if (*target == NULL) {
2403                         *target = NewStrBufPlain(source->buf, source->BufUsed);
2404                 }
2405                 else {
2406                         FlushStrBuf(*target);
2407                         StrBufAppendBuf(*target, source, 0);
2408                 }
2409                 return (*target)->BufUsed;
2410         }
2411         if (*target == NULL)
2412                 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
2413         else if (sizeof(headerStr) + source->BufUsed >= (*target)->BufSize)
2414                 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
2415         memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
2416         (*target)->BufUsed = sizeof(headerStr) - 1;
2417         for (i=0; (i < source->BufUsed); ++i) {
2418                 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2419                         IncreaseBuf(*target, 1, 0);
2420                 ch = (unsigned char) source->buf[i];
2421                 if ((ch < 32) || (ch > 126) || (ch == 61)) {
2422                         sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
2423                         (*target)->BufUsed += 3;
2424                 }
2425                 else {
2426                         (*target)->buf[(*target)->BufUsed] = ch;
2427                         (*target)->BufUsed++;
2428                 }
2429         }
2430         
2431         if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2432                 IncreaseBuf(*target, 1, 0);
2433
2434         (*target)->buf[(*target)->BufUsed++] = '?';
2435         (*target)->buf[(*target)->BufUsed++] = '=';
2436         (*target)->buf[(*target)->BufUsed] = '\0';
2437         return (*target)->BufUsed;;
2438 }
2439
2440
2441
2442 static void AddRecipient(StrBuf *Target, 
2443                          StrBuf *UserName, 
2444                          StrBuf *EmailAddress, 
2445                          StrBuf *EncBuf)
2446 {
2447         int QuoteMe = 0;
2448
2449         if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
2450         if (strchr(ChrPtr(UserName), ',') != NULL) QuoteMe = 1;
2451
2452         if (QuoteMe)  StrBufAppendBufPlain(Target, HKEY("\""), 0);
2453         StrBufRFC2047encode(&EncBuf, UserName);
2454         StrBufAppendBuf(Target, EncBuf, 0);
2455         if (QuoteMe)  StrBufAppendBufPlain(Target, HKEY("\" "), 0);
2456         else          StrBufAppendBufPlain(Target, HKEY(" "), 0);
2457
2458         if (StrLength(EmailAddress) > 0){
2459                 StrBufAppendBufPlain(Target, HKEY("<"), 0);
2460                 StrBufAppendBuf(Target, EmailAddress, 0); /* TODO: what about IDN???? */
2461                 StrBufAppendBufPlain(Target, HKEY(">"), 0);
2462         }
2463 }
2464
2465
2466 /**
2467  * \brief QP encode parts of an email TO/CC/BCC vector, and strip/filter invalid parts
2468  * \param Recp Source list of email recipients
2469  * \param UserName Temporary buffer for internal use; Please provide valid buffer.
2470  * \param EmailAddress Temporary buffer for internal use; Please provide valid buffer.
2471  * \param EncBuf Temporary buffer for internal use; Please provide valid buffer.
2472  * \returns encoded & sanitized buffer with the contents of Recp; Caller owns this memory.
2473  */
2474 StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp, 
2475                                            StrBuf *UserName, 
2476                                            StrBuf *EmailAddress,
2477                                            StrBuf *EncBuf)
2478 {
2479         StrBuf *Target;
2480         int need_to_encode;
2481
2482         const char *pch, *pche;
2483         const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
2484
2485         if ((Recp == NULL) || (StrLength(Recp) == 0))
2486                 return NULL;
2487
2488         need_to_encode = 0;
2489         pch = ChrPtr(Recp);
2490         pche = pch + StrLength(Recp);
2491
2492         if (!CheckEncode(pch, -1, pche))
2493                 return NewStrBufDup(Recp);
2494
2495         Target = NewStrBufPlain(NULL, StrLength(Recp));
2496
2497         while ((pch != NULL) && (pch < pche))
2498         {
2499                 int ColonOk = 0;
2500
2501                 while (isspace(*pch)) pch++;
2502                 UserStart = UserEnd = EmailStart = EmailEnd = NULL;
2503                 
2504                 if ((*pch == '"') || (*pch == '\'')) {
2505                         UserStart = pch + 1;
2506                         
2507                         UserEnd = strchr(UserStart, *pch);
2508                         if (UserEnd == NULL) 
2509                                 break; ///TODO: Userfeedback??
2510                         EmailStart = UserEnd + 1;
2511                         while (isspace(*EmailStart))
2512                                 EmailStart++;
2513                         if (UserEnd == UserStart) {
2514                                 UserStart = UserEnd = NULL;
2515                         }
2516                         
2517                         if (*EmailStart == '<') {
2518                                 EmailStart++;
2519                                 EmailEnd = strchr(EmailStart, '>');
2520                                 if (EmailEnd == NULL)
2521                                         EmailEnd = strchr(EmailStart, ',');
2522                                 
2523                         }
2524                         else {
2525                                 EmailEnd = strchr(EmailStart, ',');
2526                         }
2527                         if (EmailEnd == NULL)
2528                                 EmailEnd = pche;
2529                         pch = EmailEnd + 1;
2530                         ColonOk = 1;
2531                 }
2532                 else {
2533                         int gt = 0;
2534                         UserStart = pch;
2535                         EmailEnd = strchr(UserStart, ',');
2536                         if (EmailEnd == NULL) {
2537                                 EmailEnd = strchr(pch, '>');
2538                                 pch = NULL;
2539                                 if (EmailEnd != NULL) {
2540                                         gt = 1;
2541                                         EmailEnd --;
2542                                 }
2543                                 else {
2544                                         EmailEnd = pche;
2545                                 }
2546                         }
2547                         else {
2548
2549                                 pch = EmailEnd + 1;
2550                                 while ((EmailEnd > UserStart) && 
2551                                        ((*EmailEnd == ',') ||
2552                                         (*EmailEnd == '>') ||
2553                                         (isspace(*EmailEnd))))
2554                                 {
2555                                         if (*EmailEnd == '>')
2556                                                 gt = 1;
2557                                         EmailEnd--;
2558                                 }
2559                                 if (EmailEnd == UserStart)
2560                                         break;
2561                         }
2562                         if (gt) {
2563                                 EmailStart = strchr(UserStart, '<');
2564                                 if ((EmailStart == NULL) || (EmailStart > EmailEnd))
2565                                         break;
2566                                 UserEnd = EmailStart - 1;
2567                                 EmailStart ++;
2568                                 if (UserStart >= UserEnd)
2569                                         UserStart = UserEnd = NULL;
2570                                 At = strchr(EmailStart, '@');
2571                         }
2572                         else { /* this is a local recipient... no domain, just a realname */
2573                                 EmailStart = UserStart;
2574                                 At = strchr(EmailStart, '@');
2575                                 if (At == NULL) {
2576                                         UserEnd = EmailEnd;
2577                                         EmailEnd = NULL;
2578                                 }
2579                                 else {
2580                                         EmailStart = UserStart;
2581                                         UserStart = NULL;
2582                                 }
2583                         }
2584                 }
2585
2586                 if ((UserStart != NULL) && (UserEnd != NULL))
2587                         StrBufPlain(UserName, UserStart, UserEnd - UserStart);
2588                 else if ((UserStart != NULL) && (UserEnd == NULL))
2589                         StrBufPlain(UserName, UserStart, UserEnd - UserStart);
2590                 else
2591                         FlushStrBuf(UserName);
2592
2593                 if ((EmailStart != NULL) && (EmailEnd != NULL))
2594                         StrBufPlain(EmailAddress, EmailStart, EmailEnd - EmailStart);
2595                 else if ((EmailStart != NULL) && (EmailEnd == NULL))
2596                         StrBufPlain(EmailAddress, EmailStart, EmailEnd - pche);
2597                 else 
2598                         FlushStrBuf(EmailAddress);
2599
2600                 AddRecipient(Target, UserName, EmailAddress, EncBuf);
2601
2602                 if (pch == NULL)
2603                         break;
2604                 
2605                 if ((pch != NULL) && (*pch == ','))
2606                         pch ++;
2607                 if (pch != NULL) while (isspace(*pch))
2608                         pch ++;
2609         }
2610         return Target;
2611 }
2612
2613
2614 /**
2615  * @ingroup StrBuf
2616  * @brief replaces all occurances of 'search' by 'replace'
2617  * @param buf Buffer to modify
2618  * @param search character to search
2619  * @param replace character to replace search by
2620  */
2621 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
2622 {
2623         long i;
2624         if (buf == NULL)
2625                 return;
2626         for (i=0; i<buf->BufUsed; i++)
2627                 if (buf->buf[i] == search)
2628                         buf->buf[i] = replace;
2629
2630 }
2631
2632 /**
2633  * @ingroup StrBuf
2634  * @brief removes all \r s from the string, or replaces them with \n if its not a combination of both.
2635  * @param buf Buffer to modify
2636  */
2637 void StrBufToUnixLF(StrBuf *buf)
2638 {
2639         char *pche, *pchS, *pchT;
2640         if (buf == NULL)
2641                 return;
2642
2643         pche = buf->buf + buf->BufUsed;
2644         pchS = pchT = buf->buf;
2645         while (pchS < pche)
2646         {
2647                 if (*pchS == '\r')
2648                 {
2649                         pchS ++;
2650                         if (*pchS != '\n') {
2651                                 *pchT = '\n';
2652                                 pchT++;
2653                         }
2654                 }
2655                 *pchT = *pchS;
2656                 pchT++; pchS++;
2657         }
2658         *pchT = '\0';
2659         buf->BufUsed = pchT - buf->buf;
2660 }
2661
2662
2663 /*******************************************************************************
2664  *                 Iconv Wrapper; RFC822 de/encoding                           *
2665  *******************************************************************************/
2666
2667 /**
2668  * @ingroup StrBuf_DeEnCoder
2669  * @brief Wrapper around iconv_open()
2670  * Our version adds aliases for non-standard Microsoft charsets
2671  * such as 'MS950', aliasing them to names like 'CP950'
2672  *
2673  * @param tocode        Target encoding
2674  * @param fromcode      Source encoding
2675  * @param pic           anonimized pointer to iconv struct
2676  */
2677 void  ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
2678 {
2679 #ifdef HAVE_ICONV
2680         iconv_t ic = (iconv_t)(-1) ;
2681         ic = iconv_open(tocode, fromcode);
2682         if (ic == (iconv_t)(-1) ) {
2683                 char alias_fromcode[64];
2684                 if ( (strlen(fromcode) == 5) && (!strncasecmp(fromcode, "MS", 2)) ) {
2685                         safestrncpy(alias_fromcode, fromcode, sizeof alias_fromcode);
2686                         alias_fromcode[0] = 'C';
2687                         alias_fromcode[1] = 'P';
2688                         ic = iconv_open(tocode, alias_fromcode);
2689                 }
2690         }
2691         *(iconv_t *)pic = ic;
2692 #endif
2693 }
2694
2695
2696 /**
2697  * @ingroup StrBuf_DeEnCoder
2698  * @brief find one chunk of a RFC822 encoded string
2699  * @param Buffer where to search
2700  * @param bptr where to start searching
2701  * @returns found position, NULL if none.
2702  */
2703 static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr)
2704 {
2705         const char * end;
2706         /* Find the next ?Q? */
2707         if (Buf->BufUsed - (bptr - Buf->buf)  < 6)
2708                 return NULL;
2709
2710         end = strchr(bptr + 2, '?');
2711
2712         if (end == NULL)
2713                 return NULL;
2714
2715         if ((Buf->BufUsed - (end - Buf->buf) > 3) &&
2716             ((*(end + 1) == 'B') || (*(end + 1) == 'Q')) && 
2717             (*(end + 2) == '?')) {
2718                 /* skip on to the end of the cluster, the next ?= */
2719                 end = strstr(end + 3, "?=");
2720         }
2721         else
2722                 /* sort of half valid encoding, try to find an end. */
2723                 end = strstr(bptr, "?=");
2724         return end;
2725 }
2726
2727 /**
2728  * @ingroup StrBuf
2729  * @brief swaps the contents of two StrBufs
2730  * this is to be used to have cheap switched between a work-buffer and a target buffer 
2731  * @param A First one
2732  * @param B second one
2733  */
2734 static inline void SwapBuffers(StrBuf *A, StrBuf *B)
2735 {
2736         StrBuf C;
2737
2738         memcpy(&C, A, sizeof(*A));
2739         memcpy(A, B, sizeof(*B));
2740         memcpy(B, &C, sizeof(C));
2741
2742 }
2743
2744
2745 /**
2746  * @ingroup StrBuf_DeEnCoder
2747  * @brief convert one buffer according to the preselected iconv pointer PIC
2748  * @param ConvertBuf buffer we need to translate
2749  * @param TmpBuf To share a workbuffer over several iterations. prepare to have it filled with useless stuff afterwards.
2750  * @param pic Pointer to the iconv-session Object
2751  */
2752 void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
2753 {
2754 #ifdef HAVE_ICONV
2755         long trycount = 0;
2756         size_t siz;
2757         iconv_t ic;
2758         char *ibuf;                     /**< Buffer of characters to be converted */
2759         char *obuf;                     /**< Buffer for converted characters */
2760         size_t ibuflen;                 /**< Length of input buffer */
2761         size_t obuflen;                 /**< Length of output buffer */
2762
2763
2764         /* since we're converting to utf-8, one glyph may take up to 6 bytes */
2765         if (ConvertBuf->BufUsed * 6 >= TmpBuf->BufSize)
2766                 IncreaseBuf(TmpBuf, 0, ConvertBuf->BufUsed * 6);
2767 TRYAGAIN:
2768         ic = *(iconv_t*)pic;
2769         ibuf = ConvertBuf->buf;
2770         ibuflen = ConvertBuf->BufUsed;
2771         obuf = TmpBuf->buf;
2772         obuflen = TmpBuf->BufSize;
2773         
2774         siz = iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
2775
2776         if (siz < 0) {
2777                 if (errno == E2BIG) {
2778                         trycount ++;                    
2779                         IncreaseBuf(TmpBuf, 0, 0);
2780                         if (trycount < 5) 
2781                                 goto TRYAGAIN;
2782
2783                 }
2784                 else if (errno == EILSEQ){ 
2785                         /* hm, invalid utf8 sequence... what to do now? */
2786                         /* An invalid multibyte sequence has been encountered in the input */
2787                 }
2788                 else if (errno == EINVAL) {
2789                         /* An incomplete multibyte sequence has been encountered in the input. */
2790                 }
2791
2792                 FlushStrBuf(TmpBuf);
2793         }
2794         else {
2795                 TmpBuf->BufUsed = TmpBuf->BufSize - obuflen;
2796                 TmpBuf->buf[TmpBuf->BufUsed] = '\0';
2797                 
2798                 /* little card game: wheres the red lady? */
2799                 SwapBuffers(ConvertBuf, TmpBuf);
2800                 FlushStrBuf(TmpBuf);
2801         }
2802 #endif
2803 }
2804
2805
2806 /**
2807  * @ingroup StrBuf_DeEnCoder
2808  * @brief catches one RFC822 encoded segment, and decodes it.
2809  * @param Target buffer to fill with result
2810  * @param DecodeMe buffer with stuff to process
2811  * @param SegmentStart points to our current segment in DecodeMe
2812  * @param SegmentEnd Points to the end of our current segment in DecodeMe
2813  * @param ConvertBuf Workbuffer shared between several iterations. Random content; needs to be valid
2814  * @param ConvertBuf2 Workbuffer shared between several iterations. Random content; needs to be valid
2815  * @param FoundCharset Characterset to default decoding to; if we find another we will overwrite it.
2816  */
2817 inline static void DecodeSegment(StrBuf *Target, 
2818                                  const StrBuf *DecodeMe, 
2819                                  const char *SegmentStart, 
2820                                  const char *SegmentEnd, 
2821                                  StrBuf *ConvertBuf,
2822                                  StrBuf *ConvertBuf2, 
2823                                  StrBuf *FoundCharset)
2824 {
2825         StrBuf StaticBuf;
2826         char charset[128];
2827         char encoding[16];
2828 #ifdef HAVE_ICONV
2829         iconv_t ic = (iconv_t)(-1);
2830 #else
2831         void *ic = NULL;
2832 #endif
2833         /* Now we handle foreign character sets properly encoded
2834          * in RFC2047 format.
2835          */
2836         StaticBuf.buf = (char*) SegmentStart; /*< it will just be read there... */
2837         StaticBuf.BufUsed = SegmentEnd - SegmentStart;
2838         StaticBuf.BufSize = DecodeMe->BufSize - (SegmentStart - DecodeMe->buf);
2839         extract_token(charset, SegmentStart, 1, '?', sizeof charset);
2840         if (FoundCharset != NULL) {
2841                 FlushStrBuf(FoundCharset);
2842                 StrBufAppendBufPlain(FoundCharset, charset, -1, 0);
2843         }
2844         extract_token(encoding, SegmentStart, 2, '?', sizeof encoding);
2845         StrBufExtract_token(ConvertBuf, &StaticBuf, 3, '?');
2846         
2847         *encoding = toupper(*encoding);
2848         if (*encoding == 'B') { /**< base64 */
2849                 ConvertBuf2->BufUsed = CtdlDecodeBase64(ConvertBuf2->buf, 
2850                                                         ConvertBuf->buf, 
2851                                                         ConvertBuf->BufUsed);
2852         }
2853         else if (*encoding == 'Q') {    /**< quoted-printable */
2854                 long pos;
2855                 
2856                 pos = 0;
2857                 while (pos < ConvertBuf->BufUsed)
2858                 {
2859                         if (ConvertBuf->buf[pos] == '_') 
2860                                 ConvertBuf->buf[pos] = ' ';
2861                         pos++;
2862                 }
2863                 
2864                 ConvertBuf2->BufUsed = CtdlDecodeQuotedPrintable(
2865                         ConvertBuf2->buf, 
2866                         ConvertBuf->buf,
2867                         ConvertBuf->BufUsed);
2868         }
2869         else {
2870                 StrBufAppendBuf(ConvertBuf2, ConvertBuf, 0);
2871         }
2872 #ifdef HAVE_ICONV
2873         ctdl_iconv_open("UTF-8", charset, &ic);
2874         if (ic != (iconv_t)(-1) ) {             
2875 #endif
2876                 StrBufConvert(ConvertBuf2, ConvertBuf, &ic);
2877                 StrBufAppendBuf(Target, ConvertBuf2, 0);
2878 #ifdef HAVE_ICONV
2879                 iconv_close(ic);
2880         }
2881         else {
2882                 StrBufAppendBufPlain(Target, HKEY("(unreadable)"), 0);
2883         }
2884 #endif
2885 }
2886
2887 /**
2888  * @ingroup StrBuf_DeEnCoder
2889  * @brief Handle subjects with RFC2047 encoding such as:
2890  * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
2891  * @param Target where to put the decoded string to 
2892  * @param DecodeMe buffer with encoded string
2893  * @param DefaultCharset if we don't find one, which should we use?
2894  * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string, 
2895  *        put it here for later use where no string might be known.
2896  */
2897 void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset)
2898 {
2899         StrBuf *DecodedInvalidBuf = NULL;
2900         StrBuf *ConvertBuf, *ConvertBuf2;
2901         const StrBuf *DecodeMee = DecodeMe;
2902         const char *start, *end, *next, *nextend, *ptr = NULL;
2903 #ifdef HAVE_ICONV
2904         iconv_t ic = (iconv_t)(-1) ;
2905 #endif
2906         const char *eptr;
2907         int passes = 0;
2908         int i, len;
2909         int illegal_non_rfc2047_encoding = 0;
2910
2911         /* Sometimes, badly formed messages contain strings which were simply
2912          *  written out directly in some foreign character set instead of
2913          *  using RFC2047 encoding.  This is illegal but we will attempt to
2914          *  handle it anyway by converting from a user-specified default
2915          *  charset to UTF-8 if we see any nonprintable characters.
2916          */
2917         
2918         len = StrLength(DecodeMe);
2919         for (i=0; i<DecodeMe->BufUsed; ++i) {
2920                 if ((DecodeMe->buf[i] < 32) || (DecodeMe->buf[i] > 126)) {
2921                         illegal_non_rfc2047_encoding = 1;
2922                         break;
2923                 }
2924         }
2925
2926         ConvertBuf = NewStrBufPlain(NULL, StrLength(DecodeMe));
2927         if ((illegal_non_rfc2047_encoding) &&
2928             (strcasecmp(ChrPtr(DefaultCharset), "UTF-8")) && 
2929             (strcasecmp(ChrPtr(DefaultCharset), "us-ascii")) )
2930         {
2931 #ifdef HAVE_ICONV
2932                 ctdl_iconv_open("UTF-8", ChrPtr(DefaultCharset), &ic);
2933                 if (ic != (iconv_t)(-1) ) {
2934                         DecodedInvalidBuf = NewStrBufDup(DecodeMe);
2935                         StrBufConvert(DecodedInvalidBuf, ConvertBuf, &ic);///TODO: don't void const?
2936                         DecodeMee = DecodedInvalidBuf;
2937                         iconv_close(ic);
2938                 }
2939 #endif
2940         }
2941
2942         /* pre evaluate the first pair */
2943         nextend = end = NULL;
2944         len = StrLength(DecodeMee);
2945         start = strstr(DecodeMee->buf, "=?");
2946         eptr = DecodeMee->buf + DecodeMee->BufUsed;
2947         if (start != NULL) 
2948                 end = FindNextEnd (DecodeMee, start);
2949         else {
2950                 StrBufAppendBuf(Target, DecodeMee, 0);
2951                 FreeStrBuf(&ConvertBuf);
2952                 FreeStrBuf(&DecodedInvalidBuf);
2953                 return;
2954         }
2955
2956         ConvertBuf2 = NewStrBufPlain(NULL, StrLength(DecodeMee));
2957
2958         if (start != DecodeMee->buf) {
2959                 long nFront;
2960                 
2961                 nFront = start - DecodeMee->buf;
2962                 StrBufAppendBufPlain(Target, DecodeMee->buf, nFront, 0);
2963                 len -= nFront;
2964         }
2965         /*
2966          * Since spammers will go to all sorts of absurd lengths to get their
2967          * messages through, there are LOTS of corrupt headers out there.
2968          * So, prevent a really badly formed RFC2047 header from throwing
2969          * this function into an infinite loop.
2970          */
2971         while ((start != NULL) && 
2972                (end != NULL) && 
2973                (start < eptr) && 
2974                (end < eptr) && 
2975                (passes < 20))
2976         {
2977                 passes++;
2978                 DecodeSegment(Target, 
2979                               DecodeMee, 
2980                               start, 
2981                               end, 
2982                               ConvertBuf,
2983                               ConvertBuf2,
2984                               FoundCharset);
2985                 
2986                 next = strstr(end, "=?");
2987                 nextend = NULL;
2988                 if ((next != NULL) && 
2989                     (next < eptr))
2990                         nextend = FindNextEnd(DecodeMee, next);
2991                 if (nextend == NULL)
2992                         next = NULL;
2993
2994                 /* did we find two partitions */
2995                 if ((next != NULL) && 
2996                     ((next - end) > 2))
2997                 {
2998                         ptr = end + 2;
2999                         while ((ptr < next) && 
3000                                (isspace(*ptr) ||
3001                                 (*ptr == '\r') ||
3002                                 (*ptr == '\n') || 
3003                                 (*ptr == '\t')))
3004                                 ptr ++;
3005                         /* 
3006                          * did we find a gab just filled with blanks?
3007                          * if not, copy its stuff over.
3008                          */
3009                         if (ptr != next)
3010                         {
3011                                 StrBufAppendBufPlain(Target, 
3012                                                      end + 2, 
3013                                                      next - end - 2,
3014                                                      0);
3015                         }
3016                 }
3017                 /* our next-pair is our new first pair now. */
3018                 ptr = end + 2;
3019                 start = next;
3020                 end = nextend;
3021         }
3022         end = ptr;
3023         nextend = DecodeMee->buf + DecodeMee->BufUsed;
3024         if ((end != NULL) && (end < nextend)) {
3025                 ptr = end;
3026                 while ( (ptr < nextend) &&
3027                         (isspace(*ptr) ||
3028                          (*ptr == '\r') ||
3029                          (*ptr == '\n') || 
3030                          (*ptr == '\t')))
3031                         ptr ++;
3032                 if (ptr < nextend)
3033                         StrBufAppendBufPlain(Target, end, nextend - end, 0);
3034         }
3035         FreeStrBuf(&ConvertBuf);
3036         FreeStrBuf(&ConvertBuf2);
3037         FreeStrBuf(&DecodedInvalidBuf);
3038 }
3039
3040 /*******************************************************************************
3041  *                   Manipulating UTF-8 Strings                                *
3042  *******************************************************************************/
3043
3044 /**
3045  * @ingroup StrBuf
3046  * @brief evaluate the length of an utf8 special character sequence
3047  * @param Char the character to examine
3048  * @returns width of utf8 chars in bytes
3049  */
3050 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
3051 {
3052         int n = 1;
3053         char test = (1<<7);
3054         
3055         while ((n < 8) && ((test & *CharS) != 0)) {
3056                 test = test << 1;
3057                 n ++;
3058         }
3059         if ((n > 6) || ((CharE - CharS) < n))
3060                 n = 1;
3061         return n;
3062 }
3063
3064 /**
3065  * @ingroup StrBuf
3066  * @brief detect whether this char starts an utf-8 encoded char
3067  * @param Char character to inspect
3068  * @returns yes or no
3069  */
3070 static inline int Ctdl_IsUtf8SequenceStart(const char Char)
3071 {
3072 /** 11??.???? indicates an UTF8 Sequence. */
3073         return ((Char & 0xC0) != 0);
3074 }
3075
3076 /**
3077  * @ingroup StrBuf
3078  * @brief measure the number of glyphs in an UTF8 string...
3079  * @param Buf string to measure
3080  * @returns the number of glyphs in Buf
3081  */
3082 long StrBuf_Utf8StrLen(StrBuf *Buf)
3083 {
3084         int n = 0;
3085         int m = 0;
3086         char *aptr, *eptr;
3087
3088         if ((Buf == NULL) || (Buf->BufUsed == 0))
3089                 return 0;
3090         aptr = Buf->buf;
3091         eptr = Buf->buf + Buf->BufUsed;
3092         while ((aptr < eptr) && (*aptr != '\0')) {
3093                 if (Ctdl_IsUtf8SequenceStart(*aptr)){
3094                         m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
3095                         while ((aptr < eptr) && (*aptr++ != '\0')&& (m-- > 0) );
3096                         n ++;
3097                 }
3098                 else {
3099                         n++;
3100                         aptr++;
3101                 }
3102         }
3103         return n;
3104 }
3105
3106 /**
3107  * @ingroup StrBuf
3108  * @brief cuts a string after maxlen glyphs
3109  * @param Buf string to cut to maxlen glyphs
3110  * @param maxlen how long may the string become?
3111  * @returns current length of the string
3112  */
3113 long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
3114 {
3115         char *aptr, *eptr;
3116         int n = 0, m = 0;
3117
3118         aptr = Buf->buf;
3119         eptr = Buf->buf + Buf->BufUsed;
3120         while ((aptr < eptr) && (*aptr != '\0')) {
3121                 if (Ctdl_IsUtf8SequenceStart(*aptr)){
3122                         m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
3123                         while ((*aptr++ != '\0') && (m-- > 0));
3124                         n ++;
3125                 }
3126                 else {
3127                         n++;
3128                         aptr++;
3129                 }
3130                 if (n > maxlen) {
3131                         *aptr = '\0';
3132                         Buf->BufUsed = aptr - Buf->buf;
3133                         return Buf->BufUsed;
3134                 }                       
3135         }
3136         return Buf->BufUsed;
3137
3138 }
3139
3140
3141
3142
3143
3144 /*******************************************************************************
3145  *                               wrapping ZLib                                 *
3146  *******************************************************************************/
3147
3148 #ifdef HAVE_ZLIB
3149 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
3150 #define OS_CODE 0x03    /*< unix */
3151
3152 /**
3153  * @ingroup StrBuf_DeEnCoder
3154  * @brief uses the same calling syntax as compress2(), but it
3155  *   creates a stream compatible with HTTP "Content-encoding: gzip"
3156  * @param dest compressed buffer
3157  * @param destLen length of the compresed data 
3158  * @param source source to encode
3159  * @param sourceLen length of source to encode 
3160  * @param level compression level
3161  */
3162 int ZEXPORT compress_gzip(Bytef * dest,
3163                           size_t * destLen,
3164                           const Bytef * source,
3165                           uLong sourceLen,     
3166                           int level)
3167 {
3168         const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
3169
3170         /* write gzip header */
3171         snprintf((char *) dest, *destLen, 
3172                  "%c%c%c%c%c%c%c%c%c%c",
3173                  gz_magic[0], gz_magic[1], Z_DEFLATED,
3174                  0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
3175                  OS_CODE);
3176
3177         /* normal deflate */
3178         z_stream stream;
3179         int err;
3180         stream.next_in = (Bytef *) source;
3181         stream.avail_in = (uInt) sourceLen;
3182         stream.next_out = dest + 10L;   // after header
3183         stream.avail_out = (uInt) * destLen;
3184         if ((uLong) stream.avail_out != *destLen)
3185                 return Z_BUF_ERROR;
3186
3187         stream.zalloc = (alloc_func) 0;
3188         stream.zfree = (free_func) 0;
3189         stream.opaque = (voidpf) 0;
3190
3191         err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
3192                            DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
3193         if (err != Z_OK)
3194                 return err;
3195
3196         err = deflate(&stream, Z_FINISH);
3197         if (err != Z_STREAM_END) {
3198                 deflateEnd(&stream);
3199                 return err == Z_OK ? Z_BUF_ERROR : err;
3200         }
3201         *destLen = stream.total_out + 10L;
3202
3203         /* write CRC and Length */
3204         uLong crc = crc32(0L, source, sourceLen);
3205         int n;
3206         for (n = 0; n < 4; ++n, ++*destLen) {
3207                 dest[*destLen] = (int) (crc & 0xff);
3208                 crc >>= 8;
3209         }
3210         uLong len = stream.total_in;
3211         for (n = 0; n < 4; ++n, ++*destLen) {
3212                 dest[*destLen] = (int) (len & 0xff);
3213                 len >>= 8;
3214         }
3215         err = deflateEnd(&stream);
3216         return err;
3217 }
3218 #endif
3219
3220
3221 /**
3222  * @ingroup StrBuf_DeEnCoder
3223  * @brief compress the buffer with gzip
3224  * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
3225  * @param Buf buffer whose content is to be gzipped
3226  */
3227 int CompressBuffer(StrBuf *Buf)
3228 {
3229 #ifdef HAVE_ZLIB
3230         char *compressed_data = NULL;
3231         size_t compressed_len, bufsize;
3232         int i = 0;
3233
3234         bufsize = compressed_len = Buf->BufUsed +  (Buf->BufUsed / 100) + 100;
3235         compressed_data = malloc(compressed_len);
3236         
3237         if (compressed_data == NULL)
3238                 return -1;
3239         /* Flush some space after the used payload so valgrind shuts up... */
3240         while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
3241                 Buf->buf[Buf->BufUsed + i++] = '\0';
3242         if (compress_gzip((Bytef *) compressed_data,
3243                           &compressed_len,
3244                           (Bytef *) Buf->buf,
3245                           (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
3246                 if (!Buf->ConstBuf)
3247                         free(Buf->buf);
3248                 Buf->buf = compressed_data;
3249                 Buf->BufUsed = compressed_len;
3250                 Buf->BufSize = bufsize;
3251                 /* Flush some space after the used payload so valgrind shuts up... */
3252                 i = 0;
3253                 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
3254                         Buf->buf[Buf->BufUsed + i++] = '\0';
3255                 return 1;
3256         } else {
3257                 free(compressed_data);
3258         }
3259 #endif  /* HAVE_ZLIB */
3260         return 0;
3261 }
3262
3263
3264
3265 /*******************************************************************************
3266  *           File I/O; Prefer buffered read since its faster!                  *
3267  *******************************************************************************/
3268
3269 /**
3270  * @ingroup StrBuf_IO
3271  * @brief Read a line from socket
3272  * flushes and closes the FD on error
3273  * @param buf the buffer to get the input to
3274  * @param fd pointer to the filedescriptor to read
3275  * @param append Append to an existing string or replace?
3276  * @param Error strerror() on error 
3277  * @returns numbers of chars read
3278  */
3279 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
3280 {
3281         int len, rlen, slen;
3282
3283         if (!append)
3284                 FlushStrBuf(buf);
3285
3286         slen = len = buf->BufUsed;
3287         while (1) {
3288                 rlen = read(*fd, &buf->buf[len], 1);
3289                 if (rlen < 1) {
3290                         *Error = strerror(errno);
3291                         
3292                         close(*fd);
3293                         *fd = -1;
3294                         
3295                         return -1;
3296                 }
3297                 if (buf->buf[len] == '\n')
3298                         break;
3299                 if (buf->buf[len] != '\r')
3300                         len ++;
3301                 if (len + 2 >= buf->BufSize) {
3302                         buf->BufUsed = len;
3303                         buf->buf[len+1] = '\0';
3304                         IncreaseBuf(buf, 1, -1);
3305                 }
3306         }
3307         buf->BufUsed = len;
3308         buf->buf[len] = '\0';
3309         return len - slen;
3310 }
3311
3312 /**
3313  * @ingroup StrBuf_BufferedIO
3314  * @brief Read a line from socket
3315  * flushes and closes the FD on error
3316  * @param Line the line to read from the fd / I/O Buffer
3317  * @param buf the buffer to get the input to
3318  * @param fd pointer to the filedescriptor to read
3319  * @param timeout number of successless selects until we bail out
3320  * @param selectresolution how long to wait on each select
3321  * @param Error strerror() on error 
3322  * @returns numbers of chars read
3323  */
3324 int StrBufTCP_read_buffered_line(StrBuf *Line, 
3325                                  StrBuf *buf, 
3326                                  int *fd, 
3327                                  int timeout, 
3328                                  int selectresolution, 
3329                                  const char **Error)
3330 {
3331         int len, rlen;
3332         int nSuccessLess = 0;
3333         fd_set rfds;
3334         char *pch = NULL;
3335         int fdflags;
3336         int IsNonBlock;
3337         struct timeval tv;
3338
3339         if (buf->BufUsed > 0) {
3340                 pch = strchr(buf->buf, '\n');
3341                 if (pch != NULL) {
3342                         rlen = 0;
3343                         len = pch - buf->buf;
3344                         if (len > 0 && (*(pch - 1) == '\r') )
3345                                 rlen ++;
3346                         StrBufSub(Line, buf, 0, len - rlen);
3347                         StrBufCutLeft(buf, len + 1);
3348                         return len - rlen;
3349                 }
3350         }
3351         
3352         if (buf->BufSize - buf->BufUsed < 10)
3353                 IncreaseBuf(buf, 1, -1);
3354
3355         fdflags = fcntl(*fd, F_GETFL);
3356         IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3357
3358         while ((nSuccessLess < timeout) && (pch == NULL)) {
3359                 if (IsNonBlock){
3360                         tv.tv_sec = selectresolution;
3361                         tv.tv_usec = 0;
3362                         
3363                         FD_ZERO(&rfds);
3364                         FD_SET(*fd, &rfds);
3365                         if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
3366                                 *Error = strerror(errno);
3367                                 close (*fd);
3368                                 *fd = -1;
3369                                 return -1;
3370                         }
3371                 }
3372                 if (IsNonBlock && !  FD_ISSET(*fd, &rfds)) {
3373                         nSuccessLess ++;
3374                         continue;
3375                 }
3376                 rlen = read(*fd, 
3377                             &buf->buf[buf->BufUsed], 
3378                             buf->BufSize - buf->BufUsed - 1);
3379                 if (rlen < 1) {
3380                         *Error = strerror(errno);
3381                         close(*fd);
3382                         *fd = -1;
3383                         return -1;
3384                 }
3385                 else if (rlen > 0) {
3386                         nSuccessLess = 0;
3387                         buf->BufUsed += rlen;
3388                         buf->buf[buf->BufUsed] = '\0';
3389                         if (buf->BufUsed + 10 > buf->BufSize) {
3390                                 IncreaseBuf(buf, 1, -1);
3391                         }
3392                         pch = strchr(buf->buf, '\n');
3393                         continue;
3394                 }
3395                 
3396         }
3397         if (pch != NULL) {
3398                 rlen = 0;
3399                 len = pch - buf->buf;
3400                 if (len > 0 && (*(pch - 1) == '\r') )
3401                         rlen ++;
3402                 StrBufSub(Line, buf, 0, len - rlen);
3403                 StrBufCutLeft(buf, len + 1);
3404                 return len - rlen;
3405         }
3406         return -1;
3407
3408 }
3409
3410 static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
3411 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
3412 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
3413 /**
3414  * @ingroup StrBuf_BufferedIO
3415  * @brief Read a line from socket
3416  * flushes and closes the FD on error
3417  * @param Line where to append our Line read from the fd / I/O Buffer; 
3418  * @param IOBuf the buffer to get the input to; lifetime pair to FD
3419  * @param Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
3420  * @param fd pointer to the filedescriptor to read
3421  * @param timeout number of successless selects until we bail out
3422  * @param selectresolution how long to wait on each select
3423  * @param Error strerror() on error 
3424  * @returns numbers of chars read or -1 in case of error. "\n" will become 0
3425  */
3426 int StrBufTCP_read_buffered_line_fast(StrBuf *Line, 
3427                                       StrBuf *IOBuf, 
3428                                       const char **Pos,
3429                                       int *fd, 
3430                                       int timeout, 
3431                                       int selectresolution, 
3432                                       const char **Error)
3433 {
3434         const char *pche = NULL;
3435         const char *pos = NULL;
3436         const char *pLF;
3437         int len, rlen, retlen;
3438         int nSuccessLess = 0;
3439         fd_set rfds;
3440         const char *pch = NULL;
3441         int fdflags;
3442         int IsNonBlock;
3443         struct timeval tv;
3444         
3445         retlen = 0;
3446         if ((Line == NULL) ||
3447             (Pos == NULL) ||
3448             (IOBuf == NULL) ||
3449             (*fd == -1))
3450         {
3451                 if (Pos != NULL)
3452                         *Pos = NULL;
3453                 *Error = ErrRBLF_PreConditionFailed;
3454                 return -1;
3455         }
3456
3457         pos = *Pos;
3458         if ((IOBuf->BufUsed > 0) && 
3459             (pos != NULL) && 
3460             (pos < IOBuf->buf + IOBuf->BufUsed)) 
3461         {
3462                 char *pcht;
3463
3464                 pche = IOBuf->buf + IOBuf->BufUsed;
3465                 pch = pos;
3466                 pcht = Line->buf;
3467
3468                 while ((pch < pche) && (*pch != '\n'))
3469                 {
3470                         if (Line->BufUsed + 10 > Line->BufSize)
3471                         {
3472                                 long apos;
3473                                 apos = pcht - Line->buf;
3474                                 *pcht = '\0';
3475                                 IncreaseBuf(Line, 1, -1);
3476                                 pcht = Line->buf + apos;
3477                         }
3478                         *pcht++ = *pch++;
3479                         Line->BufUsed++;
3480                         retlen++;
3481                 }
3482
3483                 len = pch - pos;
3484                 if (len > 0 && (*(pch - 1) == '\r') )
3485                 {
3486                         retlen--;
3487                         len --;
3488                         pcht --;
3489                         Line->BufUsed --;
3490                 }
3491                 *pcht = '\0';
3492
3493                 if ((pch >= pche) || (*pch == '\0'))
3494                 {
3495                         FlushStrBuf(IOBuf);
3496                         *Pos = NULL;
3497                         pch = NULL;
3498                         pos = 0;
3499                 }
3500
3501                 if ((pch != NULL) && 
3502                     (pch <= pche)) 
3503                 {
3504                         if (pch + 1 >= pche) {
3505                                 *Pos = NULL;
3506                                 FlushStrBuf(IOBuf);
3507                         }
3508                         else
3509                                 *Pos = pch + 1;
3510                         
3511                         return retlen;
3512                 }
3513                 else 
3514                         FlushStrBuf(IOBuf);
3515         }
3516
3517         /* If we come here, Pos is Unset since we read everything into Line, and now go for more. */
3518         
3519         if (IOBuf->BufSize - IOBuf->BufUsed < 10)
3520                 IncreaseBuf(IOBuf, 1, -1);
3521
3522         fdflags = fcntl(*fd, F_GETFL);
3523         IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3524
3525         pLF = NULL;
3526         while ((nSuccessLess < timeout) && 
3527                (pLF == NULL) &&
3528                (*fd != -1)) {
3529                 if (IsNonBlock)
3530                 {
3531                         tv.tv_sec = 1;
3532                         tv.tv_usec = 0;
3533                 
3534                         FD_ZERO(&rfds);
3535                         FD_SET(*fd, &rfds);
3536                         if (select((*fd) + 1, &rfds, NULL, NULL, &tv) == -1) {
3537                                 *Error = strerror(errno);
3538                                 close (*fd);
3539                                 *fd = -1;
3540                                 if (*Error == NULL)
3541                                         *Error = ErrRBLF_SelectFailed;
3542                                 return -1;
3543                         }
3544                         if (! FD_ISSET(*fd, &rfds) != 0) {
3545                                 nSuccessLess ++;
3546                                 continue;
3547                         }
3548                 }
3549                 rlen = read(*fd, 
3550                             &IOBuf->buf[IOBuf->BufUsed], 
3551                             IOBuf->BufSize - IOBuf->BufUsed - 1);
3552                 if (rlen < 1) {
3553                         *Error = strerror(errno);
3554                         close(*fd);
3555                         *fd = -1;
3556                         return -1;
3557                 }
3558                 else if (rlen > 0) {
3559                         nSuccessLess = 0;
3560                         pLF = IOBuf->buf + IOBuf->BufUsed;
3561                         IOBuf->BufUsed += rlen;
3562                         IOBuf->buf[IOBuf->BufUsed] = '\0';
3563                         
3564                         pche = IOBuf->buf + IOBuf->BufUsed;
3565                         
3566                         while ((pLF < pche) && (*pLF != '\n'))
3567                                 pLF ++;
3568                         if ((pLF >= pche) || (*pLF == '\0'))
3569                                 pLF = NULL;
3570
3571                         if (IOBuf->BufUsed + 10 > IOBuf->BufSize)
3572                         {
3573                                 long apos = 0;
3574
3575                                 if (pLF != NULL) apos = pLF - IOBuf->buf;
3576                                 IncreaseBuf(IOBuf, 1, -1);      
3577                                 if (pLF != NULL) pLF = IOBuf->buf + apos;
3578                         }
3579
3580                         continue;
3581                 }
3582         }
3583         *Pos = NULL;
3584         if (pLF != NULL) {
3585                 pos = IOBuf->buf;
3586                 len = pLF - pos;
3587                 if (len > 0 && (*(pLF - 1) == '\r') )
3588                         len --;
3589                 StrBufAppendBufPlain(Line, ChrPtr(IOBuf), len, 0);
3590                 if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed)
3591                 {
3592                         FlushStrBuf(IOBuf);
3593                 }
3594                 else 
3595                         *Pos = pLF + 1;
3596                 return retlen + len;
3597         }
3598         *Error = ErrRBLF_NotEnoughSentFromServer;
3599         return -1;
3600
3601 }
3602
3603 static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong arguments or invalid Filedescriptor";
3604 /**
3605  * @ingroup StrBuf_IO
3606  * @brief Input binary data from socket
3607  * flushes and closes the FD on error
3608  * @param Buf the buffer to get the input to
3609  * @param fd pointer to the filedescriptor to read
3610  * @param append Append to an existing string or replace?
3611  * @param nBytes the maximal number of bytes to read
3612  * @param Error strerror() on error 
3613  * @returns numbers of chars read
3614  */
3615 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
3616 {
3617         int fdflags;
3618         int len, rlen, slen;
3619         int nSuccessLess;
3620         int nRead = 0;
3621         char *ptr;
3622         int IsNonBlock;
3623         struct timeval tv;
3624         fd_set rfds;
3625
3626         if ((Buf == NULL) || (*fd == -1))
3627         {
3628                 *Error = ErrRBLF_BLOBPreConditionFailed;
3629                 return -1;
3630         }
3631         if (!append)
3632                 FlushStrBuf(Buf);
3633         if (Buf->BufUsed + nBytes >= Buf->BufSize)
3634                 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
3635
3636         ptr = Buf->buf + Buf->BufUsed;
3637
3638         slen = len = Buf->BufUsed;
3639
3640         fdflags = fcntl(*fd, F_GETFL);
3641         IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3642         nSuccessLess = 0;
3643         while ((nRead < nBytes) && 
3644                (*fd != -1)) 
3645         {
3646                 if (IsNonBlock)
3647                 {
3648                         tv.tv_sec = 1;
3649                         tv.tv_usec = 0;
3650                 
3651                         FD_ZERO(&rfds);
3652                         FD_SET(*fd, &rfds);
3653                         if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
3654                                 *Error = strerror(errno);
3655                                 close (*fd);
3656                                 *fd = -1;
3657                                 if (*Error == NULL)
3658                                         *Error = ErrRBLF_SelectFailed;
3659                                 return -1;
3660                         }
3661                         if (! FD_ISSET(*fd, &rfds) != 0) {
3662                                 nSuccessLess ++;
3663                                 continue;
3664                         }
3665                 }
3666
3667                 if ((rlen = read(*fd, 
3668                                  ptr,
3669                                  nBytes - nRead)) == -1) {
3670                         close(*fd);
3671                         *fd = -1;
3672                         *Error = strerror(errno);
3673                         return rlen;
3674                 }
3675                 nRead += rlen;
3676                 ptr += rlen;
3677                 Buf->BufUsed += rlen;
3678         }
3679         Buf->buf[Buf->BufUsed] = '\0';
3680         return nRead;
3681 }
3682
3683 const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
3684 const char *ErrRBB_too_many_selects        = "StrBufReadBLOBBuffered: to many selects; aborting.";
3685 /**
3686  * @ingroup StrBuf_BufferedIO
3687  * @brief Input binary data from socket
3688  * flushes and closes the FD on error
3689  * @param Blob put binary thing here
3690  * @param IOBuf the buffer to get the input to
3691  * @param Pos offset inside of IOBuf
3692  * @param fd pointer to the filedescriptor to read
3693  * @param append Append to an existing string or replace?
3694  * @param nBytes the maximal number of bytes to read
3695  * @param check whether we should search for '000\n' terminators in case of timeouts
3696  * @param Error strerror() on error 
3697  * @returns numbers of chars read
3698  */
3699 int StrBufReadBLOBBuffered(StrBuf *Blob, 
3700                            StrBuf *IOBuf, 
3701                            const char **Pos,
3702                            int *fd, 
3703                            int append, 
3704                            long nBytes, 
3705                            int check, 
3706                            const char **Error)
3707 {
3708         const char *pche;
3709         const char *pos;
3710         int fdflags;
3711         int len = 0;
3712         int rlen, slen;
3713         int nRead = 0;
3714         int nAlreadyRead = 0;
3715         int IsNonBlock;
3716         char *ptr;
3717         fd_set rfds;
3718         const char *pch;
3719         struct timeval tv;
3720         int nSuccessLess = 0;
3721         int MaxTries;
3722
3723         if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL))
3724         {
3725                 if (*Pos != NULL)
3726                         *Pos = NULL;
3727                 *Error = ErrRBB_BLOBFPreConditionFailed;
3728                 return -1;
3729         }
3730
3731         if (!append)
3732                 FlushStrBuf(Blob);
3733         if (Blob->BufUsed + nBytes >= Blob->BufSize) 
3734                 IncreaseBuf(Blob, append, Blob->BufUsed + nBytes);
3735         
3736         pos = *Pos;
3737
3738         if (pos != NULL)
3739                 len = pos - IOBuf->buf;
3740         rlen = IOBuf->BufUsed - len;
3741
3742
3743         if ((IOBuf->BufUsed > 0) && 
3744             (pos != NULL) && 
3745             (pos < IOBuf->buf + IOBuf->BufUsed)) 
3746         {
3747                 pche = IOBuf->buf + IOBuf->BufUsed;
3748                 pch = pos;
3749
3750                 if (rlen < nBytes) {
3751                         memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
3752                         Blob->BufUsed += rlen;
3753                         Blob->buf[Blob->BufUsed] = '\0';
3754                         nAlreadyRead = nRead = rlen;
3755                         *Pos = NULL; 
3756                 }
3757                 if (rlen >= nBytes) {
3758                         memcpy(Blob->buf + Blob->BufUsed, pos, nBytes);
3759                         Blob->BufUsed += nBytes;
3760                         Blob->buf[Blob->BufUsed] = '\0';
3761                         if (rlen == nBytes) {
3762                                 *Pos = NULL; 
3763                                 FlushStrBuf(IOBuf);
3764                         }
3765                         else 
3766                                 *Pos += nBytes;
3767                         return nBytes;
3768                 }
3769         }
3770
3771         FlushStrBuf(IOBuf);
3772         *Pos = NULL;
3773         if (IOBuf->BufSize < nBytes - nRead)
3774                 IncreaseBuf(IOBuf, 0, nBytes - nRead);
3775         ptr = IOBuf->buf;
3776
3777         slen = len = Blob->BufUsed;
3778
3779         fdflags = fcntl(*fd, F_GETFL);
3780         IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3781         if (IsNonBlock)
3782                 MaxTries =   1000;
3783         else
3784                 MaxTries = 100000;
3785
3786         nBytes -= nRead;
3787         nRead = 0;
3788         while ((nSuccessLess < MaxTries) && 
3789                (nRead < nBytes) &&
3790                (*fd != -1)) {
3791                 if (IsNonBlock)
3792                 {
3793                         tv.tv_sec = 1;
3794                         tv.tv_usec = 0;
3795                 
3796                         FD_ZERO(&rfds);
3797                         FD_SET(*fd, &rfds);
3798                         if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
3799                                 *Error = strerror(errno);
3800                                 close (*fd);
3801                                 *fd = -1;
3802                                 if (*Error == NULL)
3803                                         *Error = ErrRBLF_SelectFailed;
3804                                 return -1;
3805                         }
3806                         if (! FD_ISSET(*fd, &rfds) != 0) {
3807                                 nSuccessLess ++;
3808                                 continue;
3809                         }
3810                 }
3811                 rlen = read(*fd, 
3812                             ptr,
3813                             IOBuf->BufSize - (ptr - IOBuf->buf));
3814                 if (rlen == -1) {
3815                         close(*fd);
3816                         *fd = -1;
3817                         *Error = strerror(errno);
3818                         return rlen;
3819                 }
3820                 else if (rlen == 0){
3821                         if ((check == NNN_TERM) && 
3822                             (nRead > 5) &&
3823                             (strncmp(IOBuf->buf + IOBuf->BufUsed - 5, "\n000\n", 5) == 0)) 
3824                         {
3825                                 StrBufPlain(Blob, HKEY("\n000\n"));
3826                                 StrBufCutRight(Blob, 5);
3827                                 return Blob->BufUsed;
3828                         }
3829                         else if (!IsNonBlock) 
3830                                 nSuccessLess ++;
3831                         else if (nSuccessLess > MaxTries) {
3832                                 FlushStrBuf(IOBuf);
3833                                 *Error = ErrRBB_too_many_selects;
3834                                 return -1;
3835                         }
3836                 }
3837                 else if (rlen > 0) {
3838                         nSuccessLess = 0;
3839                         nRead += rlen;
3840                         ptr += rlen;
3841                         IOBuf->BufUsed += rlen;
3842                 }
3843         }
3844         if (nSuccessLess >= MaxTries) {
3845                 FlushStrBuf(IOBuf);
3846                 *Error = ErrRBB_too_many_selects;
3847                 return -1;
3848         }
3849
3850         if (nRead > nBytes) {
3851                 *Pos = IOBuf->buf + nBytes;
3852         }
3853         Blob->buf[Blob->BufUsed] = '\0';
3854         StrBufAppendBufPlain(Blob, IOBuf->buf, nBytes, 0);
3855         if (*Pos == NULL) {
3856                 FlushStrBuf(IOBuf);
3857         }
3858         return nRead + nAlreadyRead;
3859 }
3860
3861 /**
3862  * @ingroup StrBuf_IO
3863  * @brief extract a "next line" from Buf; Ptr to persist across several iterations
3864  * @param LineBuf your line will be copied here.
3865  * @param Buf BLOB with lines of text...
3866  * @param Ptr moved arround to keep the next-line across several iterations
3867  *        has to be &NULL on start; will be &NotNULL on end of buffer
3868  * @returns size of copied buffer
3869  */
3870 int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr)
3871 {
3872         const char *aptr, *ptr, *eptr;
3873         char *optr, *xptr;
3874
3875         if ((Buf == NULL) || (*Ptr == StrBufNOTNULL)) {
3876                 *Ptr = StrBufNOTNULL;
3877                 return 0;
3878         }
3879
3880         FlushStrBuf(LineBuf);
3881         if (*Ptr==NULL)
3882                 ptr = aptr = Buf->buf;
3883         else
3884                 ptr = aptr = *Ptr;
3885
3886         optr = LineBuf->buf;
3887         eptr = Buf->buf + Buf->BufUsed;
3888         xptr = LineBuf->buf + LineBuf->BufSize - 1;
3889
3890         while ((ptr <= eptr) && 
3891                (*ptr != '\n') &&
3892                (*ptr != '\r') )
3893         {
3894                 *optr = *ptr;
3895                 optr++; ptr++;
3896                 if (optr == xptr) {
3897                         LineBuf->BufUsed = optr - LineBuf->buf;
3898                         IncreaseBuf(LineBuf,  1, LineBuf->BufUsed + 1);
3899                         optr = LineBuf->buf + LineBuf->BufUsed;
3900                         xptr = LineBuf->buf + LineBuf->BufSize - 1;
3901                 }
3902         }
3903
3904         if ((ptr >= eptr) && (optr > LineBuf->buf))
3905                 optr --;
3906         LineBuf->BufUsed = optr - LineBuf->buf;
3907         *optr = '\0';       
3908         if ((ptr <= eptr) && (*ptr == '\r'))
3909                 ptr ++;
3910         if ((ptr <= eptr) && (*ptr == '\n'))
3911                 ptr ++;
3912         
3913         if (ptr < eptr) {
3914                 *Ptr = ptr;
3915         }
3916         else {
3917                 *Ptr = StrBufNOTNULL;
3918         }
3919
3920         return Buf->BufUsed - (ptr - Buf->buf);
3921 }
3922
3923
3924 /**
3925  * @ingroup StrBuf_IO
3926  * @brief removes double slashes from pathnames
3927  * @param Dir directory string to filter
3928  * @param RemoveTrailingSlash allows / disallows trailing slashes
3929  */
3930 void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
3931 {
3932         char *a, *b;
3933
3934         a = b = Dir->buf;
3935
3936         while (!IsEmptyStr(a)) {
3937                 if (*a == '/') {
3938                         while (*a == '/')
3939                                 a++;
3940                         *b = '/';
3941                         b++;
3942                 }
3943                 else {
3944                         *b = *a;
3945                         b++; a++;
3946                 }
3947         }
3948         if ((RemoveTrailingSlash) && (*(b - 1) != '/')){
3949                 *b = '/';
3950                 b++;
3951         }
3952         *b = '\0';
3953         Dir->BufUsed = b - Dir->buf;
3954 }
3955
3956