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)
515 size_t BufSize = Buf->BufSize;
516 size_t nWritten = Buf->BufSize + 1;
517 size_t Offset = Buf->BufUsed;
518 size_t newused = Offset + nWritten;
520 while (newused >= BufSize) {
522 nWritten = vsnprintf(Buf->buf + Offset,
523 Buf->BufSize - Offset,
526 newused = Offset + nWritten;
527 if (newused >= Buf->BufSize) {
528 IncreaseBuf(Buf, 1, newused);
531 Buf->BufUsed = Offset + nWritten;
532 BufSize = Buf->BufSize;
538 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
540 size_t BufSize = Buf->BufSize;
541 size_t nWritten = Buf->BufSize + 1;
542 size_t Offset = Buf->BufUsed;
543 size_t newused = Offset + nWritten;
546 while (newused >= BufSize) {
547 va_start(arg_ptr, format);
548 nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
549 Buf->BufSize - Buf->BufUsed,
552 newused = Buf->BufUsed + nWritten;
553 if (newused >= Buf->BufSize) {
554 IncreaseBuf(Buf, 1, newused);
557 Buf->BufUsed += nWritten;
558 BufSize = Buf->BufSize;
564 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
566 size_t nWritten = Buf->BufSize + 1;
569 while (nWritten >= Buf->BufSize) {
570 va_start(arg_ptr, format);
571 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
573 Buf->BufUsed = nWritten ;
574 if (nWritten >= Buf->BufSize)
575 IncreaseBuf(Buf, 0, 0);
581 * \brief a string tokenizer
582 * \param dest Destination StringBuffer
583 * \param Source StringBuffer to read into
584 * \param separator tokenizer param
585 * \returns -1 if not found, else length of token.
587 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
589 const char *s, *e; //* source * /
590 int len = 0; //* running total length of extracted string * /
591 int current_token = 0; //* token currently being processed * /
593 if ((Source == NULL) || (Source->BufUsed ==0)) {
597 e = s + Source->BufUsed;
603 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
607 while ((s<e) && !IsEmptyStr(s)) {
608 if (*s == separator) {
611 if (len >= dest->BufSize)
612 if (!IncreaseBuf(dest, 1, -1))
614 if ( (current_token == parmnum) &&
619 else if (current_token > parmnum) {
625 dest->buf[len] = '\0';
628 if (current_token < parmnum) {
629 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
632 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
638 * extract_int() - extract an int parm w/o supplying a buffer
640 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
649 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
656 * extract_long() - extract an long parm w/o supplying a buffer
658 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
667 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
675 * extract_unsigned_long() - extract an unsigned long parm
677 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
686 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
695 * \brief Input binary data from socket
696 * \param buf the buffer to get the input to
697 * \param bytes the maximal number of bytes to read
699 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
706 slen = len = buf->BufUsed;
708 rlen = read(*fd, &buf->buf[len], 1);
710 *Error = strerror(errno);
717 if (buf->buf[len] == '\n')
719 if (buf->buf[len] != '\r')
721 if (!(len < buf->BufSize)) {
723 buf->buf[len+1] = '\0';
724 IncreaseBuf(buf, 1, -1);
728 buf->buf[len] = '\0';
733 * \brief Input binary data from socket
734 * \param buf the buffer to get the input to
735 * \param bytes the maximal number of bytes to read
737 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
745 if ((Buf == NULL) || (*fd == -1))
749 if (Buf->BufUsed + nBytes > Buf->BufSize)
750 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
752 ptr = Buf->buf + Buf->BufUsed;
754 slen = len = Buf->BufUsed;
756 fdflags = fcntl(*fd, F_GETFL);
758 while (nRead < nBytes) {
759 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
762 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
763 *Error = strerror(errno);
768 if ((rlen = read(*fd,
770 nBytes - nRead)) == -1) {
773 *Error = strerror(errno);
777 Buf->BufUsed += rlen;
779 Buf->buf[Buf->BufUsed] = '\0';
783 void StrBufCutLeft(StrBuf *Buf, int nChars)
785 if (nChars >= Buf->BufUsed) {
789 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
790 Buf->BufUsed -= nChars;
791 Buf->buf[Buf->BufUsed] = '\0';
794 void StrBufCutRight(StrBuf *Buf, int nChars)
796 if (nChars >= Buf->BufUsed) {
800 Buf->BufUsed -= nChars;
801 Buf->buf[Buf->BufUsed] = '\0';
806 * string conversion function
808 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
816 if (source == NULL ||target == NULL)
821 len = source->BufUsed;
822 for (a = 0; a < len; ++a) {
823 if (target->BufUsed >= target->BufSize)
824 IncreaseBuf(target, 1, -1);
826 if (source->buf[a] == '=') {
827 hex[0] = source->buf[a + 1];
828 hex[1] = source->buf[a + 2];
831 sscanf(hex, "%02x", &b);
832 target->buf[target->BufUsed] = b;
833 target->buf[++target->BufUsed] = 0;
837 target->buf[target->BufUsed] = source->buf[a];
838 target->buf[++target->BufUsed] = 0;
845 * string conversion function
847 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
854 if (source == NULL ||target == NULL)
859 len = source->BufUsed;
860 for (i=0; i<len; ++i) {
861 if (target->BufUsed + 4 >= target->BufSize)
862 IncreaseBuf(target, 1, -1);
863 if ( (isalnum(source->buf[i])) ||
864 (source->buf[i]=='-') ||
865 (source->buf[i]=='_') ) {
866 target->buf[target->BufUsed++] = source->buf[i];
869 sprintf(&target->buf[target->BufUsed],
871 (0xFF &source->buf[i]));
872 target->BufUsed += 3;
875 target->buf[target->BufUsed + 1] = '\0';
879 * \brief uses the same calling syntax as compress2(), but it
880 * creates a stream compatible with HTTP "Content-encoding: gzip"
883 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
884 #define OS_CODE 0x03 /*< unix */
885 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
886 size_t * destLen, /*< length of the compresed data */
887 const Bytef * source, /*< source to encode */
888 uLong sourceLen, /*< length of source to encode */
889 int level) /*< compression level */
891 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
893 /* write gzip header */
894 snprintf((char *) dest, *destLen,
895 "%c%c%c%c%c%c%c%c%c%c",
896 gz_magic[0], gz_magic[1], Z_DEFLATED,
897 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
903 stream.next_in = (Bytef *) source;
904 stream.avail_in = (uInt) sourceLen;
905 stream.next_out = dest + 10L; // after header
906 stream.avail_out = (uInt) * destLen;
907 if ((uLong) stream.avail_out != *destLen)
910 stream.zalloc = (alloc_func) 0;
911 stream.zfree = (free_func) 0;
912 stream.opaque = (voidpf) 0;
914 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
915 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
919 err = deflate(&stream, Z_FINISH);
920 if (err != Z_STREAM_END) {
922 return err == Z_OK ? Z_BUF_ERROR : err;
924 *destLen = stream.total_out + 10L;
926 /* write CRC and Length */
927 uLong crc = crc32(0L, source, sourceLen);
929 for (n = 0; n < 4; ++n, ++*destLen) {
930 dest[*destLen] = (int) (crc & 0xff);
933 uLong len = stream.total_in;
934 for (n = 0; n < 4; ++n, ++*destLen) {
935 dest[*destLen] = (int) (len & 0xff);
938 err = deflateEnd(&stream);
945 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
947 int CompressBuffer(StrBuf *Buf)
950 char *compressed_data = NULL;
951 size_t compressed_len, bufsize;
953 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
954 compressed_data = malloc(compressed_len);
956 if (compress_gzip((Bytef *) compressed_data,
959 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
962 Buf->buf = compressed_data;
963 Buf->BufUsed = compressed_len;
964 Buf->BufSize = bufsize;
967 free(compressed_data);
969 #endif /* HAVE_ZLIB */
973 int StrBufDecodeBase64(StrBuf *Buf)
977 if (Buf == NULL) return -1;
979 xferbuf = (char*) malloc(Buf->BufSize);
980 siz = CtdlDecodeBase64(xferbuf,
991 * remove escaped strings from i.e. the url string (like %20 for blanks)
993 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
999 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
1000 Buf->buf[Buf->BufUsed - 1] = '\0';
1005 while (a < Buf->BufUsed) {
1006 if (Buf->buf[a] == '+')
1008 else if (Buf->buf[a] == '%') {
1009 /* don't let % chars through, rather truncate the input. */
1010 if (a + 2 > Buf->BufUsed) {
1015 hex[0] = Buf->buf[a + 1];
1016 hex[1] = Buf->buf[a + 2];
1019 sscanf(hex, "%02x", &b);
1020 Buf->buf[a] = (char) b;
1021 len = Buf->BufUsed - a - 2;
1023 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
1035 * \brief RFC2047-encode a header field if necessary.
1036 * If no non-ASCII characters are found, the string
1037 * will be copied verbatim without encoding.
1039 * \param target Target buffer.
1040 * \param source Source string to be encoded.
1041 * \returns encoded length; -1 if non success.
1043 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1045 const char headerStr[] = "=?UTF-8?Q?";
1046 int need_to_encode = 0;
1050 if ((source == NULL) ||
1054 while ((i < source->BufUsed) &&
1055 (!IsEmptyStr (&source->buf[i])) &&
1056 (need_to_encode == 0)) {
1057 if (((unsigned char) source->buf[i] < 32) ||
1058 ((unsigned char) source->buf[i] > 126)) {
1064 if (!need_to_encode) {
1065 if (*target == NULL) {
1066 *target = NewStrBufPlain(source->buf, source->BufUsed);
1069 FlushStrBuf(*target);
1070 StrBufAppendBuf(*target, source, 0);
1072 return (*target)->BufUsed;
1074 if (*target == NULL)
1075 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
1076 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
1077 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
1078 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
1079 (*target)->BufUsed = sizeof(headerStr) - 1;
1080 for (i=0; (i < source->BufUsed); ++i) {
1081 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1082 IncreaseBuf(*target, 1, 0);
1083 ch = (unsigned char) source->buf[i];
1084 if ((ch < 32) || (ch > 126) || (ch == 61)) {
1085 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
1086 (*target)->BufUsed += 3;
1089 (*target)->buf[(*target)->BufUsed] = ch;
1090 (*target)->BufUsed++;
1094 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1095 IncreaseBuf(*target, 1, 0);
1097 (*target)->buf[(*target)->BufUsed++] = '?';
1098 (*target)->buf[(*target)->BufUsed++] = '=';
1099 (*target)->buf[(*target)->BufUsed] = '\0';
1100 return (*target)->BufUsed;;
1103 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1108 for (i=0; i<buf->BufUsed; i++)
1109 if (buf->buf[i] == search)
1110 buf->buf[i] = replace;