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);
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 eiptr = Target->buf + Target->BufSize - 6;
445 tptr = Target->buf + Target->BufUsed;
447 while (aptr < eiptr){
449 IncreaseBuf(Target, 1, -1);
450 eiptr = 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 nWritten = Buf->BufSize + 1;
515 size_t Offset = Buf->BufUsed;
516 size_t newused = Offset + nWritten;
518 while (newused >= Buf->BufSize) {
519 nWritten = vsnprintf(Buf->buf + Offset,
520 Buf->BufSize - Offset,
522 newused = Offset + nWritten;
523 if (newused >= Buf->BufSize)
524 IncreaseBuf(Buf, 1, 0);
526 Buf->BufUsed = Offset + nWritten ;
531 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
533 size_t nWritten = Buf->BufSize + 1;
534 size_t Offset = Buf->BufUsed;
535 size_t newused = Offset + nWritten;
538 while (newused >= Buf->BufSize) {
539 va_start(arg_ptr, format);
540 nWritten = vsnprintf(Buf->buf + Offset,
541 Buf->BufSize - Offset,
544 newused = Offset + nWritten;
545 if (newused >= Buf->BufSize)
546 IncreaseBuf(Buf, 1, 0);
548 Buf->BufUsed = Offset + nWritten ;
553 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
555 size_t nWritten = Buf->BufSize + 1;
558 while (nWritten >= Buf->BufSize) {
559 va_start(arg_ptr, format);
560 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
562 Buf->BufUsed = nWritten ;
563 if (nWritten >= Buf->BufSize)
564 IncreaseBuf(Buf, 0, 0);
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.
576 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
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 * /
582 if ((Source == NULL) || (Source->BufUsed ==0)) {
586 e = s + Source->BufUsed;
592 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
596 while ((s<e) && !IsEmptyStr(s)) {
597 if (*s == separator) {
600 if (len >= dest->BufSize)
601 if (!IncreaseBuf(dest, 1, -1))
603 if ( (current_token == parmnum) &&
608 else if (current_token > parmnum) {
614 dest->buf[len] = '\0';
617 if (current_token < parmnum) {
618 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
621 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
627 * extract_int() - extract an int parm w/o supplying a buffer
629 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
638 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
645 * extract_long() - extract an long parm w/o supplying a buffer
647 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
656 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
664 * extract_unsigned_long() - extract an unsigned long parm
666 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
675 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
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
688 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
695 slen = len = buf->BufUsed;
697 rlen = read(*fd, &buf->buf[len], 1);
699 *Error = strerror(errno);
706 if (buf->buf[len] == '\n')
708 if (buf->buf[len] != '\r')
710 if (!(len < buf->BufSize)) {
712 buf->buf[len+1] = '\0';
713 IncreaseBuf(buf, 1, -1);
717 buf->buf[len] = '\0';
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
726 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
734 if ((Buf == NULL) || (*fd == -1))
738 if (Buf->BufUsed + nBytes > Buf->BufSize)
739 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
741 ptr = Buf->buf + Buf->BufUsed;
743 slen = len = Buf->BufUsed;
745 fdflags = fcntl(*fd, F_GETFL);
747 while (nRead < nBytes) {
748 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
751 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
752 *Error = strerror(errno);
757 if ((rlen = read(*fd,
759 nBytes - nRead)) == -1) {
762 *Error = strerror(errno);
766 Buf->BufUsed += rlen;
768 Buf->buf[Buf->BufUsed] = '\0';
772 void StrBufCutLeft(StrBuf *Buf, int nChars)
774 if (nChars >= Buf->BufUsed) {
778 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
779 Buf->BufUsed -= nChars;
780 Buf->buf[Buf->BufUsed] = '\0';
783 void StrBufCutRight(StrBuf *Buf, int nChars)
785 if (nChars >= Buf->BufUsed) {
789 Buf->BufUsed -= nChars;
790 Buf->buf[Buf->BufUsed] = '\0';
795 * string conversion function
797 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
805 if (source == NULL ||target == NULL)
810 len = source->BufUsed;
811 for (a = 0; a < len; ++a) {
812 if (target->BufUsed >= target->BufSize)
813 IncreaseBuf(target, 1, -1);
815 if (source->buf[a] == '=') {
816 hex[0] = source->buf[a + 1];
817 hex[1] = source->buf[a + 2];
820 sscanf(hex, "%02x", &b);
821 target->buf[target->BufUsed] = b;
822 target->buf[++target->BufUsed] = 0;
826 target->buf[target->BufUsed] = source->buf[a];
827 target->buf[++target->BufUsed] = 0;
834 * string conversion function
836 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
843 if (source == NULL ||target == NULL)
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];
858 sprintf(&target->buf[target->BufUsed],
860 (0xFF &source->buf[i]));
861 target->BufUsed += 3;
864 target->buf[target->BufUsed + 1] = '\0';
868 * \brief uses the same calling syntax as compress2(), but it
869 * creates a stream compatible with HTTP "Content-encoding: gzip"
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 */
880 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
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 */ ,
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)
899 stream.zalloc = (alloc_func) 0;
900 stream.zfree = (free_func) 0;
901 stream.opaque = (voidpf) 0;
903 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
904 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
908 err = deflate(&stream, Z_FINISH);
909 if (err != Z_STREAM_END) {
911 return err == Z_OK ? Z_BUF_ERROR : err;
913 *destLen = stream.total_out + 10L;
915 /* write CRC and Length */
916 uLong crc = crc32(0L, source, sourceLen);
918 for (n = 0; n < 4; ++n, ++*destLen) {
919 dest[*destLen] = (int) (crc & 0xff);
922 uLong len = stream.total_in;
923 for (n = 0; n < 4; ++n, ++*destLen) {
924 dest[*destLen] = (int) (len & 0xff);
927 err = deflateEnd(&stream);
934 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
936 int CompressBuffer(StrBuf *Buf)
939 char *compressed_data = NULL;
940 size_t compressed_len, bufsize;
942 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
943 compressed_data = malloc(compressed_len);
945 if (compress_gzip((Bytef *) compressed_data,
948 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
951 Buf->buf = compressed_data;
952 Buf->BufUsed = compressed_len;
953 Buf->BufSize = bufsize;
956 free(compressed_data);
958 #endif /* HAVE_ZLIB */
962 int StrBufDecodeBase64(StrBuf *Buf)
966 if (Buf == NULL) return -1;
968 xferbuf = (char*) malloc(Buf->BufSize);
969 siz = CtdlDecodeBase64(xferbuf,
980 * remove escaped strings from i.e. the url string (like %20 for blanks)
982 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
988 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
989 Buf->buf[Buf->BufUsed - 1] = '\0';
994 while (a < Buf->BufUsed) {
995 if (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) {
1004 hex[0] = Buf->buf[a + 1];
1005 hex[1] = Buf->buf[a + 2];
1008 sscanf(hex, "%02x", &b);
1009 Buf->buf[a] = (char) b;
1010 len = Buf->BufUsed - a - 2;
1012 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
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.
1028 * \param target Target buffer.
1029 * \param source Source string to be encoded.
1030 * \returns encoded length; -1 if non success.
1032 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1034 const char headerStr[] = "=?UTF-8?Q?";
1035 int need_to_encode = 0;
1039 if ((source == NULL) ||
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)) {
1053 if (!need_to_encode) {
1054 if (*target == NULL) {
1055 *target = NewStrBufPlain(source->buf, source->BufUsed);
1058 FlushStrBuf(*target);
1059 StrBufAppendBuf(*target, source, 0);
1061 return (*target)->BufUsed;
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;
1078 (*target)->buf[(*target)->BufUsed] = ch;
1079 (*target)->BufUsed++;
1083 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1084 IncreaseBuf(*target, 1, 0);
1086 (*target)->buf[(*target)->BufUsed++] = '?';
1087 (*target)->buf[(*target)->BufUsed++] = '=';
1088 (*target)->buf[(*target)->BufUsed] = '\0';
1089 return (*target)->BufUsed;;
1092 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1097 for (i=0; i<buf->BufUsed; i++)
1098 if (buf->buf[i] == search)
1099 buf->buf[i] = replace;