* catch empty source string condition
[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         if (len == 0) 
284                 return;
285
286         pt = OutBuf->buf + OutBuf->BufUsed;
287         pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
288
289         while (pch < pche) {
290                 if (pt >= pte) {
291                         IncreaseBuf(OutBuf, 1, -1);
292                         pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
293                         pt = OutBuf->buf + OutBuf->BufUsed;
294                 }
295                 
296                 c = 0;
297                 for (b = 0; b < eclen; ++b) {
298                         if (*pch == ec[b]) {
299                                 c = 1;
300                                 b += eclen;
301                         }
302                 }
303                 if (c == 1) {
304                         sprintf(pt,"%%%02X", *pch);
305                         pt += 3;
306                         OutBuf->BufUsed += 3;
307                         pch ++;
308                 }
309                 else {
310                         *(pt++) = *(pch++);
311                         OutBuf->BufUsed++;
312                 }
313         }
314         *pt = '\0';
315 }
316
317 /*
318  * Copy a string, escaping characters which have meaning in HTML.  
319  *
320  * target               target buffer
321  * strbuf               source buffer
322  * nbsp                 If nonzero, spaces are converted to non-breaking spaces.
323  * nolinebreaks         if set, linebreaks are removed from the string.
324  */
325 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
326 {
327         const char *aptr, *eiptr;
328         char *bptr, *eptr;
329         long len;
330
331         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
332                 return -1;
333
334         if (PlainIn != NULL) {
335                 aptr = PlainIn;
336                 len = strlen(PlainIn);
337                 eiptr = aptr + len;
338         }
339         else {
340                 aptr = Source->buf;
341                 eiptr = aptr + Source->BufUsed;
342                 len = Source->BufUsed;
343         }
344
345         if (len == 0) 
346                 return;
347
348         bptr = Target->buf + Target->BufUsed;
349         eptr = Target->buf + Target->BufSize - 6; /* our biggest unit to put in...  */
350
351         while (aptr < eiptr){
352                 if(bptr >= eptr) {
353                         IncreaseBuf(Target, 1, -1);
354                         eptr = Target->buf + Target->BufSize - 6; 
355                         bptr = Target->buf + Target->BufUsed;
356                 }
357                 if (*aptr == '<') {
358                         memcpy(bptr, "&lt;", 4);
359                         bptr += 4;
360                         Target->BufUsed += 4;
361                 }
362                 else if (*aptr == '>') {
363                         memcpy(bptr, "&gt;", 4);
364                         bptr += 4;
365                         Target->BufUsed += 4;
366                 }
367                 else if (*aptr == '&') {
368                         memcpy(bptr, "&amp;", 5);
369                         bptr += 5;
370                         Target->BufUsed += 5;
371                 }
372                 else if (*aptr == '\"') {
373                         memcpy(bptr, "&quot;", 6);
374                         bptr += 6;
375                         Target->BufUsed += 6;
376                 }
377                 else if (*aptr == '\'') {
378                         memcpy(bptr, "&#39;", 5);
379                         bptr += 5;
380                         Target->BufUsed += 5;
381                 }
382                 else if (*aptr == LB) {
383                         *bptr = '<';
384                         bptr ++;
385                         Target->BufUsed ++;
386                 }
387                 else if (*aptr == RB) {
388                         *bptr = '>';
389                         bptr ++;
390                         Target->BufUsed ++;
391                 }
392                 else if (*aptr == QU) {
393                         *bptr ='"';
394                         bptr ++;
395                         Target->BufUsed ++;
396                 }
397                 else if ((*aptr == 32) && (nbsp == 1)) {
398                         memcpy(bptr, "&nbsp;", 6);
399                         bptr += 6;
400                         Target->BufUsed += 6;
401                 }
402                 else if ((*aptr == '\n') && (nolinebreaks)) {
403                         *bptr='\0';     /* nothing */
404                 }
405                 else if ((*aptr == '\r') && (nolinebreaks)) {
406                         *bptr='\0';     /* nothing */
407                 }
408                 else{
409                         *bptr = *aptr;
410                         bptr++;
411                         Target->BufUsed ++;
412                 }
413                 aptr ++;
414         }
415         *bptr = '\0';
416         if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
417                 return -1;
418         return Target->BufUsed;
419 }
420
421 void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn)
422 {
423         const char *aptr, *eiptr;
424         char *tptr, *eptr;
425         long len;
426
427         if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
428                 return ;
429
430         if (PlainIn != NULL) {
431                 aptr = PlainIn;
432                 len = strlen(PlainIn);
433                 eiptr = aptr + len;
434         }
435         else {
436                 aptr = Source->buf;
437                 eiptr = aptr + Source->BufUsed;
438                 len = Source->BufUsed;
439         }
440
441         if (len == 0) 
442                 return;
443
444         eiptr = Target->buf + Target->BufSize - 6; 
445         tptr = Target->buf + Target->BufUsed;
446         
447         while (aptr < eiptr){
448                 if(tptr >= eptr) {
449                         IncreaseBuf(Target, 1, -1);
450                         eiptr = Target->buf + Target->BufSize - 6; 
451                         tptr = Target->buf + Target->BufUsed;
452                 }
453                
454                 if (*aptr == '\n') {
455                         *tptr = ' ';
456                         Target->BufUsed++;
457                 }
458                 else if (*aptr == '\r') {
459                         *tptr = ' ';
460                         Target->BufUsed++;
461                 }
462                 else if (*aptr == '\'') {
463                         *(tptr++) = '&';
464                         *(tptr++) = '#';
465                         *(tptr++) = '3';
466                         *(tptr++) = '9';
467                         *tptr = ';';
468                         Target->BufUsed += 5;
469                 } else {
470                         *tptr = *aptr;
471                         Target->BufUsed++;
472                 }
473                 tptr++; aptr++;
474         }
475         *tptr = '\0';
476 }
477
478
479 inline int StrBufNum_tokens(const StrBuf *source, char tok)
480 {
481         return num_tokens(source->buf, tok);
482 }
483
484
485 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
486 {
487         size_t NCharsRemain;
488         if (Offset > Source->BufUsed)
489         {
490                 FlushStrBuf(dest);
491                 return 0;
492         }
493         if (Offset + nChars < Source->BufUsed)
494         {
495                 if (nChars > dest->BufSize)
496                         IncreaseBuf(dest, 0, nChars + 1);
497                 memcpy(dest->buf, Source->buf + Offset, nChars);
498                 dest->BufUsed = nChars;
499                 dest->buf[dest->BufUsed] = '\0';
500                 return nChars;
501         }
502         NCharsRemain = Source->BufUsed - Offset;
503         if (NCharsRemain > dest->BufSize)
504                 IncreaseBuf(dest, 0, NCharsRemain + 1);
505         memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
506         dest->BufUsed = NCharsRemain;
507         dest->buf[dest->BufUsed] = '\0';
508         return NCharsRemain;
509 }
510
511
512 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
513 {
514         size_t nWritten = Buf->BufSize + 1;
515         size_t Offset = Buf->BufUsed;
516         size_t newused = Offset + nWritten;
517         
518         while (newused >= Buf->BufSize) {
519                 nWritten = vsnprintf(Buf->buf + Offset, 
520                                      Buf->BufSize - Offset, 
521                                      format, ap);
522                 newused = Offset + nWritten;
523                 if (newused >= Buf->BufSize)
524                         IncreaseBuf(Buf, 1, 0);
525                 else
526                         Buf->BufUsed = Offset + nWritten ;
527
528         }
529 }
530
531 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
532 {
533         size_t nWritten = Buf->BufSize + 1;
534         size_t Offset = Buf->BufUsed;
535         size_t newused = Offset + nWritten;
536         va_list arg_ptr;
537         
538         while (newused >= Buf->BufSize) {
539                 va_start(arg_ptr, format);
540                 nWritten = vsnprintf(Buf->buf + Offset, 
541                                      Buf->BufSize - Offset, 
542                                      format, arg_ptr);
543                 va_end(arg_ptr);
544                 newused = Offset + nWritten;
545                 if (newused >= Buf->BufSize)
546                         IncreaseBuf(Buf, 1, 0);
547                 else
548                         Buf->BufUsed = Offset + nWritten ;
549
550         }
551 }
552
553 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
554 {
555         size_t nWritten = Buf->BufSize + 1;
556         va_list arg_ptr;
557         
558         while (nWritten >= Buf->BufSize) {
559                 va_start(arg_ptr, format);
560                 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
561                 va_end(arg_ptr);
562                 Buf->BufUsed = nWritten ;
563                 if (nWritten >= Buf->BufSize)
564                         IncreaseBuf(Buf, 0, 0);
565         }
566 }
567
568
569 /**
570  * \brief a string tokenizer
571  * \param dest Destination StringBuffer
572  * \param Source StringBuffer to read into
573  * \param separator tokenizer param
574  * \returns -1 if not found, else length of token.
575  */
576 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
577 {
578         const char *s, *e;              //* source * /
579         int len = 0;                    //* running total length of extracted string * /
580         int current_token = 0;          //* token currently being processed * /
581
582         if ((Source == NULL) || (Source->BufUsed ==0)) {
583                 return(-1);
584         }
585         s = Source->buf;
586         e = s + Source->BufUsed;
587         if (dest == NULL) {
588                 return(-1);
589         }
590
591         //cit_backtrace();
592         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
593         dest->buf[0] = '\0';
594         dest->BufUsed = 0;
595
596         while ((s<e) && !IsEmptyStr(s)) {
597                 if (*s == separator) {
598                         ++current_token;
599                 }
600                 if (len >= dest->BufSize)
601                         if (!IncreaseBuf(dest, 1, -1))
602                                 break;
603                 if ( (current_token == parmnum) && 
604                      (*s != separator)) {
605                         dest->buf[len] = *s;
606                         ++len;
607                 }
608                 else if (current_token > parmnum) {
609                         break;
610                 }
611                 ++s;
612         }
613         
614         dest->buf[len] = '\0';
615         dest->BufUsed = len;
616                 
617         if (current_token < parmnum) {
618                 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
619                 return(-1);
620         }
621         //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
622         return(len);
623 }
624
625
626 /*
627  * extract_int()  -  extract an int parm w/o supplying a buffer
628  */
629 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
630 {
631         StrBuf tmp;
632         char buf[64];
633         
634         tmp.buf = buf;
635         buf[0] = '\0';
636         tmp.BufSize = 64;
637         tmp.BufUsed = 0;
638         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
639                 return(atoi(buf));
640         else
641                 return 0;
642 }
643
644 /*
645  * extract_long()  -  extract an long parm w/o supplying a buffer
646  */
647 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
648 {
649         StrBuf tmp;
650         char buf[64];
651         
652         tmp.buf = buf;
653         buf[0] = '\0';
654         tmp.BufSize = 64;
655         tmp.BufUsed = 0;
656         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
657                 return(atoi(buf));
658         else
659                 return 0;
660 }
661
662
663 /*
664  * extract_unsigned_long() - extract an unsigned long parm
665  */
666 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
667 {
668         StrBuf tmp;
669         char buf[64];
670         
671         tmp.buf = buf;
672         buf[0] = '\0';
673         tmp.BufSize = 64;
674         tmp.BufUsed = 0;
675         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
676                 return(atoi(buf));
677         else 
678                 return 0;
679 }
680
681
682
683 /**
684  * \brief Input binary data from socket
685  * \param buf the buffer to get the input to
686  * \param bytes the maximal number of bytes to read
687  */
688 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
689 {
690         int len, rlen, slen;
691
692         if (!append)
693                 FlushStrBuf(buf);
694
695         slen = len = buf->BufUsed;
696         while (1) {
697                 rlen = read(*fd, &buf->buf[len], 1);
698                 if (rlen < 1) {
699                         *Error = strerror(errno);
700                         
701                         close(*fd);
702                         *fd = -1;
703                         
704                         return -1;
705                 }
706                 if (buf->buf[len] == '\n')
707                         break;
708                 if (buf->buf[len] != '\r')
709                         len ++;
710                 if (!(len < buf->BufSize)) {
711                         buf->BufUsed = len;
712                         buf->buf[len+1] = '\0';
713                         IncreaseBuf(buf, 1, -1);
714                 }
715         }
716         buf->BufUsed = len;
717         buf->buf[len] = '\0';
718         return len - slen;
719 }
720
721 /**
722  * \brief Input binary data from socket
723  * \param buf the buffer to get the input to
724  * \param bytes the maximal number of bytes to read
725  */
726 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
727 {
728         fd_set wset;
729         int fdflags;
730         int len, rlen, slen;
731         int nRead = 0;
732         char *ptr;
733
734         if ((Buf == NULL) || (*fd == -1))
735                 return -1;
736         if (!append)
737                 FlushStrBuf(Buf);
738         if (Buf->BufUsed + nBytes > Buf->BufSize)
739                 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
740
741         ptr = Buf->buf + Buf->BufUsed;
742
743         slen = len = Buf->BufUsed;
744
745         fdflags = fcntl(*fd, F_GETFL);
746
747         while (nRead < nBytes) {
748                if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
749                         FD_ZERO(&wset);
750                         FD_SET(*fd, &wset);
751                         if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
752                                 *Error = strerror(errno);
753                                 return -1;
754                         }
755                 }
756
757                 if ((rlen = read(*fd, 
758                                  ptr,
759                                  nBytes - nRead)) == -1) {
760                         close(*fd);
761                         *fd = -1;
762                         *Error = strerror(errno);
763                         return rlen;
764                 }
765                 nRead += rlen;
766                 Buf->BufUsed += rlen;
767         }
768         Buf->buf[Buf->BufUsed] = '\0';
769         return nRead;
770 }
771
772 void StrBufCutLeft(StrBuf *Buf, int nChars)
773 {
774         if (nChars >= Buf->BufUsed) {
775                 FlushStrBuf(Buf);
776                 return;
777         }
778         memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
779         Buf->BufUsed -= nChars;
780         Buf->buf[Buf->BufUsed] = '\0';
781 }
782
783 void StrBufCutRight(StrBuf *Buf, int nChars)
784 {
785         if (nChars >= Buf->BufUsed) {
786                 FlushStrBuf(Buf);
787                 return;
788         }
789         Buf->BufUsed -= nChars;
790         Buf->buf[Buf->BufUsed] = '\0';
791 }
792
793
794 /*
795  * string conversion function
796  */
797 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source) 
798 {
799         int a, b, len;
800         char hex[3];
801
802         if (target != NULL)
803                 FlushStrBuf(target);
804
805         if (source == NULL ||target == NULL)
806         {
807                 return;
808         }
809
810         len = source->BufUsed;
811         for (a = 0; a < len; ++a) {
812                 if (target->BufUsed >= target->BufSize)
813                         IncreaseBuf(target, 1, -1);
814
815                 if (source->buf[a] == '=') {
816                         hex[0] = source->buf[a + 1];
817                         hex[1] = source->buf[a + 2];
818                         hex[2] = 0;
819                         b = 0;
820                         sscanf(hex, "%02x", &b);
821                         target->buf[target->BufUsed] = b;
822                         target->buf[++target->BufUsed] = 0;
823                         a += 2;
824                 }
825                 else {
826                         target->buf[target->BufUsed] = source->buf[a];
827                         target->buf[++target->BufUsed] = 0;
828                 }
829         }
830 }
831
832
833 /*
834  * string conversion function
835  */
836 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source) 
837 {
838         int i, len;
839
840         if (target != NULL)
841                 FlushStrBuf(target);
842
843         if (source == NULL ||target == NULL)
844         {
845                 return;
846         }
847
848         len = source->BufUsed;
849         for (i=0; i<len; ++i) {
850                 if (target->BufUsed + 4 >= target->BufSize)
851                         IncreaseBuf(target, 1, -1);
852                 if ( (isalnum(source->buf[i])) || 
853                      (source->buf[i]=='-') || 
854                      (source->buf[i]=='_') ) {
855                         target->buf[target->BufUsed++] = source->buf[i];
856                 }
857                 else {
858                         sprintf(&target->buf[target->BufUsed], 
859                                 "=%02X", 
860                                 (0xFF &source->buf[i]));
861                         target->BufUsed += 3;
862                 }
863         }
864         target->buf[target->BufUsed + 1] = '\0';
865 }
866
867 /*
868  * \brief uses the same calling syntax as compress2(), but it
869  * creates a stream compatible with HTTP "Content-encoding: gzip"
870  */
871 #ifdef HAVE_ZLIB
872 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
873 #define OS_CODE 0x03    /*< unix */
874 int ZEXPORT compress_gzip(Bytef * dest,         /*< compressed buffer*/
875                           size_t * destLen,     /*< length of the compresed data */
876                           const Bytef * source, /*< source to encode */
877                           uLong sourceLen,      /*< length of source to encode */
878                           int level)            /*< compression level */
879 {
880         const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
881
882         /* write gzip header */
883         snprintf((char *) dest, *destLen, 
884                  "%c%c%c%c%c%c%c%c%c%c",
885                  gz_magic[0], gz_magic[1], Z_DEFLATED,
886                  0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
887                  OS_CODE);
888
889         /* normal deflate */
890         z_stream stream;
891         int err;
892         stream.next_in = (Bytef *) source;
893         stream.avail_in = (uInt) sourceLen;
894         stream.next_out = dest + 10L;   // after header
895         stream.avail_out = (uInt) * destLen;
896         if ((uLong) stream.avail_out != *destLen)
897                 return Z_BUF_ERROR;
898
899         stream.zalloc = (alloc_func) 0;
900         stream.zfree = (free_func) 0;
901         stream.opaque = (voidpf) 0;
902
903         err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
904                            DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
905         if (err != Z_OK)
906                 return err;
907
908         err = deflate(&stream, Z_FINISH);
909         if (err != Z_STREAM_END) {
910                 deflateEnd(&stream);
911                 return err == Z_OK ? Z_BUF_ERROR : err;
912         }
913         *destLen = stream.total_out + 10L;
914
915         /* write CRC and Length */
916         uLong crc = crc32(0L, source, sourceLen);
917         int n;
918         for (n = 0; n < 4; ++n, ++*destLen) {
919                 dest[*destLen] = (int) (crc & 0xff);
920                 crc >>= 8;
921         }
922         uLong len = stream.total_in;
923         for (n = 0; n < 4; ++n, ++*destLen) {
924                 dest[*destLen] = (int) (len & 0xff);
925                 len >>= 8;
926         }
927         err = deflateEnd(&stream);
928         return err;
929 }
930 #endif
931
932
933 /**
934  * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
935  */
936 int CompressBuffer(StrBuf *Buf)
937 {
938 #ifdef HAVE_ZLIB
939         char *compressed_data = NULL;
940         size_t compressed_len, bufsize;
941         
942         bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
943         compressed_data = malloc(compressed_len);
944         
945         if (compress_gzip((Bytef *) compressed_data,
946                           &compressed_len,
947                           (Bytef *) Buf->buf,
948                           (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
949                 if (!ConstBuf)
950                         free(Buf->buf);
951                 Buf->buf = compressed_data;
952                 Buf->BufUsed = compressed_len;
953                 Buf->BufSize = bufsize;
954                 return 1;
955         } else {
956                 free(compressed_data);
957         }
958 #endif  /* HAVE_ZLIB */
959         return 0;
960 }
961
962 int StrBufDecodeBase64(StrBuf *Buf)
963 {
964         char *xferbuf;
965         size_t siz;
966         if (Buf == NULL) return -1;
967
968         xferbuf = (char*) malloc(Buf->BufSize);
969         siz = CtdlDecodeBase64(xferbuf,
970                                Buf->buf,
971                                Buf->BufUsed);
972         free(Buf->buf);
973         Buf->buf = xferbuf;
974         Buf->BufUsed = siz;
975         return siz;
976 }
977
978
979 /*   
980  * remove escaped strings from i.e. the url string (like %20 for blanks)
981  */
982 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
983 {
984         int a, b;
985         char hex[3];
986         long len;
987
988         while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
989                 Buf->buf[Buf->BufUsed - 1] = '\0';
990                 Buf->BufUsed --;
991         }
992
993         a = 0; 
994         while (a < Buf->BufUsed) {
995                 if (Buf->buf[a] == '+')
996                         Buf->buf[a] = ' ';
997                 else if (Buf->buf[a] == '%') {
998                         /* don't let % chars through, rather truncate the input. */
999                         if (a + 2 > Buf->BufUsed) {
1000                                 Buf->buf[a] = '\0';
1001                                 Buf->BufUsed = a;
1002                         }
1003                         else {                  
1004                                 hex[0] = Buf->buf[a + 1];
1005                                 hex[1] = Buf->buf[a + 2];
1006                                 hex[2] = 0;
1007                                 b = 0;
1008                                 sscanf(hex, "%02x", &b);
1009                                 Buf->buf[a] = (char) b;
1010                                 len = Buf->BufUsed - a - 2;
1011                                 if (len > 0)
1012                                         memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
1013                         
1014                                 Buf->BufUsed -=2;
1015                         }
1016                 }
1017                 a++;
1018         }
1019         return a;
1020 }
1021
1022
1023 /**
1024  * \brief       RFC2047-encode a header field if necessary.
1025  *              If no non-ASCII characters are found, the string
1026  *              will be copied verbatim without encoding.
1027  *
1028  * \param       target          Target buffer.
1029  * \param       source          Source string to be encoded.
1030  * \returns     encoded length; -1 if non success.
1031  */
1032 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1033 {
1034         const char headerStr[] = "=?UTF-8?Q?";
1035         int need_to_encode = 0;
1036         int i = 0;
1037         unsigned char ch;
1038
1039         if ((source == NULL) || 
1040             (target == NULL))
1041             return -1;
1042
1043         while ((i < source->BufUsed) &&
1044                (!IsEmptyStr (&source->buf[i])) &&
1045                (need_to_encode == 0)) {
1046                 if (((unsigned char) source->buf[i] < 32) || 
1047                     ((unsigned char) source->buf[i] > 126)) {
1048                         need_to_encode = 1;
1049                 }
1050                 i++;
1051         }
1052
1053         if (!need_to_encode) {
1054                 if (*target == NULL) {
1055                         *target = NewStrBufPlain(source->buf, source->BufUsed);
1056                 }
1057                 else {
1058                         FlushStrBuf(*target);
1059                         StrBufAppendBuf(*target, source, 0);
1060                 }
1061                 return (*target)->BufUsed;
1062         }
1063         if (*target == NULL)
1064                 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
1065         else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
1066                 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
1067         memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
1068         (*target)->BufUsed = sizeof(headerStr) - 1;
1069         for (i=0; (i < source->BufUsed); ++i) {
1070                 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1071                         IncreaseBuf(*target, 1, 0);
1072                 ch = (unsigned char) source->buf[i];
1073                 if ((ch < 32) || (ch > 126) || (ch == 61)) {
1074                         sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
1075                         (*target)->BufUsed += 3;
1076                 }
1077                 else {
1078                         (*target)->buf[(*target)->BufUsed] = ch;
1079                         (*target)->BufUsed++;
1080                 }
1081         }
1082         
1083         if ((*target)->BufUsed + 4 > (*target)->BufSize)
1084                 IncreaseBuf(*target, 1, 0);
1085
1086         (*target)->buf[(*target)->BufUsed++] = '?';
1087         (*target)->buf[(*target)->BufUsed++] = '=';
1088         (*target)->buf[(*target)->BufUsed] = '\0';
1089         return (*target)->BufUsed;;
1090 }
1091
1092 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1093 {
1094         long i;
1095         if (buf == NULL)
1096                 return;
1097         for (i=0; i<buf->BufUsed; i++)
1098                 if (buf->buf[i] == search)
1099                         buf->buf[i] = replace;
1100
1101 }