]> code.citadel.org Git - citadel.git/blob - libcitadel/lib/stringbuf.c
* redid sprintf wrapper logic
[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 -1;
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 BufSize = Buf->BufSize;
515         size_t nWritten = Buf->BufSize + 1;
516         size_t Offset = Buf->BufUsed;
517         size_t newused = Offset + nWritten;
518         
519         while (newused >= BufSize) {
520                 nWritten = vsnprintf(Buf->buf + Offset, 
521                                      Buf->BufSize - Offset, 
522                                      format, ap);
523                 newused = Offset + nWritten;
524                 if (newused >= Buf->BufSize) {
525                         IncreaseBuf(Buf, 1, newused);
526                 }
527                 else {
528                         Buf->BufUsed = Offset + nWritten;
529                         BufSize = Buf->BufSize;
530                 }
531
532         }
533 }
534
535 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
536 {
537         size_t BufSize = Buf->BufSize;
538         size_t nWritten = Buf->BufSize + 1;
539         size_t Offset = Buf->BufUsed;
540         size_t newused = Offset + nWritten;
541         va_list arg_ptr;
542         
543         while (newused >= BufSize) {
544                 va_start(arg_ptr, format);
545                 nWritten = vsnprintf(Buf->buf + Buf->BufUsed, 
546                                      Buf->BufSize - Buf->BufUsed, 
547                                      format, arg_ptr);
548                 va_end(arg_ptr);
549                 newused = Buf->BufUsed + nWritten;
550                 if (newused >= Buf->BufSize) {
551                         IncreaseBuf(Buf, 1, newused);
552                 }
553                 else {
554                         Buf->BufUsed += nWritten;
555                         BufSize = Buf->BufSize;
556                 }
557
558         }
559 }
560
561 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
562 {
563         size_t nWritten = Buf->BufSize + 1;
564         va_list arg_ptr;
565         
566         while (nWritten >= Buf->BufSize) {
567                 va_start(arg_ptr, format);
568                 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
569                 va_end(arg_ptr);
570                 Buf->BufUsed = nWritten ;
571                 if (nWritten >= Buf->BufSize)
572                         IncreaseBuf(Buf, 0, 0);
573         }
574 }
575
576
577 /**
578  * \brief a string tokenizer
579  * \param dest Destination StringBuffer
580  * \param Source StringBuffer to read into
581  * \param separator tokenizer param
582  * \returns -1 if not found, else length of token.
583  */
584 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
585 {
586         const char *s, *e;              //* source * /
587         int len = 0;                    //* running total length of extracted string * /
588         int current_token = 0;          //* token currently being processed * /
589
590         if ((Source == NULL) || (Source->BufUsed ==0)) {
591                 return(-1);
592         }
593         s = Source->buf;
594         e = s + Source->BufUsed;
595         if (dest == NULL) {
596                 return(-1);
597         }
598
599         //cit_backtrace();
600         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
601         dest->buf[0] = '\0';
602         dest->BufUsed = 0;
603
604         while ((s<e) && !IsEmptyStr(s)) {
605                 if (*s == separator) {
606                         ++current_token;
607                 }
608                 if (len >= dest->BufSize)
609                         if (!IncreaseBuf(dest, 1, -1))
610                                 break;
611                 if ( (current_token == parmnum) && 
612                      (*s != separator)) {
613                         dest->buf[len] = *s;
614                         ++len;
615                 }
616                 else if (current_token > parmnum) {
617                         break;
618                 }
619                 ++s;
620         }
621         
622         dest->buf[len] = '\0';
623         dest->BufUsed = len;
624                 
625         if (current_token < parmnum) {
626                 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
627                 return(-1);
628         }
629         //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
630         return(len);
631 }
632
633
634 /*
635  * extract_int()  -  extract an int parm w/o supplying a buffer
636  */
637 int StrBufExtract_int(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  * extract_long()  -  extract an long parm w/o supplying a buffer
654  */
655 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
656 {
657         StrBuf tmp;
658         char buf[64];
659         
660         tmp.buf = buf;
661         buf[0] = '\0';
662         tmp.BufSize = 64;
663         tmp.BufUsed = 0;
664         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
665                 return(atoi(buf));
666         else
667                 return 0;
668 }
669
670
671 /*
672  * extract_unsigned_long() - extract an unsigned long parm
673  */
674 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
675 {
676         StrBuf tmp;
677         char buf[64];
678         
679         tmp.buf = buf;
680         buf[0] = '\0';
681         tmp.BufSize = 64;
682         tmp.BufUsed = 0;
683         if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
684                 return(atoi(buf));
685         else 
686                 return 0;
687 }
688
689
690
691 /**
692  * \brief Input binary data from socket
693  * \param buf the buffer to get the input to
694  * \param bytes the maximal number of bytes to read
695  */
696 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
697 {
698         int len, rlen, slen;
699
700         if (!append)
701                 FlushStrBuf(buf);
702
703         slen = len = buf->BufUsed;
704         while (1) {
705                 rlen = read(*fd, &buf->buf[len], 1);
706                 if (rlen < 1) {
707                         *Error = strerror(errno);
708                         
709                         close(*fd);
710                         *fd = -1;
711                         
712                         return -1;
713                 }
714                 if (buf->buf[len] == '\n')
715                         break;
716                 if (buf->buf[len] != '\r')
717                         len ++;
718                 if (!(len < buf->BufSize)) {
719                         buf->BufUsed = len;
720                         buf->buf[len+1] = '\0';
721                         IncreaseBuf(buf, 1, -1);
722                 }
723         }
724         buf->BufUsed = len;
725         buf->buf[len] = '\0';
726         return len - slen;
727 }
728
729 /**
730  * \brief Input binary data from socket
731  * \param buf the buffer to get the input to
732  * \param bytes the maximal number of bytes to read
733  */
734 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
735 {
736         fd_set wset;
737         int fdflags;
738         int len, rlen, slen;
739         int nRead = 0;
740         char *ptr;
741
742         if ((Buf == NULL) || (*fd == -1))
743                 return -1;
744         if (!append)
745                 FlushStrBuf(Buf);
746         if (Buf->BufUsed + nBytes > Buf->BufSize)
747                 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
748
749         ptr = Buf->buf + Buf->BufUsed;
750
751         slen = len = Buf->BufUsed;
752
753         fdflags = fcntl(*fd, F_GETFL);
754
755         while (nRead < nBytes) {
756                if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
757                         FD_ZERO(&wset);
758                         FD_SET(*fd, &wset);
759                         if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
760                                 *Error = strerror(errno);
761                                 return -1;
762                         }
763                 }
764
765                 if ((rlen = read(*fd, 
766                                  ptr,
767                                  nBytes - nRead)) == -1) {
768                         close(*fd);
769                         *fd = -1;
770                         *Error = strerror(errno);
771                         return rlen;
772                 }
773                 nRead += rlen;
774                 Buf->BufUsed += rlen;
775         }
776         Buf->buf[Buf->BufUsed] = '\0';
777         return nRead;
778 }
779
780 void StrBufCutLeft(StrBuf *Buf, int nChars)
781 {
782         if (nChars >= Buf->BufUsed) {
783                 FlushStrBuf(Buf);
784                 return;
785         }
786         memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
787         Buf->BufUsed -= nChars;
788         Buf->buf[Buf->BufUsed] = '\0';
789 }
790
791 void StrBufCutRight(StrBuf *Buf, int nChars)
792 {
793         if (nChars >= Buf->BufUsed) {
794                 FlushStrBuf(Buf);
795                 return;
796         }
797         Buf->BufUsed -= nChars;
798         Buf->buf[Buf->BufUsed] = '\0';
799 }
800
801
802 /*
803  * string conversion function
804  */
805 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source) 
806 {
807         int a, b, len;
808         char hex[3];
809
810         if (target != NULL)
811                 FlushStrBuf(target);
812
813         if (source == NULL ||target == NULL)
814         {
815                 return;
816         }
817
818         len = source->BufUsed;
819         for (a = 0; a < len; ++a) {
820                 if (target->BufUsed >= target->BufSize)
821                         IncreaseBuf(target, 1, -1);
822
823                 if (source->buf[a] == '=') {
824                         hex[0] = source->buf[a + 1];
825                         hex[1] = source->buf[a + 2];
826                         hex[2] = 0;
827                         b = 0;
828                         sscanf(hex, "%02x", &b);
829                         target->buf[target->BufUsed] = b;
830                         target->buf[++target->BufUsed] = 0;
831                         a += 2;
832                 }
833                 else {
834                         target->buf[target->BufUsed] = source->buf[a];
835                         target->buf[++target->BufUsed] = 0;
836                 }
837         }
838 }
839
840
841 /*
842  * string conversion function
843  */
844 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source) 
845 {
846         int i, len;
847
848         if (target != NULL)
849                 FlushStrBuf(target);
850
851         if (source == NULL ||target == NULL)
852         {
853                 return;
854         }
855
856         len = source->BufUsed;
857         for (i=0; i<len; ++i) {
858                 if (target->BufUsed + 4 >= target->BufSize)
859                         IncreaseBuf(target, 1, -1);
860                 if ( (isalnum(source->buf[i])) || 
861                      (source->buf[i]=='-') || 
862                      (source->buf[i]=='_') ) {
863                         target->buf[target->BufUsed++] = source->buf[i];
864                 }
865                 else {
866                         sprintf(&target->buf[target->BufUsed], 
867                                 "=%02X", 
868                                 (0xFF &source->buf[i]));
869                         target->BufUsed += 3;
870                 }
871         }
872         target->buf[target->BufUsed + 1] = '\0';
873 }
874
875 /*
876  * \brief uses the same calling syntax as compress2(), but it
877  * creates a stream compatible with HTTP "Content-encoding: gzip"
878  */
879 #ifdef HAVE_ZLIB
880 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
881 #define OS_CODE 0x03    /*< unix */
882 int ZEXPORT compress_gzip(Bytef * dest,         /*< compressed buffer*/
883                           size_t * destLen,     /*< length of the compresed data */
884                           const Bytef * source, /*< source to encode */
885                           uLong sourceLen,      /*< length of source to encode */
886                           int level)            /*< compression level */
887 {
888         const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
889
890         /* write gzip header */
891         snprintf((char *) dest, *destLen, 
892                  "%c%c%c%c%c%c%c%c%c%c",
893                  gz_magic[0], gz_magic[1], Z_DEFLATED,
894                  0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
895                  OS_CODE);
896
897         /* normal deflate */
898         z_stream stream;
899         int err;
900         stream.next_in = (Bytef *) source;
901         stream.avail_in = (uInt) sourceLen;
902         stream.next_out = dest + 10L;   // after header
903         stream.avail_out = (uInt) * destLen;
904         if ((uLong) stream.avail_out != *destLen)
905                 return Z_BUF_ERROR;
906
907         stream.zalloc = (alloc_func) 0;
908         stream.zfree = (free_func) 0;
909         stream.opaque = (voidpf) 0;
910
911         err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
912                            DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
913         if (err != Z_OK)
914                 return err;
915
916         err = deflate(&stream, Z_FINISH);
917         if (err != Z_STREAM_END) {
918                 deflateEnd(&stream);
919                 return err == Z_OK ? Z_BUF_ERROR : err;
920         }
921         *destLen = stream.total_out + 10L;
922
923         /* write CRC and Length */
924         uLong crc = crc32(0L, source, sourceLen);
925         int n;
926         for (n = 0; n < 4; ++n, ++*destLen) {
927                 dest[*destLen] = (int) (crc & 0xff);
928                 crc >>= 8;
929         }
930         uLong len = stream.total_in;
931         for (n = 0; n < 4; ++n, ++*destLen) {
932                 dest[*destLen] = (int) (len & 0xff);
933                 len >>= 8;
934         }
935         err = deflateEnd(&stream);
936         return err;
937 }
938 #endif
939
940
941 /**
942  * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
943  */
944 int CompressBuffer(StrBuf *Buf)
945 {
946 #ifdef HAVE_ZLIB
947         char *compressed_data = NULL;
948         size_t compressed_len, bufsize;
949         
950         bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
951         compressed_data = malloc(compressed_len);
952         
953         if (compress_gzip((Bytef *) compressed_data,
954                           &compressed_len,
955                           (Bytef *) Buf->buf,
956                           (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
957                 if (!ConstBuf)
958                         free(Buf->buf);
959                 Buf->buf = compressed_data;
960                 Buf->BufUsed = compressed_len;
961                 Buf->BufSize = bufsize;
962                 return 1;
963         } else {
964                 free(compressed_data);
965         }
966 #endif  /* HAVE_ZLIB */
967         return 0;
968 }
969
970 int StrBufDecodeBase64(StrBuf *Buf)
971 {
972         char *xferbuf;
973         size_t siz;
974         if (Buf == NULL) return -1;
975
976         xferbuf = (char*) malloc(Buf->BufSize);
977         siz = CtdlDecodeBase64(xferbuf,
978                                Buf->buf,
979                                Buf->BufUsed);
980         free(Buf->buf);
981         Buf->buf = xferbuf;
982         Buf->BufUsed = siz;
983         return siz;
984 }
985
986
987 /*   
988  * remove escaped strings from i.e. the url string (like %20 for blanks)
989  */
990 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
991 {
992         int a, b;
993         char hex[3];
994         long len;
995
996         while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
997                 Buf->buf[Buf->BufUsed - 1] = '\0';
998                 Buf->BufUsed --;
999         }
1000
1001         a = 0; 
1002         while (a < Buf->BufUsed) {
1003                 if (Buf->buf[a] == '+')
1004                         Buf->buf[a] = ' ';
1005                 else if (Buf->buf[a] == '%') {
1006                         /* don't let % chars through, rather truncate the input. */
1007                         if (a + 2 > Buf->BufUsed) {
1008                                 Buf->buf[a] = '\0';
1009                                 Buf->BufUsed = a;
1010                         }
1011                         else {                  
1012                                 hex[0] = Buf->buf[a + 1];
1013                                 hex[1] = Buf->buf[a + 2];
1014                                 hex[2] = 0;
1015                                 b = 0;
1016                                 sscanf(hex, "%02x", &b);
1017                                 Buf->buf[a] = (char) b;
1018                                 len = Buf->BufUsed - a - 2;
1019                                 if (len > 0)
1020                                         memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
1021                         
1022                                 Buf->BufUsed -=2;
1023                         }
1024                 }
1025                 a++;
1026         }
1027         return a;
1028 }
1029
1030
1031 /**
1032  * \brief       RFC2047-encode a header field if necessary.
1033  *              If no non-ASCII characters are found, the string
1034  *              will be copied verbatim without encoding.
1035  *
1036  * \param       target          Target buffer.
1037  * \param       source          Source string to be encoded.
1038  * \returns     encoded length; -1 if non success.
1039  */
1040 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1041 {
1042         const char headerStr[] = "=?UTF-8?Q?";
1043         int need_to_encode = 0;
1044         int i = 0;
1045         unsigned char ch;
1046
1047         if ((source == NULL) || 
1048             (target == NULL))
1049             return -1;
1050
1051         while ((i < source->BufUsed) &&
1052                (!IsEmptyStr (&source->buf[i])) &&
1053                (need_to_encode == 0)) {
1054                 if (((unsigned char) source->buf[i] < 32) || 
1055                     ((unsigned char) source->buf[i] > 126)) {
1056                         need_to_encode = 1;
1057                 }
1058                 i++;
1059         }
1060
1061         if (!need_to_encode) {
1062                 if (*target == NULL) {
1063                         *target = NewStrBufPlain(source->buf, source->BufUsed);
1064                 }
1065                 else {
1066                         FlushStrBuf(*target);
1067                         StrBufAppendBuf(*target, source, 0);
1068                 }
1069                 return (*target)->BufUsed;
1070         }
1071         if (*target == NULL)
1072                 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
1073         else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
1074                 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
1075         memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
1076         (*target)->BufUsed = sizeof(headerStr) - 1;
1077         for (i=0; (i < source->BufUsed); ++i) {
1078                 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1079                         IncreaseBuf(*target, 1, 0);
1080                 ch = (unsigned char) source->buf[i];
1081                 if ((ch < 32) || (ch > 126) || (ch == 61)) {
1082                         sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
1083                         (*target)->BufUsed += 3;
1084                 }
1085                 else {
1086                         (*target)->buf[(*target)->BufUsed] = ch;
1087                         (*target)->BufUsed++;
1088                 }
1089         }
1090         
1091         if ((*target)->BufUsed + 4 > (*target)->BufSize)
1092                 IncreaseBuf(*target, 1, 0);
1093
1094         (*target)->buf[(*target)->BufUsed++] = '?';
1095         (*target)->buf[(*target)->BufUsed++] = '=';
1096         (*target)->buf[(*target)->BufUsed] = '\0';
1097         return (*target)->BufUsed;;
1098 }
1099
1100 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1101 {
1102         long i;
1103         if (buf == NULL)
1104                 return;
1105         for (i=0; i<buf->BufUsed; i++)
1106                 if (buf->buf[i] == search)
1107                         buf->buf[i] = replace;
1108
1109 }