* fix some minor flaws
[citadel.git] / libcitadel / lib / stringbuf.c
1 #include <ctype.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <sys/select.h>
8 #include <fcntl.h>
9 #define SHOW_ME_VAPPEND_PRINTF
10 #include <stdarg.h>
11 #include "libcitadel.h"
12
13
14 struct StrBuf {
15         char *buf;
16         long BufSize;
17         long BufUsed;
18         int ConstBuf;
19 };
20
21
22 inline const char *ChrPtr(const StrBuf *Str)
23 {
24         if (Str == NULL)
25                 return "";
26         return Str->buf;
27 }
28
29 inline int StrLength(const StrBuf *Str)
30 {
31         return (Str != NULL) ? Str->BufUsed : 0;
32 }
33
34 StrBuf* NewStrBuf(void)
35 {
36         StrBuf *NewBuf;
37
38         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
39         NewBuf->buf = (char*) malloc(SIZ);
40         NewBuf->buf[0] = '\0';
41         NewBuf->BufSize = SIZ;
42         NewBuf->BufUsed = 0;
43         NewBuf->ConstBuf = 0;
44         return NewBuf;
45 }
46
47 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
48 {
49         StrBuf *NewBuf;
50         
51         if (CopyMe == NULL)
52                 return NewStrBuf();
53
54         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
55         NewBuf->buf = (char*) malloc(CopyMe->BufSize);
56         memcpy(NewBuf->buf, CopyMe->buf, CopyMe->BufUsed + 1);
57         NewBuf->BufUsed = CopyMe->BufUsed;
58         NewBuf->BufSize = CopyMe->BufSize;
59         NewBuf->ConstBuf = 0;
60         return NewBuf;
61 }
62
63 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
64 {
65         StrBuf *NewBuf;
66         size_t Siz = SIZ;
67         size_t CopySize;
68
69         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
70         if (nChars < 0)
71                 CopySize = strlen((ptr != NULL)?ptr:"");
72         else
73                 CopySize = nChars;
74
75         while (Siz <= CopySize)
76                 Siz *= 2;
77
78         NewBuf->buf = (char*) malloc(Siz);
79         NewBuf->BufSize = Siz;
80         if (ptr != NULL) {
81                 memcpy(NewBuf->buf, ptr, CopySize);
82                 NewBuf->buf[CopySize] = '\0';
83                 NewBuf->BufUsed = CopySize;
84         }
85         else {
86                 NewBuf->buf[0] = '\0';
87                 NewBuf->BufUsed = 0;
88         }
89         NewBuf->ConstBuf = 0;
90         return NewBuf;
91 }
92
93
94 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
95 {
96         StrBuf *NewBuf;
97
98         NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
99         NewBuf->buf = (char*) StringConstant;
100         NewBuf->BufSize = SizeOfStrConstant;
101         NewBuf->BufUsed = SizeOfStrConstant;
102         NewBuf->ConstBuf = 1;
103         return NewBuf;
104 }
105
106
107 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
108 {
109         char *NewBuf;
110         size_t NewSize = Buf->BufSize * 2;
111
112         if (Buf->ConstBuf)
113                 return -1;
114                 
115         if (DestSize > 0)
116                 while (NewSize < DestSize)
117                         NewSize *= 2;
118
119         NewBuf= (char*) malloc(NewSize);
120         if (KeepOriginal)
121         {
122                 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
123         }
124         else
125         {
126                 NewBuf[0] = '\0';
127                 Buf->BufUsed = 0;
128         }
129         free (Buf->buf);
130         Buf->buf = NewBuf;
131         Buf->BufSize *= 2;
132         return Buf->BufSize;
133 }
134
135 int FlushStrBuf(StrBuf *buf)
136 {
137         if (buf->ConstBuf)
138                 return -1;       
139         buf->buf[0] ='\0';
140         buf->BufUsed = 0;
141         return 0;
142 }
143
144 void FreeStrBuf (StrBuf **FreeMe)
145 {
146         if (*FreeMe == NULL)
147                 return;
148         if (!(*FreeMe)->ConstBuf) 
149                 free((*FreeMe)->buf);
150         free(*FreeMe);
151         *FreeMe = NULL;
152 }
153
154 void HFreeStrBuf (void *VFreeMe)
155 {
156         StrBuf *FreeMe = (StrBuf*)VFreeMe;
157         if (FreeMe == NULL)
158                 return;
159         if (!FreeMe->ConstBuf) 
160                 free(FreeMe->buf);
161         free(FreeMe);
162 }
163
164 long StrTol(const StrBuf *Buf)
165 {
166         if(Buf->BufUsed > 0)
167                 return atol(Buf->buf);
168         else
169                 return 0;
170 }
171
172 int StrToi(const StrBuf *Buf)
173 {
174         if(Buf->BufUsed > 0)
175                 return atoi(Buf->buf);
176         else
177                 return 0;
178 }
179
180 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
181 {
182         if (Buf == NULL)
183                 return -1;
184         if (ptr != NULL)
185                 nThChar = ptr - Buf->buf;
186         if ((nThChar < 0) || (nThChar > Buf->BufUsed))
187                 return -1;
188         Buf->buf[nThChar] = PeekValue;
189         return nThChar;
190 }
191
192
193 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
194 {
195         size_t Siz = Buf->BufSize;
196         size_t CopySize;
197
198         if (nChars < 0)
199                 CopySize = strlen(ptr);
200         else
201                 CopySize = nChars;
202
203         while (Siz <= CopySize)
204                 Siz *= 2;
205
206         if (Siz != Buf->BufSize)
207                 IncreaseBuf(Buf, 0, Siz);
208         memcpy(Buf->buf, ptr, CopySize);
209         Buf->buf[CopySize] = '\0';
210         Buf->BufUsed = CopySize;
211         Buf->ConstBuf = 0;
212         return CopySize;
213 }
214
215 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, size_t Offset)
216 {
217         if ((AppendBuf == NULL) || (Buf == NULL))
218                 return;
219
220         if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed)
221                 IncreaseBuf(Buf, 
222                             (Buf->BufUsed > 0), 
223                             AppendBuf->BufUsed + Buf->BufUsed);
224
225         memcpy(Buf->buf + Buf->BufUsed, 
226                AppendBuf->buf + Offset, 
227                AppendBuf->BufUsed - Offset);
228         Buf->BufUsed += AppendBuf->BufUsed - Offset;
229         Buf->buf[Buf->BufUsed] = '\0';
230 }
231
232
233 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, size_t Offset)
234 {
235         long aps;
236
237         if ((AppendBuf == NULL) || (Buf == NULL))
238                 return;
239
240         if (AppendSize < 0 )
241                 aps = strlen(AppendBuf + Offset);
242         else
243                 aps = AppendSize - Offset;
244
245         if (Buf->BufSize < Buf->BufUsed + aps)
246                 IncreaseBuf(Buf, (Buf->BufUsed > 0), Buf->BufUsed + aps);
247
248         memcpy(Buf->buf + Buf->BufUsed, 
249                AppendBuf + Offset, 
250                aps);
251         Buf->BufUsed += aps;
252         Buf->buf[Buf->BufUsed] = '\0';
253 }
254
255
256 /** 
257  * \brief Escape a string for feeding out as a URL.
258  * \param outbuf the output buffer
259  * \param oblen the size of outbuf to sanitize
260  * \param strbuf the input buffer
261  */
262 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
263 {
264         const char *pch, *pche;
265         char *pt, *pte;
266         int b, c, len;
267         const char ec[] = " +#&;`'|*?-~<>^()[]{}/$\"\\";
268         int eclen = sizeof(ec) -1;
269
270         if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
271                 return;
272         if (PlainIn != NULL) {
273                 len = strlen(PlainIn);
274                 pch = PlainIn;
275                 pche = pch + len;
276         }
277         else {
278                 pch = In->buf;
279                 pche = pch + In->BufUsed;
280                 len = In->BufUsed;
281         }
282
283         pt = OutBuf->buf + OutBuf->BufUsed;
284         pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
285
286         while (pch < pche) {
287                 if (pt >= pte) {
288                         IncreaseBuf(OutBuf, 1, -1);
289                         pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
290                         pt = OutBuf->buf + OutBuf->BufUsed;
291                 }
292                 
293                 c = 0;
294                 for (b = 0; b < eclen; ++b) {
295                         if (*pch == ec[b]) {
296                                 c = 1;
297                                 b += eclen;
298                         }
299                 }
300                 if (c == 1) {
301                         sprintf(pt,"%%%02X", *pch);
302                         pt += 3;
303                         OutBuf->BufUsed += 3;
304                         pch ++;
305                 }
306                 else {
307                         *(pt++) = *(pch++);
308                         OutBuf->BufUsed++;
309                 }
310         }
311         *pt = '\0';
312 }
313
314 /*
315  * Copy a string, escaping characters which have meaning in HTML.  
316  *
317  * target               target buffer
318  * strbuf               source buffer
319  * nbsp                 If nonzero, spaces are converted to non-breaking spaces.
320  * nolinebreaks         if set, linebreaks are removed from the string.
321  */
322 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
323 {
324         const char *aptr, *eiptr;
325         char *bptr, *eptr;
326         long len;
327
328         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
329                 return -1;
330
331         if (PlainIn != NULL) {
332                 aptr = PlainIn;
333                 len = strlen(PlainIn);
334                 eiptr = aptr + len;
335         }
336         else {
337                 aptr = Source->buf;
338                 eiptr = aptr + Source->BufUsed;
339                 len = Source->BufUsed;
340         }
341
342         bptr = Target->buf + Target->BufUsed;
343         eptr = Target->buf + Target->BufSize - 6; /* our biggest unit to put in...  */
344
345         while (aptr < eiptr){
346                 if(bptr >= eptr) {
347                         IncreaseBuf(Target, 1, -1);
348                         eptr = Target->buf + Target->BufSize - 6; 
349                         bptr = Target->buf + Target->BufUsed;
350                 }
351                 if (*aptr == '<') {
352                         memcpy(bptr, "&lt;", 4);
353                         bptr += 4;
354                         Target->BufUsed += 4;
355                 }
356                 else if (*aptr == '>') {
357                         memcpy(bptr, "&gt;", 4);
358                         bptr += 4;
359                         Target->BufUsed += 4;
360                 }
361                 else if (*aptr == '&') {
362                         memcpy(bptr, "&amp;", 5);
363                         bptr += 5;
364                         Target->BufUsed += 5;
365                 }
366                 else if (*aptr == '\"') {
367                         memcpy(bptr, "&quot;", 6);
368                         bptr += 6;
369                         Target->BufUsed += 6;
370                 }
371                 else if (*aptr == '\'') {
372                         memcpy(bptr, "&#39;", 5);
373                         bptr += 5;
374                         Target->BufUsed += 5;
375                 }
376                 else if (*aptr == LB) {
377                         *bptr = '<';
378                         bptr ++;
379                         Target->BufUsed ++;
380                 }
381                 else if (*aptr == RB) {
382                         *bptr = '>';
383                         bptr ++;
384                         Target->BufUsed ++;
385                 }
386                 else if (*aptr == QU) {
387                         *bptr ='"';
388                         bptr ++;
389                         Target->BufUsed ++;
390                 }
391                 else if ((*aptr == 32) && (nbsp == 1)) {
392                         memcpy(bptr, "&nbsp;", 6);
393                         bptr += 6;
394                         Target->BufUsed += 6;
395                 }
396                 else if ((*aptr == '\n') && (nolinebreaks)) {
397                         *bptr='\0';     /* nothing */
398                 }
399                 else if ((*aptr == '\r') && (nolinebreaks)) {
400                         *bptr='\0';     /* nothing */
401                 }
402                 else{
403                         *bptr = *aptr;
404                         bptr++;
405                         Target->BufUsed ++;
406                 }
407                 aptr ++;
408         }
409         *bptr = '\0';
410         if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
411                 return -1;
412         return Target->BufUsed;
413 }
414
415 void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn)
416 {
417         const char *aptr, *eiptr;
418         char *tptr, *eptr;
419         long len;
420
421         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
422                 return ;
423
424         if (PlainIn != NULL) {
425                 aptr = PlainIn;
426                 len = strlen(PlainIn);
427                 eiptr = aptr + len;
428         }
429         else {
430                 aptr = Source->buf;
431                 eiptr = aptr + Source->BufUsed;
432                 len = Source->BufUsed;
433         }
434         eiptr = Target->buf + Target->BufSize - 6; 
435         tptr = Target->buf + Target->BufUsed;
436         
437         while (aptr < eiptr){
438                 if(tptr >= eptr) {
439                         IncreaseBuf(Target, 1, -1);
440                         eiptr = Target->buf + Target->BufSize - 6; 
441                         tptr = Target->buf + Target->BufUsed;
442                 }
443                
444                 if (*aptr == '\n') {
445                         *tptr = ' ';
446                         Target->BufUsed++;
447                 }
448                 else if (*aptr == '\r') {
449                         *tptr = ' ';
450                         Target->BufUsed++;
451                 }
452                 else if (*aptr == '\'') {
453                         *(tptr++) = '&';
454                         *(tptr++) = '#';
455                         *(tptr++) = '3';
456                         *(tptr++) = '9';
457                         *tptr = ';';
458                         Target->BufUsed += 5;
459                 } else {
460                         *tptr = *aptr;
461                         Target->BufUsed++;
462                 }
463                 tptr++; aptr++;
464         }
465         *tptr = '\0';
466 }
467
468
469 inline int StrBufNum_tokens(const StrBuf *source, char tok)
470 {
471         return num_tokens(source->buf, tok);
472 }
473
474
475 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
476 {
477         size_t NCharsRemain;
478         if (Offset > Source->BufUsed)
479         {
480                 FlushStrBuf(dest);
481                 return 0;
482         }
483         if (Offset + nChars < Source->BufUsed)
484         {
485                 if (nChars > dest->BufSize)
486                         IncreaseBuf(dest, 0, nChars + 1);
487                 memcpy(dest->buf, Source->buf + Offset, nChars);
488                 dest->BufUsed = nChars;
489                 dest->buf[dest->BufUsed] = '\0';
490                 return nChars;
491         }
492         NCharsRemain = Source->BufUsed - Offset;
493         if (NCharsRemain > dest->BufSize)
494                 IncreaseBuf(dest, 0, NCharsRemain + 1);
495         memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
496         dest->BufUsed = NCharsRemain;
497         dest->buf[dest->BufUsed] = '\0';
498         return NCharsRemain;
499 }
500
501
502 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
503 {
504         size_t nWritten = Buf->BufSize + 1;
505         size_t Offset = Buf->BufUsed;
506         size_t newused = Offset + nWritten;
507         
508         while (newused >= Buf->BufSize) {
509                 nWritten = vsnprintf(Buf->buf + Offset, 
510                                      Buf->BufSize - Offset, 
511                                      format, ap);
512                 newused = Offset + nWritten;
513                 if (newused >= Buf->BufSize)
514                         IncreaseBuf(Buf, 1, 0);
515                 else
516                         Buf->BufUsed = Offset + nWritten ;
517
518         }
519 }
520
521 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
522 {
523         size_t nWritten = Buf->BufSize + 1;
524         size_t Offset = Buf->BufUsed;
525         size_t newused = Offset + nWritten;
526         va_list arg_ptr;
527         
528         while (newused >= Buf->BufSize) {
529                 va_start(arg_ptr, format);
530                 nWritten = vsnprintf(Buf->buf + Offset, 
531                                      Buf->BufSize - Offset, 
532                                      format, arg_ptr);
533                 va_end(arg_ptr);
534                 newused = Offset + nWritten;
535                 if (newused >= Buf->BufSize)
536                         IncreaseBuf(Buf, 1, 0);
537                 else
538                         Buf->BufUsed = Offset + nWritten ;
539
540         }
541 }
542
543 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
544 {
545         size_t nWritten = Buf->BufSize + 1;
546         va_list arg_ptr;
547         
548         while (nWritten >= Buf->BufSize) {
549                 va_start(arg_ptr, format);
550                 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
551                 va_end(arg_ptr);
552                 Buf->BufUsed = nWritten ;
553                 if (nWritten >= Buf->BufSize)
554                         IncreaseBuf(Buf, 0, 0);
555         }
556 }
557
558
559 /**
560  * \brief a string tokenizer
561  * \param dest Destination StringBuffer
562  * \param Source StringBuffer to read into
563  * \param separator tokenizer param
564  * \returns -1 if not found, else length of token.
565  */
566 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
567 {
568         const char *s, *e;              //* source * /
569         int len = 0;                    //* running total length of extracted string * /
570         int current_token = 0;          //* token currently being processed * /
571
572         if ((Source == NULL) || (Source->BufUsed ==0)) {
573                 return(-1);
574         }
575         s = Source->buf;
576         e = s + Source->BufUsed;
577         if (dest == NULL) {
578                 return(-1);
579         }
580
581         //cit_backtrace();
582         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
583         dest->buf[0] = '\0';
584         dest->BufUsed = 0;
585
586         while ((s<e) && !IsEmptyStr(s)) {
587                 if (*s == separator) {
588                         ++current_token;
589                 }
590                 if (len >= dest->BufSize)
591                         if (!IncreaseBuf(dest, 1, -1))
592                                 break;
593                 if ( (current_token == parmnum) && 
594                      (*s != separator)) {
595                         dest->buf[len] = *s;
596                         ++len;
597                 }
598                 else if (current_token > parmnum) {
599                         break;
600                 }
601                 ++s;
602         }
603         
604         dest->buf[len] = '\0';
605         dest->BufUsed = len;
606                 
607         if (current_token < parmnum) {
608                 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
609                 return(-1);
610         }
611         //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
612         return(len);
613 }
614
615
616 /*
617  * extract_int()  -  extract an int parm w/o supplying a buffer
618  */
619 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
620 {
621         StrBuf tmp;
622         char buf[64];
623         
624         tmp.buf = buf;
625         buf[0] = '\0';
626         tmp.BufSize = 64;
627         tmp.BufUsed = 0;
628         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
629                 return(atoi(buf));
630         else
631                 return 0;
632 }
633
634 /*
635  * extract_long()  -  extract an long parm w/o supplying a buffer
636  */
637 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
638 {
639         StrBuf tmp;
640         char buf[64];
641         
642         tmp.buf = buf;
643         buf[0] = '\0';
644         tmp.BufSize = 64;
645         tmp.BufUsed = 0;
646         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
647                 return(atoi(buf));
648         else
649                 return 0;
650 }
651
652
653 /*
654  * extract_unsigned_long() - extract an unsigned long parm
655  */
656 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
657 {
658         StrBuf tmp;
659         char buf[64];
660         
661         tmp.buf = buf;
662         buf[0] = '\0';
663         tmp.BufSize = 64;
664         tmp.BufUsed = 0;
665         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
666                 return(atoi(buf));
667         else 
668                 return 0;
669 }
670
671
672
673 /**
674  * \brief Input binary data from socket
675  * \param buf the buffer to get the input to
676  * \param bytes the maximal number of bytes to read
677  */
678 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
679 {
680         int len, rlen, slen;
681
682         if (!append)
683                 FlushStrBuf(buf);
684
685         slen = len = buf->BufUsed;
686         while (1) {
687                 rlen = read(*fd, &buf->buf[len], 1);
688                 if (rlen < 1) {
689                         *Error = strerror(errno);
690                         
691                         close(*fd);
692                         *fd = -1;
693                         
694                         return -1;
695                 }
696                 if (buf->buf[len] == '\n')
697                         break;
698                 if (buf->buf[len] != '\r')
699                         len ++;
700                 if (!(len < buf->BufSize)) {
701                         buf->BufUsed = len;
702                         buf->buf[len+1] = '\0';
703                         IncreaseBuf(buf, 1, -1);
704                 }
705         }
706         buf->BufUsed = len;
707         buf->buf[len] = '\0';
708         return len - slen;
709 }
710
711 /**
712  * \brief Input binary data from socket
713  * \param buf the buffer to get the input to
714  * \param bytes the maximal number of bytes to read
715  */
716 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
717 {
718         fd_set wset;
719         int fdflags;
720         int len, rlen, slen;
721         int nRead = 0;
722         char *ptr;
723
724         if ((Buf == NULL) || (*fd == -1))
725                 return -1;
726         if (!append)
727                 FlushStrBuf(Buf);
728         if (Buf->BufUsed + nBytes > Buf->BufSize)
729                 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
730
731         ptr = Buf->buf + Buf->BufUsed;
732
733         slen = len = Buf->BufUsed;
734
735         fdflags = fcntl(*fd, F_GETFL);
736
737         while (nRead < nBytes) {
738                if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
739                         FD_ZERO(&wset);
740                         FD_SET(*fd, &wset);
741                         if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
742                                 *Error = strerror(errno);
743                                 return -1;
744                         }
745                 }
746
747                 if ((rlen = read(*fd, 
748                                  ptr,
749                                  nBytes - nRead)) == -1) {
750                         close(*fd);
751                         *fd = -1;
752                         *Error = strerror(errno);
753                         return rlen;
754                 }
755                 nRead += rlen;
756                 Buf->BufUsed += rlen;
757         }
758         Buf->buf[Buf->BufUsed] = '\0';
759         return nRead;
760 }
761
762 void StrBufCutLeft(StrBuf *Buf, int nChars)
763 {
764         if (nChars >= Buf->BufUsed) {
765                 FlushStrBuf(Buf);
766                 return;
767         }
768         memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
769         Buf->BufUsed -= nChars;
770         Buf->buf[Buf->BufUsed] = '\0';
771 }
772
773 void StrBufCutRight(StrBuf *Buf, int nChars)
774 {
775         if (nChars >= Buf->BufUsed) {
776                 FlushStrBuf(Buf);
777                 return;
778         }
779         Buf->BufUsed -= nChars;
780         Buf->buf[Buf->BufUsed] = '\0';
781 }
782
783
784 /*
785  * string conversion function
786  */
787 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source) 
788 {
789         int a, b, len;
790         char hex[3];
791
792         if (target != NULL)
793                 FlushStrBuf(target);
794
795         if (source == NULL ||target == NULL)
796         {
797                 return;
798         }
799
800         len = source->BufUsed;
801         for (a = 0; a < len; ++a) {
802                 if (target->BufUsed >= target->BufSize)
803                         IncreaseBuf(target, 1, -1);
804
805                 if (source->buf[a] == '=') {
806                         hex[0] = source->buf[a + 1];
807                         hex[1] = source->buf[a + 2];
808                         hex[2] = 0;
809                         b = 0;
810                         sscanf(hex, "%02x", &b);
811                         target->buf[target->BufUsed] = b;
812                         target->buf[++target->BufUsed] = 0;
813                         a += 2;
814                 }
815                 else {
816                         target->buf[target->BufUsed] = source->buf[a];
817                         target->buf[++target->BufUsed] = 0;
818                 }
819         }
820 }
821
822
823 /*
824  * string conversion function
825  */
826 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source) 
827 {
828         int i, len;
829
830         if (target != NULL)
831                 FlushStrBuf(target);
832
833         if (source == NULL ||target == NULL)
834         {
835                 return;
836         }
837
838         len = source->BufUsed;
839         for (i=0; i<len; ++i) {
840                 if (target->BufUsed + 4 >= target->BufSize)
841                         IncreaseBuf(target, 1, -1);
842                 if ( (isalnum(source->buf[i])) || 
843                      (source->buf[i]=='-') || 
844                      (source->buf[i]=='_') ) {
845                         target->buf[target->BufUsed++] = source->buf[i];
846                 }
847                 else {
848                         sprintf(&target->buf[target->BufUsed], 
849                                 "=%02X", 
850                                 (0xFF &source->buf[i]));
851                         target->BufUsed += 3;
852                 }
853         }
854         target->buf[target->BufUsed + 1] = '\0';
855 }
856
857 /*
858  * \brief uses the same calling syntax as compress2(), but it
859  * creates a stream compatible with HTTP "Content-encoding: gzip"
860  */
861 #ifdef HAVE_ZLIB
862 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
863 #define OS_CODE 0x03    /*< unix */
864 int ZEXPORT compress_gzip(Bytef * dest,         /*< compressed buffer*/
865                           size_t * destLen,     /*< length of the compresed data */
866                           const Bytef * source, /*< source to encode */
867                           uLong sourceLen,      /*< length of source to encode */
868                           int level)            /*< compression level */
869 {
870         const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
871
872         /* write gzip header */
873         snprintf((char *) dest, *destLen, 
874                  "%c%c%c%c%c%c%c%c%c%c",
875                  gz_magic[0], gz_magic[1], Z_DEFLATED,
876                  0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
877                  OS_CODE);
878
879         /* normal deflate */
880         z_stream stream;
881         int err;
882         stream.next_in = (Bytef *) source;
883         stream.avail_in = (uInt) sourceLen;
884         stream.next_out = dest + 10L;   // after header
885         stream.avail_out = (uInt) * destLen;
886         if ((uLong) stream.avail_out != *destLen)
887                 return Z_BUF_ERROR;
888
889         stream.zalloc = (alloc_func) 0;
890         stream.zfree = (free_func) 0;
891         stream.opaque = (voidpf) 0;
892
893         err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
894                            DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
895         if (err != Z_OK)
896                 return err;
897
898         err = deflate(&stream, Z_FINISH);
899         if (err != Z_STREAM_END) {
900                 deflateEnd(&stream);
901                 return err == Z_OK ? Z_BUF_ERROR : err;
902         }
903         *destLen = stream.total_out + 10L;
904
905         /* write CRC and Length */
906         uLong crc = crc32(0L, source, sourceLen);
907         int n;
908         for (n = 0; n < 4; ++n, ++*destLen) {
909                 dest[*destLen] = (int) (crc & 0xff);
910                 crc >>= 8;
911         }
912         uLong len = stream.total_in;
913         for (n = 0; n < 4; ++n, ++*destLen) {
914                 dest[*destLen] = (int) (len & 0xff);
915                 len >>= 8;
916         }
917         err = deflateEnd(&stream);
918         return err;
919 }
920 #endif
921
922
923 /**
924  * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
925  */
926 int CompressBuffer(StrBuf *Buf)
927 {
928 #ifdef HAVE_ZLIB
929         char *compressed_data = NULL;
930         size_t compressed_len, bufsize;
931         
932         bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
933         compressed_data = malloc(compressed_len);
934         
935         if (compress_gzip((Bytef *) compressed_data,
936                           &compressed_len,
937                           (Bytef *) Buf->buf,
938                           (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
939                 if (!ConstBuf)
940                         free(Buf->buf);
941                 Buf->buf = compressed_data;
942                 Buf->BufUsed = compressed_len;
943                 Buf->BufSize = bufsize;
944                 return 1;
945         } else {
946                 free(compressed_data);
947         }
948 #endif  /* HAVE_ZLIB */
949         return 0;
950 }
951
952 int StrBufDecodeBase64(StrBuf *Buf)
953 {
954         char *xferbuf;
955         size_t siz;
956         if (Buf == NULL) return -1;
957
958         xferbuf = (char*) malloc(Buf->BufSize);
959         siz = CtdlDecodeBase64(xferbuf,
960                                Buf->buf,
961                                Buf->BufUsed);
962         free(Buf->buf);
963         Buf->buf = xferbuf;
964         Buf->BufUsed = siz;
965         return siz;
966 }
967
968
969 /*   
970  * remove escaped strings from i.e. the url string (like %20 for blanks)
971  */
972 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
973 {
974         int a, b;
975         char hex[3];
976         long len;
977
978         while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
979                 Buf->buf[Buf->BufUsed - 1] = '\0';
980                 Buf->BufUsed --;
981         }
982
983         a = 0; 
984         while (a < Buf->BufUsed) {
985                 if (Buf->buf[a] == '+')
986                         Buf->buf[a] = ' ';
987                 else if (Buf->buf[a] == '%') {
988                         /* don't let % chars through, rather truncate the input. */
989                         if (a + 2 > Buf->BufUsed) {
990                                 Buf->buf[a] = '\0';
991                                 Buf->BufUsed = a;
992                         }
993                         else {                  
994                                 hex[0] = Buf->buf[a + 1];
995                                 hex[1] = Buf->buf[a + 2];
996                                 hex[2] = 0;
997                                 b = 0;
998                                 sscanf(hex, "%02x", &b);
999                                 Buf->buf[a] = (char) b;
1000                                 len = Buf->BufUsed - a - 2;
1001                                 if (len > 0)
1002                                         memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
1003                         
1004                                 Buf->BufUsed -=2;
1005                         }
1006                 }
1007                 a++;
1008         }
1009         return a;
1010 }
1011
1012
1013 /**
1014  * \brief       RFC2047-encode a header field if necessary.
1015  *              If no non-ASCII characters are found, the string
1016  *              will be copied verbatim without encoding.
1017  *
1018  * \param       target          Target buffer.
1019  * \param       source          Source string to be encoded.
1020  * \returns     encoded length; -1 if non success.
1021  */
1022 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1023 {
1024         const char headerStr[] = "=?UTF-8?Q?";
1025         int need_to_encode = 0;
1026         int i = 0;
1027         unsigned char ch;
1028
1029         if ((source == NULL) || 
1030             (target == NULL))
1031             return -1;
1032
1033         while ((i < source->BufUsed) &&
1034                (!IsEmptyStr (&source->buf[i])) &&
1035                (need_to_encode == 0)) {
1036                 if (((unsigned char) source->buf[i] < 32) || 
1037                     ((unsigned char) source->buf[i] > 126)) {
1038                         need_to_encode = 1;
1039                 }
1040                 i++;
1041         }
1042
1043         if (!need_to_encode) {
1044                 if (*target == NULL) {
1045                         *target = NewStrBufPlain(source->buf, source->BufUsed);
1046                 }
1047                 else {
1048                         FlushStrBuf(*target);
1049                         StrBufAppendBuf(*target, source, 0);
1050                 }
1051                 return (*target)->BufUsed;
1052         }
1053         if (*target == NULL)
1054                 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
1055         else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
1056                 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
1057         memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
1058         (*target)->BufUsed = sizeof(headerStr) - 1;
1059         for (i=0; (i < source->BufUsed); ++i) {
1060                 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1061                         IncreaseBuf(*target, 1, 0);
1062                 ch = (unsigned char) source->buf[i];
1063                 if ((ch < 32) || (ch > 126) || (ch == 61)) {
1064                         sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
1065                         (*target)->BufUsed += 3;
1066                 }
1067                 else {
1068                         (*target)->buf[(*target)->BufUsed] = ch;
1069                         (*target)->BufUsed++;
1070                 }
1071         }
1072         
1073         if ((*target)->BufUsed + 4 > (*target)->BufSize)
1074                 IncreaseBuf(*target, 1, 0);
1075
1076         (*target)->buf[(*target)->BufUsed++] = '?';
1077         (*target)->buf[(*target)->BufUsed++] = '=';
1078         (*target)->buf[(*target)->BufUsed] = '\0';
1079         return (*target)->BufUsed;;
1080 }
1081
1082 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1083 {
1084         long i;
1085         if (buf == NULL)
1086                 return;
1087         for (i=0; i<buf->BufUsed; i++)
1088                 if (buf->buf[i] == search)
1089                         buf->buf[i] = replace;
1090
1091 }