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