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;
48 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
54 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
56 CopySize = strlen((ptr != NULL)?ptr:"");
60 while (Siz <= CopySize)
63 NewBuf->buf = (char*) malloc(Siz);
64 NewBuf->BufSize = Siz;
66 memcpy(NewBuf->buf, ptr, CopySize);
67 NewBuf->buf[CopySize] = '\0';
68 NewBuf->BufUsed = CopySize;
71 NewBuf->buf[0] = '\0';
79 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
83 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
84 NewBuf->buf = (char*) StringConstant;
85 NewBuf->BufSize = SizeOfStrConstant;
86 NewBuf->BufUsed = SizeOfStrConstant;
92 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
95 size_t NewSize = Buf->BufSize * 2;
101 while (NewSize < DestSize)
104 NewBuf= (char*) malloc(NewSize);
107 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
120 int FlushStrBuf(StrBuf *buf)
129 void FreeStrBuf (StrBuf **FreeMe)
133 if (!(*FreeMe)->ConstBuf)
134 free((*FreeMe)->buf);
139 void HFreeStrBuf (void *VFreeMe)
141 StrBuf *FreeMe = (StrBuf*)VFreeMe;
144 if (!FreeMe->ConstBuf)
149 long StrTol(const StrBuf *Buf)
152 return atol(Buf->buf);
157 int StrToi(const StrBuf *Buf)
160 return atoi(Buf->buf);
165 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
167 size_t Siz = Buf->BufSize;
171 CopySize = strlen(ptr);
175 while (Siz <= CopySize)
178 if (Siz != Buf->BufSize)
179 IncreaseBuf(Buf, 0, Siz);
180 memcpy(Buf->buf, ptr, CopySize);
181 Buf->buf[CopySize] = '\0';
182 Buf->BufUsed = CopySize;
187 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, size_t Offset)
189 if ((AppendBuf == NULL) || (Buf == NULL))
192 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed)
195 AppendBuf->BufUsed + Buf->BufUsed);
197 memcpy(Buf->buf + Buf->BufUsed,
198 AppendBuf->buf + Offset,
199 AppendBuf->BufUsed - Offset);
200 Buf->BufUsed += AppendBuf->BufUsed - Offset;
201 Buf->buf[Buf->BufUsed] = '\0';
205 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, size_t Offset)
209 if ((AppendBuf == NULL) || (Buf == NULL))
213 aps = strlen(AppendBuf + Offset);
215 aps = AppendSize - Offset;
217 if (Buf->BufSize < Buf->BufUsed + aps)
218 IncreaseBuf(Buf, (Buf->BufUsed > 0), Buf->BufUsed + aps);
220 memcpy(Buf->buf + Buf->BufUsed,
224 Buf->buf[Buf->BufUsed] = '\0';
228 inline int StrBufNum_tokens(const StrBuf *source, char tok)
230 return num_tokens(source->buf, tok);
234 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
237 if (Offset > Source->BufUsed)
242 if (Offset + nChars < Source->BufUsed)
244 if (nChars > dest->BufSize)
245 IncreaseBuf(dest, 0, nChars + 1);
246 memcpy(dest->buf, Source->buf + Offset, nChars);
247 dest->BufUsed = nChars;
248 dest->buf[dest->BufUsed] = '\0';
251 NCharsRemain = Source->BufUsed - Offset;
252 if (NCharsRemain > dest->BufSize)
253 IncreaseBuf(dest, 0, NCharsRemain + 1);
254 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
255 dest->BufUsed = NCharsRemain;
256 dest->buf[dest->BufUsed] = '\0';
261 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
263 size_t nWritten = Buf->BufSize + 1;
264 size_t Offset = Buf->BufUsed;
265 size_t newused = Offset + nWritten;
267 while (newused >= Buf->BufSize) {
268 nWritten = vsnprintf(Buf->buf + Offset,
269 Buf->BufSize - Offset,
271 newused = Offset + nWritten;
272 if (newused >= Buf->BufSize)
273 IncreaseBuf(Buf, 1, 0);
275 Buf->BufUsed = Offset + nWritten ;
280 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
282 size_t nWritten = Buf->BufSize + 1;
285 while (nWritten >= Buf->BufSize) {
286 va_start(arg_ptr, format);
287 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
289 Buf->BufUsed = nWritten ;
290 if (nWritten >= Buf->BufSize)
291 IncreaseBuf(Buf, 0, 0);
297 * \brief a string tokenizer
298 * \param dest Destination StringBuffer
299 * \param Source StringBuffer to read into
300 * \param separator tokenizer param
301 * \returns -1 if not found, else length of token.
303 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
305 const char *s, *e; //* source * /
306 int len = 0; //* running total length of extracted string * /
307 int current_token = 0; //* token currently being processed * /
309 if ((Source == NULL) || (Source->BufUsed ==0)) {
313 e = s + Source->BufUsed;
319 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
323 while ((s<e) && !IsEmptyStr(s)) {
324 if (*s == separator) {
327 if (len >= dest->BufSize)
328 if (!IncreaseBuf(dest, 1, -1))
330 if ( (current_token == parmnum) &&
335 else if (current_token > parmnum) {
341 dest->buf[len] = '\0';
344 if (current_token < parmnum) {
345 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
348 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
354 * extract_int() - extract an int parm w/o supplying a buffer
356 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
365 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
372 * extract_long() - extract an long parm w/o supplying a buffer
374 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
383 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
391 * extract_unsigned_long() - extract an unsigned long parm
393 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
402 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
411 * \brief Input binary data from socket
412 * \param buf the buffer to get the input to
413 * \param bytes the maximal number of bytes to read
415 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
422 slen = len = buf->BufUsed;
424 rlen = read(*fd, &buf->buf[len], 1);
426 *Error = strerror(errno);
433 if (buf->buf[len] == '\n')
435 if (buf->buf[len] != '\r')
437 if (!(len < buf->BufSize)) {
439 buf->buf[len+1] = '\0';
440 IncreaseBuf(buf, 1, -1);
444 buf->buf[len] = '\0';
449 * \brief Input binary data from socket
450 * \param buf the buffer to get the input to
451 * \param bytes the maximal number of bytes to read
453 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
461 if ((Buf == NULL) || (*fd == -1))
465 if (Buf->BufUsed + nBytes > Buf->BufSize)
466 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
468 ptr = Buf->buf + Buf->BufUsed;
470 slen = len = Buf->BufUsed;
472 fdflags = fcntl(*fd, F_GETFL);
474 while (nRead < nBytes) {
475 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
478 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
479 *Error = strerror(errno);
484 if ((rlen = read(*fd,
486 nBytes - nRead)) == -1) {
489 *Error = strerror(errno);
493 Buf->BufUsed += rlen;
495 Buf->buf[Buf->BufUsed] = '\0';
499 void StrBufCutLeft(StrBuf *Buf, int nChars)
501 if (nChars >= Buf->BufUsed) {
505 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
506 Buf->BufUsed -= nChars;
507 Buf->buf[Buf->BufUsed] = '\0';
510 void StrBufCutRight(StrBuf *Buf, int nChars)
512 if (nChars >= Buf->BufUsed) {
516 Buf->BufUsed -= nChars;
517 Buf->buf[Buf->BufUsed] = '\0';
522 * string conversion function
524 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
532 if (source == NULL ||target == NULL)
537 len = source->BufUsed;
538 for (a = 0; a < len; ++a) {
539 if (target->BufUsed >= target->BufSize)
540 IncreaseBuf(target, 1, -1);
542 if (source->buf[a] == '=') {
543 hex[0] = source->buf[a + 1];
544 hex[1] = source->buf[a + 2];
547 sscanf(hex, "%02x", &b);
548 target->buf[target->BufUsed] = b;
549 target->buf[++target->BufUsed] = 0;
553 target->buf[target->BufUsed] = source->buf[a];
554 target->buf[++target->BufUsed] = 0;
561 * string conversion function
563 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
570 if (source == NULL ||target == NULL)
575 len = source->BufUsed;
576 for (i=0; i<len; ++i) {
577 if (target->BufUsed + 4 >= target->BufSize)
578 IncreaseBuf(target, 1, -1);
579 if ( (isalnum(source->buf[i])) ||
580 (source->buf[i]=='-') ||
581 (source->buf[i]=='_') ) {
582 target->buf[target->BufUsed++] = source->buf[i];
585 sprintf(&target->buf[target->BufUsed],
587 (0xFF &source->buf[i]));
588 target->BufUsed += 3;
591 target->buf[target->BufUsed + 1] = '\0';
595 * \brief uses the same calling syntax as compress2(), but it
596 * creates a stream compatible with HTTP "Content-encoding: gzip"
599 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
600 #define OS_CODE 0x03 /*< unix */
601 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
602 size_t * destLen, /*< length of the compresed data */
603 const Bytef * source, /*< source to encode */
604 uLong sourceLen, /*< length of source to encode */
605 int level) /*< compression level */
607 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
609 /* write gzip header */
610 snprintf((char *) dest, *destLen,
611 "%c%c%c%c%c%c%c%c%c%c",
612 gz_magic[0], gz_magic[1], Z_DEFLATED,
613 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
619 stream.next_in = (Bytef *) source;
620 stream.avail_in = (uInt) sourceLen;
621 stream.next_out = dest + 10L; // after header
622 stream.avail_out = (uInt) * destLen;
623 if ((uLong) stream.avail_out != *destLen)
626 stream.zalloc = (alloc_func) 0;
627 stream.zfree = (free_func) 0;
628 stream.opaque = (voidpf) 0;
630 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
631 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
635 err = deflate(&stream, Z_FINISH);
636 if (err != Z_STREAM_END) {
638 return err == Z_OK ? Z_BUF_ERROR : err;
640 *destLen = stream.total_out + 10L;
642 /* write CRC and Length */
643 uLong crc = crc32(0L, source, sourceLen);
645 for (n = 0; n < 4; ++n, ++*destLen) {
646 dest[*destLen] = (int) (crc & 0xff);
649 uLong len = stream.total_in;
650 for (n = 0; n < 4; ++n, ++*destLen) {
651 dest[*destLen] = (int) (len & 0xff);
654 err = deflateEnd(&stream);
661 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
663 int CompressBuffer(StrBuf *Buf)
666 char *compressed_data = NULL;
667 size_t compressed_len, bufsize;
669 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
670 compressed_data = malloc(compressed_len);
672 if (compress_gzip((Bytef *) compressed_data,
675 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
678 Buf->buf = compressed_data;
679 Buf->BufUsed = compressed_len;
680 Buf->BufSize = bufsize;
683 free(compressed_data);
685 #endif /* HAVE_ZLIB */
689 int StrBufDecodeBase64(StrBuf *Buf)
693 if (Buf == NULL) return -1;
695 xferbuf = (char*) malloc(Buf->BufSize);
696 siz = CtdlDecodeBase64(xferbuf,
707 * remove escaped strings from i.e. the url string (like %20 for blanks)
709 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
715 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
716 Buf->buf[Buf->BufUsed - 1] = '\0';
721 while (a < Buf->BufUsed) {
722 if (Buf->buf[a] == '+')
724 else if (Buf->buf[a] == '%') {
725 /* don't let % chars through, rather truncate the input. */
726 if (a + 2 > Buf->BufUsed) {
731 hex[0] = Buf->buf[a + 1];
732 hex[1] = Buf->buf[a + 2];
735 sscanf(hex, "%02x", &b);
736 Buf->buf[a] = (char) b;
737 len = Buf->BufUsed - a - 2;
739 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
751 * \brief RFC2047-encode a header field if necessary.
752 * If no non-ASCII characters are found, the string
753 * will be copied verbatim without encoding.
755 * \param target Target buffer.
756 * \param source Source string to be encoded.
757 * \returns encoded length; -1 if non success.
759 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
761 const char headerStr[] = "=?UTF-8?Q?";
762 int need_to_encode = 0;
766 if ((source == NULL) ||
770 while ((i < source->BufUsed) &&
771 (!IsEmptyStr (&source->buf[i])) &&
772 (need_to_encode == 0)) {
773 if (((unsigned char) source->buf[i] < 32) ||
774 ((unsigned char) source->buf[i] > 126)) {
780 if (!need_to_encode) {
781 if (*target == NULL) {
782 *target = NewStrBufPlain(source->buf, source->BufUsed);
785 FlushStrBuf(*target);
786 StrBufAppendBuf(*target, source, 0);
788 return (*target)->BufUsed;
791 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
792 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
793 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
794 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
795 (*target)->BufUsed = sizeof(headerStr) - 1;
796 for (i=0; (i < source->BufUsed); ++i) {
797 if ((*target)->BufUsed + 4 > (*target)->BufSize)
798 IncreaseBuf(*target, 1, 0);
799 ch = (unsigned char) source->buf[i];
800 if ((ch < 32) || (ch > 126) || (ch == 61)) {
801 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
802 (*target)->BufUsed += 3;
805 (*target)->buf[(*target)->BufUsed] = ch;
806 (*target)->BufUsed++;
810 if ((*target)->BufUsed + 4 > (*target)->BufSize)
811 IncreaseBuf(*target, 1, 0);
813 (*target)->buf[(*target)->BufUsed++] = '?';
814 (*target)->buf[(*target)->BufUsed++] = '=';
815 (*target)->buf[(*target)->BufUsed] = '\0';
816 return (*target)->BufUsed;;
819 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
824 for (i=0; i<buf->BufUsed; i++)
825 if (buf->buf[i] == search)
826 buf->buf[i] = replace;