7 #include <sys/select.h>
9 #define SHOW_ME_VAPPEND_PRINTF
11 #include "libcitadel.h"
22 inline const char *ChrPtr(const StrBuf *Str)
29 inline int StrLength(const StrBuf *Str)
31 return (Str != NULL) ? Str->BufUsed : 0;
34 StrBuf* NewStrBuf(void)
38 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
39 NewBuf->buf = (char*) malloc(SIZ);
40 NewBuf->buf[0] = '\0';
41 NewBuf->BufSize = SIZ;
47 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
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;
63 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
69 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
71 CopySize = strlen((ptr != NULL)?ptr:"");
75 while (Siz <= CopySize)
78 NewBuf->buf = (char*) malloc(Siz);
79 NewBuf->BufSize = Siz;
81 memcpy(NewBuf->buf, ptr, CopySize);
82 NewBuf->buf[CopySize] = '\0';
83 NewBuf->BufUsed = CopySize;
86 NewBuf->buf[0] = '\0';
94 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
98 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
99 NewBuf->buf = (char*) StringConstant;
100 NewBuf->BufSize = SizeOfStrConstant;
101 NewBuf->BufUsed = SizeOfStrConstant;
102 NewBuf->ConstBuf = 1;
107 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
110 size_t NewSize = Buf->BufSize * 2;
116 while (NewSize < DestSize)
119 NewBuf= (char*) malloc(NewSize);
120 if (KeepOriginal && (Buf->BufUsed > 0))
122 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
135 int FlushStrBuf(StrBuf *buf)
144 void FreeStrBuf (StrBuf **FreeMe)
148 if (!(*FreeMe)->ConstBuf)
149 free((*FreeMe)->buf);
154 void HFreeStrBuf (void *VFreeMe)
156 StrBuf *FreeMe = (StrBuf*)VFreeMe;
159 if (!FreeMe->ConstBuf)
164 long StrTol(const StrBuf *Buf)
167 return atol(Buf->buf);
172 int StrToi(const StrBuf *Buf)
175 return atoi(Buf->buf);
180 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
185 nThChar = ptr - Buf->buf;
186 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
188 Buf->buf[nThChar] = PeekValue;
193 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
195 size_t Siz = Buf->BufSize;
199 CopySize = strlen(ptr);
203 while (Siz <= CopySize)
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;
215 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, size_t Offset)
217 if ((AppendBuf == NULL) || (Buf == NULL))
220 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed)
223 AppendBuf->BufUsed + Buf->BufUsed);
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';
233 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, size_t Offset)
237 if ((AppendBuf == NULL) || (Buf == NULL))
241 aps = strlen(AppendBuf + Offset);
243 aps = AppendSize - Offset;
245 if (Buf->BufSize < Buf->BufUsed + aps)
246 IncreaseBuf(Buf, (Buf->BufUsed > 0), Buf->BufUsed + aps);
248 memcpy(Buf->buf + Buf->BufUsed,
252 Buf->buf[Buf->BufUsed] = '\0';
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
262 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
264 const char *pch, *pche;
267 const char ec[] = " +#&;`'|*?-~<>^()[]{}/$\"\\";
268 int eclen = sizeof(ec) -1;
270 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
272 if (PlainIn != NULL) {
273 len = strlen(PlainIn);
279 pche = pch + In->BufUsed;
286 pt = OutBuf->buf + OutBuf->BufUsed;
287 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
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;
297 for (b = 0; b < eclen; ++b) {
304 sprintf(pt,"%%%02X", *pch);
306 OutBuf->BufUsed += 3;
318 * Copy a string, escaping characters which have meaning in HTML.
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.
325 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
327 const char *aptr, *eiptr;
331 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
334 if (PlainIn != NULL) {
336 len = strlen(PlainIn);
341 eiptr = aptr + Source->BufUsed;
342 len = Source->BufUsed;
348 bptr = Target->buf + Target->BufUsed;
349 eptr = Target->buf + Target->BufSize - 6; /* our biggest unit to put in... */
351 while (aptr < eiptr){
353 IncreaseBuf(Target, 1, -1);
354 eptr = Target->buf + Target->BufSize - 6;
355 bptr = Target->buf + Target->BufUsed;
358 memcpy(bptr, "<", 4);
360 Target->BufUsed += 4;
362 else if (*aptr == '>') {
363 memcpy(bptr, ">", 4);
365 Target->BufUsed += 4;
367 else if (*aptr == '&') {
368 memcpy(bptr, "&", 5);
370 Target->BufUsed += 5;
372 else if (*aptr == '\"') {
373 memcpy(bptr, """, 6);
375 Target->BufUsed += 6;
377 else if (*aptr == '\'') {
378 memcpy(bptr, "'", 5);
380 Target->BufUsed += 5;
382 else if (*aptr == LB) {
387 else if (*aptr == RB) {
392 else if (*aptr == QU) {
397 else if ((*aptr == 32) && (nbsp == 1)) {
398 memcpy(bptr, " ", 6);
400 Target->BufUsed += 6;
402 else if ((*aptr == '\n') && (nolinebreaks)) {
403 *bptr='\0'; /* nothing */
405 else if ((*aptr == '\r') && (nolinebreaks)) {
406 *bptr='\0'; /* nothing */
416 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
418 return Target->BufUsed;
421 void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn)
423 const char *aptr, *eiptr;
427 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
430 if (PlainIn != NULL) {
432 len = strlen(PlainIn);
437 eiptr = aptr + Source->BufUsed;
438 len = Source->BufUsed;
444 eptr = Target->buf + Target->BufSize - 6;
445 tptr = Target->buf + Target->BufUsed;
447 while (aptr < eiptr){
449 IncreaseBuf(Target, 1, -1);
450 eptr = Target->buf + Target->BufSize - 6;
451 tptr = Target->buf + Target->BufUsed;
458 else if (*aptr == '\r') {
462 else if (*aptr == '\'') {
468 Target->BufUsed += 5;
479 inline int StrBufNum_tokens(const StrBuf *source, char tok)
481 return num_tokens(source->buf, tok);
485 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
488 if (Offset > Source->BufUsed)
493 if (Offset + nChars < Source->BufUsed)
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';
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';
512 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
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;
519 while (newused >= BufSize) {
520 nWritten = vsnprintf(Buf->buf + Offset,
521 Buf->BufSize - Offset,
523 newused = Offset + nWritten;
524 if (newused >= Buf->BufSize) {
525 IncreaseBuf(Buf, 1, newused);
528 Buf->BufUsed = Offset + nWritten;
529 BufSize = Buf->BufSize;
535 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
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;
543 while (newused >= BufSize) {
544 va_start(arg_ptr, format);
545 nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
546 Buf->BufSize - Buf->BufUsed,
549 newused = Buf->BufUsed + nWritten;
550 if (newused >= Buf->BufSize) {
551 IncreaseBuf(Buf, 1, newused);
554 Buf->BufUsed += nWritten;
555 BufSize = Buf->BufSize;
561 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
563 size_t nWritten = Buf->BufSize + 1;
566 while (nWritten >= Buf->BufSize) {
567 va_start(arg_ptr, format);
568 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
570 Buf->BufUsed = nWritten ;
571 if (nWritten >= Buf->BufSize)
572 IncreaseBuf(Buf, 0, 0);
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.
584 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
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 * /
590 if ((Source == NULL) || (Source->BufUsed ==0)) {
594 e = s + Source->BufUsed;
600 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
604 while ((s<e) && !IsEmptyStr(s)) {
605 if (*s == separator) {
608 if (len >= dest->BufSize)
609 if (!IncreaseBuf(dest, 1, -1))
611 if ( (current_token == parmnum) &&
616 else if (current_token > parmnum) {
622 dest->buf[len] = '\0';
625 if (current_token < parmnum) {
626 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
629 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
635 * extract_int() - extract an int parm w/o supplying a buffer
637 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
646 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
653 * extract_long() - extract an long parm w/o supplying a buffer
655 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
664 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
672 * extract_unsigned_long() - extract an unsigned long parm
674 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
683 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
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
696 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
703 slen = len = buf->BufUsed;
705 rlen = read(*fd, &buf->buf[len], 1);
707 *Error = strerror(errno);
714 if (buf->buf[len] == '\n')
716 if (buf->buf[len] != '\r')
718 if (!(len < buf->BufSize)) {
720 buf->buf[len+1] = '\0';
721 IncreaseBuf(buf, 1, -1);
725 buf->buf[len] = '\0';
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
734 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
742 if ((Buf == NULL) || (*fd == -1))
746 if (Buf->BufUsed + nBytes > Buf->BufSize)
747 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
749 ptr = Buf->buf + Buf->BufUsed;
751 slen = len = Buf->BufUsed;
753 fdflags = fcntl(*fd, F_GETFL);
755 while (nRead < nBytes) {
756 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
759 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
760 *Error = strerror(errno);
765 if ((rlen = read(*fd,
767 nBytes - nRead)) == -1) {
770 *Error = strerror(errno);
774 Buf->BufUsed += rlen;
776 Buf->buf[Buf->BufUsed] = '\0';
780 void StrBufCutLeft(StrBuf *Buf, int nChars)
782 if (nChars >= Buf->BufUsed) {
786 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
787 Buf->BufUsed -= nChars;
788 Buf->buf[Buf->BufUsed] = '\0';
791 void StrBufCutRight(StrBuf *Buf, int nChars)
793 if (nChars >= Buf->BufUsed) {
797 Buf->BufUsed -= nChars;
798 Buf->buf[Buf->BufUsed] = '\0';
803 * string conversion function
805 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
813 if (source == NULL ||target == NULL)
818 len = source->BufUsed;
819 for (a = 0; a < len; ++a) {
820 if (target->BufUsed >= target->BufSize)
821 IncreaseBuf(target, 1, -1);
823 if (source->buf[a] == '=') {
824 hex[0] = source->buf[a + 1];
825 hex[1] = source->buf[a + 2];
828 sscanf(hex, "%02x", &b);
829 target->buf[target->BufUsed] = b;
830 target->buf[++target->BufUsed] = 0;
834 target->buf[target->BufUsed] = source->buf[a];
835 target->buf[++target->BufUsed] = 0;
842 * string conversion function
844 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
851 if (source == NULL ||target == NULL)
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];
866 sprintf(&target->buf[target->BufUsed],
868 (0xFF &source->buf[i]));
869 target->BufUsed += 3;
872 target->buf[target->BufUsed + 1] = '\0';
876 * \brief uses the same calling syntax as compress2(), but it
877 * creates a stream compatible with HTTP "Content-encoding: gzip"
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 */
888 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
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 */ ,
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)
907 stream.zalloc = (alloc_func) 0;
908 stream.zfree = (free_func) 0;
909 stream.opaque = (voidpf) 0;
911 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
912 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
916 err = deflate(&stream, Z_FINISH);
917 if (err != Z_STREAM_END) {
919 return err == Z_OK ? Z_BUF_ERROR : err;
921 *destLen = stream.total_out + 10L;
923 /* write CRC and Length */
924 uLong crc = crc32(0L, source, sourceLen);
926 for (n = 0; n < 4; ++n, ++*destLen) {
927 dest[*destLen] = (int) (crc & 0xff);
930 uLong len = stream.total_in;
931 for (n = 0; n < 4; ++n, ++*destLen) {
932 dest[*destLen] = (int) (len & 0xff);
935 err = deflateEnd(&stream);
942 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
944 int CompressBuffer(StrBuf *Buf)
947 char *compressed_data = NULL;
948 size_t compressed_len, bufsize;
950 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
951 compressed_data = malloc(compressed_len);
953 if (compress_gzip((Bytef *) compressed_data,
956 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
959 Buf->buf = compressed_data;
960 Buf->BufUsed = compressed_len;
961 Buf->BufSize = bufsize;
964 free(compressed_data);
966 #endif /* HAVE_ZLIB */
970 int StrBufDecodeBase64(StrBuf *Buf)
974 if (Buf == NULL) return -1;
976 xferbuf = (char*) malloc(Buf->BufSize);
977 siz = CtdlDecodeBase64(xferbuf,
988 * remove escaped strings from i.e. the url string (like %20 for blanks)
990 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
996 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
997 Buf->buf[Buf->BufUsed - 1] = '\0';
1002 while (a < Buf->BufUsed) {
1003 if (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) {
1012 hex[0] = Buf->buf[a + 1];
1013 hex[1] = Buf->buf[a + 2];
1016 sscanf(hex, "%02x", &b);
1017 Buf->buf[a] = (char) b;
1018 len = Buf->BufUsed - a - 2;
1020 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
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.
1036 * \param target Target buffer.
1037 * \param source Source string to be encoded.
1038 * \returns encoded length; -1 if non success.
1040 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1042 const char headerStr[] = "=?UTF-8?Q?";
1043 int need_to_encode = 0;
1047 if ((source == NULL) ||
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)) {
1061 if (!need_to_encode) {
1062 if (*target == NULL) {
1063 *target = NewStrBufPlain(source->buf, source->BufUsed);
1066 FlushStrBuf(*target);
1067 StrBufAppendBuf(*target, source, 0);
1069 return (*target)->BufUsed;
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;
1086 (*target)->buf[(*target)->BufUsed] = ch;
1087 (*target)->BufUsed++;
1091 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1092 IncreaseBuf(*target, 1, 0);
1094 (*target)->buf[(*target)->BufUsed++] = '?';
1095 (*target)->buf[(*target)->BufUsed++] = '=';
1096 (*target)->buf[(*target)->BufUsed] = '\0';
1097 return (*target)->BufUsed;;
1100 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1105 for (i=0; i<buf->BufUsed; i++)
1106 if (buf->buf[i] == search)
1107 buf->buf[i] = replace;