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);
60 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
66 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
68 CopySize = strlen((ptr != NULL)?ptr:"");
72 while (Siz <= CopySize)
75 NewBuf->buf = (char*) malloc(Siz);
76 NewBuf->BufSize = Siz;
78 memcpy(NewBuf->buf, ptr, CopySize);
79 NewBuf->buf[CopySize] = '\0';
80 NewBuf->BufUsed = CopySize;
83 NewBuf->buf[0] = '\0';
91 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
95 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
96 NewBuf->buf = (char*) StringConstant;
97 NewBuf->BufSize = SizeOfStrConstant;
98 NewBuf->BufUsed = SizeOfStrConstant;
104 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
107 size_t NewSize = Buf->BufSize * 2;
113 while (NewSize < DestSize)
116 NewBuf= (char*) malloc(NewSize);
119 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
132 int FlushStrBuf(StrBuf *buf)
141 void FreeStrBuf (StrBuf **FreeMe)
145 if (!(*FreeMe)->ConstBuf)
146 free((*FreeMe)->buf);
151 void HFreeStrBuf (void *VFreeMe)
153 StrBuf *FreeMe = (StrBuf*)VFreeMe;
156 if (!FreeMe->ConstBuf)
161 long StrTol(const StrBuf *Buf)
164 return atol(Buf->buf);
169 int StrToi(const StrBuf *Buf)
172 return atoi(Buf->buf);
177 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
179 size_t Siz = Buf->BufSize;
183 CopySize = strlen(ptr);
187 while (Siz <= CopySize)
190 if (Siz != Buf->BufSize)
191 IncreaseBuf(Buf, 0, Siz);
192 memcpy(Buf->buf, ptr, CopySize);
193 Buf->buf[CopySize] = '\0';
194 Buf->BufUsed = CopySize;
199 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, size_t Offset)
201 if ((AppendBuf == NULL) || (Buf == NULL))
204 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed)
207 AppendBuf->BufUsed + Buf->BufUsed);
209 memcpy(Buf->buf + Buf->BufUsed,
210 AppendBuf->buf + Offset,
211 AppendBuf->BufUsed - Offset);
212 Buf->BufUsed += AppendBuf->BufUsed - Offset;
213 Buf->buf[Buf->BufUsed] = '\0';
217 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, size_t Offset)
221 if ((AppendBuf == NULL) || (Buf == NULL))
225 aps = strlen(AppendBuf + Offset);
227 aps = AppendSize - Offset;
229 if (Buf->BufSize < Buf->BufUsed + aps)
230 IncreaseBuf(Buf, (Buf->BufUsed > 0), Buf->BufUsed + aps);
232 memcpy(Buf->buf + Buf->BufUsed,
236 Buf->buf[Buf->BufUsed] = '\0';
240 inline int StrBufNum_tokens(const StrBuf *source, char tok)
242 return num_tokens(source->buf, tok);
246 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
249 if (Offset > Source->BufUsed)
254 if (Offset + nChars < Source->BufUsed)
256 if (nChars > dest->BufSize)
257 IncreaseBuf(dest, 0, nChars + 1);
258 memcpy(dest->buf, Source->buf + Offset, nChars);
259 dest->BufUsed = nChars;
260 dest->buf[dest->BufUsed] = '\0';
263 NCharsRemain = Source->BufUsed - Offset;
264 if (NCharsRemain > dest->BufSize)
265 IncreaseBuf(dest, 0, NCharsRemain + 1);
266 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
267 dest->BufUsed = NCharsRemain;
268 dest->buf[dest->BufUsed] = '\0';
273 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
275 size_t nWritten = Buf->BufSize + 1;
276 size_t Offset = Buf->BufUsed;
277 size_t newused = Offset + nWritten;
279 while (newused >= Buf->BufSize) {
280 nWritten = vsnprintf(Buf->buf + Offset,
281 Buf->BufSize - Offset,
283 newused = Offset + nWritten;
284 if (newused >= Buf->BufSize)
285 IncreaseBuf(Buf, 1, 0);
287 Buf->BufUsed = Offset + nWritten ;
292 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
294 size_t nWritten = Buf->BufSize + 1;
297 while (nWritten >= Buf->BufSize) {
298 va_start(arg_ptr, format);
299 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
301 Buf->BufUsed = nWritten ;
302 if (nWritten >= Buf->BufSize)
303 IncreaseBuf(Buf, 0, 0);
309 * \brief a string tokenizer
310 * \param dest Destination StringBuffer
311 * \param Source StringBuffer to read into
312 * \param separator tokenizer param
313 * \returns -1 if not found, else length of token.
315 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
317 const char *s, *e; //* source * /
318 int len = 0; //* running total length of extracted string * /
319 int current_token = 0; //* token currently being processed * /
321 if ((Source == NULL) || (Source->BufUsed ==0)) {
325 e = s + Source->BufUsed;
331 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
335 while ((s<e) && !IsEmptyStr(s)) {
336 if (*s == separator) {
339 if (len >= dest->BufSize)
340 if (!IncreaseBuf(dest, 1, -1))
342 if ( (current_token == parmnum) &&
347 else if (current_token > parmnum) {
353 dest->buf[len] = '\0';
356 if (current_token < parmnum) {
357 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
360 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
366 * extract_int() - extract an int parm w/o supplying a buffer
368 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
377 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
384 * extract_long() - extract an long parm w/o supplying a buffer
386 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
395 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
403 * extract_unsigned_long() - extract an unsigned long parm
405 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
414 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
423 * \brief Input binary data from socket
424 * \param buf the buffer to get the input to
425 * \param bytes the maximal number of bytes to read
427 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
434 slen = len = buf->BufUsed;
436 rlen = read(*fd, &buf->buf[len], 1);
438 *Error = strerror(errno);
445 if (buf->buf[len] == '\n')
447 if (buf->buf[len] != '\r')
449 if (!(len < buf->BufSize)) {
451 buf->buf[len+1] = '\0';
452 IncreaseBuf(buf, 1, -1);
456 buf->buf[len] = '\0';
461 * \brief Input binary data from socket
462 * \param buf the buffer to get the input to
463 * \param bytes the maximal number of bytes to read
465 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
473 if ((Buf == NULL) || (*fd == -1))
477 if (Buf->BufUsed + nBytes > Buf->BufSize)
478 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
480 ptr = Buf->buf + Buf->BufUsed;
482 slen = len = Buf->BufUsed;
484 fdflags = fcntl(*fd, F_GETFL);
486 while (nRead < nBytes) {
487 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
490 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
491 *Error = strerror(errno);
496 if ((rlen = read(*fd,
498 nBytes - nRead)) == -1) {
501 *Error = strerror(errno);
505 Buf->BufUsed += rlen;
507 Buf->buf[Buf->BufUsed] = '\0';
511 void StrBufCutLeft(StrBuf *Buf, int nChars)
513 if (nChars >= Buf->BufUsed) {
517 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
518 Buf->BufUsed -= nChars;
519 Buf->buf[Buf->BufUsed] = '\0';
522 void StrBufCutRight(StrBuf *Buf, int nChars)
524 if (nChars >= Buf->BufUsed) {
528 Buf->BufUsed -= nChars;
529 Buf->buf[Buf->BufUsed] = '\0';
534 * string conversion function
536 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
544 if (source == NULL ||target == NULL)
549 len = source->BufUsed;
550 for (a = 0; a < len; ++a) {
551 if (target->BufUsed >= target->BufSize)
552 IncreaseBuf(target, 1, -1);
554 if (source->buf[a] == '=') {
555 hex[0] = source->buf[a + 1];
556 hex[1] = source->buf[a + 2];
559 sscanf(hex, "%02x", &b);
560 target->buf[target->BufUsed] = b;
561 target->buf[++target->BufUsed] = 0;
565 target->buf[target->BufUsed] = source->buf[a];
566 target->buf[++target->BufUsed] = 0;
573 * string conversion function
575 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
582 if (source == NULL ||target == NULL)
587 len = source->BufUsed;
588 for (i=0; i<len; ++i) {
589 if (target->BufUsed + 4 >= target->BufSize)
590 IncreaseBuf(target, 1, -1);
591 if ( (isalnum(source->buf[i])) ||
592 (source->buf[i]=='-') ||
593 (source->buf[i]=='_') ) {
594 target->buf[target->BufUsed++] = source->buf[i];
597 sprintf(&target->buf[target->BufUsed],
599 (0xFF &source->buf[i]));
600 target->BufUsed += 3;
603 target->buf[target->BufUsed + 1] = '\0';
607 * \brief uses the same calling syntax as compress2(), but it
608 * creates a stream compatible with HTTP "Content-encoding: gzip"
611 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
612 #define OS_CODE 0x03 /*< unix */
613 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
614 size_t * destLen, /*< length of the compresed data */
615 const Bytef * source, /*< source to encode */
616 uLong sourceLen, /*< length of source to encode */
617 int level) /*< compression level */
619 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
621 /* write gzip header */
622 snprintf((char *) dest, *destLen,
623 "%c%c%c%c%c%c%c%c%c%c",
624 gz_magic[0], gz_magic[1], Z_DEFLATED,
625 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
631 stream.next_in = (Bytef *) source;
632 stream.avail_in = (uInt) sourceLen;
633 stream.next_out = dest + 10L; // after header
634 stream.avail_out = (uInt) * destLen;
635 if ((uLong) stream.avail_out != *destLen)
638 stream.zalloc = (alloc_func) 0;
639 stream.zfree = (free_func) 0;
640 stream.opaque = (voidpf) 0;
642 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
643 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
647 err = deflate(&stream, Z_FINISH);
648 if (err != Z_STREAM_END) {
650 return err == Z_OK ? Z_BUF_ERROR : err;
652 *destLen = stream.total_out + 10L;
654 /* write CRC and Length */
655 uLong crc = crc32(0L, source, sourceLen);
657 for (n = 0; n < 4; ++n, ++*destLen) {
658 dest[*destLen] = (int) (crc & 0xff);
661 uLong len = stream.total_in;
662 for (n = 0; n < 4; ++n, ++*destLen) {
663 dest[*destLen] = (int) (len & 0xff);
666 err = deflateEnd(&stream);
673 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
675 int CompressBuffer(StrBuf *Buf)
678 char *compressed_data = NULL;
679 size_t compressed_len, bufsize;
681 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
682 compressed_data = malloc(compressed_len);
684 if (compress_gzip((Bytef *) compressed_data,
687 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
690 Buf->buf = compressed_data;
691 Buf->BufUsed = compressed_len;
692 Buf->BufSize = bufsize;
695 free(compressed_data);
697 #endif /* HAVE_ZLIB */
701 int StrBufDecodeBase64(StrBuf *Buf)
705 if (Buf == NULL) return -1;
707 xferbuf = (char*) malloc(Buf->BufSize);
708 siz = CtdlDecodeBase64(xferbuf,
719 * remove escaped strings from i.e. the url string (like %20 for blanks)
721 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
727 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
728 Buf->buf[Buf->BufUsed - 1] = '\0';
733 while (a < Buf->BufUsed) {
734 if (Buf->buf[a] == '+')
736 else if (Buf->buf[a] == '%') {
737 /* don't let % chars through, rather truncate the input. */
738 if (a + 2 > Buf->BufUsed) {
743 hex[0] = Buf->buf[a + 1];
744 hex[1] = Buf->buf[a + 2];
747 sscanf(hex, "%02x", &b);
748 Buf->buf[a] = (char) b;
749 len = Buf->BufUsed - a - 2;
751 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
763 * \brief RFC2047-encode a header field if necessary.
764 * If no non-ASCII characters are found, the string
765 * will be copied verbatim without encoding.
767 * \param target Target buffer.
768 * \param source Source string to be encoded.
769 * \returns encoded length; -1 if non success.
771 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
773 const char headerStr[] = "=?UTF-8?Q?";
774 int need_to_encode = 0;
778 if ((source == NULL) ||
782 while ((i < source->BufUsed) &&
783 (!IsEmptyStr (&source->buf[i])) &&
784 (need_to_encode == 0)) {
785 if (((unsigned char) source->buf[i] < 32) ||
786 ((unsigned char) source->buf[i] > 126)) {
792 if (!need_to_encode) {
793 if (*target == NULL) {
794 *target = NewStrBufPlain(source->buf, source->BufUsed);
797 FlushStrBuf(*target);
798 StrBufAppendBuf(*target, source, 0);
800 return (*target)->BufUsed;
803 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
804 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
805 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
806 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
807 (*target)->BufUsed = sizeof(headerStr) - 1;
808 for (i=0; (i < source->BufUsed); ++i) {
809 if ((*target)->BufUsed + 4 > (*target)->BufSize)
810 IncreaseBuf(*target, 1, 0);
811 ch = (unsigned char) source->buf[i];
812 if ((ch < 32) || (ch > 126) || (ch == 61)) {
813 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
814 (*target)->BufUsed += 3;
817 (*target)->buf[(*target)->BufUsed] = ch;
818 (*target)->BufUsed++;
822 if ((*target)->BufUsed + 4 > (*target)->BufSize)
823 IncreaseBuf(*target, 1, 0);
825 (*target)->buf[(*target)->BufUsed++] = '?';
826 (*target)->buf[(*target)->BufUsed++] = '=';
827 (*target)->buf[(*target)->BufUsed] = '\0';
828 return (*target)->BufUsed;;
831 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
836 for (i=0; i<buf->BufUsed; i++)
837 if (buf->buf[i] == search)
838 buf->buf[i] = replace;