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 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
182 size_t Siz = Buf->BufSize;
186 CopySize = strlen(ptr);
190 while (Siz <= CopySize)
193 if (Siz != Buf->BufSize)
194 IncreaseBuf(Buf, 0, Siz);
195 memcpy(Buf->buf, ptr, CopySize);
196 Buf->buf[CopySize] = '\0';
197 Buf->BufUsed = CopySize;
202 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, size_t Offset)
204 if ((AppendBuf == NULL) || (Buf == NULL))
207 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed)
210 AppendBuf->BufUsed + Buf->BufUsed);
212 memcpy(Buf->buf + Buf->BufUsed,
213 AppendBuf->buf + Offset,
214 AppendBuf->BufUsed - Offset);
215 Buf->BufUsed += AppendBuf->BufUsed - Offset;
216 Buf->buf[Buf->BufUsed] = '\0';
220 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, size_t Offset)
224 if ((AppendBuf == NULL) || (Buf == NULL))
228 aps = strlen(AppendBuf + Offset);
230 aps = AppendSize - Offset;
232 if (Buf->BufSize < Buf->BufUsed + aps)
233 IncreaseBuf(Buf, (Buf->BufUsed > 0), Buf->BufUsed + aps);
235 memcpy(Buf->buf + Buf->BufUsed,
239 Buf->buf[Buf->BufUsed] = '\0';
243 inline int StrBufNum_tokens(const StrBuf *source, char tok)
245 return num_tokens(source->buf, tok);
249 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
252 if (Offset > Source->BufUsed)
257 if (Offset + nChars < Source->BufUsed)
259 if (nChars > dest->BufSize)
260 IncreaseBuf(dest, 0, nChars + 1);
261 memcpy(dest->buf, Source->buf + Offset, nChars);
262 dest->BufUsed = nChars;
263 dest->buf[dest->BufUsed] = '\0';
266 NCharsRemain = Source->BufUsed - Offset;
267 if (NCharsRemain > dest->BufSize)
268 IncreaseBuf(dest, 0, NCharsRemain + 1);
269 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
270 dest->BufUsed = NCharsRemain;
271 dest->buf[dest->BufUsed] = '\0';
276 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
278 size_t nWritten = Buf->BufSize + 1;
279 size_t Offset = Buf->BufUsed;
280 size_t newused = Offset + nWritten;
282 while (newused >= Buf->BufSize) {
283 nWritten = vsnprintf(Buf->buf + Offset,
284 Buf->BufSize - Offset,
286 newused = Offset + nWritten;
287 if (newused >= Buf->BufSize)
288 IncreaseBuf(Buf, 1, 0);
290 Buf->BufUsed = Offset + nWritten ;
295 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
297 size_t nWritten = Buf->BufSize + 1;
300 while (nWritten >= Buf->BufSize) {
301 va_start(arg_ptr, format);
302 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
304 Buf->BufUsed = nWritten ;
305 if (nWritten >= Buf->BufSize)
306 IncreaseBuf(Buf, 0, 0);
312 * \brief a string tokenizer
313 * \param dest Destination StringBuffer
314 * \param Source StringBuffer to read into
315 * \param separator tokenizer param
316 * \returns -1 if not found, else length of token.
318 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
320 const char *s, *e; //* source * /
321 int len = 0; //* running total length of extracted string * /
322 int current_token = 0; //* token currently being processed * /
324 if ((Source == NULL) || (Source->BufUsed ==0)) {
328 e = s + Source->BufUsed;
334 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
338 while ((s<e) && !IsEmptyStr(s)) {
339 if (*s == separator) {
342 if (len >= dest->BufSize)
343 if (!IncreaseBuf(dest, 1, -1))
345 if ( (current_token == parmnum) &&
350 else if (current_token > parmnum) {
356 dest->buf[len] = '\0';
359 if (current_token < parmnum) {
360 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
363 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
369 * extract_int() - extract an int parm w/o supplying a buffer
371 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
380 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
387 * extract_long() - extract an long parm w/o supplying a buffer
389 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
398 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
406 * extract_unsigned_long() - extract an unsigned long parm
408 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
417 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
426 * \brief Input binary data from socket
427 * \param buf the buffer to get the input to
428 * \param bytes the maximal number of bytes to read
430 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
437 slen = len = buf->BufUsed;
439 rlen = read(*fd, &buf->buf[len], 1);
441 *Error = strerror(errno);
448 if (buf->buf[len] == '\n')
450 if (buf->buf[len] != '\r')
452 if (!(len < buf->BufSize)) {
454 buf->buf[len+1] = '\0';
455 IncreaseBuf(buf, 1, -1);
459 buf->buf[len] = '\0';
464 * \brief Input binary data from socket
465 * \param buf the buffer to get the input to
466 * \param bytes the maximal number of bytes to read
468 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
476 if ((Buf == NULL) || (*fd == -1))
480 if (Buf->BufUsed + nBytes > Buf->BufSize)
481 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
483 ptr = Buf->buf + Buf->BufUsed;
485 slen = len = Buf->BufUsed;
487 fdflags = fcntl(*fd, F_GETFL);
489 while (nRead < nBytes) {
490 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
493 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
494 *Error = strerror(errno);
499 if ((rlen = read(*fd,
501 nBytes - nRead)) == -1) {
504 *Error = strerror(errno);
508 Buf->BufUsed += rlen;
510 Buf->buf[Buf->BufUsed] = '\0';
514 void StrBufCutLeft(StrBuf *Buf, int nChars)
516 if (nChars >= Buf->BufUsed) {
520 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
521 Buf->BufUsed -= nChars;
522 Buf->buf[Buf->BufUsed] = '\0';
525 void StrBufCutRight(StrBuf *Buf, int nChars)
527 if (nChars >= Buf->BufUsed) {
531 Buf->BufUsed -= nChars;
532 Buf->buf[Buf->BufUsed] = '\0';
537 * string conversion function
539 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
547 if (source == NULL ||target == NULL)
552 len = source->BufUsed;
553 for (a = 0; a < len; ++a) {
554 if (target->BufUsed >= target->BufSize)
555 IncreaseBuf(target, 1, -1);
557 if (source->buf[a] == '=') {
558 hex[0] = source->buf[a + 1];
559 hex[1] = source->buf[a + 2];
562 sscanf(hex, "%02x", &b);
563 target->buf[target->BufUsed] = b;
564 target->buf[++target->BufUsed] = 0;
568 target->buf[target->BufUsed] = source->buf[a];
569 target->buf[++target->BufUsed] = 0;
576 * string conversion function
578 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
585 if (source == NULL ||target == NULL)
590 len = source->BufUsed;
591 for (i=0; i<len; ++i) {
592 if (target->BufUsed + 4 >= target->BufSize)
593 IncreaseBuf(target, 1, -1);
594 if ( (isalnum(source->buf[i])) ||
595 (source->buf[i]=='-') ||
596 (source->buf[i]=='_') ) {
597 target->buf[target->BufUsed++] = source->buf[i];
600 sprintf(&target->buf[target->BufUsed],
602 (0xFF &source->buf[i]));
603 target->BufUsed += 3;
606 target->buf[target->BufUsed + 1] = '\0';
610 * \brief uses the same calling syntax as compress2(), but it
611 * creates a stream compatible with HTTP "Content-encoding: gzip"
614 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
615 #define OS_CODE 0x03 /*< unix */
616 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
617 size_t * destLen, /*< length of the compresed data */
618 const Bytef * source, /*< source to encode */
619 uLong sourceLen, /*< length of source to encode */
620 int level) /*< compression level */
622 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
624 /* write gzip header */
625 snprintf((char *) dest, *destLen,
626 "%c%c%c%c%c%c%c%c%c%c",
627 gz_magic[0], gz_magic[1], Z_DEFLATED,
628 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
634 stream.next_in = (Bytef *) source;
635 stream.avail_in = (uInt) sourceLen;
636 stream.next_out = dest + 10L; // after header
637 stream.avail_out = (uInt) * destLen;
638 if ((uLong) stream.avail_out != *destLen)
641 stream.zalloc = (alloc_func) 0;
642 stream.zfree = (free_func) 0;
643 stream.opaque = (voidpf) 0;
645 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
646 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
650 err = deflate(&stream, Z_FINISH);
651 if (err != Z_STREAM_END) {
653 return err == Z_OK ? Z_BUF_ERROR : err;
655 *destLen = stream.total_out + 10L;
657 /* write CRC and Length */
658 uLong crc = crc32(0L, source, sourceLen);
660 for (n = 0; n < 4; ++n, ++*destLen) {
661 dest[*destLen] = (int) (crc & 0xff);
664 uLong len = stream.total_in;
665 for (n = 0; n < 4; ++n, ++*destLen) {
666 dest[*destLen] = (int) (len & 0xff);
669 err = deflateEnd(&stream);
676 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
678 int CompressBuffer(StrBuf *Buf)
681 char *compressed_data = NULL;
682 size_t compressed_len, bufsize;
684 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
685 compressed_data = malloc(compressed_len);
687 if (compress_gzip((Bytef *) compressed_data,
690 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
693 Buf->buf = compressed_data;
694 Buf->BufUsed = compressed_len;
695 Buf->BufSize = bufsize;
698 free(compressed_data);
700 #endif /* HAVE_ZLIB */
704 int StrBufDecodeBase64(StrBuf *Buf)
708 if (Buf == NULL) return -1;
710 xferbuf = (char*) malloc(Buf->BufSize);
711 siz = CtdlDecodeBase64(xferbuf,
722 * remove escaped strings from i.e. the url string (like %20 for blanks)
724 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
730 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
731 Buf->buf[Buf->BufUsed - 1] = '\0';
736 while (a < Buf->BufUsed) {
737 if (Buf->buf[a] == '+')
739 else if (Buf->buf[a] == '%') {
740 /* don't let % chars through, rather truncate the input. */
741 if (a + 2 > Buf->BufUsed) {
746 hex[0] = Buf->buf[a + 1];
747 hex[1] = Buf->buf[a + 2];
750 sscanf(hex, "%02x", &b);
751 Buf->buf[a] = (char) b;
752 len = Buf->BufUsed - a - 2;
754 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
766 * \brief RFC2047-encode a header field if necessary.
767 * If no non-ASCII characters are found, the string
768 * will be copied verbatim without encoding.
770 * \param target Target buffer.
771 * \param source Source string to be encoded.
772 * \returns encoded length; -1 if non success.
774 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
776 const char headerStr[] = "=?UTF-8?Q?";
777 int need_to_encode = 0;
781 if ((source == NULL) ||
785 while ((i < source->BufUsed) &&
786 (!IsEmptyStr (&source->buf[i])) &&
787 (need_to_encode == 0)) {
788 if (((unsigned char) source->buf[i] < 32) ||
789 ((unsigned char) source->buf[i] > 126)) {
795 if (!need_to_encode) {
796 if (*target == NULL) {
797 *target = NewStrBufPlain(source->buf, source->BufUsed);
800 FlushStrBuf(*target);
801 StrBufAppendBuf(*target, source, 0);
803 return (*target)->BufUsed;
806 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
807 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
808 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
809 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
810 (*target)->BufUsed = sizeof(headerStr) - 1;
811 for (i=0; (i < source->BufUsed); ++i) {
812 if ((*target)->BufUsed + 4 > (*target)->BufSize)
813 IncreaseBuf(*target, 1, 0);
814 ch = (unsigned char) source->buf[i];
815 if ((ch < 32) || (ch > 126) || (ch == 61)) {
816 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
817 (*target)->BufUsed += 3;
820 (*target)->buf[(*target)->BufUsed] = ch;
821 (*target)->BufUsed++;
825 if ((*target)->BufUsed + 4 > (*target)->BufSize)
826 IncreaseBuf(*target, 1, 0);
828 (*target)->buf[(*target)->BufUsed++] = '?';
829 (*target)->buf[(*target)->BufUsed++] = '=';
830 (*target)->buf[(*target)->BufUsed] = '\0';
831 return (*target)->BufUsed;;
834 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
839 for (i=0; i<buf->BufUsed; i++)
840 if (buf->buf[i] == search)
841 buf->buf[i] = replace;