2 * Copyright (c) 1987-2013 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/select.h>
29 #include <sys/types.h>
30 #define SHOW_ME_VAPPEND_PRINTF
33 #include "libcitadel.h"
49 int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
50 const Bytef * source, uLong sourceLen, int level);
52 int BaseStrBufSize = 64;
55 const char *StrBufNOTNULL = ((char*) NULL) - 1;
57 const char HexList[256][3] = {
58 "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
59 "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
60 "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
61 "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
62 "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
63 "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
64 "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
65 "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
66 "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
67 "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
68 "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
69 "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
70 "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
71 "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
72 "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
73 "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
76 * @defgroup StrBuf Stringbuffer, A class for manipulating strings with dynamic buffers
77 * StrBuf is a versatile class, aiding the handling of dynamic strings
78 * * reduce de/reallocations
79 * * reduce the need to remeasure it
80 * * reduce scanning over the string (in @ref StrBuf_NextTokenizer "Tokenizers")
81 * * allow asyncroneous IO for line and Blob based operations
82 * * reduce the use of memove in those
83 * * Quick filling in several operations with append functions
87 * @defgroup StrBuf_DeConstructors Create/Destroy StrBufs
92 * @defgroup StrBuf_Cast Cast operators to interact with char* based code
94 * use these operators to interfere with code demanding char*;
95 * if you need to own the content, smash me. Avoid, since we loose the length information.
99 * @defgroup StrBuf_Filler Create/Replace/Append Content into a StrBuf
101 * operations to get your Strings into a StrBuf, manipulating them, or appending
104 * @defgroup StrBuf_NextTokenizer Fast tokenizer to pull tokens in sequence
106 * Quick tokenizer; demands of the user to pull its tokens in sequence
110 * @defgroup StrBuf_Tokenizer tokenizer Functions; Slow ones.
112 * versatile tokenizer; random access to tokens, but slower; Prefer the @ref StrBuf_NextTokenizer "Next Tokenizer"
116 * @defgroup StrBuf_BufferedIO Buffered IO with Asynchroneous reads and no unneeded memmoves (the fast ones)
118 * File IO to fill StrBufs; Works with work-buffer shared across several calls;
119 * External Cursor to maintain the current read position inside of the buffer
120 * the non-fast ones will use memove to keep the start of the buffer the read buffer (which is slower)
124 * @defgroup StrBuf_IO FileIO; Prefer @ref StrBuf_BufferedIO
130 * @defgroup StrBuf_DeEnCoder functions to translate the contents of a buffer
132 * these functions translate the content of a buffer into another representation;
133 * some are combined Fillers and encoders
137 * Private Structure for the Stringbuffer
140 char *buf; /**< the pointer to the dynamic buffer */
141 long BufSize; /**< how many spcae do we optain */
142 long BufUsed; /**< StNumber of Chars used excluding the trailing \\0 */
143 int ConstBuf; /**< are we just a wrapper arround a static buffer and musn't we be changed? */
145 long nIncreases; /**< for profiling; cound how many times we needed more */
146 char bt [SIZ]; /**< Stacktrace of last increase */
147 char bt_lastinc [SIZ]; /**< How much did we increase last time? */
152 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE);
153 static inline int Ctdl_IsUtf8SequenceStart(const char Char);
156 #ifdef HAVE_BACKTRACE
157 static void StrBufBacktrace(StrBuf *Buf, int which)
161 void *stack_frames[50];
166 pstart = pch = Buf->bt;
168 pstart = pch = Buf->bt_lastinc;
169 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
170 strings = backtrace_symbols(stack_frames, size);
171 for (i = 0; i < size; i++) {
173 n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
175 n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
184 void dbg_FreeStrBuf(StrBuf *FreeMe, char *FromWhere)
186 if (hFreeDbglog == -1){
187 pid_t pid = getpid();
189 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
190 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
192 if ((*FreeMe)->nIncreases > 0)
196 n = snprintf(buf, SIZ * 3, "%c+|%ld|%ld|%ld|%s|%s|\n",
198 (*FreeMe)->nIncreases,
202 (*FreeMe)->bt_lastinc);
203 n = write(hFreeDbglog, buf, n);
209 n = snprintf(buf, 128, "%c_|0|%ld%ld|\n",
213 n = write(hFreeDbglog, buf, n);
217 void dbg_IncreaseBuf(StrBuf *IncMe)
220 #ifdef HAVE_BACKTRACE
221 StrBufBacktrace(Buf, 1);
225 void dbg_Init(StrBuf *Buf)
229 Buf->bt_lastinc[0] = '\0';
230 #ifdef HAVE_BACKTRACE
231 StrBufBacktrace(Buf, 0);
237 #define dbg_FreeStrBuf(a, b)
238 #define dbg_IncreaseBuf(a)
245 * @brief swaps the contents of two StrBufs
246 * this is to be used to have cheap switched between a work-buffer and a target buffer
248 * @param B second one
250 static inline void SwapBuffers(StrBuf *A, StrBuf *B)
254 memcpy(&C, A, sizeof(*A));
255 memcpy(A, B, sizeof(*B));
256 memcpy(B, &C, sizeof(C));
261 * @ingroup StrBuf_Cast
262 * @brief Cast operator to Plain String
263 * @note if the buffer is altered by StrBuf operations, this pointer may become
264 * invalid. So don't lean on it after altering the buffer!
265 * Since this operation is considered cheap, rather call it often than risking
266 * your pointer to become invalid!
267 * @param Str the string we want to get the c-string representation for
268 * @returns the Pointer to the Content. Don't mess with it!
270 inline const char *ChrPtr(const StrBuf *Str)
278 * @ingroup StrBuf_Cast
279 * @brief since we know strlen()'s result, provide it here.
280 * @param Str the string to return the length to
281 * @returns contentlength of the buffer
283 inline int StrLength(const StrBuf *Str)
285 return (Str != NULL) ? Str->BufUsed : 0;
289 * @ingroup StrBuf_DeConstructors
290 * @brief local utility function to resize the buffer
291 * @param Buf the buffer whichs storage we should increase
292 * @param KeepOriginal should we copy the original buffer or just start over with a new one
293 * @param DestSize what should fit in after?
295 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
298 size_t NewSize = Buf->BufSize * 2;
304 while ((NewSize <= DestSize) && (NewSize != 0))
310 NewBuf= (char*) malloc(NewSize);
314 if (KeepOriginal && (Buf->BufUsed > 0))
316 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
325 Buf->BufSize = NewSize;
327 dbg_IncreaseBuf(Buf);
333 * @ingroup StrBuf_DeConstructors
334 * @brief shrink / increase an _EMPTY_ buffer to NewSize. Buffercontent is thoroughly ignored and flushed.
335 * @param Buf Buffer to shrink (has to be empty)
336 * @param ThreshHold if the buffer is bigger then this, its readjusted
337 * @param NewSize if we Shrink it, how big are we going to be afterwards?
339 void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize)
342 (Buf->BufUsed == 0) &&
343 (Buf->BufSize < ThreshHold)) {
345 Buf->buf = (char*) malloc(NewSize);
347 Buf->BufSize = NewSize;
352 * @ingroup StrBuf_DeConstructors
353 * @brief shrink long term buffers to their real size so they don't waste memory
354 * @param Buf buffer to shrink
355 * @param Force if not set, will just executed if the buffer is much to big; set for lifetime strings
356 * @returns physical size of the buffer
358 long StrBufShrinkToFit(StrBuf *Buf, int Force)
363 (Buf->BufUsed + (Buf->BufUsed / 3) > Buf->BufSize))
367 TmpBuf = (char*) malloc(Buf->BufUsed + 1);
371 memcpy (TmpBuf, Buf->buf, Buf->BufUsed + 1);
372 Buf->BufSize = Buf->BufUsed + 1;
380 * @ingroup StrBuf_DeConstructors
381 * @brief Allocate a new buffer with default buffer size
382 * @returns the new stringbuffer
384 StrBuf* NewStrBuf(void)
388 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
392 NewBuf->buf = (char*) malloc(BaseStrBufSize);
393 if (NewBuf->buf == NULL)
398 NewBuf->buf[0] = '\0';
399 NewBuf->BufSize = BaseStrBufSize;
401 NewBuf->ConstBuf = 0;
409 * @ingroup StrBuf_DeConstructors
410 * @brief Copy Constructor; returns a duplicate of CopyMe
411 * @param CopyMe Buffer to faxmilate
412 * @returns the new stringbuffer
414 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
421 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
425 NewBuf->buf = (char*) malloc(CopyMe->BufSize);
426 if (NewBuf->buf == NULL)
432 memcpy(NewBuf->buf, CopyMe->buf, CopyMe->BufUsed + 1);
433 NewBuf->BufUsed = CopyMe->BufUsed;
434 NewBuf->BufSize = CopyMe->BufSize;
435 NewBuf->ConstBuf = 0;
443 * @ingroup StrBuf_DeConstructors
444 * @brief Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards.
445 * @param NoMe if non-NULL, we will use that buffer as value; KeepOriginal will abused as len.
446 * @param CopyFlushMe Buffer to faxmilate if KeepOriginal, or to move into CreateRelpaceMe if !KeepOriginal.
447 * @param CreateRelpaceMe If NULL, will be created, else Flushed and filled CopyFlushMe
448 * @param KeepOriginal should CopyFlushMe remain intact? or may we Steal its buffer?
449 * @returns the new stringbuffer
451 void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, const char *NoMe, int KeepOriginal)
455 if (CreateRelpaceMe == NULL)
460 if (*CreateRelpaceMe != NULL)
461 StrBufPlain(*CreateRelpaceMe, NoMe, KeepOriginal);
463 *CreateRelpaceMe = NewStrBufPlain(NoMe, KeepOriginal);
467 if (CopyFlushMe == NULL)
469 if (*CreateRelpaceMe != NULL)
470 FlushStrBuf(*CreateRelpaceMe);
472 *CreateRelpaceMe = NewStrBuf();
477 * Randomly Chosen: bigger than 64 chars is cheaper to swap the buffers instead of copying.
478 * else *CreateRelpaceMe may use more memory than needed in a longer term, CopyFlushMe might
479 * be a big IO-Buffer...
481 if (KeepOriginal || (StrLength(CopyFlushMe) < 256))
483 if (*CreateRelpaceMe == NULL)
485 *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed);
490 NewBuf = *CreateRelpaceMe;
493 StrBufAppendBuf(NewBuf, CopyFlushMe, 0);
497 if (*CreateRelpaceMe == NULL)
499 *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed);
503 NewBuf = *CreateRelpaceMe;
504 SwapBuffers (NewBuf, CopyFlushMe);
507 FlushStrBuf(CopyFlushMe);
512 * @ingroup StrBuf_DeConstructors
513 * @brief create a new Buffer using an existing c-string
514 * this function should also be used if you want to pre-suggest
515 * the buffer size to allocate in conjunction with ptr == NULL
516 * @param ptr the c-string to copy; may be NULL to create a blank instance
517 * @param nChars How many chars should we copy; -1 if we should measure the length ourselves
518 * @returns the new stringbuffer
520 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
523 size_t Siz = BaseStrBufSize;
526 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
531 CopySize = strlen((ptr != NULL)?ptr:"");
535 while ((Siz <= CopySize) && (Siz != 0))
544 NewBuf->buf = (char*) malloc(Siz);
545 if (NewBuf->buf == NULL)
550 NewBuf->BufSize = Siz;
552 memcpy(NewBuf->buf, ptr, CopySize);
553 NewBuf->buf[CopySize] = '\0';
554 NewBuf->BufUsed = CopySize;
557 NewBuf->buf[0] = '\0';
560 NewBuf->ConstBuf = 0;
568 * @ingroup StrBuf_DeConstructors
569 * @brief Set an existing buffer from a c-string
570 * @param Buf buffer to load
571 * @param ptr c-string to put into
572 * @param nChars set to -1 if we should work 0-terminated
573 * @returns the new length of the string
575 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
590 CopySize = strlen(ptr);
594 while ((Siz <= CopySize) && (Siz != 0))
602 if (Siz != Buf->BufSize)
603 IncreaseBuf(Buf, 0, Siz);
604 memcpy(Buf->buf, ptr, CopySize);
605 Buf->buf[CopySize] = '\0';
606 Buf->BufUsed = CopySize;
613 * @ingroup StrBuf_DeConstructors
614 * @brief use strbuf as wrapper for a string constant for easy handling
615 * @param StringConstant a string to wrap
616 * @param SizeOfStrConstant should be sizeof(StringConstant)-1
618 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
622 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
625 NewBuf->buf = (char*) StringConstant;
626 NewBuf->BufSize = SizeOfStrConstant;
627 NewBuf->BufUsed = SizeOfStrConstant;
628 NewBuf->ConstBuf = 1;
637 * @ingroup StrBuf_DeConstructors
638 * @brief flush the content of a Buf; keep its struct
639 * @param buf Buffer to flush
641 int FlushStrBuf(StrBuf *buf)
643 if ((buf == NULL) || (buf->buf == NULL))
653 * @ingroup StrBuf_DeConstructors
654 * @brief wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
655 * @param buf Buffer to wipe
657 int FLUSHStrBuf(StrBuf *buf)
663 if (buf->BufUsed > 0) {
664 memset(buf->buf, 0, buf->BufUsed);
671 int hFreeDbglog = -1;
674 * @ingroup StrBuf_DeConstructors
675 * @brief Release a Buffer
676 * Its a double pointer, so it can NULL your pointer
677 * so fancy SIG11 appear instead of random results
678 * @param FreeMe Pointer Pointer to the buffer to free
680 void FreeStrBuf (StrBuf **FreeMe)
685 dbg_FreeStrBuf(FreeMe, 'F');
687 if (!(*FreeMe)->ConstBuf)
688 free((*FreeMe)->buf);
694 * @ingroup StrBuf_DeConstructors
695 * @brief flatten a Buffer to the Char * we return
696 * Its a double pointer, so it can NULL your pointer
697 * so fancy SIG11 appear instead of random results
698 * The Callee then owns the buffer and is responsible for freeing it.
699 * @param SmashMe Pointer Pointer to the buffer to release Buf from and free
700 * @returns the pointer of the buffer; Callee owns the memory thereafter.
702 char *SmashStrBuf (StrBuf **SmashMe)
706 if ((SmashMe == NULL) || (*SmashMe == NULL))
709 dbg_FreeStrBuf(SmashMe, 'S');
711 Ret = (*SmashMe)->buf;
718 * @ingroup StrBuf_DeConstructors
719 * @brief Release the buffer
720 * If you want put your StrBuf into a Hash, use this as Destructor.
721 * @param VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
723 void HFreeStrBuf (void *VFreeMe)
725 StrBuf *FreeMe = (StrBuf*)VFreeMe;
729 dbg_FreeStrBuf(SmashMe, 'H');
731 if (!FreeMe->ConstBuf)
737 /*******************************************************************************
738 * Simple string transformations *
739 *******************************************************************************/
743 * @brief Wrapper around atol
745 long StrTol(const StrBuf *Buf)
750 return atol(Buf->buf);
757 * @brief Wrapper around atoi
759 int StrToi(const StrBuf *Buf)
763 if (Buf->BufUsed > 0)
764 return atoi(Buf->buf);
771 * @brief Checks to see if the string is a pure number
772 * @param Buf The buffer to inspect
773 * @returns 1 if its a pure number, 0, if not.
775 int StrBufIsNumber(const StrBuf *Buf) {
777 if ((Buf == NULL) || (Buf->BufUsed == 0)) {
780 strtoll(Buf->buf, &pEnd, 10);
781 if (pEnd == Buf->buf)
783 if ((pEnd != NULL) && (pEnd == Buf->buf + Buf->BufUsed))
785 if (Buf->buf == pEnd)
791 * @ingroup StrBuf_Filler
792 * @brief modifies a Single char of the Buf
793 * You can point to it via char* or a zero-based integer
794 * @param Buf The buffer to manipulate
795 * @param ptr char* to zero; use NULL if unused
796 * @param nThChar zero based pointer into the string; use -1 if unused
797 * @param PeekValue The Character to place into the position
799 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
804 nThChar = ptr - Buf->buf;
805 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
807 Buf->buf[nThChar] = PeekValue;
812 * @ingroup StrBuf_Filler
813 * @brief modifies a range of chars of the Buf
814 * You can point to it via char* or a zero-based integer
815 * @param Buf The buffer to manipulate
816 * @param ptr char* to zero; use NULL if unused
817 * @param nThChar zero based pointer into the string; use -1 if unused
818 * @param nChars how many chars are to be flushed?
819 * @param PookValue The Character to place into that area
821 long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue)
826 nThChar = ptr - Buf->buf;
827 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
829 if (nThChar + nChars > Buf->BufUsed)
830 nChars = Buf->BufUsed - nThChar;
832 memset(Buf->buf + nThChar, PookValue, nChars);
833 /* just to be shure... */
834 Buf->buf[Buf->BufUsed] = 0;
839 * @ingroup StrBuf_Filler
840 * @brief Append a StringBuffer to the buffer
841 * @param Buf Buffer to modify
842 * @param AppendBuf Buffer to copy at the end of our buffer
843 * @param Offset Should we start copying from an offset?
845 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
847 if ((AppendBuf == NULL) || (AppendBuf->buf == NULL) ||
848 (Buf == NULL) || (Buf->buf == NULL))
851 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1)
854 AppendBuf->BufUsed + Buf->BufUsed);
856 memcpy(Buf->buf + Buf->BufUsed,
857 AppendBuf->buf + Offset,
858 AppendBuf->BufUsed - Offset);
859 Buf->BufUsed += AppendBuf->BufUsed - Offset;
860 Buf->buf[Buf->BufUsed] = '\0';
865 * @ingroup StrBuf_Filler
866 * @brief Append a C-String to the buffer
867 * @param Buf Buffer to modify
868 * @param AppendBuf Buffer to copy at the end of our buffer
869 * @param AppendSize number of bytes to copy; set to -1 if we should count it in advance
870 * @param Offset Should we start copying from an offset?
872 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset)
875 long BufSizeRequired;
877 if ((AppendBuf == NULL) || (Buf == NULL))
881 aps = strlen(AppendBuf + Offset);
883 aps = AppendSize - Offset;
885 BufSizeRequired = Buf->BufUsed + aps + 1;
886 if (Buf->BufSize <= BufSizeRequired)
887 IncreaseBuf(Buf, (Buf->BufUsed > 0), BufSizeRequired);
889 memcpy(Buf->buf + Buf->BufUsed,
893 Buf->buf[Buf->BufUsed] = '\0';
897 * @ingroup StrBuf_Filler
898 * @brief sprintf like function appending the formated string to the buffer
899 * vsnprintf version to wrap into own calls
900 * @param Buf Buffer to extend by format and Params
901 * @param format printf alike format to add
902 * @param ap va_list containing the items for format
904 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
912 if ((Buf == NULL) || (format == NULL))
915 BufSize = Buf->BufSize;
916 nWritten = Buf->BufSize + 1;
917 Offset = Buf->BufUsed;
918 newused = Offset + nWritten;
920 while (newused >= BufSize) {
922 nWritten = vsnprintf(Buf->buf + Offset,
923 Buf->BufSize - Offset,
926 newused = Offset + nWritten;
927 if (newused >= Buf->BufSize) {
928 if (IncreaseBuf(Buf, 1, newused) == -1)
929 return; /* TODO: error handling? */
930 newused = Buf->BufSize + 1;
933 Buf->BufUsed = Offset + nWritten;
934 BufSize = Buf->BufSize;
941 * @ingroup StrBuf_Filler
942 * @brief sprintf like function appending the formated string to the buffer
943 * @param Buf Buffer to extend by format and Params
944 * @param format printf alike format to add
946 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
954 if ((Buf == NULL) || (format == NULL))
957 BufSize = Buf->BufSize;
958 nWritten = Buf->BufSize + 1;
959 Offset = Buf->BufUsed;
960 newused = Offset + nWritten;
962 while (newused >= BufSize) {
963 va_start(arg_ptr, format);
964 nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
965 Buf->BufSize - Buf->BufUsed,
968 newused = Buf->BufUsed + nWritten;
969 if (newused >= Buf->BufSize) {
970 if (IncreaseBuf(Buf, 1, newused) == -1)
971 return; /* TODO: error handling? */
972 newused = Buf->BufSize + 1;
975 Buf->BufUsed += nWritten;
976 BufSize = Buf->BufSize;
983 * @ingroup StrBuf_Filler
984 * @brief sprintf like function putting the formated string into the buffer
985 * @param Buf Buffer to extend by format and Parameters
986 * @param format printf alike format to add
988 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
993 if ((Buf == NULL) || (format == NULL))
996 nWritten = Buf->BufSize + 1;
997 while (nWritten >= Buf->BufSize) {
998 va_start(arg_ptr, format);
999 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
1001 if (nWritten >= Buf->BufSize) {
1002 if (IncreaseBuf(Buf, 0, 0) == -1)
1003 return; /* TODO: error handling? */
1004 nWritten = Buf->BufSize + 1;
1007 Buf->BufUsed = nWritten ;
1012 * @ingroup StrBuf_Filler
1013 * @brief Callback for cURL to append the webserver reply to a buffer
1014 * @param ptr pre-defined by the cURL API; see man 3 curl for mre info
1015 * @param size pre-defined by the cURL API; see man 3 curl for mre info
1016 * @param nmemb pre-defined by the cURL API; see man 3 curl for mre info
1017 * @param stream pre-defined by the cURL API; see man 3 curl for mre info
1019 size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
1028 StrBufAppendBufPlain(Target, ptr, size * nmemb, 0);
1029 return size * nmemb;
1035 * @brief extracts a substring from Source into dest
1036 * @param dest buffer to place substring into
1037 * @param Source string to copy substring from
1038 * @param Offset chars to skip from start
1039 * @param nChars number of chars to copy
1040 * @returns the number of chars copied; may be different from nChars due to the size of Source
1042 int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
1044 size_t NCharsRemain;
1045 if (Offset > Source->BufUsed)
1051 if (Offset + nChars < Source->BufUsed)
1053 if ((nChars >= dest->BufSize) &&
1054 (IncreaseBuf(dest, 0, nChars + 1) == -1))
1056 memcpy(dest->buf, Source->buf + Offset, nChars);
1057 dest->BufUsed = nChars;
1058 dest->buf[dest->BufUsed] = '\0';
1061 NCharsRemain = Source->BufUsed - Offset;
1062 if ((NCharsRemain >= dest->BufSize) &&
1063 (IncreaseBuf(dest, 0, NCharsRemain + 1) == -1))
1065 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
1066 dest->BufUsed = NCharsRemain;
1067 dest->buf[dest->BufUsed] = '\0';
1068 return NCharsRemain;
1073 * @brief Cut nChars from the start of the string
1074 * @param Buf Buffer to modify
1075 * @param nChars how many chars should be skipped?
1077 void StrBufCutLeft(StrBuf *Buf, int nChars)
1079 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1080 if (nChars >= Buf->BufUsed) {
1084 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
1085 Buf->BufUsed -= nChars;
1086 Buf->buf[Buf->BufUsed] = '\0';
1091 * @brief Cut the trailing n Chars from the string
1092 * @param Buf Buffer to modify
1093 * @param nChars how many chars should be trunkated?
1095 void StrBufCutRight(StrBuf *Buf, int nChars)
1097 if ((Buf == NULL) || (Buf->BufUsed == 0) || (Buf->buf == NULL))
1100 if (nChars >= Buf->BufUsed) {
1104 Buf->BufUsed -= nChars;
1105 Buf->buf[Buf->BufUsed] = '\0';
1110 * @brief Cut the string after n Chars
1111 * @param Buf Buffer to modify
1112 * @param AfternChars after how many chars should we trunkate the string?
1113 * @param At if non-null and points inside of our string, cut it there.
1115 void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
1117 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1119 AfternChars = At - Buf->buf;
1122 if ((AfternChars < 0) || (AfternChars >= Buf->BufUsed))
1124 Buf->BufUsed = AfternChars;
1125 Buf->buf[Buf->BufUsed] = '\0';
1131 * @brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
1132 * @param Buf the string to modify
1134 void StrBufTrim(StrBuf *Buf)
1137 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1139 while ((Buf->BufUsed > 0) &&
1140 isspace(Buf->buf[Buf->BufUsed - 1]))
1144 Buf->buf[Buf->BufUsed] = '\0';
1146 if (Buf->BufUsed == 0) return;
1148 while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
1151 if (delta > 0) StrBufCutLeft(Buf, delta);
1155 * @brief changes all spaces in the string (tab, linefeed...) to Blank (0x20)
1156 * @param Buf the string to modify
1158 void StrBufSpaceToBlank(StrBuf *Buf)
1162 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1165 pche = pch + Buf->BufUsed;
1174 void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
1179 if ((Buf == NULL) || (Buf->buf == NULL)) {
1183 pRight = strchr(Buf->buf, rightboundary);
1184 if (pRight != NULL) {
1185 StrBufCutAt(Buf, 0, pRight);
1188 pLeft = strrchr(ChrPtr(Buf), leftboundary);
1189 if (pLeft != NULL) {
1190 StrBufCutLeft(Buf, pLeft - Buf->buf + 1);
1196 * @ingroup StrBuf_Filler
1197 * @brief uppercase the contents of a buffer
1198 * @param Buf the buffer to translate
1200 void StrBufUpCase(StrBuf *Buf)
1204 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1207 pche = pch + Buf->BufUsed;
1208 while (pch < pche) {
1209 *pch = toupper(*pch);
1216 * @ingroup StrBuf_Filler
1217 * @brief lowercase the contents of a buffer
1218 * @param Buf the buffer to translate
1220 void StrBufLowerCase(StrBuf *Buf)
1224 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1227 pche = pch + Buf->BufUsed;
1228 while (pch < pche) {
1229 *pch = tolower(*pch);
1235 /*******************************************************************************
1236 * a tokenizer that kills, maims, and destroys *
1237 *******************************************************************************/
1240 * @ingroup StrBuf_Tokenizer
1241 * @brief Replace a token at a given place with a given length by another token with given length
1242 * @param Buf String where to work on
1243 * @param where where inside of the Buf is the search-token
1244 * @param HowLong How long is the token to be replaced
1245 * @param Repl Token to insert at 'where'
1246 * @param ReplLen Length of repl
1247 * @returns -1 if fail else length of resulting Buf
1249 int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong,
1250 const char *Repl, long ReplLen)
1253 if ((Buf == NULL) ||
1254 (where > Buf->BufUsed) ||
1255 (where + HowLong > Buf->BufUsed))
1258 if (where + ReplLen - HowLong > Buf->BufSize)
1259 if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0)
1262 memmove(Buf->buf + where + ReplLen,
1263 Buf->buf + where + HowLong,
1264 Buf->BufUsed - where - HowLong);
1266 memcpy(Buf->buf + where,
1269 Buf->BufUsed += ReplLen - HowLong;
1271 return Buf->BufUsed;
1275 * @ingroup StrBuf_Tokenizer
1276 * @brief Counts the numbmer of tokens in a buffer
1277 * @param source String to count tokens in
1278 * @param tok Tokenizer char to count
1279 * @returns numbers of tokenizer chars found
1281 int StrBufNum_tokens(const StrBuf *source, char tok)
1285 if ((source == NULL) || (source->BufUsed == 0))
1287 if ((source->BufUsed == 1) && (*source->buf == tok))
1291 pche = pch + source->BufUsed;
1302 * @ingroup StrBuf_Tokenizer
1303 * @brief a string tokenizer
1304 * @param Source StringBuffer to read into
1305 * @param parmnum n'th Parameter to remove
1306 * @param separator tokenizer character
1307 * @returns -1 if not found, else length of token.
1309 int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
1312 char *d, *s, *end; /* dest, source */
1315 /* Find desired @parameter */
1316 end = Source->buf + Source->BufUsed;
1318 while ((d <= end) &&
1321 /* End of string, bail! */
1326 if (*d == separator) {
1331 if ((d == NULL) || (d >= end))
1332 return 0; /* @Parameter not found */
1334 /* Find next @parameter */
1336 while ((s <= end) &&
1337 (*s && *s != separator))
1341 if (*s == separator)
1345 /* Hack and slash */
1350 memmove(d, s, Source->BufUsed - (s - Source->buf));
1351 Source->BufUsed += ReducedBy;
1352 Source->buf[Source->BufUsed] = '\0';
1354 else if (d == Source->buf) {
1356 Source->BufUsed = 0;
1360 Source->BufUsed += ReducedBy;
1371 int StrBufExtract_tokenFromStr(StrBuf *dest, const char *Source, long SourceLen, int parmnum, char separator)
1373 const StrBuf Temp = {
1386 return StrBufExtract_token(dest, &Temp, parmnum, separator);
1390 * @ingroup StrBuf_Tokenizer
1391 * @brief a string tokenizer
1392 * @param dest Destination StringBuffer
1393 * @param Source StringBuffer to read into
1394 * @param parmnum n'th Parameter to extract
1395 * @param separator tokenizer character
1396 * @returns -1 if not found, else length of token.
1398 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
1400 const char *s, *e; //* source * /
1401 int len = 0; //* running total length of extracted string * /
1402 int current_token = 0; //* token currently being processed * /
1405 dest->buf[0] = '\0';
1411 if ((Source == NULL) || (Source->BufUsed ==0)) {
1415 e = s + Source->BufUsed;
1418 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1420 while ((s < e) && !IsEmptyStr(s)) {
1421 if (*s == separator) {
1424 if (len >= dest->BufSize) {
1425 dest->BufUsed = len;
1426 if (IncreaseBuf(dest, 1, -1) < 0) {
1431 if ( (current_token == parmnum) &&
1432 (*s != separator)) {
1433 dest->buf[len] = *s;
1436 else if (current_token > parmnum) {
1442 dest->buf[len] = '\0';
1443 dest->BufUsed = len;
1445 if (current_token < parmnum) {
1446 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
1449 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
1458 * @ingroup StrBuf_Tokenizer
1459 * @brief a string tokenizer to fetch an integer
1460 * @param Source String containing tokens
1461 * @param parmnum n'th Parameter to extract
1462 * @param separator tokenizer character
1463 * @returns 0 if not found, else integer representation of the token
1465 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
1475 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1482 * @ingroup StrBuf_Tokenizer
1483 * @brief a string tokenizer to fetch a long integer
1484 * @param Source String containing tokens
1485 * @param parmnum n'th Parameter to extract
1486 * @param separator tokenizer character
1487 * @returns 0 if not found, else long integer representation of the token
1489 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
1499 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1507 * @ingroup StrBuf_Tokenizer
1508 * @brief a string tokenizer to fetch an unsigned long
1509 * @param Source String containing tokens
1510 * @param parmnum n'th Parameter to extract
1511 * @param separator tokenizer character
1512 * @returns 0 if not found, else unsigned long representation of the token
1514 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
1525 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0) {
1529 return (unsigned long) atol(pnum);
1538 * @ingroup StrBuf_NextTokenizer
1539 * @brief a string tokenizer; Bounds checker
1540 * function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
1541 * @param Source our tokenbuffer
1542 * @param pStart the token iterator pointer to inspect
1543 * @returns whether the revolving pointer is inside of the search range
1545 int StrBufHaveNextToken(const StrBuf *Source, const char **pStart)
1547 if ((Source == NULL) ||
1548 (*pStart == StrBufNOTNULL) ||
1549 (Source->BufUsed == 0))
1553 if (*pStart == NULL)
1557 else if (*pStart > Source->buf + Source->BufUsed)
1561 else if (*pStart <= Source->buf)
1570 * @ingroup StrBuf_NextTokenizer
1571 * @brief a string tokenizer
1572 * @param dest Destination StringBuffer
1573 * @param Source StringBuffer to read into
1574 * @param pStart pointer to the end of the last token. Feed with NULL on start.
1575 * @param separator tokenizer
1576 * @returns -1 if not found, else length of token.
1578 int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
1580 const char *s; /* source */
1581 const char *EndBuffer; /* end stop of source buffer */
1582 int current_token = 0; /* token currently being processed */
1583 int len = 0; /* running total length of extracted string */
1585 if ((Source == NULL) ||
1586 (Source->BufUsed == 0) )
1588 *pStart = StrBufNOTNULL;
1594 EndBuffer = Source->buf + Source->BufUsed;
1598 dest->buf[0] = '\0';
1603 *pStart = EndBuffer + 1;
1607 if (*pStart == NULL)
1609 *pStart = Source->buf; /* we're starting to examine this buffer. */
1611 else if ((*pStart < Source->buf) ||
1612 (*pStart > EndBuffer ) )
1614 return -1; /* no more tokens to find. */
1618 /* start to find the next token */
1619 while ((s <= EndBuffer) &&
1620 (current_token == 0) )
1622 if (*s == separator)
1624 /* we found the next token */
1628 if (len >= dest->BufSize)
1630 /* our Dest-buffer isn't big enough, increase it. */
1631 dest->BufUsed = len;
1633 if (IncreaseBuf(dest, 1, -1) < 0) {
1634 /* WHUT? no more mem? bail out. */
1641 if ( (current_token == 0 ) && /* are we in our target token? */
1642 (!IsEmptyStr(s) ) &&
1643 (separator != *s) ) /* don't copy the token itself */
1645 dest->buf[len] = *s; /* Copy the payload */
1646 ++len; /* remember the bigger size. */
1652 /* did we reach the end? */
1653 if ((s > EndBuffer)) {
1654 EndBuffer = StrBufNOTNULL;
1655 *pStart = EndBuffer;
1658 *pStart = s; /* remember the position for the next run */
1661 /* sanitize our extracted token */
1662 dest->buf[len] = '\0';
1663 dest->BufUsed = len;
1670 * @ingroup StrBuf_NextTokenizer
1671 * @brief a string tokenizer
1672 * @param Source StringBuffer to read from
1673 * @param pStart pointer to the end of the last token. Feed with NULL.
1674 * @param separator tokenizer character
1675 * @param nTokens number of tokens to fastforward over
1676 * @returns -1 if not found, else length of token.
1678 int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens)
1680 const char *s, *EndBuffer; //* source * /
1681 int len = 0; //* running total length of extracted string * /
1682 int current_token = 0; //* token currently being processed * /
1684 if ((Source == NULL) ||
1685 (Source->BufUsed ==0)) {
1689 return Source->BufUsed;
1691 if (*pStart == NULL)
1692 *pStart = Source->buf;
1694 EndBuffer = Source->buf + Source->BufUsed;
1696 if ((*pStart < Source->buf) ||
1697 (*pStart > EndBuffer)) {
1705 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1707 while ((s < EndBuffer) && !IsEmptyStr(s)) {
1708 if (*s == separator) {
1711 if (current_token >= nTokens) {
1723 * @ingroup StrBuf_NextTokenizer
1724 * @brief a string tokenizer to fetch an integer
1725 * @param Source StringBuffer to read from
1726 * @param pStart Cursor on the tokenstring
1727 * @param separator tokenizer character
1728 * @returns 0 if not found, else integer representation of the token
1730 int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator)
1740 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1747 * @ingroup StrBuf_NextTokenizer
1748 * @brief a string tokenizer to fetch a long integer
1749 * @param Source StringBuffer to read from
1750 * @param pStart Cursor on the tokenstring
1751 * @param separator tokenizer character
1752 * @returns 0 if not found, else long integer representation of the token
1754 long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator)
1764 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1772 * @ingroup StrBuf_NextTokenizer
1773 * @brief a string tokenizer to fetch an unsigned long
1774 * @param Source StringBuffer to read from
1775 * @param pStart Cursor on the tokenstring
1776 * @param separator tokenizer character
1777 * @returns 0 if not found, else unsigned long representation of the token
1779 unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator)
1790 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) {
1794 return (unsigned long) atol(pnum);
1804 /*******************************************************************************
1805 * Escape Appending *
1806 *******************************************************************************/
1809 * @ingroup StrBuf_DeEnCoder
1810 * @brief Escape a string for feeding out as a URL while appending it to a Buffer
1811 * @param OutBuf the output buffer
1812 * @param In Buffer to encode
1813 * @param PlainIn way in from plain old c strings
1815 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1817 const char *pch, *pche;
1821 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1823 if (PlainIn != NULL) {
1824 len = strlen(PlainIn);
1830 pche = pch + In->BufUsed;
1837 pt = OutBuf->buf + OutBuf->BufUsed;
1838 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1840 while (pch < pche) {
1842 IncreaseBuf(OutBuf, 1, -1);
1843 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1844 pt = OutBuf->buf + OutBuf->BufUsed;
1847 if((*pch >= 'a' && *pch <= 'z') ||
1848 (*pch >= '@' && *pch <= 'Z') || /* @ A-Z */
1849 (*pch >= '0' && *pch <= ':') || /* 0-9 : */
1850 (*pch == '!') || (*pch == '_') ||
1851 (*pch == ',') || (*pch == '.'))
1858 *(pt + 1) = HexList[(unsigned char)*pch][0];
1859 *(pt + 2) = HexList[(unsigned char)*pch][1];
1861 OutBuf->BufUsed += 3;
1869 * @ingroup StrBuf_DeEnCoder
1870 * @brief Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer
1871 * @param OutBuf the output buffer
1872 * @param In Buffer to encode
1873 * @param PlainIn way in from plain old c strings
1875 void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1877 const char *pch, *pche;
1881 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1883 if (PlainIn != NULL) {
1884 len = strlen(PlainIn);
1890 pche = pch + In->BufUsed;
1897 pt = OutBuf->buf + OutBuf->BufUsed;
1898 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1900 while (pch < pche) {
1902 IncreaseBuf(OutBuf, 1, -1);
1903 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1904 pt = OutBuf->buf + OutBuf->BufUsed;
1907 if((*pch >= 'a' && *pch <= 'z') ||
1908 (*pch >= 'A' && *pch <= 'Z') || /* A-Z */
1909 (*pch >= '0' && *pch <= ':') || /* 0-9 : */
1910 (*pch == '!') || (*pch == '_') ||
1911 (*pch == ',') || (*pch == '.'))
1918 *(pt + 1) = HexList[(unsigned char)*pch][0];
1919 *(pt + 2) = HexList[(unsigned char)*pch][1];
1921 OutBuf->BufUsed += 3;
1929 * @ingroup StrBuf_DeEnCoder
1930 * @brief append a string with characters having a special meaning in xml encoded to the buffer
1931 * @param OutBuf the output buffer
1932 * @param In Buffer to encode
1933 * @param PlainIn way in from plain old c strings
1934 * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
1935 * @param OverrideLowChars should chars < 0x20 be replaced by _ or escaped as xml entity?
1937 void StrBufXMLEscAppend(StrBuf *OutBuf,
1939 const char *PlainIn,
1941 int OverrideLowChars)
1943 const char *pch, *pche;
1948 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1950 if (PlainIn != NULL) {
1952 len = strlen((const char*)PlainIn);
1959 pch = (const char*)In->buf;
1960 pche = pch + In->BufUsed;
1967 pt = OutBuf->buf + OutBuf->BufUsed;
1968 /**< we max append 6 chars at once plus the \0 */
1969 pte = OutBuf->buf + OutBuf->BufSize - 6;
1971 while (pch < pche) {
1973 OutBuf->BufUsed = pt - OutBuf->buf;
1974 IncreaseBuf(OutBuf, 1, -1);
1975 pte = OutBuf->buf + OutBuf->BufSize - 6;
1976 /**< we max append 3 chars at once plus the \0 */
1978 pt = OutBuf->buf + OutBuf->BufUsed;
1982 memcpy(pt, HKEY("<"));
1986 else if (*pch == '>') {
1987 memcpy(pt, HKEY(">"));
1991 else if (*pch == '&') {
1992 memcpy(pt, HKEY("&"));
1996 else if ((*pch >= 0x20) && (*pch <= 0x7F)) {
2000 else if (*pch < 0x20) {
2001 /* we probably shouldn't be doing this */
2002 if (OverrideLowChars)
2012 *pt = HexList[*(unsigned char*)pch][0];
2014 *pt = HexList[*(unsigned char*)pch][1];
2022 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(pch, pche);
2025 while (IsUtf8Sequence > 0){
2036 *pt = HexList[*(unsigned char*)pch][0];
2038 *pt = HexList[*(unsigned char*)pch][1];
2047 OutBuf->BufUsed = pt - OutBuf->buf;
2052 * @ingroup StrBuf_DeEnCoder
2053 * @brief append a string in hex encoding to the buffer
2054 * @param OutBuf the output buffer
2055 * @param In Buffer to encode
2056 * @param PlainIn way in from plain old c strings
2057 * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
2059 void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
2061 const unsigned char *pch, *pche;
2065 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
2067 if (PlainIn != NULL) {
2069 len = strlen((const char*)PlainIn);
2076 pch = (const unsigned char*)In->buf;
2077 pche = pch + In->BufUsed;
2084 pt = OutBuf->buf + OutBuf->BufUsed;
2085 pte = OutBuf->buf + OutBuf->BufSize - 3; /**< we max append 3 chars at once plus the \0 */
2087 while (pch < pche) {
2089 IncreaseBuf(OutBuf, 1, -1);
2090 pte = OutBuf->buf + OutBuf->BufSize - 3; /**< we max append 3 chars at once plus the \0 */
2091 pt = OutBuf->buf + OutBuf->BufUsed;
2094 *pt = HexList[*pch][0];
2096 *pt = HexList[*pch][1];
2097 pt ++; pch ++; OutBuf->BufUsed += 2;
2102 void StrBufBase64Append(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn, long PlainInLen, int linebreaks)
2109 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
2111 if (PlainIn != NULL) {
2113 len = strlen(PlainIn);
2126 ExpectLen = ((len * 134) / 100) + OutBuf->BufUsed;
2128 if (ExpectLen > OutBuf->BufSize)
2129 if (IncreaseBuf(OutBuf, 1, ExpectLen) < ExpectLen)
2132 pt = OutBuf->buf + OutBuf->BufUsed;
2134 len = CtdlEncodeBase64(pt, pch, len, linebreaks);
2137 OutBuf->BufUsed += len;
2142 * @ingroup StrBuf_DeEnCoder
2143 * @brief append a string in hex encoding to the buffer
2144 * @param OutBuf the output buffer
2145 * @param In Buffer to encode
2146 * @param PlainIn way in from plain old c strings
2148 void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
2150 StrBufHexEscAppend(OutBuf, In, (const unsigned char*) PlainIn, -1);
2154 * @ingroup StrBuf_DeEnCoder
2155 * @brief Append a string, escaping characters which have meaning in HTML.
2157 * @param Target target buffer
2158 * @param Source source buffer; set to NULL if you just have a C-String
2159 * @param PlainIn Plain-C string to append; set to NULL if unused
2160 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
2161 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
2162 * if set to 2, linebreaks are replaced by <br/>
2164 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
2166 const char *aptr, *eiptr;
2170 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2173 if (PlainIn != NULL) {
2175 len = strlen(PlainIn);
2180 eiptr = aptr + Source->BufUsed;
2181 len = Source->BufUsed;
2187 bptr = Target->buf + Target->BufUsed;
2188 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2190 while (aptr < eiptr){
2192 IncreaseBuf(Target, 1, -1);
2193 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2194 bptr = Target->buf + Target->BufUsed;
2197 memcpy(bptr, "<", 4);
2199 Target->BufUsed += 4;
2201 else if (*aptr == '>') {
2202 memcpy(bptr, ">", 4);
2204 Target->BufUsed += 4;
2206 else if (*aptr == '&') {
2207 memcpy(bptr, "&", 5);
2209 Target->BufUsed += 5;
2211 else if (*aptr == '"') {
2212 memcpy(bptr, """, 6);
2214 Target->BufUsed += 6;
2216 else if (*aptr == '\'') {
2217 memcpy(bptr, "'", 5);
2219 Target->BufUsed += 5;
2221 else if (*aptr == LB) {
2226 else if (*aptr == RB) {
2231 else if (*aptr == QU) {
2236 else if ((*aptr == 32) && (nbsp == 1)) {
2237 memcpy(bptr, " ", 6);
2239 Target->BufUsed += 6;
2241 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
2242 *bptr='\0'; /* nothing */
2244 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
2245 memcpy(bptr, "<br/>", 11);
2247 Target->BufUsed += 11;
2251 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
2252 *bptr='\0'; /* nothing */
2262 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
2264 return Target->BufUsed;
2268 * @ingroup StrBuf_DeEnCoder
2269 * @brief Append a string, escaping characters which have meaning in HTML.
2270 * Converts linebreaks into blanks; escapes single quotes
2271 * @param Target target buffer
2272 * @param Source source buffer; set to NULL if you just have a C-String
2273 * @param PlainIn Plain-C string to append; set to NULL if unused
2275 void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
2277 const char *aptr, *eiptr;
2281 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2284 if (PlainIn != NULL) {
2286 len = strlen(PlainIn);
2291 eiptr = aptr + Source->BufUsed;
2292 len = Source->BufUsed;
2298 eptr = Target->buf + Target->BufSize - 8;
2299 tptr = Target->buf + Target->BufUsed;
2301 while (aptr < eiptr){
2303 IncreaseBuf(Target, 1, -1);
2304 eptr = Target->buf + Target->BufSize - 8;
2305 tptr = Target->buf + Target->BufUsed;
2308 if (*aptr == '\n') {
2312 else if (*aptr == '\r') {
2316 else if (*aptr == '\'') {
2322 Target->BufUsed += 5;
2335 * @ingroup StrBuf_DeEnCoder
2336 * @brief Append a string, escaping characters which have meaning in ICAL.
2338 * @param Target target buffer
2339 * @param Source source buffer; set to NULL if you just have a C-String
2340 * @param PlainIn Plain-C string to append; set to NULL if unused
2342 void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
2344 const char *aptr, *eiptr;
2348 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2351 if (PlainIn != NULL) {
2353 len = strlen(PlainIn);
2358 eiptr = aptr + Source->BufUsed;
2359 len = Source->BufUsed;
2365 eptr = Target->buf + Target->BufSize - 8;
2366 tptr = Target->buf + Target->BufUsed;
2368 while (aptr < eiptr){
2369 if(tptr + 3 >= eptr) {
2370 IncreaseBuf(Target, 1, -1);
2371 eptr = Target->buf + Target->BufSize - 8;
2372 tptr = Target->buf + Target->BufUsed;
2375 if (*aptr == '\n') {
2382 else if (*aptr == '\r') {
2389 else if (*aptr == ',') {
2405 * @ingroup StrBuf_DeEnCoder
2406 * @brief Append a string, escaping characters which have meaning in JavaScript strings .
2408 * @param Target target buffer
2409 * @param Source source buffer; set to NULL if you just have a C-String
2410 * @param PlainIn Plain-C string to append; set to NULL if unused
2411 * @returns size of result or -1
2413 long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
2415 const char *aptr, *eiptr;
2420 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2423 if (PlainIn != NULL) {
2425 len = strlen(PlainIn);
2430 eiptr = aptr + Source->BufUsed;
2431 len = Source->BufUsed;
2437 bptr = Target->buf + Target->BufUsed;
2438 eptr = Target->buf + Target->BufSize - 7; /* our biggest unit to put in... */
2440 while (aptr < eiptr){
2442 IncreaseBuf(Target, 1, -1);
2443 eptr = Target->buf + Target->BufSize - 7; /* our biggest unit to put in... */
2444 bptr = Target->buf + Target->BufUsed;
2448 memcpy(bptr, HKEY("\\n"));
2450 Target->BufUsed += 2;
2453 memcpy(bptr, HKEY("\\r"));
2455 Target->BufUsed += 2;
2462 Target->BufUsed += 2;
2465 if ((*(aptr + 1) == 'u') &&
2466 isxdigit(*(aptr + 2)) &&
2467 isxdigit(*(aptr + 3)) &&
2468 isxdigit(*(aptr + 4)) &&
2469 isxdigit(*(aptr + 5)))
2470 { /* oh, a unicode escaper. let it pass through. */
2471 memcpy(bptr, aptr, 6);
2474 Target->BufUsed += 6;
2482 Target->BufUsed += 2;
2490 Target->BufUsed += 2;
2497 Target->BufUsed += 2;
2504 Target->BufUsed += 2;
2507 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
2508 while (IsUtf8Sequence > 0){
2511 if (--IsUtf8Sequence)
2519 if ((bptr == eptr - 1 ) && !IsEmptyStr(aptr) )
2521 return Target->BufUsed;
2525 * @ingroup StrBuf_DeEnCoder
2526 * @brief Append a string, escaping characters which have meaning in HTML + json.
2528 * @param Target target buffer
2529 * @param Source source buffer; set to NULL if you just have a C-String
2530 * @param PlainIn Plain-C string to append; set to NULL if unused
2531 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
2532 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
2533 * if set to 2, linebreaks are replaced by <br/>
2535 long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
2537 const char *aptr, *eiptr;
2540 int IsUtf8Sequence = 0;
2542 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2545 if (PlainIn != NULL) {
2547 len = strlen(PlainIn);
2552 eiptr = aptr + Source->BufUsed;
2553 len = Source->BufUsed;
2559 bptr = Target->buf + Target->BufUsed;
2560 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2562 while (aptr < eiptr){
2564 IncreaseBuf(Target, 1, -1);
2565 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2566 bptr = Target->buf + Target->BufUsed;
2570 memcpy(bptr, HKEY("<"));
2572 Target->BufUsed += 4;
2575 memcpy(bptr, HKEY(">"));
2577 Target->BufUsed += 4;
2580 memcpy(bptr, HKEY("&"));
2582 Target->BufUsed += 5;
2595 switch (nolinebreaks) {
2597 *bptr='\0'; /* nothing */
2600 memcpy(bptr, HKEY("<br/>"));
2602 Target->BufUsed += 11;
2605 memcpy(bptr, HKEY("\\n"));
2607 Target->BufUsed += 2;
2611 switch (nolinebreaks) {
2614 *bptr='\0'; /* nothing */
2617 memcpy(bptr, HKEY("\\r"));
2619 Target->BufUsed += 2;
2629 Target->BufUsed += 2;
2632 if ((*(aptr + 1) == 'u') &&
2633 isxdigit(*(aptr + 2)) &&
2634 isxdigit(*(aptr + 3)) &&
2635 isxdigit(*(aptr + 4)) &&
2636 isxdigit(*(aptr + 5)))
2637 { /* oh, a unicode escaper. let it pass through. */
2638 memcpy(bptr, aptr, 6);
2641 Target->BufUsed += 6;
2649 Target->BufUsed += 2;
2657 Target->BufUsed += 2;
2664 Target->BufUsed += 2;
2671 Target->BufUsed += 2;
2675 memcpy(bptr, HKEY(" "));
2677 Target->BufUsed += 6;
2681 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
2682 while (IsUtf8Sequence > 0){
2685 if (--IsUtf8Sequence)
2693 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
2695 return Target->BufUsed;
2700 * @ingroup StrBuf_DeEnCoder
2701 * @brief replace all non-Ascii characters by another
2702 * @param Buf buffer to inspect
2703 * @param repl charater to stamp over non ascii chars
2705 void StrBufAsciify(StrBuf *Buf, const char repl)
2709 for (offset = 0; offset < Buf->BufUsed; offset ++)
2710 if (!isascii(Buf->buf[offset]))
2711 Buf->buf[offset] = repl;
2716 * @ingroup StrBuf_DeEnCoder
2717 * @brief unhide special chars hidden to the HTML escaper
2718 * @param target buffer to put the unescaped string in
2719 * @param source buffer to unescape
2721 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
2726 if ((source == NULL) || (target == NULL) || (target->buf == NULL))
2732 FlushStrBuf(target);
2734 len = source->BufUsed;
2735 for (a = 0; a < len; ++a) {
2736 if (target->BufUsed >= target->BufSize)
2737 IncreaseBuf(target, 1, -1);
2739 if (source->buf[a] == '=') {
2740 hex[0] = source->buf[a + 1];
2741 hex[1] = source->buf[a + 2];
2744 sscanf(hex, "%02x", &b);
2745 target->buf[target->BufUsed] = b;
2746 target->buf[++target->BufUsed] = 0;
2750 target->buf[target->BufUsed] = source->buf[a];
2751 target->buf[++target->BufUsed] = 0;
2758 * @ingroup StrBuf_DeEnCoder
2759 * @brief hide special chars from the HTML escapers and friends
2760 * @param target buffer to put the escaped string in
2761 * @param source buffer to escape
2763 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
2768 FlushStrBuf(target);
2770 if ((source == NULL) || (target == NULL) || (target->buf == NULL))
2775 len = source->BufUsed;
2776 for (i=0; i<len; ++i) {
2777 if (target->BufUsed + 4 >= target->BufSize)
2778 IncreaseBuf(target, 1, -1);
2779 if ( (isalnum(source->buf[i])) ||
2780 (source->buf[i]=='-') ||
2781 (source->buf[i]=='_') ) {
2782 target->buf[target->BufUsed++] = source->buf[i];
2785 sprintf(&target->buf[target->BufUsed],
2787 (0xFF &source->buf[i]));
2788 target->BufUsed += 3;
2791 target->buf[target->BufUsed + 1] = '\0';
2795 /*******************************************************************************
2796 * Quoted Printable de/encoding *
2797 *******************************************************************************/
2800 * @ingroup StrBuf_DeEnCoder
2801 * @brief decode a buffer from base 64 encoding; destroys original
2802 * @param Buf Buffor to transform
2804 int StrBufDecodeBase64(StrBuf *Buf)
2812 xferbuf = (char*) malloc(Buf->BufSize);
2813 if (xferbuf == NULL)
2817 siz = CtdlDecodeBase64(xferbuf,
2827 * @ingroup StrBuf_DeEnCoder
2828 * @brief decode a buffer from base 64 encoding; destroys original
2829 * @param Buf Buffor to transform
2831 int StrBufDecodeHex(StrBuf *Buf)
2834 char *pch, *pche, *pchi;
2836 if (Buf == NULL) return -1;
2838 pch = pchi = Buf->buf;
2839 pche = pch + Buf->BufUsed;
2841 while (pchi < pche){
2842 ch = decode_hex(pchi);
2849 Buf->BufUsed = pch - Buf->buf;
2850 return Buf->BufUsed;
2854 * @ingroup StrBuf_DeEnCoder
2855 * @brief replace all chars >0x20 && < 0x7F with Mute
2856 * @param Mute char to put over invalid chars
2857 * @param Buf Buffor to transform
2859 int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
2863 if (Buf == NULL) return -1;
2864 pch = (unsigned char *)Buf->buf;
2865 while (pch < (unsigned char *)Buf->buf + Buf->BufUsed) {
2866 if ((*pch < 0x20) || (*pch > 0x7F))
2870 return Buf->BufUsed;
2875 * @ingroup StrBuf_DeEnCoder
2876 * @brief remove escaped strings from i.e. the url string (like %20 for blanks)
2877 * @param Buf Buffer to translate
2878 * @param StripBlanks Reduce several blanks to one?
2880 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
2889 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
2890 Buf->buf[Buf->BufUsed - 1] = '\0';
2895 while (a < Buf->BufUsed) {
2896 if (Buf->buf[a] == '+')
2898 else if (Buf->buf[a] == '%') {
2899 /* don't let % chars through, rather truncate the input. */
2900 if (a + 2 > Buf->BufUsed) {
2905 hex[0] = Buf->buf[a + 1];
2906 hex[1] = Buf->buf[a + 2];
2909 sscanf(hex, "%02x", &b);
2910 Buf->buf[a] = (char) b;
2911 len = Buf->BufUsed - a - 2;
2913 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
2925 * @ingroup StrBuf_DeEnCoder
2926 * @brief RFC2047-encode a header field if necessary.
2927 * If no non-ASCII characters are found, the string
2928 * will be copied verbatim without encoding.
2930 * @param target Target buffer.
2931 * @param source Source string to be encoded.
2932 * @returns encoded length; -1 if non success.
2934 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
2936 const char headerStr[] = "=?UTF-8?Q?";
2937 int need_to_encode = 0;
2941 if ((source == NULL) ||
2945 while ((i < source->BufUsed) &&
2946 (!IsEmptyStr (&source->buf[i])) &&
2947 (need_to_encode == 0)) {
2948 if (((unsigned char) source->buf[i] < 32) ||
2949 ((unsigned char) source->buf[i] > 126)) {
2955 if (!need_to_encode) {
2956 if (*target == NULL) {
2957 *target = NewStrBufPlain(source->buf, source->BufUsed);
2960 FlushStrBuf(*target);
2961 StrBufAppendBuf(*target, source, 0);
2964 return (*target)->BufUsed;
2968 if (*target == NULL)
2969 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
2970 else if (sizeof(headerStr) + source->BufUsed >= (*target)->BufSize)
2971 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
2972 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
2973 (*target)->BufUsed = sizeof(headerStr) - 1;
2974 for (i=0; (i < source->BufUsed); ++i) {
2975 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
2976 IncreaseBuf(*target, 1, 0);
2977 ch = (unsigned char) source->buf[i];
2987 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
2988 (*target)->BufUsed += 3;
2992 (*target)->buf[(*target)->BufUsed] = '_';
2994 (*target)->buf[(*target)->BufUsed] = ch;
2995 (*target)->BufUsed++;
2999 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
3000 IncreaseBuf(*target, 1, 0);
3002 (*target)->buf[(*target)->BufUsed++] = '?';
3003 (*target)->buf[(*target)->BufUsed++] = '=';
3004 (*target)->buf[(*target)->BufUsed] = '\0';
3005 return (*target)->BufUsed;;
3009 * @ingroup StrBuf_DeEnCoder
3010 * @brief Quoted-Printable encode a message; make it < 80 columns width.
3011 * @param source Source string to be encoded.
3012 * @returns buffer with encoded message.
3014 StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
3018 const char *ptr, *eptr;
3022 OutBuf = NewStrBufPlain(NULL, StrLength(EncodeMe) * 4);
3024 OEptr = OutBuf->buf + OutBuf->BufSize;
3025 ptr = EncodeMe->buf;
3026 eptr = EncodeMe->buf + EncodeMe->BufUsed;
3031 if (Optr + 4 >= OEptr)
3034 Offset = Optr - OutBuf->buf;
3035 OutBuf->BufUsed = Optr - OutBuf->buf;
3036 IncreaseBuf(OutBuf, 1, 0);
3037 Optr = OutBuf->buf + Offset;
3038 OEptr = OutBuf->buf + OutBuf->BufSize;
3040 if ((*ptr == '\r') || (*ptr == '\n'))
3042 /* ignore carriage returns */
3045 else if (*ptr == 10) {
3046 /* hard line break */
3047 if ((LinePos > 0) && (isspace(*(Optr-1))))
3049 memcpy(Optr, HKEY("=0A"));
3055 else if (( (*ptr >= 32) && (*ptr <= 60) ) ||
3056 ( (*ptr >= 62) && (*ptr <= 126) ))
3067 *Optr = HexList[ch][0];
3069 *Optr = HexList[ch][1];
3076 /* soft line break */
3077 if (isspace(*(Optr - 1))) {
3082 *Optr = HexList[ch][0];
3084 *Optr = HexList[ch][1];
3096 OutBuf->BufUsed = Optr - OutBuf->buf;
3102 static void AddRecipient(StrBuf *Target,
3104 StrBuf *EmailAddress,
3109 if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
3110 if (strchr(ChrPtr(UserName), ',') != NULL) QuoteMe = 1;
3112 if (QuoteMe) StrBufAppendBufPlain(Target, HKEY("\""), 0);
3113 StrBufRFC2047encode(&EncBuf, UserName);
3114 StrBufAppendBuf(Target, EncBuf, 0);
3115 if (QuoteMe) StrBufAppendBufPlain(Target, HKEY("\" "), 0);
3116 else StrBufAppendBufPlain(Target, HKEY(" "), 0);
3118 if (StrLength(EmailAddress) > 0){
3119 StrBufAppendBufPlain(Target, HKEY("<"), 0);
3120 StrBufAppendBuf(Target, EmailAddress, 0); /* TODO: what about IDN???? */
3121 StrBufAppendBufPlain(Target, HKEY(">"), 0);
3127 * \brief QP encode parts of an email TO/CC/BCC vector, and strip/filter invalid parts
3128 * \param Recp Source list of email recipients
3129 * \param UserName Temporary buffer for internal use; Please provide valid buffer.
3130 * \param EmailAddress Temporary buffer for internal use; Please provide valid buffer.
3131 * \param EncBuf Temporary buffer for internal use; Please provide valid buffer.
3132 * \returns encoded & sanitized buffer with the contents of Recp; Caller owns this memory.
3134 StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
3136 StrBuf *EmailAddress,
3140 const char *pch, *pche;
3141 const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
3143 if ((Recp == NULL) || (StrLength(Recp) == 0))
3147 pche = pch + StrLength(Recp);
3149 if (!CheckEncode(pch, -1, pche))
3150 return NewStrBufDup(Recp);
3152 Target = NewStrBufPlain(NULL, StrLength(Recp));
3154 while ((pch != NULL) && (pch < pche))
3156 while (isspace(*pch)) pch++;
3157 UserEnd = EmailStart = EmailEnd = NULL;
3159 if ((*pch == '"') || (*pch == '\'')) {
3160 UserStart = pch + 1;
3162 UserEnd = strchr(UserStart, *pch);
3163 if (UserEnd == NULL)
3164 break; ///TODO: Userfeedback??
3165 EmailStart = UserEnd + 1;
3166 while (isspace(*EmailStart))
3168 if (UserEnd == UserStart) {
3169 UserStart = UserEnd = NULL;
3172 if (*EmailStart == '<') {
3174 EmailEnd = strchr(EmailStart, '>');
3175 if (EmailEnd == NULL)
3176 EmailEnd = strchr(EmailStart, ',');
3180 EmailEnd = strchr(EmailStart, ',');
3182 if (EmailEnd == NULL)
3189 EmailEnd = strchr(UserStart, ',');
3190 if (EmailEnd == NULL) {
3191 EmailEnd = strchr(pch, '>');
3193 if (EmailEnd != NULL) {
3203 while ((EmailEnd > UserStart) && !gt &&
3204 ((*EmailEnd == ',') ||
3205 (*EmailEnd == '>') ||
3206 (isspace(*EmailEnd))))
3208 if (*EmailEnd == '>')
3213 if (EmailEnd == UserStart)
3217 EmailStart = strchr(UserStart, '<');
3218 if ((EmailStart == NULL) || (EmailStart > EmailEnd))
3220 UserEnd = EmailStart;
3222 while ((UserEnd > UserStart) &&
3223 isspace (*(UserEnd - 1)))
3226 if (UserStart >= UserEnd)
3227 UserStart = UserEnd = NULL;
3229 else { /* this is a local recipient... no domain, just a realname */
3230 EmailStart = UserStart;
3231 At = strchr(EmailStart, '@');
3237 EmailStart = UserStart;
3243 if ((UserStart != NULL) && (UserEnd != NULL))
3244 StrBufPlain(UserName, UserStart, UserEnd - UserStart);
3245 else if ((UserStart != NULL) && (UserEnd == NULL))
3246 StrBufPlain(UserName, UserStart, UserEnd - UserStart);
3248 FlushStrBuf(UserName);
3250 if ((EmailStart != NULL) && (EmailEnd != NULL))
3251 StrBufPlain(EmailAddress, EmailStart, EmailEnd - EmailStart);
3252 else if ((EmailStart != NULL) && (EmailEnd == NULL))
3253 StrBufPlain(EmailAddress, EmailStart, EmailEnd - pche);
3255 FlushStrBuf(EmailAddress);
3257 AddRecipient(Target, UserName, EmailAddress, EncBuf);
3262 if ((pch != NULL) && (*pch == ','))
3264 if (pch != NULL) while (isspace(*pch))
3273 * @brief replaces all occurances of 'search' by 'replace'
3274 * @param buf Buffer to modify
3275 * @param search character to search
3276 * @param replace character to replace search by
3278 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
3283 for (i=0; i<buf->BufUsed; i++)
3284 if (buf->buf[i] == search)
3285 buf->buf[i] = replace;
3291 * @brief removes all \\r s from the string, or replaces them with \n if its not a combination of both.
3292 * @param buf Buffer to modify
3294 void StrBufToUnixLF(StrBuf *buf)
3296 char *pche, *pchS, *pchT;
3300 pche = buf->buf + buf->BufUsed;
3301 pchS = pchT = buf->buf;
3307 if (*pchS != '\n') {
3316 buf->BufUsed = pchT - buf->buf;
3320 /*******************************************************************************
3321 * Iconv Wrapper; RFC822 de/encoding *
3322 *******************************************************************************/
3325 * @ingroup StrBuf_DeEnCoder
3326 * @brief Wrapper around iconv_open()
3327 * Our version adds aliases for non-standard Microsoft charsets
3328 * such as 'MS950', aliasing them to names like 'CP950'
3330 * @param tocode Target encoding
3331 * @param fromcode Source encoding
3332 * @param pic anonimized pointer to iconv struct
3334 void ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
3337 iconv_t ic = (iconv_t)(-1) ;
3338 ic = iconv_open(tocode, fromcode);
3339 if (ic == (iconv_t)(-1) ) {
3340 char alias_fromcode[64];
3341 if ( (strlen(fromcode) == 5) && (!strncasecmp(fromcode, "MS", 2)) ) {
3342 safestrncpy(alias_fromcode, fromcode, sizeof alias_fromcode);
3343 alias_fromcode[0] = 'C';
3344 alias_fromcode[1] = 'P';
3345 ic = iconv_open(tocode, alias_fromcode);
3348 *(iconv_t *)pic = ic;
3354 * @ingroup StrBuf_DeEnCoder
3355 * @brief find one chunk of a RFC822 encoded string
3356 * @param Buffer where to search
3357 * @param bptr where to start searching
3358 * @returns found position, NULL if none.
3360 static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr)
3363 /* Find the next ?Q? */
3364 if (Buf->BufUsed - (bptr - Buf->buf) < 6)
3367 end = strchr(bptr + 2, '?');
3372 if ((Buf->BufUsed - (end - Buf->buf) > 3) &&
3373 (((*(end + 1) == 'B') || (*(end + 1) == 'Q')) ||
3374 ((*(end + 1) == 'b') || (*(end + 1) == 'q'))) &&
3375 (*(end + 2) == '?')) {
3376 /* skip on to the end of the cluster, the next ?= */
3377 end = strstr(end + 3, "?=");
3380 /* sort of half valid encoding, try to find an end. */
3381 end = strstr(bptr, "?=");
3388 * @ingroup StrBuf_DeEnCoder
3389 * @brief convert one buffer according to the preselected iconv pointer PIC
3390 * @param ConvertBuf buffer we need to translate
3391 * @param TmpBuf To share a workbuffer over several iterations. prepare to have it filled with useless stuff afterwards.
3392 * @param pic Pointer to the iconv-session Object
3394 void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
3400 char *ibuf; /**< Buffer of characters to be converted */
3401 char *obuf; /**< Buffer for converted characters */
3402 size_t ibuflen; /**< Length of input buffer */
3403 size_t obuflen; /**< Length of output buffer */
3406 if ((ConvertBuf == NULL) || (TmpBuf == NULL))
3409 /* since we're converting to utf-8, one glyph may take up to 6 bytes */
3410 if (ConvertBuf->BufUsed * 6 >= TmpBuf->BufSize)
3411 IncreaseBuf(TmpBuf, 0, ConvertBuf->BufUsed * 6);
3413 ic = *(iconv_t*)pic;
3414 ibuf = ConvertBuf->buf;
3415 ibuflen = ConvertBuf->BufUsed;
3417 obuflen = TmpBuf->BufSize;
3419 siz = iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
3422 if (errno == E2BIG) {
3424 IncreaseBuf(TmpBuf, 0, 0);
3429 else if (errno == EILSEQ){
3430 /* hm, invalid utf8 sequence... what to do now? */
3431 /* An invalid multibyte sequence has been encountered in the input */
3433 else if (errno == EINVAL) {
3434 /* An incomplete multibyte sequence has been encountered in the input. */
3437 FlushStrBuf(TmpBuf);
3440 TmpBuf->BufUsed = TmpBuf->BufSize - obuflen;
3441 TmpBuf->buf[TmpBuf->BufUsed] = '\0';
3443 /* little card game: wheres the red lady? */
3444 SwapBuffers(ConvertBuf, TmpBuf);
3445 FlushStrBuf(TmpBuf);
3452 * @ingroup StrBuf_DeEnCoder
3453 * @brief catches one RFC822 encoded segment, and decodes it.
3454 * @param Target buffer to fill with result
3455 * @param DecodeMe buffer with stuff to process
3456 * @param SegmentStart points to our current segment in DecodeMe
3457 * @param SegmentEnd Points to the end of our current segment in DecodeMe
3458 * @param ConvertBuf Workbuffer shared between several iterations. Random content; needs to be valid
3459 * @param ConvertBuf2 Workbuffer shared between several iterations. Random content; needs to be valid
3460 * @param FoundCharset Characterset to default decoding to; if we find another we will overwrite it.
3462 inline static void DecodeSegment(StrBuf *Target,
3463 const StrBuf *DecodeMe,
3464 const char *SegmentStart,
3465 const char *SegmentEnd,
3467 StrBuf *ConvertBuf2,
3468 StrBuf *FoundCharset)
3474 iconv_t ic = (iconv_t)(-1);
3478 /* Now we handle foreign character sets properly encoded
3479 * in RFC2047 format.
3481 StaticBuf.buf = (char*) SegmentStart; /*< it will just be read there... */
3482 StaticBuf.BufUsed = SegmentEnd - SegmentStart;
3483 StaticBuf.BufSize = DecodeMe->BufSize - (SegmentStart - DecodeMe->buf);
3484 extract_token(charset, SegmentStart, 1, '?', sizeof charset);
3485 if (FoundCharset != NULL) {
3486 FlushStrBuf(FoundCharset);
3487 StrBufAppendBufPlain(FoundCharset, charset, -1, 0);
3489 extract_token(encoding, SegmentStart, 2, '?', sizeof encoding);
3490 StrBufExtract_token(ConvertBuf, &StaticBuf, 3, '?');
3492 *encoding = toupper(*encoding);
3493 if (*encoding == 'B') { /**< base64 */
3494 if (ConvertBuf2->BufSize < ConvertBuf->BufUsed)
3495 IncreaseBuf(ConvertBuf2, 0, ConvertBuf->BufUsed);
3496 ConvertBuf2->BufUsed = CtdlDecodeBase64(ConvertBuf2->buf,
3498 ConvertBuf->BufUsed);
3500 else if (*encoding == 'Q') { /**< quoted-printable */
3504 while (pos < ConvertBuf->BufUsed)
3506 if (ConvertBuf->buf[pos] == '_')
3507 ConvertBuf->buf[pos] = ' ';
3511 if (ConvertBuf2->BufSize < ConvertBuf->BufUsed)
3512 IncreaseBuf(ConvertBuf2, 0, ConvertBuf->BufUsed);
3514 ConvertBuf2->BufUsed = CtdlDecodeQuotedPrintable(
3517 ConvertBuf->BufUsed);
3520 StrBufAppendBuf(ConvertBuf2, ConvertBuf, 0);
3523 ctdl_iconv_open("UTF-8", charset, &ic);
3524 if (ic != (iconv_t)(-1) ) {
3526 StrBufConvert(ConvertBuf2, ConvertBuf, &ic);
3527 StrBufAppendBuf(Target, ConvertBuf2, 0);
3532 StrBufAppendBufPlain(Target, HKEY("(unreadable)"), 0);
3538 * @ingroup StrBuf_DeEnCoder
3539 * @brief Handle subjects with RFC2047 encoding such as: [deprecated old syntax!]
3540 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
3541 * @param Target where to put the decoded string to
3542 * @param DecodeMe buffer with encoded string
3543 * @param DefaultCharset if we don't find one, which should we use?
3544 * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string,
3545 * put it here for later use where no string might be known.
3547 void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset)
3550 StrBuf *ConvertBuf2;
3551 ConvertBuf = NewStrBufPlain(NULL, StrLength(DecodeMe));
3552 ConvertBuf2 = NewStrBufPlain(NULL, StrLength(DecodeMe));
3554 StrBuf_RFC822_2_Utf8(Target,
3560 FreeStrBuf(&ConvertBuf);
3561 FreeStrBuf(&ConvertBuf2);
3565 * @ingroup StrBuf_DeEnCoder
3566 * @brief Handle subjects with RFC2047 encoding such as:
3567 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
3568 * @param Target where to put the decoded string to
3569 * @param DecodeMe buffer with encoded string
3570 * @param DefaultCharset if we don't find one, which should we use?
3571 * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string,
3572 * put it here for later use where no string might be known.
3573 * @param ConvertBuf workbuffer. feed in, you shouldn't care about its content.
3574 * @param ConvertBuf2 workbuffer. feed in, you shouldn't care about its content.
3576 void StrBuf_RFC822_2_Utf8(StrBuf *Target,
3577 const StrBuf *DecodeMe,
3578 const StrBuf* DefaultCharset,
3579 StrBuf *FoundCharset,
3581 StrBuf *ConvertBuf2)
3583 StrBuf *DecodedInvalidBuf = NULL;
3584 const StrBuf *DecodeMee = DecodeMe;
3585 const char *start, *end, *next, *nextend, *ptr = NULL;
3587 iconv_t ic = (iconv_t)(-1) ;
3592 int illegal_non_rfc2047_encoding = 0;
3595 if (DecodeMe == NULL)
3597 /* Sometimes, badly formed messages contain strings which were simply
3598 * written out directly in some foreign character set instead of
3599 * using RFC2047 encoding. This is illegal but we will attempt to
3600 * handle it anyway by converting from a user-specified default
3601 * charset to UTF-8 if we see any nonprintable characters.
3604 for (i=0; i<DecodeMe->BufUsed; ++i) {
3605 if ((DecodeMe->buf[i] < 32) || (DecodeMe->buf[i] > 126)) {
3606 illegal_non_rfc2047_encoding = 1;
3611 if ((illegal_non_rfc2047_encoding) &&
3612 (strcasecmp(ChrPtr(DefaultCharset), "UTF-8")) &&
3613 (strcasecmp(ChrPtr(DefaultCharset), "us-ascii")) )
3616 ctdl_iconv_open("UTF-8", ChrPtr(DefaultCharset), &ic);
3617 if (ic != (iconv_t)(-1) ) {
3618 DecodedInvalidBuf = NewStrBufDup(DecodeMe);
3619 StrBufConvert(DecodedInvalidBuf, ConvertBuf, &ic);///TODO: don't void const?
3620 DecodeMee = DecodedInvalidBuf;
3626 /* pre evaluate the first pair */
3628 start = strstr(DecodeMee->buf, "=?");
3629 eptr = DecodeMee->buf + DecodeMee->BufUsed;
3631 end = FindNextEnd (DecodeMee, start + 2);
3633 StrBufAppendBuf(Target, DecodeMee, 0);
3634 FreeStrBuf(&DecodedInvalidBuf);
3639 if (start != DecodeMee->buf) {
3642 nFront = start - DecodeMee->buf;
3643 StrBufAppendBufPlain(Target, DecodeMee->buf, nFront, 0);
3646 * Since spammers will go to all sorts of absurd lengths to get their
3647 * messages through, there are LOTS of corrupt headers out there.
3648 * So, prevent a really badly formed RFC2047 header from throwing
3649 * this function into an infinite loop.
3651 while ((start != NULL) &&
3658 DecodeSegment(Target,
3666 next = strstr(end, "=?");
3668 if ((next != NULL) &&
3670 nextend = FindNextEnd(DecodeMee, next);
3671 if (nextend == NULL)
3674 /* did we find two partitions */
3675 if ((next != NULL) &&
3679 while ((ptr < next) &&
3686 * did we find a gab just filled with blanks?
3687 * if not, copy its stuff over.
3691 StrBufAppendBufPlain(Target,
3697 /* our next-pair is our new first pair now. */
3703 nextend = DecodeMee->buf + DecodeMee->BufUsed;
3704 if ((end != NULL) && (end < nextend)) {
3706 while ( (ptr < nextend) &&
3713 StrBufAppendBufPlain(Target, end, nextend - end, 0);
3715 FreeStrBuf(&DecodedInvalidBuf);
3718 /*******************************************************************************
3719 * Manipulating UTF-8 Strings *
3720 *******************************************************************************/
3724 * @brief evaluate the length of an utf8 special character sequence
3725 * @param Char the character to examine
3726 * @returns width of utf8 chars in bytes; if the sequence is broken 0 is returned; 1 if its simply ASCII.
3728 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
3731 unsigned char test = (1<<7);
3733 if ((*CharS & 0xC0) != 0xC0)
3737 ((test & ((unsigned char)*CharS)) != 0))
3742 if ((n > 6) || ((CharE - CharS) < n))
3749 * @brief detect whether this char starts an utf-8 encoded char
3750 * @param Char character to inspect
3751 * @returns yes or no
3753 static inline int Ctdl_IsUtf8SequenceStart(const char Char)
3755 /** 11??.???? indicates an UTF8 Sequence. */
3756 return ((Char & 0xC0) == 0xC0);
3761 * @brief measure the number of glyphs in an UTF8 string...
3762 * @param Buf string to measure
3763 * @returns the number of glyphs in Buf
3765 long StrBuf_Utf8StrLen(StrBuf *Buf)
3771 if ((Buf == NULL) || (Buf->BufUsed == 0))
3774 eptr = Buf->buf + Buf->BufUsed;
3775 while ((aptr < eptr) && (*aptr != '\0')) {
3776 if (Ctdl_IsUtf8SequenceStart(*aptr)){
3777 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
3778 while ((aptr < eptr) && (*aptr++ != '\0')&& (m-- > 0) );
3791 * @brief cuts a string after maxlen glyphs
3792 * @param Buf string to cut to maxlen glyphs
3793 * @param maxlen how long may the string become?
3794 * @returns current length of the string
3796 long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
3802 eptr = Buf->buf + Buf->BufUsed;
3803 while ((aptr < eptr) && (*aptr != '\0')) {
3804 if (Ctdl_IsUtf8SequenceStart(*aptr)){
3805 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
3806 while ((*aptr++ != '\0') && (m-- > 0));
3815 Buf->BufUsed = aptr - Buf->buf;
3816 return Buf->BufUsed;
3819 return Buf->BufUsed;
3827 /*******************************************************************************
3829 *******************************************************************************/
3832 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
3833 #define OS_CODE 0x03 /*< unix */
3836 * @ingroup StrBuf_DeEnCoder
3837 * @brief uses the same calling syntax as compress2(), but it
3838 * creates a stream compatible with HTTP "Content-encoding: gzip"
3839 * @param dest compressed buffer
3840 * @param destLen length of the compresed data
3841 * @param source source to encode
3842 * @param sourceLen length of source to encode
3843 * @param level compression level
3845 int ZEXPORT compress_gzip(Bytef * dest,
3847 const Bytef * source,
3851 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
3853 /* write gzip header */
3854 snprintf((char *) dest, *destLen,
3855 "%c%c%c%c%c%c%c%c%c%c",
3856 gz_magic[0], gz_magic[1], Z_DEFLATED,
3857 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
3860 /* normal deflate */
3863 stream.next_in = (Bytef *) source;
3864 stream.avail_in = (uInt) sourceLen;
3865 stream.next_out = dest + 10L; // after header
3866 stream.avail_out = (uInt) * destLen;
3867 if ((uLong) stream.avail_out != *destLen)
3870 stream.zalloc = (alloc_func) 0;
3871 stream.zfree = (free_func) 0;
3872 stream.opaque = (voidpf) 0;
3874 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
3875 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
3879 err = deflate(&stream, Z_FINISH);
3880 if (err != Z_STREAM_END) {
3881 deflateEnd(&stream);
3882 return err == Z_OK ? Z_BUF_ERROR : err;
3884 *destLen = stream.total_out + 10L;
3886 /* write CRC and Length */
3887 uLong crc = crc32(0L, source, sourceLen);
3889 for (n = 0; n < 4; ++n, ++*destLen) {
3890 dest[*destLen] = (int) (crc & 0xff);
3893 uLong len = stream.total_in;
3894 for (n = 0; n < 4; ++n, ++*destLen) {
3895 dest[*destLen] = (int) (len & 0xff);
3898 err = deflateEnd(&stream);
3905 * @ingroup StrBuf_DeEnCoder
3906 * @brief compress the buffer with gzip
3907 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
3908 * @param Buf buffer whose content is to be gzipped
3910 int CompressBuffer(StrBuf *Buf)
3913 char *compressed_data = NULL;
3914 size_t compressed_len, bufsize;
3917 bufsize = compressed_len = Buf->BufUsed + (Buf->BufUsed / 100) + 100;
3918 compressed_data = malloc(compressed_len);
3920 if (compressed_data == NULL)
3922 /* Flush some space after the used payload so valgrind shuts up... */
3923 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
3924 Buf->buf[Buf->BufUsed + i++] = '\0';
3925 if (compress_gzip((Bytef *) compressed_data,
3928 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
3931 Buf->buf = compressed_data;
3932 Buf->BufUsed = compressed_len;
3933 Buf->BufSize = bufsize;
3934 /* Flush some space after the used payload so valgrind shuts up... */
3936 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
3937 Buf->buf[Buf->BufUsed + i++] = '\0';
3940 free(compressed_data);
3942 #endif /* HAVE_ZLIB */
3946 /*******************************************************************************
3947 * File I/O; Callbacks to libevent *
3948 *******************************************************************************/
3950 long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
3955 if ((FB == NULL) || (FB->Buf == NULL))
3959 * check whether the read pointer is somewhere in a range
3960 * where a cut left is inexpensive
3963 if (FB->ReadWritePointer != NULL)
3967 already_read = FB->ReadWritePointer - FB->Buf->buf;
3968 bufremain = FB->Buf->BufSize - FB->Buf->BufUsed - 1;
3970 if (already_read != 0) {
3973 unread = FB->Buf->BufUsed - already_read;
3975 /* else nothing to compact... */
3977 FB->ReadWritePointer = FB->Buf->buf;
3978 bufremain = FB->Buf->BufSize;
3980 else if ((unread < 64) ||
3981 (bufremain < already_read))
3984 * if its just a tiny bit remaining, or we run out of space...
3987 FB->Buf->BufUsed = unread;
3988 if (unread < already_read)
3989 memcpy(FB->Buf->buf, FB->ReadWritePointer, unread);
3991 memmove(FB->Buf->buf, FB->ReadWritePointer, unread);
3992 FB->ReadWritePointer = FB->Buf->buf;
3993 bufremain = FB->Buf->BufSize - unread - 1;
3995 else if (bufremain < (FB->Buf->BufSize / 10))
3997 /* get a bigger buffer */
3999 IncreaseBuf(FB->Buf, 0, FB->Buf->BufUsed + 1);
4001 FB->ReadWritePointer = FB->Buf->buf + unread;
4003 bufremain = FB->Buf->BufSize - unread - 1;
4004 /*TODO: special increase function that won't copy the already read! */
4007 else if (bufremain < 10) {
4008 IncreaseBuf(FB->Buf, 1, FB->Buf->BufUsed + 10);
4010 FB->ReadWritePointer = FB->Buf->buf;
4012 bufremain = FB->Buf->BufSize - FB->Buf->BufUsed - 1;
4017 FB->ReadWritePointer = FB->Buf->buf;
4018 bufremain = FB->Buf->BufSize - 1;
4021 n = read(fd, FB->Buf->buf + FB->Buf->BufUsed, bufremain);
4024 FB->Buf->BufUsed += n;
4025 FB->Buf->buf[FB->Buf->BufUsed] = '\0';
4030 int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB)
4035 if ((FB == NULL) || (FB->Buf == NULL))
4038 if (FB->ReadWritePointer != NULL)
4040 WriteRemain = FB->Buf->BufUsed -
4041 (FB->ReadWritePointer -
4045 FB->ReadWritePointer = FB->Buf->buf;
4046 WriteRemain = FB->Buf->BufUsed;
4049 n = write(fd, FB->ReadWritePointer, WriteRemain);
4051 FB->ReadWritePointer += n;
4053 if (FB->ReadWritePointer ==
4054 FB->Buf->buf + FB->Buf->BufUsed)
4056 FlushStrBuf(FB->Buf);
4057 FB->ReadWritePointer = NULL;
4060 // check whether we've got something to write
4061 // get the maximum chunk plus the pointer we can send
4062 // write whats there
4063 // if not all was sent, remember the send pointer for the next time
4064 return FB->ReadWritePointer - FB->Buf->buf + FB->Buf->BufUsed;
4070 * @ingroup StrBuf_IO
4071 * @brief extract a "next line" from Buf; Ptr to persist across several iterations
4072 * @param LineBuf your line will be copied here.
4073 * @param FB BLOB with lines of text...
4074 * @param Ptr moved arround to keep the next-line across several iterations
4075 * has to be &NULL on start; will be &NotNULL on end of buffer
4076 * @returns size of copied buffer
4078 eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB)
4080 const char *aptr, *ptr, *eptr;
4083 if ((FB == NULL) || (LineBuf == NULL) || (LineBuf->buf == NULL))
4087 if ((FB->Buf == NULL) || (FB->ReadWritePointer == StrBufNOTNULL)) {
4088 FB->ReadWritePointer = StrBufNOTNULL;
4092 FlushStrBuf(LineBuf);
4093 if (FB->ReadWritePointer == NULL)
4094 ptr = aptr = FB->Buf->buf;
4096 ptr = aptr = FB->ReadWritePointer;
4098 optr = LineBuf->buf;
4099 eptr = FB->Buf->buf + FB->Buf->BufUsed;
4100 xptr = LineBuf->buf + LineBuf->BufSize - 1;
4102 while ((ptr <= eptr) &&
4109 LineBuf->BufUsed = optr - LineBuf->buf;
4110 IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
4111 optr = LineBuf->buf + LineBuf->BufUsed;
4112 xptr = LineBuf->buf + LineBuf->BufSize - 1;
4117 if (optr > LineBuf->buf)
4119 if ((*(ptr - 1) != '\r') && (*(ptr - 1) != '\n')) {
4120 LineBuf->BufUsed = optr - LineBuf->buf;
4122 if ((FB->ReadWritePointer != NULL) &&
4123 (FB->ReadWritePointer != FB->Buf->buf))
4125 /* Ok, the client application read all the data
4126 it was interested in so far. Since there is more to read,
4127 we now shrink the buffer, and move the rest over.
4129 StrBufCutLeft(FB->Buf,
4130 FB->ReadWritePointer - FB->Buf->buf);
4131 FB->ReadWritePointer = FB->Buf->buf;
4133 return eMustReadMore;
4136 LineBuf->BufUsed = optr - LineBuf->buf;
4138 if ((ptr <= eptr) && (*ptr == '\r'))
4140 if ((ptr <= eptr) && (*ptr == '\n'))
4144 FB->ReadWritePointer = ptr;
4147 FlushStrBuf(FB->Buf);
4148 FB->ReadWritePointer = NULL;
4151 return eReadSuccess;
4155 * @ingroup StrBuf_CHUNKED_IO
4156 * @brief check whether the chunk-buffer has more data waiting or not.
4157 * @param FB Chunk-Buffer to inspect
4159 eReadState StrBufCheckBuffer(IOBuffer *FB)
4163 if (FB->Buf->BufUsed == 0)
4164 return eReadSuccess;
4165 if (FB->ReadWritePointer == NULL)
4166 return eBufferNotEmpty;
4167 if (FB->Buf->buf + FB->Buf->BufUsed > FB->ReadWritePointer)
4168 return eBufferNotEmpty;
4169 return eReadSuccess;
4172 long IOBufferStrLength(IOBuffer *FB)
4174 if ((FB == NULL) || (FB->Buf == NULL))
4176 if (FB->ReadWritePointer == NULL)
4177 return StrLength(FB->Buf);
4179 return StrLength(FB->Buf) - (FB->ReadWritePointer - FB->Buf->buf);
4182 inline static void FDIOBufferFlush(FDIOBuffer *FDB)
4184 memset(FDB, 0, sizeof(FDIOBuffer));
4186 FDB->SplicePipe[0] = -1;
4187 FDB->SplicePipe[1] = -1;
4190 void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
4192 FDIOBufferFlush(FDB);
4194 FDB->TotalSendSize = TotalSendSize;
4198 pipe(FDB->SplicePipe);
4201 FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize + 1);
4206 void FDIOBufferDelete(FDIOBuffer *FDB)
4211 if (FDB->SplicePipe[0] > 0)
4212 close(FDB->SplicePipe[0]);
4213 if (FDB->SplicePipe[1] > 0)
4214 close(FDB->SplicePipe[1]);
4218 FreeStrBuf(&FDB->ChunkBuffer);
4220 if (FDB->OtherFD > 0)
4221 close(FDB->OtherFD);
4222 FDIOBufferFlush(FDB);
4225 int FileSendChunked(FDIOBuffer *FDB, const char **Err)
4227 ssize_t sent, pipesize;
4231 if (FDB->PipeSize == 0)
4233 pipesize = splice(FDB->OtherFD,
4234 &FDB->TotalSentAlready,
4237 FDB->ChunkSendRemain,
4242 *Err = strerror(errno);
4245 FDB->PipeSize = pipesize;
4247 sent = splice(FDB->SplicePipe[0],
4252 SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
4255 *Err = strerror(errno);
4258 FDB->PipeSize -= sent;
4259 FDB->ChunkSendRemain -= sent;
4268 pRead = FDB->ChunkBuffer->buf;
4269 while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0))
4271 nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed);
4273 FDB->ChunkBuffer->BufUsed += nRead;
4274 FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0';
4276 else if (nRead == 0) {}
4281 nRead = write(FDB->IOB->fd, FDB->ChunkBuffer->buf + FDB->TotalSentAlready, FDB->ChunkSendRemain);
4284 FDB->TotalSentAlready += nRead;
4285 FDB->ChunkSendRemain -= nRead;
4286 return FDB->ChunkSendRemain;
4294 int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
4296 ssize_t sent, pipesize;
4301 if (FDB->PipeSize == 0)
4303 pipesize = splice(FDB->IOB->fd,
4307 FDB->ChunkSendRemain,
4308 SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
4312 *Err = strerror(errno);
4315 FDB->PipeSize = pipesize;
4318 sent = splice(FDB->SplicePipe[0],
4321 &FDB->TotalSentAlready,
4323 SPLICE_F_MORE | SPLICE_F_MOVE);
4327 *Err = strerror(errno);
4330 FDB->PipeSize -= sent;
4331 FDB->ChunkSendRemain -= sent;
4337 sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
4342 FDB->ChunkBuffer->BufUsed = sent;
4344 while (nWritten < FDB->ChunkBuffer->BufUsed) {
4345 rc = write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
4347 *Err = strerror(errno);
4353 FDB->ChunkBuffer->BufUsed = 0;
4354 FDB->TotalSentAlready += sent;
4355 FDB->ChunkSendRemain -= sent;
4356 return FDB->ChunkSendRemain;
4358 else if (sent < 0) {
4359 *Err = strerror(errno);
4366 int FileMoveChunked(FDIOBuffer *FDB, const char **Err)
4368 ssize_t sent, pipesize;
4373 if (FDB->PipeSize == 0)
4375 pipesize = splice(FDB->IOB->fd,
4376 &FDB->TotalReadAlready,
4379 FDB->ChunkSendRemain,
4380 SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
4384 *Err = strerror(errno);
4387 FDB->PipeSize = pipesize;
4390 sent = splice(FDB->SplicePipe[0],
4393 &FDB->TotalSentAlready,
4395 SPLICE_F_MORE | SPLICE_F_MOVE);
4399 *Err = strerror(errno);
4402 FDB->PipeSize -= sent;
4403 FDB->ChunkSendRemain -= sent;
4409 sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
4414 FDB->ChunkBuffer->BufUsed = sent;
4416 while (nWritten < FDB->ChunkBuffer->BufUsed) {
4417 rc = write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
4419 *Err = strerror(errno);
4425 FDB->ChunkBuffer->BufUsed = 0;
4426 FDB->TotalSentAlready += sent;
4427 FDB->ChunkSendRemain -= sent;
4428 return FDB->ChunkSendRemain;
4430 else if (sent < 0) {
4431 *Err = strerror(errno);
4438 eReadState WriteIOBAlreadyRead(FDIOBuffer *FDB, const char **Error)
4444 int nSuccessLess = 0;
4448 fdflags = fcntl(FDB->OtherFD, F_GETFL);
4449 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
4451 while ((FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf < FDB->IOB->Buf->BufUsed) &&
4452 (FDB->ChunkSendRemain > 0))
4455 tv.tv_sec = 1; /* selectresolution; */
4459 FD_SET(FDB->OtherFD, &rfds);
4460 if (select(FDB->OtherFD + 1, NULL, &rfds, NULL, &tv) == -1) {
4461 *Error = strerror(errno);
4465 if (IsNonBlock && ! FD_ISSET(FDB->OtherFD, &rfds)) {
4470 should_write = FDB->IOB->Buf->BufUsed -
4471 (FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf);
4472 if (should_write > FDB->ChunkSendRemain)
4473 should_write = FDB->ChunkSendRemain;
4475 rlen = write(FDB->OtherFD,
4476 FDB->IOB->ReadWritePointer,
4479 *Error = strerror(errno);
4483 FDB->TotalSentAlready += rlen;
4484 FDB->IOB->ReadWritePointer += rlen;
4485 FDB->ChunkSendRemain -= rlen;
4487 if (FDB->IOB->ReadWritePointer >= FDB->IOB->Buf->buf + FDB->IOB->Buf->BufUsed)
4489 FlushStrBuf(FDB->IOB->Buf);
4490 FDB->IOB->ReadWritePointer = NULL;
4493 if (FDB->ChunkSendRemain == 0)
4494 return eReadSuccess;
4496 return eMustReadMore;
4499 /*******************************************************************************
4500 * File I/O; Prefer buffered read since its faster! *
4501 *******************************************************************************/
4504 * @ingroup StrBuf_IO
4505 * @brief Read a line from socket
4506 * flushes and closes the FD on error
4507 * @param buf the buffer to get the input to
4508 * @param fd pointer to the filedescriptor to read
4509 * @param append Append to an existing string or replace?
4510 * @param Error strerror() on error
4511 * @returns numbers of chars read
4513 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
4515 int len, rlen, slen;
4517 if ((buf == NULL) || (buf->buf == NULL)) {
4518 *Error = strerror(EINVAL);
4525 slen = len = buf->BufUsed;
4527 rlen = read(*fd, &buf->buf[len], 1);
4529 *Error = strerror(errno);
4536 if (buf->buf[len] == '\n')
4538 if (buf->buf[len] != '\r')
4540 if (len + 2 >= buf->BufSize) {
4542 buf->buf[len+1] = '\0';
4543 IncreaseBuf(buf, 1, -1);
4547 buf->buf[len] = '\0';
4552 * @ingroup StrBuf_BufferedIO
4553 * @brief Read a line from socket
4554 * flushes and closes the FD on error
4555 * @param Line the line to read from the fd / I/O Buffer
4556 * @param buf the buffer to get the input to
4557 * @param fd pointer to the filedescriptor to read
4558 * @param timeout number of successless selects until we bail out
4559 * @param selectresolution how long to wait on each select
4560 * @param Error strerror() on error
4561 * @returns numbers of chars read
4563 int StrBufTCP_read_buffered_line(StrBuf *Line,
4567 int selectresolution,
4571 int nSuccessLess = 0;
4578 if (buf->BufUsed > 0) {
4579 pch = strchr(buf->buf, '\n');
4582 len = pch - buf->buf;
4583 if (len > 0 && (*(pch - 1) == '\r') )
4585 StrBufSub(Line, buf, 0, len - rlen);
4586 StrBufCutLeft(buf, len + 1);
4591 if (buf->BufSize - buf->BufUsed < 10)
4592 IncreaseBuf(buf, 1, -1);
4594 fdflags = fcntl(*fd, F_GETFL);
4595 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
4597 while ((nSuccessLess < timeout) && (pch == NULL)) {
4599 tv.tv_sec = selectresolution;
4604 if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
4605 *Error = strerror(errno);
4611 if (IsNonBlock && ! FD_ISSET(*fd, &rfds)) {
4616 &buf->buf[buf->BufUsed],
4617 buf->BufSize - buf->BufUsed - 1);
4619 *Error = strerror(errno);
4624 else if (rlen > 0) {
4626 buf->BufUsed += rlen;
4627 buf->buf[buf->BufUsed] = '\0';
4628 pch = strchr(buf->buf, '\n');
4629 if ((pch == NULL) &&
4630 (buf->BufUsed + 10 > buf->BufSize) &&
4631 (IncreaseBuf(buf, 1, -1) == -1))
4639 len = pch - buf->buf;
4640 if (len > 0 && (*(pch - 1) == '\r') )
4642 StrBufSub(Line, buf, 0, len - rlen);
4643 StrBufCutLeft(buf, len + 1);
4650 static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
4651 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
4652 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
4654 * @ingroup StrBuf_BufferedIO
4655 * @brief Read a line from socket
4656 * flushes and closes the FD on error
4657 * @param Line where to append our Line read from the fd / I/O Buffer;
4658 * @param IOBuf the buffer to get the input to; lifetime pair to FD
4659 * @param Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
4660 * @param fd pointer to the filedescriptor to read
4661 * @param timeout number of successless selects until we bail out
4662 * @param selectresolution how long to wait on each select
4663 * @param Error strerror() on error
4664 * @returns numbers of chars read or -1 in case of error. "\n" will become 0
4666 int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
4671 int selectresolution,
4674 const char *pche = NULL;
4675 const char *pos = NULL;
4677 int len, rlen, retlen;
4678 int nSuccessLess = 0;
4680 const char *pch = NULL;
4686 if ((Line == NULL) ||
4693 *Error = ErrRBLF_PreConditionFailed;
4698 if ((IOBuf->BufUsed > 0) &&
4700 (pos < IOBuf->buf + IOBuf->BufUsed))
4704 pche = IOBuf->buf + IOBuf->BufUsed;
4708 while ((pch < pche) && (*pch != '\n'))
4710 if (Line->BufUsed + 10 > Line->BufSize)
4713 apos = pcht - Line->buf;
4715 IncreaseBuf(Line, 1, -1);
4716 pcht = Line->buf + apos;
4724 if (len > 0 && (*(pch - 1) == '\r') )
4733 if ((pch >= pche) || (*pch == '\0'))
4741 if ((pch != NULL) &&
4744 if (pch + 1 >= pche) {
4757 /* If we come here, Pos is Unset since we read everything into Line, and now go for more. */
4759 if (IOBuf->BufSize - IOBuf->BufUsed < 10)
4760 IncreaseBuf(IOBuf, 1, -1);
4762 fdflags = fcntl(*fd, F_GETFL);
4763 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
4766 while ((nSuccessLess < timeout) &&
4776 if (select((*fd) + 1, &rfds, NULL, NULL, &tv) == -1) {
4777 *Error = strerror(errno);
4781 *Error = ErrRBLF_SelectFailed;
4784 if (! FD_ISSET(*fd, &rfds) != 0) {
4790 &IOBuf->buf[IOBuf->BufUsed],
4791 IOBuf->BufSize - IOBuf->BufUsed - 1);
4793 *Error = strerror(errno);
4798 else if (rlen > 0) {
4800 pLF = IOBuf->buf + IOBuf->BufUsed;
4801 IOBuf->BufUsed += rlen;
4802 IOBuf->buf[IOBuf->BufUsed] = '\0';
4804 pche = IOBuf->buf + IOBuf->BufUsed;
4806 while ((pLF < pche) && (*pLF != '\n'))
4808 if ((pLF >= pche) || (*pLF == '\0'))
4811 if (IOBuf->BufUsed + 10 > IOBuf->BufSize)
4815 if (pLF != NULL) apos = pLF - IOBuf->buf;
4816 IncreaseBuf(IOBuf, 1, -1);
4817 if (pLF != NULL) pLF = IOBuf->buf + apos;
4831 if (len > 0 && (*(pLF - 1) == '\r') )
4833 StrBufAppendBufPlain(Line, ChrPtr(IOBuf), len, 0);
4834 if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed)
4840 return retlen + len;
4842 *Error = ErrRBLF_NotEnoughSentFromServer;
4847 static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong arguments or invalid Filedescriptor";
4849 * @ingroup StrBuf_IO
4850 * @brief Input binary data from socket
4851 * flushes and closes the FD on error
4852 * @param Buf the buffer to get the input to
4853 * @param fd pointer to the filedescriptor to read
4854 * @param append Append to an existing string or replace?
4855 * @param nBytes the maximal number of bytes to read
4856 * @param Error strerror() on error
4857 * @returns numbers of chars read
4859 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
4870 if ((Buf == NULL) || (Buf->buf == NULL) || (*fd == -1))
4872 *Error = ErrRBLF_BLOBPreConditionFailed;
4877 if (Buf->BufUsed + nBytes >= Buf->BufSize)
4878 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
4880 ptr = Buf->buf + Buf->BufUsed;
4882 fdflags = fcntl(*fd, F_GETFL);
4883 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
4885 while ((nRead < nBytes) &&
4895 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
4896 *Error = strerror(errno);
4900 *Error = ErrRBLF_SelectFailed;
4903 if (! FD_ISSET(*fd, &rfds) != 0) {
4909 if ((rlen = read(*fd,
4911 nBytes - nRead)) == -1) {
4914 *Error = strerror(errno);
4919 Buf->BufUsed += rlen;
4921 Buf->buf[Buf->BufUsed] = '\0';
4925 const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
4926 const char *ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting.";
4928 * @ingroup StrBuf_BufferedIO
4929 * @brief Input binary data from socket
4930 * flushes and closes the FD on error
4931 * @param Blob put binary thing here
4932 * @param IOBuf the buffer to get the input to
4933 * @param Pos offset inside of IOBuf
4934 * @param fd pointer to the filedescriptor to read
4935 * @param append Append to an existing string or replace?
4936 * @param nBytes the maximal number of bytes to read
4937 * @param check whether we should search for '000\n' terminators in case of timeouts
4938 * @param Error strerror() on error
4939 * @returns numbers of chars read
4941 int StrBufReadBLOBBuffered(StrBuf *Blob,
4954 int nAlreadyRead = 0;
4959 int nSuccessLess = 0;
4962 if ((Blob == NULL) ||
4969 *Error = ErrRBB_BLOBFPreConditionFailed;
4975 if (Blob->BufUsed + nBytes >= Blob->BufSize)
4976 IncreaseBuf(Blob, append, Blob->BufUsed + nBytes);
4981 rlen = pos - IOBuf->buf;
4982 rlen = IOBuf->BufUsed - rlen;
4985 if ((IOBuf->BufUsed > 0) &&
4987 (pos < IOBuf->buf + IOBuf->BufUsed))
4989 if (rlen < nBytes) {
4990 memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
4991 Blob->BufUsed += rlen;
4992 Blob->buf[Blob->BufUsed] = '\0';
4993 nAlreadyRead = nRead = rlen;
4996 if (rlen >= nBytes) {
4997 memcpy(Blob->buf + Blob->BufUsed, pos, nBytes);
4998 Blob->BufUsed += nBytes;
4999 Blob->buf[Blob->BufUsed] = '\0';
5000 if (rlen == nBytes) {
5012 if (IOBuf->BufSize < nBytes - nRead)
5013 IncreaseBuf(IOBuf, 0, nBytes - nRead);
5016 fdflags = fcntl(*fd, F_GETFL);
5017 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
5025 while ((nSuccessLess < MaxTries) &&
5035 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
5036 *Error = strerror(errno);
5040 *Error = ErrRBLF_SelectFailed;
5043 if (! FD_ISSET(*fd, &rfds) != 0) {
5050 IOBuf->BufSize - (ptr - IOBuf->buf));
5054 *Error = strerror(errno);
5057 else if (rlen == 0){
5058 if ((check == NNN_TERM) &&
5060 (strncmp(IOBuf->buf + IOBuf->BufUsed - 5, "\n000\n", 5) == 0))
5062 StrBufPlain(Blob, HKEY("\n000\n"));
5063 StrBufCutRight(Blob, 5);
5064 return Blob->BufUsed;
5066 else if (!IsNonBlock)
5068 else if (nSuccessLess > MaxTries) {
5070 *Error = ErrRBB_too_many_selects;
5074 else if (rlen > 0) {
5078 IOBuf->BufUsed += rlen;
5081 if (nSuccessLess >= MaxTries) {
5083 *Error = ErrRBB_too_many_selects;
5087 if (nRead > nBytes) {
5088 *Pos = IOBuf->buf + nBytes;
5090 Blob->buf[Blob->BufUsed] = '\0';
5091 StrBufAppendBufPlain(Blob, IOBuf->buf, nBytes, 0);
5095 return nRead + nAlreadyRead;
5099 * @ingroup StrBuf_IO
5100 * @brief extract a "next line" from Buf; Ptr to persist across several iterations
5101 * @param LineBuf your line will be copied here.
5102 * @param Buf BLOB with lines of text...
5103 * @param Ptr moved arround to keep the next-line across several iterations
5104 * has to be &NULL on start; will be &NotNULL on end of buffer
5105 * @returns size of remaining buffer
5107 int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
5109 const char *aptr, *ptr, *eptr;
5112 if ((Buf == NULL) ||
5113 (*Ptr == StrBufNOTNULL) ||
5115 (LineBuf->buf == NULL))
5117 *Ptr = StrBufNOTNULL;
5121 FlushStrBuf(LineBuf);
5123 ptr = aptr = Buf->buf;
5127 optr = LineBuf->buf;
5128 eptr = Buf->buf + Buf->BufUsed;
5129 xptr = LineBuf->buf + LineBuf->BufSize - 1;
5131 while ((ptr <= eptr) &&
5138 LineBuf->BufUsed = optr - LineBuf->buf;
5139 IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
5140 optr = LineBuf->buf + LineBuf->BufUsed;
5141 xptr = LineBuf->buf + LineBuf->BufSize - 1;
5145 if ((ptr >= eptr) && (optr > LineBuf->buf))
5147 LineBuf->BufUsed = optr - LineBuf->buf;
5149 if ((ptr <= eptr) && (*ptr == '\r'))
5151 if ((ptr <= eptr) && (*ptr == '\n'))
5158 *Ptr = StrBufNOTNULL;
5161 return Buf->BufUsed - (ptr - Buf->buf);
5166 * @ingroup StrBuf_IO
5167 * @brief removes double slashes from pathnames
5168 * @param Dir directory string to filter
5169 * @param RemoveTrailingSlash allows / disallows trailing slashes
5171 void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
5177 while (!IsEmptyStr(a)) {
5189 if ((RemoveTrailingSlash) &&
5195 Dir->BufUsed = b - Dir->buf;