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;
283 pt = OutBuf->buf + OutBuf->BufUsed;
284 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
288 IncreaseBuf(OutBuf, 1, -1);
289 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
290 pt = OutBuf->buf + OutBuf->BufUsed;
294 for (b = 0; b < eclen; ++b) {
301 sprintf(pt,"%%%02X", *pch);
303 OutBuf->BufUsed += 3;
315 * Copy a string, escaping characters which have meaning in HTML.
317 * target target buffer
318 * strbuf source buffer
319 * nbsp If nonzero, spaces are converted to non-breaking spaces.
320 * nolinebreaks if set, linebreaks are removed from the string.
322 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
324 const char *aptr, *eiptr;
328 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
331 if (PlainIn != NULL) {
333 len = strlen(PlainIn);
338 eiptr = aptr + Source->BufUsed;
339 len = Source->BufUsed;
342 bptr = Target->buf + Target->BufUsed;
343 eptr = Target->buf + Target->BufSize - 6; /* our biggest unit to put in... */
345 while (aptr < eiptr){
347 IncreaseBuf(Target, 1, -1);
348 eptr = Target->buf + Target->BufSize - 6;
349 bptr = Target->buf + Target->BufUsed;
352 memcpy(bptr, "<", 4);
354 Target->BufUsed += 4;
356 else if (*aptr == '>') {
357 memcpy(bptr, ">", 4);
359 Target->BufUsed += 4;
361 else if (*aptr == '&') {
362 memcpy(bptr, "&", 5);
364 Target->BufUsed += 5;
366 else if (*aptr == '\"') {
367 memcpy(bptr, """, 6);
369 Target->BufUsed += 6;
371 else if (*aptr == '\'') {
372 memcpy(bptr, "'", 5);
374 Target->BufUsed += 5;
376 else if (*aptr == LB) {
381 else if (*aptr == RB) {
386 else if (*aptr == QU) {
391 else if ((*aptr == 32) && (nbsp == 1)) {
392 memcpy(bptr, " ", 6);
394 Target->BufUsed += 6;
396 else if ((*aptr == '\n') && (nolinebreaks)) {
397 *bptr='\0'; /* nothing */
399 else if ((*aptr == '\r') && (nolinebreaks)) {
400 *bptr='\0'; /* nothing */
410 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
412 return Target->BufUsed;
415 void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn)
417 const char *aptr, *eiptr;
421 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
424 if (PlainIn != NULL) {
426 len = strlen(PlainIn);
431 eiptr = aptr + Source->BufUsed;
432 len = Source->BufUsed;
434 eiptr = Target->buf + Target->BufSize - 6;
435 tptr = Target->buf + Target->BufUsed;
437 while (aptr < eiptr){
439 IncreaseBuf(Target, 1, -1);
440 eiptr = Target->buf + Target->BufSize - 6;
441 tptr = Target->buf + Target->BufUsed;
448 else if (*aptr == '\r') {
452 else if (*aptr == '\'') {
458 Target->BufUsed += 5;
469 inline int StrBufNum_tokens(const StrBuf *source, char tok)
471 return num_tokens(source->buf, tok);
475 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
478 if (Offset > Source->BufUsed)
483 if (Offset + nChars < Source->BufUsed)
485 if (nChars > dest->BufSize)
486 IncreaseBuf(dest, 0, nChars + 1);
487 memcpy(dest->buf, Source->buf + Offset, nChars);
488 dest->BufUsed = nChars;
489 dest->buf[dest->BufUsed] = '\0';
492 NCharsRemain = Source->BufUsed - Offset;
493 if (NCharsRemain > dest->BufSize)
494 IncreaseBuf(dest, 0, NCharsRemain + 1);
495 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
496 dest->BufUsed = NCharsRemain;
497 dest->buf[dest->BufUsed] = '\0';
502 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
504 size_t nWritten = Buf->BufSize + 1;
505 size_t Offset = Buf->BufUsed;
506 size_t newused = Offset + nWritten;
508 while (newused >= Buf->BufSize) {
509 nWritten = vsnprintf(Buf->buf + Offset,
510 Buf->BufSize - Offset,
512 newused = Offset + nWritten;
513 if (newused >= Buf->BufSize)
514 IncreaseBuf(Buf, 1, 0);
516 Buf->BufUsed = Offset + nWritten ;
521 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
523 size_t nWritten = Buf->BufSize + 1;
524 size_t Offset = Buf->BufUsed;
525 size_t newused = Offset + nWritten;
528 while (newused >= Buf->BufSize) {
529 va_start(arg_ptr, format);
530 nWritten = vsnprintf(Buf->buf + Offset,
531 Buf->BufSize - Offset,
534 newused = Offset + nWritten;
535 if (newused >= Buf->BufSize)
536 IncreaseBuf(Buf, 1, 0);
538 Buf->BufUsed = Offset + nWritten ;
543 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
545 size_t nWritten = Buf->BufSize + 1;
548 while (nWritten >= Buf->BufSize) {
549 va_start(arg_ptr, format);
550 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
552 Buf->BufUsed = nWritten ;
553 if (nWritten >= Buf->BufSize)
554 IncreaseBuf(Buf, 0, 0);
560 * \brief a string tokenizer
561 * \param dest Destination StringBuffer
562 * \param Source StringBuffer to read into
563 * \param separator tokenizer param
564 * \returns -1 if not found, else length of token.
566 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
568 const char *s, *e; //* source * /
569 int len = 0; //* running total length of extracted string * /
570 int current_token = 0; //* token currently being processed * /
572 if ((Source == NULL) || (Source->BufUsed ==0)) {
576 e = s + Source->BufUsed;
582 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
586 while ((s<e) && !IsEmptyStr(s)) {
587 if (*s == separator) {
590 if (len >= dest->BufSize)
591 if (!IncreaseBuf(dest, 1, -1))
593 if ( (current_token == parmnum) &&
598 else if (current_token > parmnum) {
604 dest->buf[len] = '\0';
607 if (current_token < parmnum) {
608 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
611 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
617 * extract_int() - extract an int parm w/o supplying a buffer
619 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
628 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
635 * extract_long() - extract an long parm w/o supplying a buffer
637 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
646 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
654 * extract_unsigned_long() - extract an unsigned long parm
656 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
665 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
674 * \brief Input binary data from socket
675 * \param buf the buffer to get the input to
676 * \param bytes the maximal number of bytes to read
678 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
685 slen = len = buf->BufUsed;
687 rlen = read(*fd, &buf->buf[len], 1);
689 *Error = strerror(errno);
696 if (buf->buf[len] == '\n')
698 if (buf->buf[len] != '\r')
700 if (!(len < buf->BufSize)) {
702 buf->buf[len+1] = '\0';
703 IncreaseBuf(buf, 1, -1);
707 buf->buf[len] = '\0';
712 * \brief Input binary data from socket
713 * \param buf the buffer to get the input to
714 * \param bytes the maximal number of bytes to read
716 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
724 if ((Buf == NULL) || (*fd == -1))
728 if (Buf->BufUsed + nBytes > Buf->BufSize)
729 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
731 ptr = Buf->buf + Buf->BufUsed;
733 slen = len = Buf->BufUsed;
735 fdflags = fcntl(*fd, F_GETFL);
737 while (nRead < nBytes) {
738 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
741 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
742 *Error = strerror(errno);
747 if ((rlen = read(*fd,
749 nBytes - nRead)) == -1) {
752 *Error = strerror(errno);
756 Buf->BufUsed += rlen;
758 Buf->buf[Buf->BufUsed] = '\0';
762 void StrBufCutLeft(StrBuf *Buf, int nChars)
764 if (nChars >= Buf->BufUsed) {
768 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
769 Buf->BufUsed -= nChars;
770 Buf->buf[Buf->BufUsed] = '\0';
773 void StrBufCutRight(StrBuf *Buf, int nChars)
775 if (nChars >= Buf->BufUsed) {
779 Buf->BufUsed -= nChars;
780 Buf->buf[Buf->BufUsed] = '\0';
785 * string conversion function
787 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
795 if (source == NULL ||target == NULL)
800 len = source->BufUsed;
801 for (a = 0; a < len; ++a) {
802 if (target->BufUsed >= target->BufSize)
803 IncreaseBuf(target, 1, -1);
805 if (source->buf[a] == '=') {
806 hex[0] = source->buf[a + 1];
807 hex[1] = source->buf[a + 2];
810 sscanf(hex, "%02x", &b);
811 target->buf[target->BufUsed] = b;
812 target->buf[++target->BufUsed] = 0;
816 target->buf[target->BufUsed] = source->buf[a];
817 target->buf[++target->BufUsed] = 0;
824 * string conversion function
826 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
833 if (source == NULL ||target == NULL)
838 len = source->BufUsed;
839 for (i=0; i<len; ++i) {
840 if (target->BufUsed + 4 >= target->BufSize)
841 IncreaseBuf(target, 1, -1);
842 if ( (isalnum(source->buf[i])) ||
843 (source->buf[i]=='-') ||
844 (source->buf[i]=='_') ) {
845 target->buf[target->BufUsed++] = source->buf[i];
848 sprintf(&target->buf[target->BufUsed],
850 (0xFF &source->buf[i]));
851 target->BufUsed += 3;
854 target->buf[target->BufUsed + 1] = '\0';
858 * \brief uses the same calling syntax as compress2(), but it
859 * creates a stream compatible with HTTP "Content-encoding: gzip"
862 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
863 #define OS_CODE 0x03 /*< unix */
864 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
865 size_t * destLen, /*< length of the compresed data */
866 const Bytef * source, /*< source to encode */
867 uLong sourceLen, /*< length of source to encode */
868 int level) /*< compression level */
870 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
872 /* write gzip header */
873 snprintf((char *) dest, *destLen,
874 "%c%c%c%c%c%c%c%c%c%c",
875 gz_magic[0], gz_magic[1], Z_DEFLATED,
876 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
882 stream.next_in = (Bytef *) source;
883 stream.avail_in = (uInt) sourceLen;
884 stream.next_out = dest + 10L; // after header
885 stream.avail_out = (uInt) * destLen;
886 if ((uLong) stream.avail_out != *destLen)
889 stream.zalloc = (alloc_func) 0;
890 stream.zfree = (free_func) 0;
891 stream.opaque = (voidpf) 0;
893 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
894 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
898 err = deflate(&stream, Z_FINISH);
899 if (err != Z_STREAM_END) {
901 return err == Z_OK ? Z_BUF_ERROR : err;
903 *destLen = stream.total_out + 10L;
905 /* write CRC and Length */
906 uLong crc = crc32(0L, source, sourceLen);
908 for (n = 0; n < 4; ++n, ++*destLen) {
909 dest[*destLen] = (int) (crc & 0xff);
912 uLong len = stream.total_in;
913 for (n = 0; n < 4; ++n, ++*destLen) {
914 dest[*destLen] = (int) (len & 0xff);
917 err = deflateEnd(&stream);
924 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
926 int CompressBuffer(StrBuf *Buf)
929 char *compressed_data = NULL;
930 size_t compressed_len, bufsize;
932 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
933 compressed_data = malloc(compressed_len);
935 if (compress_gzip((Bytef *) compressed_data,
938 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
941 Buf->buf = compressed_data;
942 Buf->BufUsed = compressed_len;
943 Buf->BufSize = bufsize;
946 free(compressed_data);
948 #endif /* HAVE_ZLIB */
952 int StrBufDecodeBase64(StrBuf *Buf)
956 if (Buf == NULL) return -1;
958 xferbuf = (char*) malloc(Buf->BufSize);
959 siz = CtdlDecodeBase64(xferbuf,
970 * remove escaped strings from i.e. the url string (like %20 for blanks)
972 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
978 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
979 Buf->buf[Buf->BufUsed - 1] = '\0';
984 while (a < Buf->BufUsed) {
985 if (Buf->buf[a] == '+')
987 else if (Buf->buf[a] == '%') {
988 /* don't let % chars through, rather truncate the input. */
989 if (a + 2 > Buf->BufUsed) {
994 hex[0] = Buf->buf[a + 1];
995 hex[1] = Buf->buf[a + 2];
998 sscanf(hex, "%02x", &b);
999 Buf->buf[a] = (char) b;
1000 len = Buf->BufUsed - a - 2;
1002 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
1014 * \brief RFC2047-encode a header field if necessary.
1015 * If no non-ASCII characters are found, the string
1016 * will be copied verbatim without encoding.
1018 * \param target Target buffer.
1019 * \param source Source string to be encoded.
1020 * \returns encoded length; -1 if non success.
1022 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
1024 const char headerStr[] = "=?UTF-8?Q?";
1025 int need_to_encode = 0;
1029 if ((source == NULL) ||
1033 while ((i < source->BufUsed) &&
1034 (!IsEmptyStr (&source->buf[i])) &&
1035 (need_to_encode == 0)) {
1036 if (((unsigned char) source->buf[i] < 32) ||
1037 ((unsigned char) source->buf[i] > 126)) {
1043 if (!need_to_encode) {
1044 if (*target == NULL) {
1045 *target = NewStrBufPlain(source->buf, source->BufUsed);
1048 FlushStrBuf(*target);
1049 StrBufAppendBuf(*target, source, 0);
1051 return (*target)->BufUsed;
1053 if (*target == NULL)
1054 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
1055 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
1056 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
1057 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
1058 (*target)->BufUsed = sizeof(headerStr) - 1;
1059 for (i=0; (i < source->BufUsed); ++i) {
1060 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1061 IncreaseBuf(*target, 1, 0);
1062 ch = (unsigned char) source->buf[i];
1063 if ((ch < 32) || (ch > 126) || (ch == 61)) {
1064 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
1065 (*target)->BufUsed += 3;
1068 (*target)->buf[(*target)->BufUsed] = ch;
1069 (*target)->BufUsed++;
1073 if ((*target)->BufUsed + 4 > (*target)->BufSize)
1074 IncreaseBuf(*target, 1, 0);
1076 (*target)->buf[(*target)->BufUsed++] = '?';
1077 (*target)->buf[(*target)->BufUsed++] = '=';
1078 (*target)->buf[(*target)->BufUsed] = '\0';
1079 return (*target)->BufUsed;;
1082 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
1087 for (i=0; i<buf->BufUsed; i++)
1088 if (buf->buf[i] == search)
1089 buf->buf[i] = replace;