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';
256 inline int StrBufNum_tokens(const StrBuf *source, char tok)
258 return num_tokens(source->buf, tok);
262 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
265 if (Offset > Source->BufUsed)
270 if (Offset + nChars < Source->BufUsed)
272 if (nChars > dest->BufSize)
273 IncreaseBuf(dest, 0, nChars + 1);
274 memcpy(dest->buf, Source->buf + Offset, nChars);
275 dest->BufUsed = nChars;
276 dest->buf[dest->BufUsed] = '\0';
279 NCharsRemain = Source->BufUsed - Offset;
280 if (NCharsRemain > dest->BufSize)
281 IncreaseBuf(dest, 0, NCharsRemain + 1);
282 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
283 dest->BufUsed = NCharsRemain;
284 dest->buf[dest->BufUsed] = '\0';
289 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
291 size_t nWritten = Buf->BufSize + 1;
292 size_t Offset = Buf->BufUsed;
293 size_t newused = Offset + nWritten;
295 while (newused >= Buf->BufSize) {
296 nWritten = vsnprintf(Buf->buf + Offset,
297 Buf->BufSize - Offset,
299 newused = Offset + nWritten;
300 if (newused >= Buf->BufSize)
301 IncreaseBuf(Buf, 1, 0);
303 Buf->BufUsed = Offset + nWritten ;
308 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
310 size_t nWritten = Buf->BufSize + 1;
311 size_t Offset = Buf->BufUsed;
312 size_t newused = Offset + nWritten;
315 while (newused >= Buf->BufSize) {
316 va_start(arg_ptr, format);
317 nWritten = vsnprintf(Buf->buf + Offset,
318 Buf->BufSize - Offset,
321 newused = Offset + nWritten;
322 if (newused >= Buf->BufSize)
323 IncreaseBuf(Buf, 1, 0);
325 Buf->BufUsed = Offset + nWritten ;
330 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
332 size_t nWritten = Buf->BufSize + 1;
335 while (nWritten >= Buf->BufSize) {
336 va_start(arg_ptr, format);
337 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
339 Buf->BufUsed = nWritten ;
340 if (nWritten >= Buf->BufSize)
341 IncreaseBuf(Buf, 0, 0);
347 * \brief a string tokenizer
348 * \param dest Destination StringBuffer
349 * \param Source StringBuffer to read into
350 * \param separator tokenizer param
351 * \returns -1 if not found, else length of token.
353 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
355 const char *s, *e; //* source * /
356 int len = 0; //* running total length of extracted string * /
357 int current_token = 0; //* token currently being processed * /
359 if ((Source == NULL) || (Source->BufUsed ==0)) {
363 e = s + Source->BufUsed;
369 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
373 while ((s<e) && !IsEmptyStr(s)) {
374 if (*s == separator) {
377 if (len >= dest->BufSize)
378 if (!IncreaseBuf(dest, 1, -1))
380 if ( (current_token == parmnum) &&
385 else if (current_token > parmnum) {
391 dest->buf[len] = '\0';
394 if (current_token < parmnum) {
395 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
398 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
404 * extract_int() - extract an int parm w/o supplying a buffer
406 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
415 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
422 * extract_long() - extract an long parm w/o supplying a buffer
424 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
433 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
441 * extract_unsigned_long() - extract an unsigned long parm
443 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
452 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 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 StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
472 slen = len = buf->BufUsed;
474 rlen = read(*fd, &buf->buf[len], 1);
476 *Error = strerror(errno);
483 if (buf->buf[len] == '\n')
485 if (buf->buf[len] != '\r')
487 if (!(len < buf->BufSize)) {
489 buf->buf[len+1] = '\0';
490 IncreaseBuf(buf, 1, -1);
494 buf->buf[len] = '\0';
499 * \brief Input binary data from socket
500 * \param buf the buffer to get the input to
501 * \param bytes the maximal number of bytes to read
503 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
511 if ((Buf == NULL) || (*fd == -1))
515 if (Buf->BufUsed + nBytes > Buf->BufSize)
516 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
518 ptr = Buf->buf + Buf->BufUsed;
520 slen = len = Buf->BufUsed;
522 fdflags = fcntl(*fd, F_GETFL);
524 while (nRead < nBytes) {
525 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
528 if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
529 *Error = strerror(errno);
534 if ((rlen = read(*fd,
536 nBytes - nRead)) == -1) {
539 *Error = strerror(errno);
543 Buf->BufUsed += rlen;
545 Buf->buf[Buf->BufUsed] = '\0';
549 void StrBufCutLeft(StrBuf *Buf, int nChars)
551 if (nChars >= Buf->BufUsed) {
555 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
556 Buf->BufUsed -= nChars;
557 Buf->buf[Buf->BufUsed] = '\0';
560 void StrBufCutRight(StrBuf *Buf, int nChars)
562 if (nChars >= Buf->BufUsed) {
566 Buf->BufUsed -= nChars;
567 Buf->buf[Buf->BufUsed] = '\0';
572 * string conversion function
574 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
582 if (source == NULL ||target == NULL)
587 len = source->BufUsed;
588 for (a = 0; a < len; ++a) {
589 if (target->BufUsed >= target->BufSize)
590 IncreaseBuf(target, 1, -1);
592 if (source->buf[a] == '=') {
593 hex[0] = source->buf[a + 1];
594 hex[1] = source->buf[a + 2];
597 sscanf(hex, "%02x", &b);
598 target->buf[target->BufUsed] = b;
599 target->buf[++target->BufUsed] = 0;
603 target->buf[target->BufUsed] = source->buf[a];
604 target->buf[++target->BufUsed] = 0;
611 * string conversion function
613 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
620 if (source == NULL ||target == NULL)
625 len = source->BufUsed;
626 for (i=0; i<len; ++i) {
627 if (target->BufUsed + 4 >= target->BufSize)
628 IncreaseBuf(target, 1, -1);
629 if ( (isalnum(source->buf[i])) ||
630 (source->buf[i]=='-') ||
631 (source->buf[i]=='_') ) {
632 target->buf[target->BufUsed++] = source->buf[i];
635 sprintf(&target->buf[target->BufUsed],
637 (0xFF &source->buf[i]));
638 target->BufUsed += 3;
641 target->buf[target->BufUsed + 1] = '\0';
645 * \brief uses the same calling syntax as compress2(), but it
646 * creates a stream compatible with HTTP "Content-encoding: gzip"
649 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
650 #define OS_CODE 0x03 /*< unix */
651 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
652 size_t * destLen, /*< length of the compresed data */
653 const Bytef * source, /*< source to encode */
654 uLong sourceLen, /*< length of source to encode */
655 int level) /*< compression level */
657 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
659 /* write gzip header */
660 snprintf((char *) dest, *destLen,
661 "%c%c%c%c%c%c%c%c%c%c",
662 gz_magic[0], gz_magic[1], Z_DEFLATED,
663 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
669 stream.next_in = (Bytef *) source;
670 stream.avail_in = (uInt) sourceLen;
671 stream.next_out = dest + 10L; // after header
672 stream.avail_out = (uInt) * destLen;
673 if ((uLong) stream.avail_out != *destLen)
676 stream.zalloc = (alloc_func) 0;
677 stream.zfree = (free_func) 0;
678 stream.opaque = (voidpf) 0;
680 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
681 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
685 err = deflate(&stream, Z_FINISH);
686 if (err != Z_STREAM_END) {
688 return err == Z_OK ? Z_BUF_ERROR : err;
690 *destLen = stream.total_out + 10L;
692 /* write CRC and Length */
693 uLong crc = crc32(0L, source, sourceLen);
695 for (n = 0; n < 4; ++n, ++*destLen) {
696 dest[*destLen] = (int) (crc & 0xff);
699 uLong len = stream.total_in;
700 for (n = 0; n < 4; ++n, ++*destLen) {
701 dest[*destLen] = (int) (len & 0xff);
704 err = deflateEnd(&stream);
711 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
713 int CompressBuffer(StrBuf *Buf)
716 char *compressed_data = NULL;
717 size_t compressed_len, bufsize;
719 bufsize = compressed_len = ((Buf->BufUsed * 101) / 100) + 100;
720 compressed_data = malloc(compressed_len);
722 if (compress_gzip((Bytef *) compressed_data,
725 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
728 Buf->buf = compressed_data;
729 Buf->BufUsed = compressed_len;
730 Buf->BufSize = bufsize;
733 free(compressed_data);
735 #endif /* HAVE_ZLIB */
739 int StrBufDecodeBase64(StrBuf *Buf)
743 if (Buf == NULL) return -1;
745 xferbuf = (char*) malloc(Buf->BufSize);
746 siz = CtdlDecodeBase64(xferbuf,
757 * remove escaped strings from i.e. the url string (like %20 for blanks)
759 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
765 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
766 Buf->buf[Buf->BufUsed - 1] = '\0';
771 while (a < Buf->BufUsed) {
772 if (Buf->buf[a] == '+')
774 else if (Buf->buf[a] == '%') {
775 /* don't let % chars through, rather truncate the input. */
776 if (a + 2 > Buf->BufUsed) {
781 hex[0] = Buf->buf[a + 1];
782 hex[1] = Buf->buf[a + 2];
785 sscanf(hex, "%02x", &b);
786 Buf->buf[a] = (char) b;
787 len = Buf->BufUsed - a - 2;
789 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
801 * \brief RFC2047-encode a header field if necessary.
802 * If no non-ASCII characters are found, the string
803 * will be copied verbatim without encoding.
805 * \param target Target buffer.
806 * \param source Source string to be encoded.
807 * \returns encoded length; -1 if non success.
809 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
811 const char headerStr[] = "=?UTF-8?Q?";
812 int need_to_encode = 0;
816 if ((source == NULL) ||
820 while ((i < source->BufUsed) &&
821 (!IsEmptyStr (&source->buf[i])) &&
822 (need_to_encode == 0)) {
823 if (((unsigned char) source->buf[i] < 32) ||
824 ((unsigned char) source->buf[i] > 126)) {
830 if (!need_to_encode) {
831 if (*target == NULL) {
832 *target = NewStrBufPlain(source->buf, source->BufUsed);
835 FlushStrBuf(*target);
836 StrBufAppendBuf(*target, source, 0);
838 return (*target)->BufUsed;
841 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
842 else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
843 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
844 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
845 (*target)->BufUsed = sizeof(headerStr) - 1;
846 for (i=0; (i < source->BufUsed); ++i) {
847 if ((*target)->BufUsed + 4 > (*target)->BufSize)
848 IncreaseBuf(*target, 1, 0);
849 ch = (unsigned char) source->buf[i];
850 if ((ch < 32) || (ch > 126) || (ch == 61)) {
851 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
852 (*target)->BufUsed += 3;
855 (*target)->buf[(*target)->BufUsed] = ch;
856 (*target)->BufUsed++;
860 if ((*target)->BufUsed + 4 > (*target)->BufSize)
861 IncreaseBuf(*target, 1, 0);
863 (*target)->buf[(*target)->BufUsed++] = '?';
864 (*target)->buf[(*target)->BufUsed++] = '=';
865 (*target)->buf[(*target)->BufUsed] = '\0';
866 return (*target)->BufUsed;;
869 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
874 for (i=0; i<buf->BufUsed; i++)
875 if (buf->buf[i] == search)
876 buf->buf[i] = replace;