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);
778 Buf->BufUsed += rlen;
780 Buf->buf[Buf->BufUsed] = '\0';
784 void StrBufCutLeft(StrBuf *Buf, int nChars)
786 if (nChars >= Buf->BufUsed) {
790 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
791 Buf->BufUsed -= nChars;
792 Buf->buf[Buf->BufUsed] = '\0';
795 void StrBufCutRight(StrBuf *Buf, int nChars)
797 if (nChars >= Buf->BufUsed) {
801 Buf->BufUsed -= nChars;
802 Buf->buf[Buf->BufUsed] = '\0';
807 * string conversion function
809 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
817 if (source == NULL ||target == NULL)
822 len = source->BufUsed;
823 for (a = 0; a < len; ++a) {
824 if (target->BufUsed >= target->BufSize)
825 IncreaseBuf(target, 1, -1);
827 if (source->buf[a] == '=') {
828 hex[0] = source->buf[a + 1];
829 hex[1] = source->buf[a + 2];
832 sscanf(hex, "%02x", &b);
833 target->buf[target->BufUsed] = b;
834 target->buf[++target->BufUsed] = 0;
838 target->buf[target->BufUsed] = source->buf[a];
839 target->buf[++target->BufUsed] = 0;
846 * string conversion function
848 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
855 if (source == NULL ||target == NULL)
860 len = source->BufUsed;
861 for (i=0; i<len; ++i) {
862 if (target->BufUsed + 4 >= target->BufSize)
863 IncreaseBuf(target, 1, -1);
864 if ( (isalnum(source->buf[i])) ||
865 (source->buf[i]=='-') ||
866 (source->buf[i]=='_') ) {
867 target->buf[target->BufUsed++] = source->buf[i];
870 sprintf(&target->buf[target->BufUsed],
872 (0xFF &source->buf[i]));
873 target->BufUsed += 3;
876 target->buf[target->BufUsed + 1] = '\0';
880 * \brief uses the same calling syntax as compress2(), but it
881 * creates a stream compatible with HTTP "Content-encoding: gzip"
884 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
885 #define OS_CODE 0x03 /*< unix */
886 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
887 size_t * destLen, /*< length of the compresed data */
888 const Bytef * source, /*< source to encode */
889 uLong sourceLen, /*< length of source to encode */
890 int level) /*< compression level */
892 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
894 /* write gzip header */
895 snprintf((char *) dest, *destLen,
896 "%c%c%c%c%c%c%c%c%c%c",
897 gz_magic[0], gz_magic[1], Z_DEFLATED,
898 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
904 stream.next_in = (Bytef *) source;
905 stream.avail_in = (uInt) sourceLen;
906 stream.next_out = dest + 10L; // after header
907 stream.avail_out = (uInt) * destLen;
908 if ((uLong) stream.avail_out != *destLen)
911 stream.zalloc = (alloc_func) 0;
912 stream.zfree = (free_func) 0;
913 stream.opaque = (voidpf) 0;
915 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
916 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
920 err = deflate(&stream, Z_FINISH);
921 if (err != Z_STREAM_END) {
923 return err == Z_OK ? Z_BUF_ERROR : err;
925 *destLen = stream.total_out + 10L;
927 /* write CRC and Length */
928 uLong crc = crc32(0L, source, sourceLen);
930 for (n = 0; n < 4; ++n, ++*destLen) {
931 dest[*destLen] = (int) (crc & 0xff);
934 uLong len = stream.total_in;
935 for (n = 0; n < 4; ++n, ++*destLen) {
936 dest[*destLen] = (int) (len & 0xff);
939 err = deflateEnd(&stream);
946 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
948 int CompressBuffer(StrBuf *Buf)
951 char *compressed_data = NULL;
952 size_t compressed_len, bufsize;
954 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
955 compressed_data = malloc(compressed_len);
957 if (compress_gzip((Bytef *) compressed_data,
960 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
963 Buf->buf = compressed_data;
964 Buf->BufUsed = compressed_len;
965 Buf->BufSize = bufsize;
968 free(compressed_data);
970 #endif /* HAVE_ZLIB */
974 int StrBufDecodeBase64(StrBuf *Buf)
978 if (Buf == NULL) return -1;
980 xferbuf = (char*) malloc(Buf->BufSize);
981 siz = CtdlDecodeBase64(xferbuf,
992 * remove escaped strings from i.e. the url string (like %20 for blanks)
994 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
1000 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
1001 Buf->buf[Buf->BufUsed - 1] = '\0';
1006 while (a < Buf->BufUsed) {
1007 if (Buf->buf[a] == '+')
1009 else if (Buf->buf[a] == '%') {
1010 /* don't let % chars through, rather truncate the input. */
1011 if (a + 2 > Buf->BufUsed) {
1016 hex[0] = Buf->buf[a + 1];
1017 hex[1] = Buf->buf[a + 2];
1020 sscanf(hex, "%02x", &b);
1021 Buf->buf[a] = (char) b;
1022 len = Buf->BufUsed - a - 2;
1024 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
1036 * \brief RFC2047-encode a header field if necessary.
1037 * If no non-ASCII characters are found, the string
1038 * will be copied verbatim without encoding.
1040 * \param target Target buffer.
1041 * \param source Source string to be encoded.
1042 * \returns encoded length; -1 if non success.
1044 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1046 const char headerStr[] = "=?UTF-8?Q?";
1047 int need_to_encode = 0;
1051 if ((source == NULL) ||
1055 while ((i < source->BufUsed) &&
1056 (!IsEmptyStr (&source->buf[i])) &&
1057 (need_to_encode == 0)) {
1058 if (((unsigned char) source->buf[i] < 32) ||
1059 ((unsigned char) source->buf[i] > 126)) {
1065 if (!need_to_encode) {
1066 if (*target == NULL) {
1067 *target = NewStrBufPlain(source->buf, source->BufUsed);
1070 FlushStrBuf(*target);
1071 StrBufAppendBuf(*target, source, 0);
1073 return (*target)->BufUsed;
1075 if (*target == NULL)
1076 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
1077 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
1078 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
1079 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
1080 (*target)->BufUsed = sizeof(headerStr) - 1;
1081 for (i=0; (i < source->BufUsed); ++i) {
1082 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1083 IncreaseBuf(*target, 1, 0);
1084 ch = (unsigned char) source->buf[i];
1085 if ((ch < 32) || (ch > 126) || (ch == 61)) {
1086 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
1087 (*target)->BufUsed += 3;
1090 (*target)->buf[(*target)->BufUsed] = ch;
1091 (*target)->BufUsed++;
1095 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1096 IncreaseBuf(*target, 1, 0);
1098 (*target)->buf[(*target)->BufUsed++] = '?';
1099 (*target)->buf[(*target)->BufUsed++] = '=';
1100 (*target)->buf[(*target)->BufUsed] = '\0';
1101 return (*target)->BufUsed;;
1104 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1109 for (i=0; i<buf->BufUsed; i++)
1110 if (buf->buf[i] == search)
1111 buf->buf[i] = replace;