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