8 #include <sys/select.h>
10 #include <sys/types.h>
11 #define SHOW_ME_VAPPEND_PRINTF
13 #include "libcitadel.h"
25 int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
26 const Bytef * source, uLong sourceLen, int level);
28 int BaseStrBufSize = 64;
30 const char *StrBufNOTNULL = ((char*) NULL) - 1;
32 const char HexList[256][3] = {
33 "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
34 "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
35 "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
36 "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
37 "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
38 "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
39 "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
40 "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
41 "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
42 "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
43 "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
44 "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
45 "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
46 "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
47 "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
48 "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
51 * @defgroup StrBuf Stringbuffer, A class for manipulating strings with dynamic buffers
52 * StrBuf is a versatile class, aiding the handling of dynamic strings
53 * * reduce de/reallocations
54 * * reduce the need to remeasure it
55 * * reduce scanning over the string (in @ref StrBuf_NextTokenizer "Tokenizers")
56 * * allow asyncroneous IO for line and Blob based operations
57 * * reduce the use of memove in those
58 * * Quick filling in several operations with append functions
62 * @defgroup StrBuf_DeConstructors Create/Destroy StrBufs
67 * @defgroup StrBuf_Cast Cast operators to interact with char* based code
69 * use these operators to interfere with code demanding char*;
70 * if you need to own the content, smash me. Avoid, since we loose the length information.
74 * @defgroup StrBuf_Filler Create/Replace/Append Content into a StrBuf
76 * operations to get your Strings into a StrBuf, manipulating them, or appending
79 * @defgroup StrBuf_NextTokenizer Fast tokenizer to pull tokens in sequence
81 * Quick tokenizer; demands of the user to pull its tokens in sequence
85 * @defgroup StrBuf_Tokenizer tokenizer Functions; Slow ones.
87 * versatile tokenizer; random access to tokens, but slower; Prefer the @ref StrBuf_NextTokenizer "Next Tokenizer"
91 * @defgroup StrBuf_BufferedIO Buffered IO with Asynchroneous reads and no unneeded memmoves (the fast ones)
93 * File IO to fill StrBufs; Works with work-buffer shared across several calls;
94 * External Cursor to maintain the current read position inside of the buffer
95 * the non-fast ones will use memove to keep the start of the buffer the read buffer (which is slower)
99 * @defgroup StrBuf_IO FileIO; Prefer @ref StrBuf_BufferedIO
105 * @defgroup StrBuf_DeEnCoder functions to translate the contents of a buffer
107 * these functions translate the content of a buffer into another representation;
108 * some are combined Fillers and encoders
112 * Private Structure for the Stringbuffer
115 char *buf; /**< the pointer to the dynamic buffer */
116 long BufSize; /**< how many spcae do we optain */
117 long BufUsed; /**< StNumber of Chars used excluding the trailing \\0 */
118 int ConstBuf; /**< are we just a wrapper arround a static buffer and musn't we be changed? */
120 long nIncreases; /**< for profiling; cound how many times we needed more */
121 char bt [SIZ]; /**< Stacktrace of last increase */
122 char bt_lastinc [SIZ]; /**< How much did we increase last time? */
127 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE);
128 static inline int Ctdl_IsUtf8SequenceStart(const char Char);
131 #ifdef HAVE_BACKTRACE
132 static void StrBufBacktrace(StrBuf *Buf, int which)
136 void *stack_frames[50];
141 pstart = pch = Buf->bt;
143 pstart = pch = Buf->bt_lastinc;
144 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
145 strings = backtrace_symbols(stack_frames, size);
146 for (i = 0; i < size; i++) {
148 n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
150 n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
159 void dbg_FreeStrBuf(StrBuf *FreeMe, char *FromWhere)
161 if (hFreeDbglog == -1){
162 pid_t pid = getpid();
164 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
165 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
167 if ((*FreeMe)->nIncreases > 0)
171 n = snprintf(buf, SIZ * 3, "%c+|%ld|%ld|%ld|%s|%s|\n",
173 (*FreeMe)->nIncreases,
177 (*FreeMe)->bt_lastinc);
178 n = write(hFreeDbglog, buf, n);
184 n = snprintf(buf, 128, "%c_|0|%ld%ld|\n",
188 n = write(hFreeDbglog, buf, n);
192 void dbg_IncreaseBuf(StrBuf *IncMe)
195 #ifdef HAVE_BACKTRACE
196 StrBufBacktrace(Buf, 1);
200 void dbg_Init(StrBuf *Buf)
204 Buf->bt_lastinc[0] = '\0';
205 #ifdef HAVE_BACKTRACE
206 StrBufBacktrace(Buf, 0);
212 #define dbg_FreeStrBuf(a, b)
213 #define dbg_IncreaseBuf(a)
219 * @ingroup StrBuf_Cast
220 * @brief Cast operator to Plain String
221 * @note if the buffer is altered by StrBuf operations, this pointer may become
222 * invalid. So don't lean on it after altering the buffer!
223 * Since this operation is considered cheap, rather call it often than risking
224 * your pointer to become invalid!
225 * @param Str the string we want to get the c-string representation for
226 * @returns the Pointer to the Content. Don't mess with it!
228 inline const char *ChrPtr(const StrBuf *Str)
236 * @ingroup StrBuf_Cast
237 * @brief since we know strlen()'s result, provide it here.
238 * @param Str the string to return the length to
239 * @returns contentlength of the buffer
241 inline int StrLength(const StrBuf *Str)
243 return (Str != NULL) ? Str->BufUsed : 0;
247 * @ingroup StrBuf_DeConstructors
248 * @brief local utility function to resize the buffer
249 * @param Buf the buffer whichs storage we should increase
250 * @param KeepOriginal should we copy the original buffer or just start over with a new one
251 * @param DestSize what should fit in after?
253 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
256 size_t NewSize = Buf->BufSize * 2;
262 while (NewSize <= DestSize)
265 NewBuf= (char*) malloc(NewSize);
269 if (KeepOriginal && (Buf->BufUsed > 0))
271 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
280 Buf->BufSize = NewSize;
282 dbg_IncreaseBuf(Buf);
288 * @ingroup StrBuf_DeConstructors
289 * @brief shrink an _EMPTY_ buffer if its Buffer superseeds threshhold to NewSize. Buffercontent is thoroughly ignored and flushed.
290 * @param Buf Buffer to shrink (has to be empty)
291 * @param ThreshHold if the buffer is bigger then this, its readjusted
292 * @param NewSize if we Shrink it, how big are we going to be afterwards?
294 void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize)
296 if ((Buf != NULL) && (Buf->BufUsed > ThreshHold)) {
298 Buf->buf = (char*) malloc(NewSize);
300 Buf->BufSize = NewSize;
305 * @ingroup StrBuf_DeConstructors
306 * @brief shrink long term buffers to their real size so they don't waste memory
307 * @param Buf buffer to shrink
308 * @param Force if not set, will just executed if the buffer is much to big; set for lifetime strings
309 * @returns physical size of the buffer
311 long StrBufShrinkToFit(StrBuf *Buf, int Force)
316 (Buf->BufUsed + (Buf->BufUsed / 3) > Buf->BufSize))
318 char *TmpBuf = (char*) malloc(Buf->BufUsed + 1);
319 memcpy (TmpBuf, Buf->buf, Buf->BufUsed + 1);
320 Buf->BufSize = Buf->BufUsed + 1;
328 * @ingroup StrBuf_DeConstructors
329 * @brief Allocate a new buffer with default buffer size
330 * @returns the new stringbuffer
332 StrBuf* NewStrBuf(void)
336 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
337 NewBuf->buf = (char*) malloc(BaseStrBufSize);
338 NewBuf->buf[0] = '\0';
339 NewBuf->BufSize = BaseStrBufSize;
341 NewBuf->ConstBuf = 0;
349 * @ingroup StrBuf_DeConstructors
350 * @brief Copy Constructor; returns a duplicate of CopyMe
351 * @param CopyMe Buffer to faxmilate
352 * @returns the new stringbuffer
354 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
361 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
362 NewBuf->buf = (char*) malloc(CopyMe->BufSize);
363 memcpy(NewBuf->buf, CopyMe->buf, CopyMe->BufUsed + 1);
364 NewBuf->BufUsed = CopyMe->BufUsed;
365 NewBuf->BufSize = CopyMe->BufSize;
366 NewBuf->ConstBuf = 0;
374 * @ingroup StrBuf_DeConstructors
375 * @brief create a new Buffer using an existing c-string
376 * this function should also be used if you want to pre-suggest
377 * the buffer size to allocate in conjunction with ptr == NULL
378 * @param ptr the c-string to copy; may be NULL to create a blank instance
379 * @param nChars How many chars should we copy; -1 if we should measure the length ourselves
380 * @returns the new stringbuffer
382 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
385 size_t Siz = BaseStrBufSize;
388 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
390 CopySize = strlen((ptr != NULL)?ptr:"");
394 while (Siz <= CopySize)
397 NewBuf->buf = (char*) malloc(Siz);
398 NewBuf->BufSize = Siz;
400 memcpy(NewBuf->buf, ptr, CopySize);
401 NewBuf->buf[CopySize] = '\0';
402 NewBuf->BufUsed = CopySize;
405 NewBuf->buf[0] = '\0';
408 NewBuf->ConstBuf = 0;
416 * @ingroup StrBuf_DeConstructors
417 * @brief Set an existing buffer from a c-string
418 * @param Buf buffer to load
419 * @param ptr c-string to put into
420 * @param nChars set to -1 if we should work 0-terminated
421 * @returns the new length of the string
423 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
425 size_t Siz = Buf->BufSize;
429 CopySize = strlen(ptr);
433 while (Siz <= CopySize)
436 if (Siz != Buf->BufSize)
437 IncreaseBuf(Buf, 0, Siz);
438 memcpy(Buf->buf, ptr, CopySize);
439 Buf->buf[CopySize] = '\0';
440 Buf->BufUsed = CopySize;
447 * @ingroup StrBuf_DeConstructors
448 * @brief use strbuf as wrapper for a string constant for easy handling
449 * @param StringConstant a string to wrap
450 * @param SizeOfStrConstant should be sizeof(StringConstant)-1
452 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
456 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
457 NewBuf->buf = (char*) StringConstant;
458 NewBuf->BufSize = SizeOfStrConstant;
459 NewBuf->BufUsed = SizeOfStrConstant;
460 NewBuf->ConstBuf = 1;
469 * @ingroup StrBuf_DeConstructors
470 * @brief flush the content of a Buf; keep its struct
471 * @param buf Buffer to flush
473 int FlushStrBuf(StrBuf *buf)
485 * @ingroup StrBuf_DeConstructors
486 * @brief wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
487 * @param buf Buffer to wipe
489 int FLUSHStrBuf(StrBuf *buf)
495 if (buf->BufUsed > 0) {
496 memset(buf->buf, 0, buf->BufUsed);
503 int hFreeDbglog = -1;
506 * @ingroup StrBuf_DeConstructors
507 * @brief Release a Buffer
508 * Its a double pointer, so it can NULL your pointer
509 * so fancy SIG11 appear instead of random results
510 * @param FreeMe Pointer Pointer to the buffer to free
512 void FreeStrBuf (StrBuf **FreeMe)
517 dbg_FreeStrBuf(FreeMe, 'F');
519 if (!(*FreeMe)->ConstBuf)
520 free((*FreeMe)->buf);
526 * @ingroup StrBuf_DeConstructors
527 * @brief flatten a Buffer to the Char * we return
528 * Its a double pointer, so it can NULL your pointer
529 * so fancy SIG11 appear instead of random results
530 * The Callee then owns the buffer and is responsible for freeing it.
531 * @param SmashMe Pointer Pointer to the buffer to release Buf from and free
532 * @returns the pointer of the buffer; Callee owns the memory thereafter.
534 char *SmashStrBuf (StrBuf **SmashMe)
538 if ((SmashMe == NULL) || (*SmashMe == NULL))
541 dbg_FreeStrBuf(SmashMe, 'S');
543 Ret = (*SmashMe)->buf;
550 * @ingroup StrBuf_DeConstructors
551 * @brief Release the buffer
552 * If you want put your StrBuf into a Hash, use this as Destructor.
553 * @param VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
555 void HFreeStrBuf (void *VFreeMe)
557 StrBuf *FreeMe = (StrBuf*)VFreeMe;
561 dbg_FreeStrBuf(SmashMe, 'H');
563 if (!FreeMe->ConstBuf)
569 /*******************************************************************************
570 * Simple string transformations *
571 *******************************************************************************/
575 * @brief Wrapper around atol
577 long StrTol(const StrBuf *Buf)
582 return atol(Buf->buf);
589 * @brief Wrapper around atoi
591 int StrToi(const StrBuf *Buf)
595 if (Buf->BufUsed > 0)
596 return atoi(Buf->buf);
603 * @brief Checks to see if the string is a pure number
604 * @param Buf The buffer to inspect
605 * @returns 1 if its a pure number, 0, if not.
607 int StrBufIsNumber(const StrBuf *Buf) {
609 if ((Buf == NULL) || (Buf->BufUsed == 0)) {
612 strtoll(Buf->buf, &pEnd, 10);
613 if (pEnd == Buf->buf)
615 if ((pEnd != NULL) && (pEnd == Buf->buf + Buf->BufUsed))
617 if (Buf->buf == pEnd)
623 * @brief modifies a Single char of the Buf
624 * You can point to it via char* or a zero-based integer
625 * @param Buf The buffer to manipulate
626 * @param ptr char* to zero; use NULL if unused
627 * @param nThChar zero based pointer into the string; use -1 if unused
628 * @param PeekValue The Character to place into the position
630 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
635 nThChar = ptr - Buf->buf;
636 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
638 Buf->buf[nThChar] = PeekValue;
644 * @brief Append a StringBuffer to the buffer
645 * @param Buf Buffer to modify
646 * @param AppendBuf Buffer to copy at the end of our buffer
647 * @param Offset Should we start copying from an offset?
649 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
651 if ((AppendBuf == NULL) || (Buf == NULL) || (AppendBuf->buf == NULL))
654 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1)
657 AppendBuf->BufUsed + Buf->BufUsed);
659 memcpy(Buf->buf + Buf->BufUsed,
660 AppendBuf->buf + Offset,
661 AppendBuf->BufUsed - Offset);
662 Buf->BufUsed += AppendBuf->BufUsed - Offset;
663 Buf->buf[Buf->BufUsed] = '\0';
669 * @brief Append a C-String to the buffer
670 * @param Buf Buffer to modify
671 * @param AppendBuf Buffer to copy at the end of our buffer
672 * @param AppendSize number of bytes to copy; set to -1 if we should count it in advance
673 * @param Offset Should we start copying from an offset?
675 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset)
678 long BufSizeRequired;
680 if ((AppendBuf == NULL) || (Buf == NULL))
684 aps = strlen(AppendBuf + Offset);
686 aps = AppendSize - Offset;
688 BufSizeRequired = Buf->BufUsed + aps + 1;
689 if (Buf->BufSize <= BufSizeRequired)
690 IncreaseBuf(Buf, (Buf->BufUsed > 0), BufSizeRequired);
692 memcpy(Buf->buf + Buf->BufUsed,
696 Buf->buf[Buf->BufUsed] = '\0';
701 * @brief sprintf like function appending the formated string to the buffer
702 * vsnprintf version to wrap into own calls
703 * @param Buf Buffer to extend by format and Params
704 * @param format printf alike format to add
705 * @param ap va_list containing the items for format
707 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
715 if ((Buf == NULL) || (format == NULL))
718 BufSize = Buf->BufSize;
719 nWritten = Buf->BufSize + 1;
720 Offset = Buf->BufUsed;
721 newused = Offset + nWritten;
723 while (newused >= BufSize) {
725 nWritten = vsnprintf(Buf->buf + Offset,
726 Buf->BufSize - Offset,
729 newused = Offset + nWritten;
730 if (newused >= Buf->BufSize) {
731 IncreaseBuf(Buf, 1, newused);
732 newused = Buf->BufSize + 1;
735 Buf->BufUsed = Offset + nWritten;
736 BufSize = Buf->BufSize;
744 * @brief sprintf like function appending the formated string to the buffer
745 * @param Buf Buffer to extend by format and Params
746 * @param format printf alike format to add
748 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
756 if ((Buf == NULL) || (format == NULL))
759 BufSize = Buf->BufSize;
760 nWritten = Buf->BufSize + 1;
761 Offset = Buf->BufUsed;
762 newused = Offset + nWritten;
764 while (newused >= BufSize) {
765 va_start(arg_ptr, format);
766 nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
767 Buf->BufSize - Buf->BufUsed,
770 newused = Buf->BufUsed + nWritten;
771 if (newused >= Buf->BufSize) {
772 IncreaseBuf(Buf, 1, newused);
773 newused = Buf->BufSize + 1;
776 Buf->BufUsed += nWritten;
777 BufSize = Buf->BufSize;
785 * @brief sprintf like function putting the formated string into the buffer
786 * @param Buf Buffer to extend by format and Parameters
787 * @param format printf alike format to add
789 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
794 if ((Buf == NULL) || (format == NULL))
797 nWritten = Buf->BufSize + 1;
798 while (nWritten >= Buf->BufSize) {
799 va_start(arg_ptr, format);
800 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
802 if (nWritten >= Buf->BufSize) {
803 IncreaseBuf(Buf, 0, 0);
804 nWritten = Buf->BufSize + 1;
807 Buf->BufUsed = nWritten ;
813 * @brief Callback for cURL to append the webserver reply to a buffer
814 * @param ptr pre-defined by the cURL API; see man 3 curl for mre info
815 * @param size pre-defined by the cURL API; see man 3 curl for mre info
816 * @param nmemb pre-defined by the cURL API; see man 3 curl for mre info
817 * @param stream pre-defined by the cURL API; see man 3 curl for mre info
819 size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
828 StrBufAppendBufPlain(Target, ptr, size * nmemb, 0);
835 * @brief extracts a substring from Source into dest
836 * @param dest buffer to place substring into
837 * @param Source string to copy substring from
838 * @param Offset chars to skip from start
839 * @param nChars number of chars to copy
840 * @returns the number of chars copied; may be different from nChars due to the size of Source
842 int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
845 if (Offset > Source->BufUsed)
850 if (Offset + nChars < Source->BufUsed)
852 if (nChars >= dest->BufSize)
853 IncreaseBuf(dest, 0, nChars + 1);
854 memcpy(dest->buf, Source->buf + Offset, nChars);
855 dest->BufUsed = nChars;
856 dest->buf[dest->BufUsed] = '\0';
859 NCharsRemain = Source->BufUsed - Offset;
860 if (NCharsRemain >= dest->BufSize)
861 IncreaseBuf(dest, 0, NCharsRemain + 1);
862 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
863 dest->BufUsed = NCharsRemain;
864 dest->buf[dest->BufUsed] = '\0';
870 * @brief Cut nChars from the start of the string
871 * @param Buf Buffer to modify
872 * @param nChars how many chars should be skipped?
874 void StrBufCutLeft(StrBuf *Buf, int nChars)
876 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
877 if (nChars >= Buf->BufUsed) {
881 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
882 Buf->BufUsed -= nChars;
883 Buf->buf[Buf->BufUsed] = '\0';
888 * @brief Cut the trailing n Chars from the string
889 * @param Buf Buffer to modify
890 * @param nChars how many chars should be trunkated?
892 void StrBufCutRight(StrBuf *Buf, int nChars)
894 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
895 if (nChars >= Buf->BufUsed) {
899 Buf->BufUsed -= nChars;
900 Buf->buf[Buf->BufUsed] = '\0';
905 * @brief Cut the string after n Chars
906 * @param Buf Buffer to modify
907 * @param AfternChars after how many chars should we trunkate the string?
908 * @param At if non-null and points inside of our string, cut it there.
910 void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
912 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
914 AfternChars = At - Buf->buf;
917 if ((AfternChars < 0) || (AfternChars >= Buf->BufUsed))
919 Buf->BufUsed = AfternChars;
920 Buf->buf[Buf->BufUsed] = '\0';
926 * @brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
927 * @param Buf the string to modify
929 void StrBufTrim(StrBuf *Buf)
932 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
934 while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
937 if (delta > 0) StrBufCutLeft(Buf, delta);
939 if (Buf->BufUsed == 0) return;
940 while (isspace(Buf->buf[Buf->BufUsed - 1])){
943 Buf->buf[Buf->BufUsed] = '\0';
948 * @brief uppercase the contents of a buffer
949 * @param Buf the buffer to translate
951 void StrBufUpCase(StrBuf *Buf)
955 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
958 pche = pch + Buf->BufUsed;
960 *pch = toupper(*pch);
968 * @brief lowercase the contents of a buffer
969 * @param Buf the buffer to translate
971 void StrBufLowerCase(StrBuf *Buf)
975 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
978 pche = pch + Buf->BufUsed;
980 *pch = tolower(*pch);
986 /*******************************************************************************
987 * a tokenizer that kills, maims, and destroys *
988 *******************************************************************************/
991 * @ingroup StrBuf_Tokenizer
992 * @brief Counts the numbmer of tokens in a buffer
993 * @param source String to count tokens in
994 * @param tok Tokenizer char to count
995 * @returns numbers of tokenizer chars found
997 int StrBufNum_tokens(const StrBuf *source, char tok)
1001 if ((source == NULL) || (source->BufUsed == 0))
1003 if ((source->BufUsed == 1) && (*source->buf == tok))
1007 pche = pch + source->BufUsed;
1018 * @ingroup StrBuf_Tokenizer
1019 * @brief a string tokenizer
1020 * @param Source StringBuffer to read into
1021 * @param parmnum n'th Parameter to remove
1022 * @param separator tokenizer character
1023 * @returns -1 if not found, else length of token.
1025 int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
1028 char *d, *s, *end; /* dest, source */
1031 /* Find desired @parameter */
1032 end = Source->buf + Source->BufUsed;
1034 while ((d <= end) &&
1037 /* End of string, bail! */
1042 if (*d == separator) {
1047 if ((d == NULL) || (d >= end))
1048 return 0; /* @Parameter not found */
1050 /* Find next @parameter */
1052 while ((s <= end) &&
1053 (*s && *s != separator))
1057 if (*s == separator)
1061 /* Hack and slash */
1066 memmove(d, s, Source->BufUsed - (s - Source->buf));
1067 Source->BufUsed += ReducedBy;
1068 Source->buf[Source->BufUsed] = '\0';
1070 else if (d == Source->buf) {
1072 Source->BufUsed = 0;
1076 Source->BufUsed += ReducedBy;
1089 * @ingroup StrBuf_Tokenizer
1090 * @brief a string tokenizer
1091 * @param dest Destination StringBuffer
1092 * @param Source StringBuffer to read into
1093 * @param parmnum n'th Parameter to extract
1094 * @param separator tokenizer character
1095 * @returns -1 if not found, else length of token.
1097 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
1099 const char *s, *e; //* source * /
1100 int len = 0; //* running total length of extracted string * /
1101 int current_token = 0; //* token currently being processed * /
1104 dest->buf[0] = '\0';
1110 if ((Source == NULL) || (Source->BufUsed ==0)) {
1114 e = s + Source->BufUsed;
1117 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1119 while ((s<e) && !IsEmptyStr(s)) {
1120 if (*s == separator) {
1123 if (len >= dest->BufSize) {
1124 dest->BufUsed = len;
1125 if (IncreaseBuf(dest, 1, -1) < 0) {
1130 if ( (current_token == parmnum) &&
1131 (*s != separator)) {
1132 dest->buf[len] = *s;
1135 else if (current_token > parmnum) {
1141 dest->buf[len] = '\0';
1142 dest->BufUsed = len;
1144 if (current_token < parmnum) {
1145 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
1148 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
1157 * @ingroup StrBuf_Tokenizer
1158 * @brief a string tokenizer to fetch an integer
1159 * @param Source String containing tokens
1160 * @param parmnum n'th Parameter to extract
1161 * @param separator tokenizer character
1162 * @returns 0 if not found, else integer representation of the token
1164 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
1174 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1181 * @ingroup StrBuf_Tokenizer
1182 * @brief a string tokenizer to fetch a long integer
1183 * @param Source String containing tokens
1184 * @param parmnum n'th Parameter to extract
1185 * @param separator tokenizer character
1186 * @returns 0 if not found, else long integer representation of the token
1188 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
1198 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1206 * @ingroup StrBuf_Tokenizer
1207 * @brief a string tokenizer to fetch an unsigned long
1208 * @param Source String containing tokens
1209 * @param parmnum n'th Parameter to extract
1210 * @param separator tokenizer character
1211 * @returns 0 if not found, else unsigned long representation of the token
1213 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
1224 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0) {
1228 return (unsigned long) atol(pnum);
1237 * @ingroup StrBuf_NextTokenizer
1238 * @brief a string tokenizer; Bounds checker
1239 * function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
1240 * @param Source our tokenbuffer
1241 * @param pStart the token iterator pointer to inspect
1242 * @returns whether the revolving pointer is inside of the search range
1244 int StrBufHaveNextToken(const StrBuf *Source, const char **pStart)
1246 if ((Source == NULL) ||
1247 (*pStart == StrBufNOTNULL) ||
1248 (Source->BufUsed == 0))
1252 if (*pStart == NULL)
1256 else if (*pStart > Source->buf + Source->BufUsed)
1260 else if (*pStart <= Source->buf)
1269 * @ingroup StrBuf_NextTokenizer
1270 * @brief a string tokenizer
1271 * @param dest Destination StringBuffer
1272 * @param Source StringBuffer to read into
1273 * @param pStart pointer to the end of the last token. Feed with NULL on start.
1274 * @param separator tokenizer
1275 * @returns -1 if not found, else length of token.
1277 int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
1279 const char *s; /* source */
1280 const char *EndBuffer; /* end stop of source buffer */
1281 int current_token = 0; /* token currently being processed */
1282 int len = 0; /* running total length of extracted string */
1284 if ((Source == NULL) ||
1285 (Source->BufUsed == 0) )
1287 *pStart = StrBufNOTNULL;
1291 EndBuffer = Source->buf + Source->BufUsed;
1295 dest->buf[0] = '\0';
1300 *pStart = EndBuffer + 1;
1304 if (*pStart == NULL)
1306 *pStart = Source->buf; /* we're starting to examine this buffer. */
1308 else if ((*pStart < Source->buf) ||
1309 (*pStart > EndBuffer ) )
1311 return -1; /* no more tokens to find. */
1315 /* start to find the next token */
1316 while ((s <= EndBuffer) &&
1317 (current_token == 0) )
1319 if (*s == separator)
1321 /* we found the next token */
1325 if (len >= dest->BufSize)
1327 /* our Dest-buffer isn't big enough, increase it. */
1328 dest->BufUsed = len;
1330 if (IncreaseBuf(dest, 1, -1) < 0) {
1331 /* WHUT? no more mem? bail out. */
1338 if ( (current_token == 0 ) && /* are we in our target token? */
1339 (!IsEmptyStr(s) ) &&
1340 (separator != *s) ) /* don't copy the token itself */
1342 dest->buf[len] = *s; /* Copy the payload */
1343 ++len; /* remember the bigger size. */
1349 /* did we reach the end? */
1350 if ((s > EndBuffer)) {
1351 EndBuffer = StrBufNOTNULL;
1352 *pStart = EndBuffer;
1355 *pStart = s; /* remember the position for the next run */
1358 /* sanitize our extracted token */
1359 dest->buf[len] = '\0';
1360 dest->BufUsed = len;
1367 * @ingroup StrBuf_NextTokenizer
1368 * @brief a string tokenizer
1369 * @param Source StringBuffer to read from
1370 * @param pStart pointer to the end of the last token. Feed with NULL.
1371 * @param separator tokenizer character
1372 * @param nTokens number of tokens to fastforward over
1373 * @returns -1 if not found, else length of token.
1375 int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens)
1377 const char *s, *EndBuffer; //* source * /
1378 int len = 0; //* running total length of extracted string * /
1379 int current_token = 0; //* token currently being processed * /
1381 if ((Source == NULL) ||
1382 (Source->BufUsed ==0)) {
1386 return Source->BufUsed;
1388 if (*pStart == NULL)
1389 *pStart = Source->buf;
1391 EndBuffer = Source->buf + Source->BufUsed;
1393 if ((*pStart < Source->buf) ||
1394 (*pStart > EndBuffer)) {
1402 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1404 while ((s<EndBuffer) && !IsEmptyStr(s)) {
1405 if (*s == separator) {
1408 if (current_token >= nTokens) {
1420 * @ingroup StrBuf_NextTokenizer
1421 * @brief a string tokenizer to fetch an integer
1422 * @param Source StringBuffer to read from
1423 * @param pStart Cursor on the tokenstring
1424 * @param separator tokenizer character
1425 * @returns 0 if not found, else integer representation of the token
1427 int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator)
1437 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1444 * @ingroup StrBuf_NextTokenizer
1445 * @brief a string tokenizer to fetch a long integer
1446 * @param Source StringBuffer to read from
1447 * @param pStart Cursor on the tokenstring
1448 * @param separator tokenizer character
1449 * @returns 0 if not found, else long integer representation of the token
1451 long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator)
1461 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1469 * @ingroup StrBuf_NextTokenizer
1470 * @brief a string tokenizer to fetch an unsigned long
1471 * @param Source StringBuffer to read from
1472 * @param pStart Cursor on the tokenstring
1473 * @param separator tokenizer character
1474 * @returns 0 if not found, else unsigned long representation of the token
1476 unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator)
1487 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) {
1491 return (unsigned long) atol(pnum);
1501 /*******************************************************************************
1502 * Escape Appending *
1503 *******************************************************************************/
1506 * @ingroup StrBuf_DeEnCoder
1507 * @brief Escape a string for feeding out as a URL while appending it to a Buffer
1508 * @param OutBuf the output buffer
1509 * @param In Buffer to encode
1510 * @param PlainIn way in from plain old c strings
1512 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1514 const char *pch, *pche;
1518 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1520 if (PlainIn != NULL) {
1521 len = strlen(PlainIn);
1527 pche = pch + In->BufUsed;
1534 pt = OutBuf->buf + OutBuf->BufUsed;
1535 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1537 while (pch < pche) {
1539 IncreaseBuf(OutBuf, 1, -1);
1540 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1541 pt = OutBuf->buf + OutBuf->BufUsed;
1544 if((*pch >= 'a' && *pch <= 'z') ||
1545 (*pch >= '@' && *pch <= 'Z') || /* @ A-Z */
1546 (*pch >= '0' && *pch <= ':') || /* 0-9 : */
1547 (*pch == '!') || (*pch == '_') ||
1548 (*pch == ',') || (*pch == '.') ||
1556 *(pt + 1) = HexList[(unsigned char)*pch][0];
1557 *(pt + 2) = HexList[(unsigned char)*pch][1];
1559 OutBuf->BufUsed += 3;
1567 * @ingroup StrBuf_DeEnCoder
1568 * @brief Append a string, escaping characters which have meaning in HTML.
1570 * @param Target target buffer
1571 * @param Source source buffer; set to NULL if you just have a C-String
1572 * @param PlainIn Plain-C string to append; set to NULL if unused
1573 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
1574 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
1575 * if set to 2, linebreaks are replaced by <br/>
1577 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
1579 const char *aptr, *eiptr;
1583 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1586 if (PlainIn != NULL) {
1588 len = strlen(PlainIn);
1593 eiptr = aptr + Source->BufUsed;
1594 len = Source->BufUsed;
1600 bptr = Target->buf + Target->BufUsed;
1601 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1603 while (aptr < eiptr){
1605 IncreaseBuf(Target, 1, -1);
1606 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1607 bptr = Target->buf + Target->BufUsed;
1610 memcpy(bptr, "<", 4);
1612 Target->BufUsed += 4;
1614 else if (*aptr == '>') {
1615 memcpy(bptr, ">", 4);
1617 Target->BufUsed += 4;
1619 else if (*aptr == '&') {
1620 memcpy(bptr, "&", 5);
1622 Target->BufUsed += 5;
1624 else if (*aptr == '"') {
1625 memcpy(bptr, """, 6);
1627 Target->BufUsed += 6;
1629 else if (*aptr == '\'') {
1630 memcpy(bptr, "'", 5);
1632 Target->BufUsed += 5;
1634 else if (*aptr == LB) {
1639 else if (*aptr == RB) {
1644 else if (*aptr == QU) {
1649 else if ((*aptr == 32) && (nbsp == 1)) {
1650 memcpy(bptr, " ", 6);
1652 Target->BufUsed += 6;
1654 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
1655 *bptr='\0'; /* nothing */
1657 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
1658 memcpy(bptr, "<br/>", 11);
1660 Target->BufUsed += 11;
1664 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
1665 *bptr='\0'; /* nothing */
1675 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
1677 return Target->BufUsed;
1681 * @ingroup StrBuf_DeEnCoder
1682 * @brief Append a string, escaping characters which have meaning in HTML.
1683 * Converts linebreaks into blanks; escapes single quotes
1684 * @param Target target buffer
1685 * @param Source source buffer; set to NULL if you just have a C-String
1686 * @param PlainIn Plain-C string to append; set to NULL if unused
1688 void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1690 const char *aptr, *eiptr;
1694 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1697 if (PlainIn != NULL) {
1699 len = strlen(PlainIn);
1704 eiptr = aptr + Source->BufUsed;
1705 len = Source->BufUsed;
1711 eptr = Target->buf + Target->BufSize - 8;
1712 tptr = Target->buf + Target->BufUsed;
1714 while (aptr < eiptr){
1716 IncreaseBuf(Target, 1, -1);
1717 eptr = Target->buf + Target->BufSize - 8;
1718 tptr = Target->buf + Target->BufUsed;
1721 if (*aptr == '\n') {
1725 else if (*aptr == '\r') {
1729 else if (*aptr == '\'') {
1735 Target->BufUsed += 5;
1748 * @ingroup StrBuf_DeEnCoder
1749 * @brief Append a string, escaping characters which have meaning in ICAL.
1751 * @param Target target buffer
1752 * @param Source source buffer; set to NULL if you just have a C-String
1753 * @param PlainIn Plain-C string to append; set to NULL if unused
1755 void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1757 const char *aptr, *eiptr;
1761 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1764 if (PlainIn != NULL) {
1766 len = strlen(PlainIn);
1771 eiptr = aptr + Source->BufUsed;
1772 len = Source->BufUsed;
1778 eptr = Target->buf + Target->BufSize - 8;
1779 tptr = Target->buf + Target->BufUsed;
1781 while (aptr < eiptr){
1782 if(tptr + 3 >= eptr) {
1783 IncreaseBuf(Target, 1, -1);
1784 eptr = Target->buf + Target->BufSize - 8;
1785 tptr = Target->buf + Target->BufUsed;
1788 if (*aptr == '\n') {
1795 else if (*aptr == '\r') {
1802 else if (*aptr == ',') {
1818 * @ingroup StrBuf_DeEnCoder
1819 * @brief Append a string, escaping characters which have meaning in JavaScript strings .
1821 * @param Target target buffer
1822 * @param Source source buffer; set to NULL if you just have a C-String
1823 * @param PlainIn Plain-C string to append; set to NULL if unused
1824 * @returns size of result or -1
1826 long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
1828 const char *aptr, *eiptr;
1832 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1835 if (PlainIn != NULL) {
1837 len = strlen(PlainIn);
1842 eiptr = aptr + Source->BufUsed;
1843 len = Source->BufUsed;
1849 bptr = Target->buf + Target->BufUsed;
1850 eptr = Target->buf + Target->BufSize - 3; /* our biggest unit to put in... */
1852 while (aptr < eiptr){
1854 IncreaseBuf(Target, 1, -1);
1855 eptr = Target->buf + Target->BufSize - 3;
1856 bptr = Target->buf + Target->BufUsed;
1863 Target->BufUsed += 2;
1864 } else if (*aptr == '\\') {
1869 Target->BufUsed += 2;
1879 if ((bptr == eptr - 1 ) && !IsEmptyStr(aptr) )
1881 return Target->BufUsed;
1885 * @ingroup StrBuf_DeEnCoder
1886 * @brief Append a string, escaping characters which have meaning in HTML + json.
1888 * @param Target target buffer
1889 * @param Source source buffer; set to NULL if you just have a C-String
1890 * @param PlainIn Plain-C string to append; set to NULL if unused
1891 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
1892 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
1893 * if set to 2, linebreaks are replaced by <br/>
1895 long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
1897 const char *aptr, *eiptr;
1900 int IsUtf8Sequence = 0;
1902 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
1905 if (PlainIn != NULL) {
1907 len = strlen(PlainIn);
1912 eiptr = aptr + Source->BufUsed;
1913 len = Source->BufUsed;
1919 bptr = Target->buf + Target->BufUsed;
1920 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1922 while (aptr < eiptr){
1924 IncreaseBuf(Target, 1, -1);
1925 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1926 bptr = Target->buf + Target->BufUsed;
1929 memcpy(bptr, "<", 4);
1931 Target->BufUsed += 4;
1933 else if (*aptr == '>') {
1934 memcpy(bptr, ">", 4);
1936 Target->BufUsed += 4;
1938 else if (*aptr == '&') {
1939 memcpy(bptr, "&", 5);
1941 Target->BufUsed += 5;
1943 else if (*aptr == LB) {
1948 else if (*aptr == RB) {
1953 else if ((*aptr == 32) && (nbsp == 1)) {
1954 memcpy(bptr, " ", 6);
1956 Target->BufUsed += 6;
1958 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
1959 *bptr='\0'; /* nothing */
1961 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
1962 memcpy(bptr, "<br/>", 11);
1964 Target->BufUsed += 11;
1967 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
1968 *bptr='\0'; /* nothing */
1971 else if ((*aptr == '"') || (*aptr == QU)) {
1976 Target->BufUsed += 2;
1977 } else if (*aptr == '\\') {
1982 Target->BufUsed += 2;
1985 if (((unsigned char)*aptr) >= 0x20)
1987 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
1991 while (IsUtf8Sequence > 1){
1992 if(bptr + IsUtf8Sequence >= eptr) {
1993 IncreaseBuf(Target, 1, -1);
1994 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
1995 bptr = Target->buf + Target->BufUsed - 1;
2009 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
2011 return Target->BufUsed;
2015 * @ingroup StrBuf_DeEnCoder
2016 * @brief unhide special chars hidden to the HTML escaper
2017 * @param target buffer to put the unescaped string in
2018 * @param source buffer to unescape
2020 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
2026 FlushStrBuf(target);
2028 if (source == NULL ||target == NULL)
2033 len = source->BufUsed;
2034 for (a = 0; a < len; ++a) {
2035 if (target->BufUsed >= target->BufSize)
2036 IncreaseBuf(target, 1, -1);
2038 if (source->buf[a] == '=') {
2039 hex[0] = source->buf[a + 1];
2040 hex[1] = source->buf[a + 2];
2043 sscanf(hex, "%02x", &b);
2044 target->buf[target->BufUsed] = b;
2045 target->buf[++target->BufUsed] = 0;
2049 target->buf[target->BufUsed] = source->buf[a];
2050 target->buf[++target->BufUsed] = 0;
2057 * @ingroup StrBuf_DeEnCoder
2058 * @brief hide special chars from the HTML escapers and friends
2059 * @param target buffer to put the escaped string in
2060 * @param source buffer to escape
2062 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
2067 FlushStrBuf(target);
2069 if (source == NULL ||target == NULL)
2074 len = source->BufUsed;
2075 for (i=0; i<len; ++i) {
2076 if (target->BufUsed + 4 >= target->BufSize)
2077 IncreaseBuf(target, 1, -1);
2078 if ( (isalnum(source->buf[i])) ||
2079 (source->buf[i]=='-') ||
2080 (source->buf[i]=='_') ) {
2081 target->buf[target->BufUsed++] = source->buf[i];
2084 sprintf(&target->buf[target->BufUsed],
2086 (0xFF &source->buf[i]));
2087 target->BufUsed += 3;
2090 target->buf[target->BufUsed + 1] = '\0';
2094 /*******************************************************************************
2095 * Quoted Printable de/encoding *
2096 *******************************************************************************/
2099 * @ingroup StrBuf_DeEnCoder
2100 * @brief decode a buffer from base 64 encoding; destroys original
2101 * @param Buf Buffor to transform
2103 int StrBufDecodeBase64(StrBuf *Buf)
2107 if (Buf == NULL) return -1;
2109 xferbuf = (char*) malloc(Buf->BufSize);
2110 siz = CtdlDecodeBase64(xferbuf,
2120 * @ingroup StrBuf_DeEnCoder
2121 * @brief decode a buffer from base 64 encoding; destroys original
2122 * @param Buf Buffor to transform
2124 int StrBufDecodeHex(StrBuf *Buf)
2127 char *pch, *pche, *pchi;
2129 if (Buf == NULL) return -1;
2131 pch = pchi = Buf->buf;
2132 pche = pch + Buf->BufUsed;
2134 while (pchi < pche){
2135 ch = decode_hex(pchi);
2142 Buf->BufUsed = pch - Buf->buf;
2143 return Buf->BufUsed;
2147 * @ingroup StrBuf_DeEnCoder
2148 * @brief replace all chars >0x20 && < 0x7F with Mute
2149 * @param Mute char to put over invalid chars
2150 * @param Buf Buffor to transform
2152 int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
2156 if (Buf == NULL) return -1;
2157 pch = (unsigned char *)Buf->buf;
2158 while (pch < (unsigned char *)Buf->buf + Buf->BufUsed) {
2159 if ((*pch < 0x20) || (*pch > 0x7F))
2163 return Buf->BufUsed;
2168 * @ingroup StrBuf_DeEnCoder
2169 * @brief remove escaped strings from i.e. the url string (like %20 for blanks)
2170 * @param Buf Buffer to translate
2171 * @param StripBlanks Reduce several blanks to one?
2173 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
2182 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
2183 Buf->buf[Buf->BufUsed - 1] = '\0';
2188 while (a < Buf->BufUsed) {
2189 if (Buf->buf[a] == '+')
2191 else if (Buf->buf[a] == '%') {
2192 /* don't let % chars through, rather truncate the input. */
2193 if (a + 2 > Buf->BufUsed) {
2198 hex[0] = Buf->buf[a + 1];
2199 hex[1] = Buf->buf[a + 2];
2202 sscanf(hex, "%02x", &b);
2203 Buf->buf[a] = (char) b;
2204 len = Buf->BufUsed - a - 2;
2206 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
2218 * @ingroup StrBuf_DeEnCoder
2219 * @brief RFC2047-encode a header field if necessary.
2220 * If no non-ASCII characters are found, the string
2221 * will be copied verbatim without encoding.
2223 * @param target Target buffer.
2224 * @param source Source string to be encoded.
2225 * @returns encoded length; -1 if non success.
2227 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
2229 const char headerStr[] = "=?UTF-8?Q?";
2230 int need_to_encode = 0;
2234 if ((source == NULL) ||
2238 while ((i < source->BufUsed) &&
2239 (!IsEmptyStr (&source->buf[i])) &&
2240 (need_to_encode == 0)) {
2241 if (((unsigned char) source->buf[i] < 32) ||
2242 ((unsigned char) source->buf[i] > 126)) {
2248 if (!need_to_encode) {
2249 if (*target == NULL) {
2250 *target = NewStrBufPlain(source->buf, source->BufUsed);
2253 FlushStrBuf(*target);
2254 StrBufAppendBuf(*target, source, 0);
2256 return (*target)->BufUsed;
2258 if (*target == NULL)
2259 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
2260 else if (sizeof(headerStr) + source->BufUsed >= (*target)->BufSize)
2261 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
2262 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
2263 (*target)->BufUsed = sizeof(headerStr) - 1;
2264 for (i=0; (i < source->BufUsed); ++i) {
2265 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2266 IncreaseBuf(*target, 1, 0);
2267 ch = (unsigned char) source->buf[i];
2268 if ((ch < 32) || (ch > 126) || (ch == 61)) {
2269 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
2270 (*target)->BufUsed += 3;
2273 (*target)->buf[(*target)->BufUsed] = ch;
2274 (*target)->BufUsed++;
2278 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2279 IncreaseBuf(*target, 1, 0);
2281 (*target)->buf[(*target)->BufUsed++] = '?';
2282 (*target)->buf[(*target)->BufUsed++] = '=';
2283 (*target)->buf[(*target)->BufUsed] = '\0';
2284 return (*target)->BufUsed;;
2289 * @brief replaces all occurances of 'search' by 'replace'
2290 * @param buf Buffer to modify
2291 * @param search character to search
2292 * @param replace character to replace search by
2294 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
2299 for (i=0; i<buf->BufUsed; i++)
2300 if (buf->buf[i] == search)
2301 buf->buf[i] = replace;
2306 /*******************************************************************************
2307 * Iconv Wrapper; RFC822 de/encoding *
2308 *******************************************************************************/
2311 * @ingroup StrBuf_DeEnCoder
2312 * @brief Wrapper around iconv_open()
2313 * Our version adds aliases for non-standard Microsoft charsets
2314 * such as 'MS950', aliasing them to names like 'CP950'
2316 * @param tocode Target encoding
2317 * @param fromcode Source encoding
2318 * @param pic anonimized pointer to iconv struct
2320 void ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
2323 iconv_t ic = (iconv_t)(-1) ;
2324 ic = iconv_open(tocode, fromcode);
2325 if (ic == (iconv_t)(-1) ) {
2326 char alias_fromcode[64];
2327 if ( (strlen(fromcode) == 5) && (!strncasecmp(fromcode, "MS", 2)) ) {
2328 safestrncpy(alias_fromcode, fromcode, sizeof alias_fromcode);
2329 alias_fromcode[0] = 'C';
2330 alias_fromcode[1] = 'P';
2331 ic = iconv_open(tocode, alias_fromcode);
2334 *(iconv_t *)pic = ic;
2340 * @ingroup StrBuf_DeEnCoder
2341 * @brief find one chunk of a RFC822 encoded string
2342 * @param Buffer where to search
2343 * @param bptr where to start searching
2344 * @returns found position, NULL if none.
2346 static inline char *FindNextEnd (const StrBuf *Buf, char *bptr)
2349 /* Find the next ?Q? */
2350 if (Buf->BufUsed - (bptr - Buf->buf) < 6)
2353 end = strchr(bptr + 2, '?');
2358 if ((Buf->BufUsed - (end - Buf->buf) > 3) &&
2359 ((*(end + 1) == 'B') || (*(end + 1) == 'Q')) &&
2360 (*(end + 2) == '?')) {
2361 /* skip on to the end of the cluster, the next ?= */
2362 end = strstr(end + 3, "?=");
2365 /* sort of half valid encoding, try to find an end. */
2366 end = strstr(bptr, "?=");
2372 * @brief swaps the contents of two StrBufs
2373 * this is to be used to have cheap switched between a work-buffer and a target buffer
2374 * @param A First one
2375 * @param B second one
2377 static inline void SwapBuffers(StrBuf *A, StrBuf *B)
2381 memcpy(&C, A, sizeof(*A));
2382 memcpy(A, B, sizeof(*B));
2383 memcpy(B, &C, sizeof(C));
2389 * @ingroup StrBuf_DeEnCoder
2390 * @brief convert one buffer according to the preselected iconv pointer PIC
2391 * @param ConvertBuf buffer we need to translate
2392 * @param TmpBuf To share a workbuffer over several iterations. prepare to have it filled with useless stuff afterwards.
2393 * @param pic Pointer to the iconv-session Object
2395 void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
2401 char *ibuf; /**< Buffer of characters to be converted */
2402 char *obuf; /**< Buffer for converted characters */
2403 size_t ibuflen; /**< Length of input buffer */
2404 size_t obuflen; /**< Length of output buffer */
2407 /* since we're converting to utf-8, one glyph may take up to 6 bytes */
2408 if (ConvertBuf->BufUsed * 6 >= TmpBuf->BufSize)
2409 IncreaseBuf(TmpBuf, 0, ConvertBuf->BufUsed * 6);
2411 ic = *(iconv_t*)pic;
2412 ibuf = ConvertBuf->buf;
2413 ibuflen = ConvertBuf->BufUsed;
2415 obuflen = TmpBuf->BufSize;
2417 siz = iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
2420 if (errno == E2BIG) {
2422 IncreaseBuf(TmpBuf, 0, 0);
2427 else if (errno == EILSEQ){
2428 /* hm, invalid utf8 sequence... what to do now? */
2429 /* An invalid multibyte sequence has been encountered in the input */
2431 else if (errno == EINVAL) {
2432 /* An incomplete multibyte sequence has been encountered in the input. */
2435 FlushStrBuf(TmpBuf);
2438 TmpBuf->BufUsed = TmpBuf->BufSize - obuflen;
2439 TmpBuf->buf[TmpBuf->BufUsed] = '\0';
2441 /* little card game: wheres the red lady? */
2442 SwapBuffers(ConvertBuf, TmpBuf);
2443 FlushStrBuf(TmpBuf);
2450 * @ingroup StrBuf_DeEnCoder
2451 * @brief catches one RFC822 encoded segment, and decodes it.
2452 * @param Target buffer to fill with result
2453 * @param DecodeMe buffer with stuff to process
2454 * @param SegmentStart points to our current segment in DecodeMe
2455 * @param SegmentEnd Points to the end of our current segment in DecodeMe
2456 * @param ConvertBuf Workbuffer shared between several iterations. Random content; needs to be valid
2457 * @param ConvertBuf2 Workbuffer shared between several iterations. Random content; needs to be valid
2458 * @param FoundCharset Characterset to default decoding to; if we find another we will overwrite it.
2460 inline static void DecodeSegment(StrBuf *Target,
2461 const StrBuf *DecodeMe,
2465 StrBuf *ConvertBuf2,
2466 StrBuf *FoundCharset)
2472 iconv_t ic = (iconv_t)(-1);
2476 /* Now we handle foreign character sets properly encoded
2477 * in RFC2047 format.
2479 StaticBuf.buf = SegmentStart;
2480 StaticBuf.BufUsed = SegmentEnd - SegmentStart;
2481 StaticBuf.BufSize = DecodeMe->BufSize - (SegmentStart - DecodeMe->buf);
2482 extract_token(charset, SegmentStart, 1, '?', sizeof charset);
2483 if (FoundCharset != NULL) {
2484 FlushStrBuf(FoundCharset);
2485 StrBufAppendBufPlain(FoundCharset, charset, -1, 0);
2487 extract_token(encoding, SegmentStart, 2, '?', sizeof encoding);
2488 StrBufExtract_token(ConvertBuf, &StaticBuf, 3, '?');
2490 *encoding = toupper(*encoding);
2491 if (*encoding == 'B') { /**< base64 */
2492 ConvertBuf2->BufUsed = CtdlDecodeBase64(ConvertBuf2->buf,
2494 ConvertBuf->BufUsed);
2496 else if (*encoding == 'Q') { /**< quoted-printable */
2500 while (pos < ConvertBuf->BufUsed)
2502 if (ConvertBuf->buf[pos] == '_')
2503 ConvertBuf->buf[pos] = ' ';
2507 ConvertBuf2->BufUsed = CtdlDecodeQuotedPrintable(
2510 ConvertBuf->BufUsed);
2513 StrBufAppendBuf(ConvertBuf2, ConvertBuf, 0);
2516 ctdl_iconv_open("UTF-8", charset, &ic);
2517 if (ic != (iconv_t)(-1) ) {
2519 StrBufConvert(ConvertBuf2, ConvertBuf, &ic);
2520 StrBufAppendBuf(Target, ConvertBuf2, 0);
2525 StrBufAppendBufPlain(Target, HKEY("(unreadable)"), 0);
2531 * @ingroup StrBuf_DeEnCoder
2532 * @brief Handle subjects with RFC2047 encoding such as:
2533 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
2534 * @param Target where to put the decoded string to
2535 * @param DecodeMe buffer with encoded string
2536 * @param DefaultCharset if we don't find one, which should we use?
2537 * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string,
2538 * put it here for later use where no string might be known.
2540 void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset)
2542 StrBuf *DecodedInvalidBuf = NULL;
2543 StrBuf *ConvertBuf, *ConvertBuf2;
2544 const StrBuf *DecodeMee = DecodeMe;
2545 char *start, *end, *next, *nextend, *ptr = NULL;
2547 iconv_t ic = (iconv_t)(-1) ;
2552 int illegal_non_rfc2047_encoding = 0;
2554 /* Sometimes, badly formed messages contain strings which were simply
2555 * written out directly in some foreign character set instead of
2556 * using RFC2047 encoding. This is illegal but we will attempt to
2557 * handle it anyway by converting from a user-specified default
2558 * charset to UTF-8 if we see any nonprintable characters.
2561 len = StrLength(DecodeMe);
2562 for (i=0; i<DecodeMe->BufUsed; ++i) {
2563 if ((DecodeMe->buf[i] < 32) || (DecodeMe->buf[i] > 126)) {
2564 illegal_non_rfc2047_encoding = 1;
2569 ConvertBuf = NewStrBufPlain(NULL, StrLength(DecodeMe));
2570 if ((illegal_non_rfc2047_encoding) &&
2571 (strcasecmp(ChrPtr(DefaultCharset), "UTF-8")) &&
2572 (strcasecmp(ChrPtr(DefaultCharset), "us-ascii")) )
2575 ctdl_iconv_open("UTF-8", ChrPtr(DefaultCharset), &ic);
2576 if (ic != (iconv_t)(-1) ) {
2577 DecodedInvalidBuf = NewStrBufDup(DecodeMe);
2578 StrBufConvert(DecodedInvalidBuf, ConvertBuf, &ic);///TODO: don't void const?
2579 DecodeMee = DecodedInvalidBuf;
2585 /* pre evaluate the first pair */
2586 nextend = end = NULL;
2587 len = StrLength(DecodeMee);
2588 start = strstr(DecodeMee->buf, "=?");
2589 eptr = DecodeMee->buf + DecodeMee->BufUsed;
2591 end = FindNextEnd (DecodeMee, start);
2593 StrBufAppendBuf(Target, DecodeMee, 0);
2594 FreeStrBuf(&ConvertBuf);
2595 FreeStrBuf(&DecodedInvalidBuf);
2599 ConvertBuf2 = NewStrBufPlain(NULL, StrLength(DecodeMee));
2601 if (start != DecodeMee->buf) {
2604 nFront = start - DecodeMee->buf;
2605 StrBufAppendBufPlain(Target, DecodeMee->buf, nFront, 0);
2609 * Since spammers will go to all sorts of absurd lengths to get their
2610 * messages through, there are LOTS of corrupt headers out there.
2611 * So, prevent a really badly formed RFC2047 header from throwing
2612 * this function into an infinite loop.
2614 while ((start != NULL) &&
2621 DecodeSegment(Target,
2629 next = strstr(end, "=?");
2631 if ((next != NULL) &&
2633 nextend = FindNextEnd(DecodeMee, next);
2634 if (nextend == NULL)
2637 /* did we find two partitions */
2638 if ((next != NULL) &&
2642 while ((ptr < next) &&
2648 /* did we find a gab just filled with blanks? */
2651 long gap = next - start;
2656 /* now terminate the gab at the end */
2657 delta = (next - end) - 2; ////TODO: const!
2658 ((StrBuf*)DecodeMee)->BufUsed -= delta;
2659 ((StrBuf*)DecodeMee)->buf[DecodeMee->BufUsed] = '\0';
2661 /* move next to its new location. */
2666 /* our next-pair is our new first pair now. */
2672 nextend = DecodeMee->buf + DecodeMee->BufUsed;
2673 if ((end != NULL) && (end < nextend)) {
2675 while ( (ptr < nextend) &&
2682 StrBufAppendBufPlain(Target, end, nextend - end, 0);
2684 FreeStrBuf(&ConvertBuf);
2685 FreeStrBuf(&ConvertBuf2);
2686 FreeStrBuf(&DecodedInvalidBuf);
2689 /*******************************************************************************
2690 * Manipulating UTF-8 Strings *
2691 *******************************************************************************/
2695 * @brief evaluate the length of an utf8 special character sequence
2696 * @param Char the character to examine
2697 * @returns width of utf8 chars in bytes
2699 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
2704 while ((n < 8) && ((test & *CharS) != 0)) {
2708 if ((n > 6) || ((CharE - CharS) < n))
2715 * @brief detect whether this char starts an utf-8 encoded char
2716 * @param Char character to inspect
2717 * @returns yes or no
2719 static inline int Ctdl_IsUtf8SequenceStart(const char Char)
2721 /** 11??.???? indicates an UTF8 Sequence. */
2722 return ((Char & 0xC0) != 0);
2727 * @brief measure the number of glyphs in an UTF8 string...
2728 * @param Buf string to measure
2729 * @returns the number of glyphs in Buf
2731 long StrBuf_Utf8StrLen(StrBuf *Buf)
2737 if ((Buf == NULL) || (Buf->BufUsed == 0))
2740 eptr = Buf->buf + Buf->BufUsed;
2741 while ((aptr < eptr) && (*aptr != '\0')) {
2742 if (Ctdl_IsUtf8SequenceStart(*aptr)){
2743 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
2744 while ((aptr < eptr) && (*aptr++ != '\0')&& (m-- > 0) );
2757 * @brief cuts a string after maxlen glyphs
2758 * @param Buf string to cut to maxlen glyphs
2759 * @param maxlen how long may the string become?
2760 * @returns current length of the string
2762 long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
2768 eptr = Buf->buf + Buf->BufUsed;
2769 while ((aptr < eptr) && (*aptr != '\0')) {
2770 if (Ctdl_IsUtf8SequenceStart(*aptr)){
2771 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
2772 while ((*aptr++ != '\0') && (m-- > 0));
2781 Buf->BufUsed = aptr - Buf->buf;
2782 return Buf->BufUsed;
2785 return Buf->BufUsed;
2793 /*******************************************************************************
2795 *******************************************************************************/
2798 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
2799 #define OS_CODE 0x03 /*< unix */
2802 * @ingroup StrBuf_DeEnCoder
2803 * @brief uses the same calling syntax as compress2(), but it
2804 * creates a stream compatible with HTTP "Content-encoding: gzip"
2805 * @param dest compressed buffer
2806 * @param destLen length of the compresed data
2807 * @param source source to encode
2808 * @param sourceLen length of source to encode
2809 * @param level compression level
2811 int ZEXPORT compress_gzip(Bytef * dest,
2813 const Bytef * source,
2817 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
2819 /* write gzip header */
2820 snprintf((char *) dest, *destLen,
2821 "%c%c%c%c%c%c%c%c%c%c",
2822 gz_magic[0], gz_magic[1], Z_DEFLATED,
2823 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
2826 /* normal deflate */
2829 stream.next_in = (Bytef *) source;
2830 stream.avail_in = (uInt) sourceLen;
2831 stream.next_out = dest + 10L; // after header
2832 stream.avail_out = (uInt) * destLen;
2833 if ((uLong) stream.avail_out != *destLen)
2836 stream.zalloc = (alloc_func) 0;
2837 stream.zfree = (free_func) 0;
2838 stream.opaque = (voidpf) 0;
2840 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
2841 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
2845 err = deflate(&stream, Z_FINISH);
2846 if (err != Z_STREAM_END) {
2847 deflateEnd(&stream);
2848 return err == Z_OK ? Z_BUF_ERROR : err;
2850 *destLen = stream.total_out + 10L;
2852 /* write CRC and Length */
2853 uLong crc = crc32(0L, source, sourceLen);
2855 for (n = 0; n < 4; ++n, ++*destLen) {
2856 dest[*destLen] = (int) (crc & 0xff);
2859 uLong len = stream.total_in;
2860 for (n = 0; n < 4; ++n, ++*destLen) {
2861 dest[*destLen] = (int) (len & 0xff);
2864 err = deflateEnd(&stream);
2871 * @ingroup StrBuf_DeEnCoder
2872 * @brief compress the buffer with gzip
2873 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
2874 * @param Buf buffer whose content is to be gzipped
2876 int CompressBuffer(StrBuf *Buf)
2879 char *compressed_data = NULL;
2880 size_t compressed_len, bufsize;
2883 bufsize = compressed_len = Buf->BufUsed + (Buf->BufUsed / 100) + 100;
2884 compressed_data = malloc(compressed_len);
2886 if (compressed_data == NULL)
2888 /* Flush some space after the used payload so valgrind shuts up... */
2889 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
2890 Buf->buf[Buf->BufUsed + i++] = '\0';
2891 if (compress_gzip((Bytef *) compressed_data,
2894 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
2897 Buf->buf = compressed_data;
2898 Buf->BufUsed = compressed_len;
2899 Buf->BufSize = bufsize;
2900 /* Flush some space after the used payload so valgrind shuts up... */
2902 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
2903 Buf->buf[Buf->BufUsed + i++] = '\0';
2906 free(compressed_data);
2908 #endif /* HAVE_ZLIB */
2914 /*******************************************************************************
2915 * File I/O; Prefer buffered read since its faster! *
2916 *******************************************************************************/
2919 * @ingroup StrBuf_IO
2920 * @brief Read a line from socket
2921 * flushes and closes the FD on error
2922 * @param buf the buffer to get the input to
2923 * @param fd pointer to the filedescriptor to read
2924 * @param append Append to an existing string or replace?
2925 * @param Error strerror() on error
2926 * @returns numbers of chars read
2928 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
2930 int len, rlen, slen;
2935 slen = len = buf->BufUsed;
2937 rlen = read(*fd, &buf->buf[len], 1);
2939 *Error = strerror(errno);
2946 if (buf->buf[len] == '\n')
2948 if (buf->buf[len] != '\r')
2950 if (len + 2 >= buf->BufSize) {
2952 buf->buf[len+1] = '\0';
2953 IncreaseBuf(buf, 1, -1);
2957 buf->buf[len] = '\0';
2962 * @ingroup StrBuf_BufferedIO
2963 * @brief Read a line from socket
2964 * flushes and closes the FD on error
2965 * @param Line the line to read from the fd / I/O Buffer
2966 * @param buf the buffer to get the input to
2967 * @param fd pointer to the filedescriptor to read
2968 * @param timeout number of successless selects until we bail out
2969 * @param selectresolution how long to wait on each select
2970 * @param Error strerror() on error
2971 * @returns numbers of chars read
2973 int StrBufTCP_read_buffered_line(StrBuf *Line,
2977 int selectresolution,
2981 int nSuccessLess = 0;
2988 if (buf->BufUsed > 0) {
2989 pch = strchr(buf->buf, '\n');
2992 len = pch - buf->buf;
2993 if (len > 0 && (*(pch - 1) == '\r') )
2995 StrBufSub(Line, buf, 0, len - rlen);
2996 StrBufCutLeft(buf, len + 1);
3001 if (buf->BufSize - buf->BufUsed < 10)
3002 IncreaseBuf(buf, 1, -1);
3004 fdflags = fcntl(*fd, F_GETFL);
3005 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3007 while ((nSuccessLess < timeout) && (pch == NULL)) {
3009 tv.tv_sec = selectresolution;
3014 if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
3015 *Error = strerror(errno);
3021 if (IsNonBlock && ! FD_ISSET(*fd, &rfds)) {
3026 &buf->buf[buf->BufUsed],
3027 buf->BufSize - buf->BufUsed - 1);
3029 *Error = strerror(errno);
3034 else if (rlen > 0) {
3036 buf->BufUsed += rlen;
3037 buf->buf[buf->BufUsed] = '\0';
3038 if (buf->BufUsed + 10 > buf->BufSize) {
3039 IncreaseBuf(buf, 1, -1);
3041 pch = strchr(buf->buf, '\n');
3048 len = pch - buf->buf;
3049 if (len > 0 && (*(pch - 1) == '\r') )
3051 StrBufSub(Line, buf, 0, len - rlen);
3052 StrBufCutLeft(buf, len + 1);
3059 static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
3060 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
3061 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
3063 * @ingroup StrBuf_BufferedIO
3064 * @brief Read a line from socket
3065 * flushes and closes the FD on error
3066 * @param Line Line to read from the fd / I/O Buffer
3067 * @param IOBuf the buffer to get the input to
3068 * @param Pos pointer to the current read position, should be NULL initialized!
3069 * @param fd pointer to the filedescriptor to read
3070 * @param timeout number of successless selects until we bail out
3071 * @param selectresolution how long to wait on each select
3072 * @param Error strerror() on error
3073 * @returns numbers of chars read
3075 int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
3080 int selectresolution,
3083 const char *pche = NULL;
3084 const char *pos = NULL;
3086 int nSuccessLess = 0;
3088 const char *pch = NULL;
3093 if ((Line == NULL) ||
3100 *Error = ErrRBLF_PreConditionFailed;
3105 if ((IOBuf->BufUsed > 0) &&
3107 (pos < IOBuf->buf + IOBuf->BufUsed))
3109 pche = IOBuf->buf + IOBuf->BufUsed;
3111 while ((pch < pche) && (*pch != '\n'))
3113 if ((pch >= pche) || (*pch == '\0'))
3115 if ((pch != NULL) &&
3120 if (len > 0 && (*(pch - 1) == '\r') )
3122 StrBufSub(Line, IOBuf, (pos - IOBuf->buf), len - rlen);
3132 StrBufCutLeft(IOBuf, (pos - IOBuf->buf));
3136 if (IOBuf->BufSize - IOBuf->BufUsed < 10) {
3137 IncreaseBuf(IOBuf, 1, -1);
3141 fdflags = fcntl(*fd, F_GETFL);
3142 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3145 while ((nSuccessLess < timeout) &&
3155 if (select((*fd) + 1, &rfds, NULL, NULL, &tv) == -1) {
3156 *Error = strerror(errno);
3160 *Error = ErrRBLF_SelectFailed;
3163 if (! FD_ISSET(*fd, &rfds) != 0) {
3169 &IOBuf->buf[IOBuf->BufUsed],
3170 IOBuf->BufSize - IOBuf->BufUsed - 1);
3172 *Error = strerror(errno);
3177 else if (rlen > 0) {
3179 IOBuf->BufUsed += rlen;
3180 IOBuf->buf[IOBuf->BufUsed] = '\0';
3181 if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
3182 IncreaseBuf(IOBuf, 1, -1);
3186 pche = IOBuf->buf + IOBuf->BufUsed;
3188 while ((pch < pche) && (*pch != '\n'))
3190 if ((pch >= pche) || (*pch == '\0'))
3199 if (len > 0 && (*(pch - 1) == '\r') )
3201 StrBufSub(Line, IOBuf, 0, len - rlen);
3202 *Pos = pos + len + 1;
3205 *Error = ErrRBLF_NotEnoughSentFromServer;
3210 static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong arguments or invalid Filedescriptor";
3212 * @ingroup StrBuf_IO
3213 * @brief Input binary data from socket
3214 * flushes and closes the FD on error
3215 * @param Buf the buffer to get the input to
3216 * @param fd pointer to the filedescriptor to read
3217 * @param append Append to an existing string or replace?
3218 * @param nBytes the maximal number of bytes to read
3219 * @param Error strerror() on error
3220 * @returns numbers of chars read
3222 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
3225 int len, rlen, slen;
3233 if ((Buf == NULL) || (*fd == -1))
3235 *Error = ErrRBLF_BLOBPreConditionFailed;
3240 if (Buf->BufUsed + nBytes >= Buf->BufSize)
3241 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
3243 ptr = Buf->buf + Buf->BufUsed;
3245 slen = len = Buf->BufUsed;
3247 fdflags = fcntl(*fd, F_GETFL);
3248 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3250 while ((nRead < nBytes) &&
3260 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
3261 *Error = strerror(errno);
3265 *Error = ErrRBLF_SelectFailed;
3268 if (! FD_ISSET(*fd, &rfds) != 0) {
3274 if ((rlen = read(*fd,
3276 nBytes - nRead)) == -1) {
3279 *Error = strerror(errno);
3284 Buf->BufUsed += rlen;
3286 Buf->buf[Buf->BufUsed] = '\0';
3290 const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
3291 const char *ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting.";
3293 * @ingroup StrBuf_BufferedIO
3294 * @brief Input binary data from socket
3295 * flushes and closes the FD on error
3296 * @param Blob put binary thing here
3297 * @param IOBuf the buffer to get the input to
3298 * @param Pos offset inside of IOBuf
3299 * @param fd pointer to the filedescriptor to read
3300 * @param append Append to an existing string or replace?
3301 * @param nBytes the maximal number of bytes to read
3302 * @param check whether we should search for '000\n' terminators in case of timeouts
3303 * @param Error strerror() on error
3304 * @returns numbers of chars read
3306 int StrBufReadBLOBBuffered(StrBuf *Blob,
3323 int nAlreadyRead = 0;
3331 if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL))
3335 *Error = ErrRBB_BLOBFPreConditionFailed;
3341 if (Blob->BufUsed + nBytes >= Blob->BufSize)
3342 IncreaseBuf(Blob, append, Blob->BufUsed + nBytes);
3347 len = pos - IOBuf->buf;
3348 rlen = IOBuf->BufUsed - len;
3351 if ((IOBuf->BufUsed > 0) &&
3353 (pos < IOBuf->buf + IOBuf->BufUsed))
3355 pche = IOBuf->buf + IOBuf->BufUsed;
3358 if (rlen < nBytes) {
3359 memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
3360 Blob->BufUsed += rlen;
3361 Blob->buf[Blob->BufUsed] = '\0';
3362 nAlreadyRead = nRead = rlen;
3365 if (rlen >= nBytes) {
3366 memcpy(Blob->buf + Blob->BufUsed, pos, nBytes);
3367 Blob->BufUsed += nBytes;
3368 Blob->buf[Blob->BufUsed] = '\0';
3369 if (rlen == nBytes) {
3381 if (IOBuf->BufSize < nBytes - nRead)
3382 IncreaseBuf(IOBuf, 0, nBytes - nRead);
3385 slen = len = Blob->BufUsed;
3387 fdflags = fcntl(*fd, F_GETFL);
3388 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
3393 while ((nRead < nBytes) &&
3402 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
3403 *Error = strerror(errno);
3407 *Error = ErrRBLF_SelectFailed;
3410 if (! FD_ISSET(*fd, &rfds) != 0) {
3422 *Error = strerror(errno);
3425 else if (rlen == 0){
3427 if ((check == NNN_TERM) &&
3429 (strncmp(IOBuf->buf + IOBuf->BufUsed - 5, "\n000\n", 5) == 0))
3431 StrBufPlain(Blob, HKEY("\n000\n"));
3432 StrBufCutRight(Blob, 5);
3433 return Blob->BufUsed;
3435 if (nSelects > 10) {
3437 *Error = ErrRBB_too_many_selects;
3441 else if (rlen > 0) {
3444 IOBuf->BufUsed += rlen;
3447 if (nRead > nBytes) {
3448 *Pos = IOBuf->buf + nBytes;
3450 Blob->buf[Blob->BufUsed] = '\0';
3451 StrBufAppendBufPlain(Blob, IOBuf->buf, nBytes, 0);
3455 return nRead + nAlreadyRead;
3459 * @ingroup StrBuf_IO
3460 * @brief extract a "next line" from Buf; Ptr to persist across several iterations
3461 * @param LineBuf your line will be copied here.
3462 * @param Buf BLOB with lines of text...
3463 * @param Ptr moved arround to keep the next-line across several iterations
3464 * has to be &NULL on start; will be &NotNULL on end of buffer
3465 * @returns size of copied buffer
3467 int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr)
3469 const char *aptr, *ptr, *eptr;
3472 if ((Buf == NULL) || (*Ptr == StrBufNOTNULL)) {
3473 *Ptr = StrBufNOTNULL;
3477 FlushStrBuf(LineBuf);
3479 ptr = aptr = Buf->buf;
3483 optr = LineBuf->buf;
3484 eptr = Buf->buf + Buf->BufUsed;
3485 xptr = LineBuf->buf + LineBuf->BufSize - 1;
3487 while ((ptr <= eptr) &&
3494 LineBuf->BufUsed = optr - LineBuf->buf;
3495 IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
3496 optr = LineBuf->buf + LineBuf->BufUsed;
3497 xptr = LineBuf->buf + LineBuf->BufSize - 1;
3501 if ((ptr >= eptr) && (optr > LineBuf->buf))
3503 LineBuf->BufUsed = optr - LineBuf->buf;
3505 if ((ptr <= eptr) && (*ptr == '\r'))
3507 if ((ptr <= eptr) && (*ptr == '\n'))
3514 *Ptr = StrBufNOTNULL;
3517 return Buf->BufUsed - (ptr - Buf->buf);
3522 * @ingroup StrBuf_IO
3523 * @brief removes double slashes from pathnames
3524 * @param Dir directory string to filter
3525 * @param RemoveTrailingSlash allows / disallows trailing slashes
3527 void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
3533 while (!IsEmptyStr(a)) {
3545 if ((RemoveTrailingSlash) && (*(b - 1) != '/')){
3550 Dir->BufUsed = b - Dir->buf;