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"
35 #include "b64/cencode.h"
36 #include "b64/cdecode.h"
52 int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
53 const Bytef * source, uLong sourceLen, int level);
55 int BaseStrBufSize = 64;
57 int ZLibCompressionRatio = -1; /* defaults to 6 */
59 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
60 #define OS_CODE 0x03 /*< unix */
61 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
64 const char *StrBufNOTNULL = ((char*) NULL) - 1;
66 const char HexList[256][3] = {
67 "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
68 "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
69 "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
70 "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
71 "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
72 "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
73 "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
74 "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
75 "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
76 "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
77 "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
78 "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
79 "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
80 "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
81 "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
82 "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
85 * @defgroup StrBuf Stringbuffer, A class for manipulating strings with dynamic buffers
86 * StrBuf is a versatile class, aiding the handling of dynamic strings
87 * * reduce de/reallocations
88 * * reduce the need to remeasure it
89 * * reduce scanning over the string (in @ref StrBuf_NextTokenizer "Tokenizers")
90 * * allow asyncroneous IO for line and Blob based operations
91 * * reduce the use of memove in those
92 * * Quick filling in several operations with append functions
96 * @defgroup StrBuf_DeConstructors Create/Destroy StrBufs
101 * @defgroup StrBuf_Cast Cast operators to interact with char* based code
103 * use these operators to interfere with code demanding char*;
104 * if you need to own the content, smash me. Avoid, since we loose the length information.
108 * @defgroup StrBuf_Filler Create/Replace/Append Content into a StrBuf
110 * operations to get your Strings into a StrBuf, manipulating them, or appending
113 * @defgroup StrBuf_NextTokenizer Fast tokenizer to pull tokens in sequence
115 * Quick tokenizer; demands of the user to pull its tokens in sequence
119 * @defgroup StrBuf_Tokenizer tokenizer Functions; Slow ones.
121 * versatile tokenizer; random access to tokens, but slower; Prefer the @ref StrBuf_NextTokenizer "Next Tokenizer"
125 * @defgroup StrBuf_BufferedIO Buffered IO with Asynchroneous reads and no unneeded memmoves (the fast ones)
127 * File IO to fill StrBufs; Works with work-buffer shared across several calls;
128 * External Cursor to maintain the current read position inside of the buffer
129 * the non-fast ones will use memove to keep the start of the buffer the read buffer (which is slower)
133 * @defgroup StrBuf_IO FileIO; Prefer @ref StrBuf_BufferedIO
139 * @defgroup StrBuf_DeEnCoder functions to translate the contents of a buffer
141 * these functions translate the content of a buffer into another representation;
142 * some are combined Fillers and encoders
146 * Private Structure for the Stringbuffer
149 char *buf; /**< the pointer to the dynamic buffer */
150 long BufSize; /**< how many spcae do we optain */
151 long BufUsed; /**< StNumber of Chars used excluding the trailing \\0 */
152 int ConstBuf; /**< are we just a wrapper arround a static buffer and musn't we be changed? */
154 long nIncreases; /**< for profiling; cound how many times we needed more */
155 char bt [SIZ]; /**< Stacktrace of last increase */
156 char bt_lastinc [SIZ]; /**< How much did we increase last time? */
161 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE);
162 static inline int Ctdl_IsUtf8SequenceStart(const char Char);
165 #ifdef HAVE_BACKTRACE
166 static void StrBufBacktrace(StrBuf *Buf, int which)
170 void *stack_frames[50];
175 pstart = pch = Buf->bt;
177 pstart = pch = Buf->bt_lastinc;
178 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
179 strings = backtrace_symbols(stack_frames, size);
180 for (i = 0; i < size; i++) {
182 n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
184 n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
193 void dbg_FreeStrBuf(StrBuf *FreeMe, char *FromWhere)
195 if (hFreeDbglog == -1){
196 pid_t pid = getpid();
198 snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
199 hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
201 if ((*FreeMe)->nIncreases > 0)
205 n = snprintf(buf, SIZ * 3, "%c+|%ld|%ld|%ld|%s|%s|\n",
207 (*FreeMe)->nIncreases,
211 (*FreeMe)->bt_lastinc);
212 n = write(hFreeDbglog, buf, n);
218 n = snprintf(buf, 128, "%c_|0|%ld%ld|\n",
222 n = write(hFreeDbglog, buf, n);
226 void dbg_IncreaseBuf(StrBuf *IncMe)
229 #ifdef HAVE_BACKTRACE
230 StrBufBacktrace(Buf, 1);
234 void dbg_Init(StrBuf *Buf)
238 Buf->bt_lastinc[0] = '\0';
239 #ifdef HAVE_BACKTRACE
240 StrBufBacktrace(Buf, 0);
246 #define dbg_FreeStrBuf(a, b)
247 #define dbg_IncreaseBuf(a)
254 * @brief swaps the contents of two StrBufs
255 * this is to be used to have cheap switched between a work-buffer and a target buffer
257 * @param B second one
259 static inline void SwapBuffers(StrBuf *A, StrBuf *B)
263 memcpy(&C, A, sizeof(*A));
264 memcpy(A, B, sizeof(*B));
265 memcpy(B, &C, sizeof(C));
270 * @ingroup StrBuf_Cast
271 * @brief Cast operator to Plain String
272 * @note if the buffer is altered by StrBuf operations, this pointer may become
273 * invalid. So don't lean on it after altering the buffer!
274 * Since this operation is considered cheap, rather call it often than risking
275 * your pointer to become invalid!
276 * @param Str the string we want to get the c-string representation for
277 * @returns the Pointer to the Content. Don't mess with it!
279 inline const char *ChrPtr(const StrBuf *Str)
287 * @ingroup StrBuf_Cast
288 * @brief since we know strlen()'s result, provide it here.
289 * @param Str the string to return the length to
290 * @returns contentlength of the buffer
292 inline int StrLength(const StrBuf *Str)
294 return (Str != NULL) ? Str->BufUsed : 0;
298 * @ingroup StrBuf_DeConstructors
299 * @brief local utility function to resize the buffer
300 * @param Buf the buffer whichs storage we should increase
301 * @param KeepOriginal should we copy the original buffer or just start over with a new one
302 * @param DestSize what should fit in after?
304 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
307 size_t NewSize = Buf->BufSize * 2;
313 while ((NewSize <= DestSize) && (NewSize != 0))
319 NewBuf= (char*) malloc(NewSize);
323 if (KeepOriginal && (Buf->BufUsed > 0))
325 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
334 Buf->BufSize = NewSize;
336 dbg_IncreaseBuf(Buf);
342 * @ingroup StrBuf_DeConstructors
343 * @brief shrink / increase an _EMPTY_ buffer to NewSize. Buffercontent is thoroughly ignored and flushed.
344 * @param Buf Buffer to shrink (has to be empty)
345 * @param ThreshHold if the buffer is bigger then this, its readjusted
346 * @param NewSize if we Shrink it, how big are we going to be afterwards?
348 void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize)
351 (Buf->BufUsed == 0) &&
352 (Buf->BufSize < ThreshHold)) {
354 Buf->buf = (char*) malloc(NewSize);
356 Buf->BufSize = NewSize;
361 * @ingroup StrBuf_DeConstructors
362 * @brief shrink long term buffers to their real size so they don't waste memory
363 * @param Buf buffer to shrink
364 * @param Force if not set, will just executed if the buffer is much to big; set for lifetime strings
365 * @returns physical size of the buffer
367 long StrBufShrinkToFit(StrBuf *Buf, int Force)
372 (Buf->BufUsed + (Buf->BufUsed / 3) > Buf->BufSize))
376 TmpBuf = (char*) malloc(Buf->BufUsed + 1);
380 memcpy (TmpBuf, Buf->buf, Buf->BufUsed + 1);
381 Buf->BufSize = Buf->BufUsed + 1;
389 * @ingroup StrBuf_DeConstructors
390 * @brief Allocate a new buffer with default buffer size
391 * @returns the new stringbuffer
393 StrBuf* NewStrBuf(void)
397 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
401 NewBuf->buf = (char*) malloc(BaseStrBufSize);
402 if (NewBuf->buf == NULL)
407 NewBuf->buf[0] = '\0';
408 NewBuf->BufSize = BaseStrBufSize;
410 NewBuf->ConstBuf = 0;
418 * @ingroup StrBuf_DeConstructors
419 * @brief Copy Constructor; returns a duplicate of CopyMe
420 * @param CopyMe Buffer to faxmilate
421 * @returns the new stringbuffer
423 StrBuf* NewStrBufDup(const StrBuf *CopyMe)
430 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
434 NewBuf->buf = (char*) malloc(CopyMe->BufSize);
435 if (NewBuf->buf == NULL)
441 memcpy(NewBuf->buf, CopyMe->buf, CopyMe->BufUsed + 1);
442 NewBuf->BufUsed = CopyMe->BufUsed;
443 NewBuf->BufSize = CopyMe->BufSize;
444 NewBuf->ConstBuf = 0;
452 * @ingroup StrBuf_DeConstructors
453 * @brief Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards.
454 * @param NoMe if non-NULL, we will use that buffer as value; KeepOriginal will abused as len.
455 * @param CopyFlushMe Buffer to faxmilate if KeepOriginal, or to move into CreateRelpaceMe if !KeepOriginal.
456 * @param CreateRelpaceMe If NULL, will be created, else Flushed and filled CopyFlushMe
457 * @param KeepOriginal should CopyFlushMe remain intact? or may we Steal its buffer?
458 * @returns the new stringbuffer
460 void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, const char *NoMe, int KeepOriginal)
464 if (CreateRelpaceMe == NULL)
469 if (*CreateRelpaceMe != NULL)
470 StrBufPlain(*CreateRelpaceMe, NoMe, KeepOriginal);
472 *CreateRelpaceMe = NewStrBufPlain(NoMe, KeepOriginal);
476 if (CopyFlushMe == NULL)
478 if (*CreateRelpaceMe != NULL)
479 FlushStrBuf(*CreateRelpaceMe);
481 *CreateRelpaceMe = NewStrBuf();
486 * Randomly Chosen: bigger than 64 chars is cheaper to swap the buffers instead of copying.
487 * else *CreateRelpaceMe may use more memory than needed in a longer term, CopyFlushMe might
488 * be a big IO-Buffer...
490 if (KeepOriginal || (StrLength(CopyFlushMe) < 256))
492 if (*CreateRelpaceMe == NULL)
494 *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed);
499 NewBuf = *CreateRelpaceMe;
502 StrBufAppendBuf(NewBuf, CopyFlushMe, 0);
506 if (*CreateRelpaceMe == NULL)
508 *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed);
512 NewBuf = *CreateRelpaceMe;
513 SwapBuffers (NewBuf, CopyFlushMe);
516 FlushStrBuf(CopyFlushMe);
521 * @ingroup StrBuf_DeConstructors
522 * @brief create a new Buffer using an existing c-string
523 * this function should also be used if you want to pre-suggest
524 * the buffer size to allocate in conjunction with ptr == NULL
525 * @param ptr the c-string to copy; may be NULL to create a blank instance
526 * @param nChars How many chars should we copy; -1 if we should measure the length ourselves
527 * @returns the new stringbuffer
529 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
532 size_t Siz = BaseStrBufSize;
535 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
540 CopySize = strlen((ptr != NULL)?ptr:"");
544 while ((Siz <= CopySize) && (Siz != 0))
553 NewBuf->buf = (char*) malloc(Siz);
554 if (NewBuf->buf == NULL)
559 NewBuf->BufSize = Siz;
561 memcpy(NewBuf->buf, ptr, CopySize);
562 NewBuf->buf[CopySize] = '\0';
563 NewBuf->BufUsed = CopySize;
566 NewBuf->buf[0] = '\0';
569 NewBuf->ConstBuf = 0;
577 * @ingroup StrBuf_DeConstructors
578 * @brief Set an existing buffer from a c-string
579 * @param Buf buffer to load
580 * @param ptr c-string to put into
581 * @param nChars set to -1 if we should work 0-terminated
582 * @returns the new length of the string
584 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
599 CopySize = strlen(ptr);
603 while ((Siz <= CopySize) && (Siz != 0))
611 if (Siz != Buf->BufSize)
612 IncreaseBuf(Buf, 0, Siz);
613 memcpy(Buf->buf, ptr, CopySize);
614 Buf->buf[CopySize] = '\0';
615 Buf->BufUsed = CopySize;
622 * @ingroup StrBuf_DeConstructors
623 * @brief use strbuf as wrapper for a string constant for easy handling
624 * @param StringConstant a string to wrap
625 * @param SizeOfStrConstant should be sizeof(StringConstant)-1
627 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
631 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
634 NewBuf->buf = (char*) StringConstant;
635 NewBuf->BufSize = SizeOfStrConstant;
636 NewBuf->BufUsed = SizeOfStrConstant;
637 NewBuf->ConstBuf = 1;
646 * @ingroup StrBuf_DeConstructors
647 * @brief flush the content of a Buf; keep its struct
648 * @param buf Buffer to flush
650 int FlushStrBuf(StrBuf *buf)
652 if ((buf == NULL) || (buf->buf == NULL))
662 * @ingroup StrBuf_DeConstructors
663 * @brief wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
664 * @param buf Buffer to wipe
666 int FLUSHStrBuf(StrBuf *buf)
672 if (buf->BufUsed > 0) {
673 memset(buf->buf, 0, buf->BufUsed);
680 int hFreeDbglog = -1;
683 * @ingroup StrBuf_DeConstructors
684 * @brief Release a Buffer
685 * Its a double pointer, so it can NULL your pointer
686 * so fancy SIG11 appear instead of random results
687 * @param FreeMe Pointer Pointer to the buffer to free
689 void FreeStrBuf (StrBuf **FreeMe)
694 dbg_FreeStrBuf(FreeMe, 'F');
696 if (!(*FreeMe)->ConstBuf)
697 free((*FreeMe)->buf);
703 * @ingroup StrBuf_DeConstructors
704 * @brief flatten a Buffer to the Char * we return
705 * Its a double pointer, so it can NULL your pointer
706 * so fancy SIG11 appear instead of random results
707 * The Callee then owns the buffer and is responsible for freeing it.
708 * @param SmashMe Pointer Pointer to the buffer to release Buf from and free
709 * @returns the pointer of the buffer; Callee owns the memory thereafter.
711 char *SmashStrBuf (StrBuf **SmashMe)
715 if ((SmashMe == NULL) || (*SmashMe == NULL))
718 dbg_FreeStrBuf(SmashMe, 'S');
720 Ret = (*SmashMe)->buf;
727 * @ingroup StrBuf_DeConstructors
728 * @brief Release the buffer
729 * If you want put your StrBuf into a Hash, use this as Destructor.
730 * @param VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
732 void HFreeStrBuf (void *VFreeMe)
734 StrBuf *FreeMe = (StrBuf*)VFreeMe;
738 dbg_FreeStrBuf(SmashMe, 'H');
740 if (!FreeMe->ConstBuf)
746 /*******************************************************************************
747 * Simple string transformations *
748 *******************************************************************************/
752 * @brief Wrapper around atol
754 long StrTol(const StrBuf *Buf)
759 return atol(Buf->buf);
766 * @brief Wrapper around atoi
768 int StrToi(const StrBuf *Buf)
772 if (Buf->BufUsed > 0)
773 return atoi(Buf->buf);
780 * @brief Checks to see if the string is a pure number
781 * @param Buf The buffer to inspect
782 * @returns 1 if its a pure number, 0, if not.
784 int StrBufIsNumber(const StrBuf *Buf) {
786 if ((Buf == NULL) || (Buf->BufUsed == 0)) {
789 strtoll(Buf->buf, &pEnd, 10);
790 if (pEnd == Buf->buf)
792 if ((pEnd != NULL) && (pEnd == Buf->buf + Buf->BufUsed))
794 if (Buf->buf == pEnd)
800 * @ingroup StrBuf_Filler
801 * @brief modifies a Single char of the Buf
802 * You can point to it via char* or a zero-based integer
803 * @param Buf The buffer to manipulate
804 * @param ptr char* to zero; use NULL if unused
805 * @param nThChar zero based pointer into the string; use -1 if unused
806 * @param PeekValue The Character to place into the position
808 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
813 nThChar = ptr - Buf->buf;
814 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
816 Buf->buf[nThChar] = PeekValue;
821 * @ingroup StrBuf_Filler
822 * @brief modifies a range of chars of the Buf
823 * You can point to it via char* or a zero-based integer
824 * @param Buf The buffer to manipulate
825 * @param ptr char* to zero; use NULL if unused
826 * @param nThChar zero based pointer into the string; use -1 if unused
827 * @param nChars how many chars are to be flushed?
828 * @param PookValue The Character to place into that area
830 long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue)
835 nThChar = ptr - Buf->buf;
836 if ((nThChar < 0) || (nThChar > Buf->BufUsed))
838 if (nThChar + nChars > Buf->BufUsed)
839 nChars = Buf->BufUsed - nThChar;
841 memset(Buf->buf + nThChar, PookValue, nChars);
842 /* just to be shure... */
843 Buf->buf[Buf->BufUsed] = 0;
848 * @ingroup StrBuf_Filler
849 * @brief Append a StringBuffer to the buffer
850 * @param Buf Buffer to modify
851 * @param AppendBuf Buffer to copy at the end of our buffer
852 * @param Offset Should we start copying from an offset?
854 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
856 if ((AppendBuf == NULL) || (AppendBuf->buf == NULL) ||
857 (Buf == NULL) || (Buf->buf == NULL))
860 if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1)
863 AppendBuf->BufUsed + Buf->BufUsed);
865 memcpy(Buf->buf + Buf->BufUsed,
866 AppendBuf->buf + Offset,
867 AppendBuf->BufUsed - Offset);
868 Buf->BufUsed += AppendBuf->BufUsed - Offset;
869 Buf->buf[Buf->BufUsed] = '\0';
874 * @ingroup StrBuf_Filler
875 * @brief Append a C-String to the buffer
876 * @param Buf Buffer to modify
877 * @param AppendBuf Buffer to copy at the end of our buffer
878 * @param AppendSize number of bytes to copy; set to -1 if we should count it in advance
879 * @param Offset Should we start copying from an offset?
881 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset)
884 long BufSizeRequired;
886 if ((AppendBuf == NULL) || (Buf == NULL))
890 aps = strlen(AppendBuf + Offset);
892 aps = AppendSize - Offset;
894 BufSizeRequired = Buf->BufUsed + aps + 1;
895 if (Buf->BufSize <= BufSizeRequired)
896 IncreaseBuf(Buf, (Buf->BufUsed > 0), BufSizeRequired);
898 memcpy(Buf->buf + Buf->BufUsed,
902 Buf->buf[Buf->BufUsed] = '\0';
906 * @ingroup StrBuf_Filler
907 * @brief sprintf like function appending the formated string to the buffer
908 * vsnprintf version to wrap into own calls
909 * @param Buf Buffer to extend by format and Params
910 * @param format printf alike format to add
911 * @param ap va_list containing the items for format
913 void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
921 if ((Buf == NULL) || (format == NULL))
924 BufSize = Buf->BufSize;
925 nWritten = Buf->BufSize + 1;
926 Offset = Buf->BufUsed;
927 newused = Offset + nWritten;
929 while (newused >= BufSize) {
931 nWritten = vsnprintf(Buf->buf + Offset,
932 Buf->BufSize - Offset,
935 newused = Offset + nWritten;
936 if (newused >= Buf->BufSize) {
937 if (IncreaseBuf(Buf, 1, newused) == -1)
938 return; /* TODO: error handling? */
939 newused = Buf->BufSize + 1;
942 Buf->BufUsed = Offset + nWritten;
943 BufSize = Buf->BufSize;
950 * @ingroup StrBuf_Filler
951 * @brief sprintf like function appending the formated string to the buffer
952 * @param Buf Buffer to extend by format and Params
953 * @param format printf alike format to add
955 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
963 if ((Buf == NULL) || (format == NULL))
966 BufSize = Buf->BufSize;
967 nWritten = Buf->BufSize + 1;
968 Offset = Buf->BufUsed;
969 newused = Offset + nWritten;
971 while (newused >= BufSize) {
972 va_start(arg_ptr, format);
973 nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
974 Buf->BufSize - Buf->BufUsed,
977 newused = Buf->BufUsed + nWritten;
978 if (newused >= Buf->BufSize) {
979 if (IncreaseBuf(Buf, 1, newused) == -1)
980 return; /* TODO: error handling? */
981 newused = Buf->BufSize + 1;
984 Buf->BufUsed += nWritten;
985 BufSize = Buf->BufSize;
992 * @ingroup StrBuf_Filler
993 * @brief sprintf like function putting the formated string into the buffer
994 * @param Buf Buffer to extend by format and Parameters
995 * @param format printf alike format to add
997 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
1002 if ((Buf == NULL) || (format == NULL))
1005 nWritten = Buf->BufSize + 1;
1006 while (nWritten >= Buf->BufSize) {
1007 va_start(arg_ptr, format);
1008 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
1010 if (nWritten >= Buf->BufSize) {
1011 if (IncreaseBuf(Buf, 0, 0) == -1)
1012 return; /* TODO: error handling? */
1013 nWritten = Buf->BufSize + 1;
1016 Buf->BufUsed = nWritten ;
1021 * @ingroup StrBuf_Filler
1022 * @brief Callback for cURL to append the webserver reply to a buffer
1023 * @param ptr pre-defined by the cURL API; see man 3 curl for mre info
1024 * @param size pre-defined by the cURL API; see man 3 curl for mre info
1025 * @param nmemb pre-defined by the cURL API; see man 3 curl for mre info
1026 * @param stream pre-defined by the cURL API; see man 3 curl for mre info
1028 size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
1037 StrBufAppendBufPlain(Target, ptr, size * nmemb, 0);
1038 return size * nmemb;
1044 * @brief extracts a substring from Source into dest
1045 * @param dest buffer to place substring into
1046 * @param Source string to copy substring from
1047 * @param Offset chars to skip from start
1048 * @param nChars number of chars to copy
1049 * @returns the number of chars copied; may be different from nChars due to the size of Source
1051 int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
1053 size_t NCharsRemain;
1054 if (Offset > Source->BufUsed)
1060 if (Offset + nChars < Source->BufUsed)
1062 if ((nChars >= dest->BufSize) &&
1063 (IncreaseBuf(dest, 0, nChars + 1) == -1))
1065 memcpy(dest->buf, Source->buf + Offset, nChars);
1066 dest->BufUsed = nChars;
1067 dest->buf[dest->BufUsed] = '\0';
1070 NCharsRemain = Source->BufUsed - Offset;
1071 if ((NCharsRemain >= dest->BufSize) &&
1072 (IncreaseBuf(dest, 0, NCharsRemain + 1) == -1))
1074 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
1075 dest->BufUsed = NCharsRemain;
1076 dest->buf[dest->BufUsed] = '\0';
1077 return NCharsRemain;
1082 * @brief Cut nChars from the start of the string
1083 * @param Buf Buffer to modify
1084 * @param nChars how many chars should be skipped?
1086 void StrBufCutLeft(StrBuf *Buf, int nChars)
1088 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1089 if (nChars >= Buf->BufUsed) {
1093 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
1094 Buf->BufUsed -= nChars;
1095 Buf->buf[Buf->BufUsed] = '\0';
1100 * @brief Cut the trailing n Chars from the string
1101 * @param Buf Buffer to modify
1102 * @param nChars how many chars should be trunkated?
1104 void StrBufCutRight(StrBuf *Buf, int nChars)
1106 if ((Buf == NULL) || (Buf->BufUsed == 0) || (Buf->buf == NULL))
1109 if (nChars >= Buf->BufUsed) {
1113 Buf->BufUsed -= nChars;
1114 Buf->buf[Buf->BufUsed] = '\0';
1119 * @brief Cut the string after n Chars
1120 * @param Buf Buffer to modify
1121 * @param AfternChars after how many chars should we trunkate the string?
1122 * @param At if non-null and points inside of our string, cut it there.
1124 void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
1126 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1128 AfternChars = At - Buf->buf;
1131 if ((AfternChars < 0) || (AfternChars >= Buf->BufUsed))
1133 Buf->BufUsed = AfternChars;
1134 Buf->buf[Buf->BufUsed] = '\0';
1140 * @brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
1141 * @param Buf the string to modify
1143 void StrBufTrim(StrBuf *Buf)
1146 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1148 while ((Buf->BufUsed > 0) &&
1149 isspace(Buf->buf[Buf->BufUsed - 1]))
1153 Buf->buf[Buf->BufUsed] = '\0';
1155 if (Buf->BufUsed == 0) return;
1157 while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
1160 if (delta > 0) StrBufCutLeft(Buf, delta);
1164 * @brief changes all spaces in the string (tab, linefeed...) to Blank (0x20)
1165 * @param Buf the string to modify
1167 void StrBufSpaceToBlank(StrBuf *Buf)
1171 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1174 pche = pch + Buf->BufUsed;
1183 void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
1188 if ((Buf == NULL) || (Buf->buf == NULL)) {
1192 pRight = strchr(Buf->buf, rightboundary);
1193 if (pRight != NULL) {
1194 StrBufCutAt(Buf, 0, pRight);
1197 pLeft = strrchr(ChrPtr(Buf), leftboundary);
1198 if (pLeft != NULL) {
1199 StrBufCutLeft(Buf, pLeft - Buf->buf + 1);
1205 * @ingroup StrBuf_Filler
1206 * @brief uppercase the contents of a buffer
1207 * @param Buf the buffer to translate
1209 void StrBufUpCase(StrBuf *Buf)
1213 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1216 pche = pch + Buf->BufUsed;
1217 while (pch < pche) {
1218 *pch = toupper(*pch);
1225 * @ingroup StrBuf_Filler
1226 * @brief lowercase the contents of a buffer
1227 * @param Buf the buffer to translate
1229 void StrBufLowerCase(StrBuf *Buf)
1233 if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
1236 pche = pch + Buf->BufUsed;
1237 while (pch < pche) {
1238 *pch = tolower(*pch);
1244 /*******************************************************************************
1245 * a tokenizer that kills, maims, and destroys *
1246 *******************************************************************************/
1249 * @ingroup StrBuf_Tokenizer
1250 * @brief Replace a token at a given place with a given length by another token with given length
1251 * @param Buf String where to work on
1252 * @param where where inside of the Buf is the search-token
1253 * @param HowLong How long is the token to be replaced
1254 * @param Repl Token to insert at 'where'
1255 * @param ReplLen Length of repl
1256 * @returns -1 if fail else length of resulting Buf
1258 int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong,
1259 const char *Repl, long ReplLen)
1262 if ((Buf == NULL) ||
1263 (where > Buf->BufUsed) ||
1264 (where + HowLong > Buf->BufUsed))
1267 if (where + ReplLen - HowLong > Buf->BufSize)
1268 if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0)
1271 memmove(Buf->buf + where + ReplLen,
1272 Buf->buf + where + HowLong,
1273 Buf->BufUsed - where - HowLong);
1275 memcpy(Buf->buf + where,
1278 Buf->BufUsed += ReplLen - HowLong;
1280 return Buf->BufUsed;
1284 * @ingroup StrBuf_Tokenizer
1285 * @brief Counts the numbmer of tokens in a buffer
1286 * @param source String to count tokens in
1287 * @param tok Tokenizer char to count
1288 * @returns numbers of tokenizer chars found
1290 int StrBufNum_tokens(const StrBuf *source, char tok)
1294 if ((source == NULL) || (source->BufUsed == 0))
1296 if ((source->BufUsed == 1) && (*source->buf == tok))
1300 pche = pch + source->BufUsed;
1311 * @ingroup StrBuf_Tokenizer
1312 * @brief a string tokenizer
1313 * @param Source StringBuffer to read into
1314 * @param parmnum n'th Parameter to remove
1315 * @param separator tokenizer character
1316 * @returns -1 if not found, else length of token.
1318 int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
1321 char *d, *s, *end; /* dest, source */
1324 /* Find desired @parameter */
1325 end = Source->buf + Source->BufUsed;
1327 while ((d <= end) &&
1330 /* End of string, bail! */
1335 if (*d == separator) {
1340 if ((d == NULL) || (d >= end))
1341 return 0; /* @Parameter not found */
1343 /* Find next @parameter */
1345 while ((s <= end) &&
1346 (*s && *s != separator))
1350 if (*s == separator)
1354 /* Hack and slash */
1359 memmove(d, s, Source->BufUsed - (s - Source->buf));
1360 Source->BufUsed += ReducedBy;
1361 Source->buf[Source->BufUsed] = '\0';
1363 else if (d == Source->buf) {
1365 Source->BufUsed = 0;
1369 Source->BufUsed += ReducedBy;
1380 int StrBufExtract_tokenFromStr(StrBuf *dest, const char *Source, long SourceLen, int parmnum, char separator)
1382 const StrBuf Temp = {
1395 return StrBufExtract_token(dest, &Temp, parmnum, separator);
1399 * @ingroup StrBuf_Tokenizer
1400 * @brief a string tokenizer
1401 * @param dest Destination StringBuffer
1402 * @param Source StringBuffer to read into
1403 * @param parmnum n'th Parameter to extract
1404 * @param separator tokenizer character
1405 * @returns -1 if not found, else length of token.
1407 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
1409 const char *s, *e; //* source * /
1410 int len = 0; //* running total length of extracted string * /
1411 int current_token = 0; //* token currently being processed * /
1414 dest->buf[0] = '\0';
1420 if ((Source == NULL) || (Source->BufUsed ==0)) {
1424 e = s + Source->BufUsed;
1427 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1429 while ((s < e) && !IsEmptyStr(s)) {
1430 if (*s == separator) {
1433 if (len >= dest->BufSize) {
1434 dest->BufUsed = len;
1435 if (IncreaseBuf(dest, 1, -1) < 0) {
1440 if ( (current_token == parmnum) &&
1441 (*s != separator)) {
1442 dest->buf[len] = *s;
1445 else if (current_token > parmnum) {
1451 dest->buf[len] = '\0';
1452 dest->BufUsed = len;
1454 if (current_token < parmnum) {
1455 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
1458 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
1467 * @ingroup StrBuf_Tokenizer
1468 * @brief a string tokenizer to fetch an integer
1469 * @param Source String containing tokens
1470 * @param parmnum n'th Parameter to extract
1471 * @param separator tokenizer character
1472 * @returns 0 if not found, else integer representation of the token
1474 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
1484 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1491 * @ingroup StrBuf_Tokenizer
1492 * @brief a string tokenizer to fetch a long integer
1493 * @param Source String containing tokens
1494 * @param parmnum n'th Parameter to extract
1495 * @param separator tokenizer character
1496 * @returns 0 if not found, else long integer representation of the token
1498 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
1508 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
1516 * @ingroup StrBuf_Tokenizer
1517 * @brief a string tokenizer to fetch an unsigned long
1518 * @param Source String containing tokens
1519 * @param parmnum n'th Parameter to extract
1520 * @param separator tokenizer character
1521 * @returns 0 if not found, else unsigned long representation of the token
1523 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
1534 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0) {
1538 return (unsigned long) atol(pnum);
1547 * @ingroup StrBuf_NextTokenizer
1548 * @brief a string tokenizer; Bounds checker
1549 * function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
1550 * @param Source our tokenbuffer
1551 * @param pStart the token iterator pointer to inspect
1552 * @returns whether the revolving pointer is inside of the search range
1554 int StrBufHaveNextToken(const StrBuf *Source, const char **pStart)
1556 if ((Source == NULL) ||
1557 (*pStart == StrBufNOTNULL) ||
1558 (Source->BufUsed == 0))
1562 if (*pStart == NULL)
1566 else if (*pStart > Source->buf + Source->BufUsed)
1570 else if (*pStart <= Source->buf)
1579 * @ingroup StrBuf_NextTokenizer
1580 * @brief a string tokenizer
1581 * @param dest Destination StringBuffer
1582 * @param Source StringBuffer to read into
1583 * @param pStart pointer to the end of the last token. Feed with NULL on start.
1584 * @param separator tokenizer
1585 * @returns -1 if not found, else length of token.
1587 int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
1589 const char *s; /* source */
1590 const char *EndBuffer; /* end stop of source buffer */
1591 int current_token = 0; /* token currently being processed */
1592 int len = 0; /* running total length of extracted string */
1594 if ((Source == NULL) ||
1595 (Source->BufUsed == 0) )
1597 *pStart = StrBufNOTNULL;
1603 EndBuffer = Source->buf + Source->BufUsed;
1607 dest->buf[0] = '\0';
1612 *pStart = EndBuffer + 1;
1616 if (*pStart == NULL)
1618 *pStart = Source->buf; /* we're starting to examine this buffer. */
1620 else if ((*pStart < Source->buf) ||
1621 (*pStart > EndBuffer ) )
1623 return -1; /* no more tokens to find. */
1627 /* start to find the next token */
1628 while ((s <= EndBuffer) &&
1629 (current_token == 0) )
1631 if (*s == separator)
1633 /* we found the next token */
1637 if (len >= dest->BufSize)
1639 /* our Dest-buffer isn't big enough, increase it. */
1640 dest->BufUsed = len;
1642 if (IncreaseBuf(dest, 1, -1) < 0) {
1643 /* WHUT? no more mem? bail out. */
1650 if ( (current_token == 0 ) && /* are we in our target token? */
1651 (!IsEmptyStr(s) ) &&
1652 (separator != *s) ) /* don't copy the token itself */
1654 dest->buf[len] = *s; /* Copy the payload */
1655 ++len; /* remember the bigger size. */
1661 /* did we reach the end? */
1662 if ((s > EndBuffer)) {
1663 EndBuffer = StrBufNOTNULL;
1664 *pStart = EndBuffer;
1667 *pStart = s; /* remember the position for the next run */
1670 /* sanitize our extracted token */
1671 dest->buf[len] = '\0';
1672 dest->BufUsed = len;
1679 * @ingroup StrBuf_NextTokenizer
1680 * @brief a string tokenizer
1681 * @param Source StringBuffer to read from
1682 * @param pStart pointer to the end of the last token. Feed with NULL.
1683 * @param separator tokenizer character
1684 * @param nTokens number of tokens to fastforward over
1685 * @returns -1 if not found, else length of token.
1687 int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens)
1689 const char *s, *EndBuffer; //* source * /
1690 int len = 0; //* running total length of extracted string * /
1691 int current_token = 0; //* token currently being processed * /
1693 if ((Source == NULL) ||
1694 (Source->BufUsed ==0)) {
1698 return Source->BufUsed;
1700 if (*pStart == NULL)
1701 *pStart = Source->buf;
1703 EndBuffer = Source->buf + Source->BufUsed;
1705 if ((*pStart < Source->buf) ||
1706 (*pStart > EndBuffer)) {
1714 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
1716 while ((s < EndBuffer) && !IsEmptyStr(s)) {
1717 if (*s == separator) {
1720 if (current_token >= nTokens) {
1732 * @ingroup StrBuf_NextTokenizer
1733 * @brief a string tokenizer to fetch an integer
1734 * @param Source StringBuffer to read from
1735 * @param pStart Cursor on the tokenstring
1736 * @param separator tokenizer character
1737 * @returns 0 if not found, else integer representation of the token
1739 int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator)
1749 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1756 * @ingroup StrBuf_NextTokenizer
1757 * @brief a string tokenizer to fetch a long integer
1758 * @param Source StringBuffer to read from
1759 * @param pStart Cursor on the tokenstring
1760 * @param separator tokenizer character
1761 * @returns 0 if not found, else long integer representation of the token
1763 long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator)
1773 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0)
1781 * @ingroup StrBuf_NextTokenizer
1782 * @brief a string tokenizer to fetch an unsigned long
1783 * @param Source StringBuffer to read from
1784 * @param pStart Cursor on the tokenstring
1785 * @param separator tokenizer character
1786 * @returns 0 if not found, else unsigned long representation of the token
1788 unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator)
1799 if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) {
1803 return (unsigned long) atol(pnum);
1813 /*******************************************************************************
1814 * Escape Appending *
1815 *******************************************************************************/
1818 * @ingroup StrBuf_DeEnCoder
1819 * @brief Escape a string for feeding out as a URL while appending it to a Buffer
1820 * @param OutBuf the output buffer
1821 * @param In Buffer to encode
1822 * @param PlainIn way in from plain old c strings
1824 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1826 const char *pch, *pche;
1830 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1832 if (PlainIn != NULL) {
1833 len = strlen(PlainIn);
1839 pche = pch + In->BufUsed;
1846 pt = OutBuf->buf + OutBuf->BufUsed;
1847 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1849 while (pch < pche) {
1851 IncreaseBuf(OutBuf, 1, -1);
1852 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1853 pt = OutBuf->buf + OutBuf->BufUsed;
1856 if((*pch >= 'a' && *pch <= 'z') ||
1857 (*pch >= '@' && *pch <= 'Z') || /* @ A-Z */
1858 (*pch >= '0' && *pch <= ':') || /* 0-9 : */
1859 (*pch == '!') || (*pch == '_') ||
1860 (*pch == ',') || (*pch == '.'))
1867 *(pt + 1) = HexList[(unsigned char)*pch][0];
1868 *(pt + 2) = HexList[(unsigned char)*pch][1];
1870 OutBuf->BufUsed += 3;
1878 * @ingroup StrBuf_DeEnCoder
1879 * @brief Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer
1880 * @param OutBuf the output buffer
1881 * @param In Buffer to encode
1882 * @param PlainIn way in from plain old c strings
1884 void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
1886 const char *pch, *pche;
1890 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1892 if (PlainIn != NULL) {
1893 len = strlen(PlainIn);
1899 pche = pch + In->BufUsed;
1906 pt = OutBuf->buf + OutBuf->BufUsed;
1907 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1909 while (pch < pche) {
1911 IncreaseBuf(OutBuf, 1, -1);
1912 pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
1913 pt = OutBuf->buf + OutBuf->BufUsed;
1916 if((*pch >= 'a' && *pch <= 'z') ||
1917 (*pch >= 'A' && *pch <= 'Z') || /* A-Z */
1918 (*pch >= '0' && *pch <= ':') || /* 0-9 : */
1919 (*pch == '!') || (*pch == '_') ||
1920 (*pch == ',') || (*pch == '.'))
1927 *(pt + 1) = HexList[(unsigned char)*pch][0];
1928 *(pt + 2) = HexList[(unsigned char)*pch][1];
1930 OutBuf->BufUsed += 3;
1938 * @ingroup StrBuf_DeEnCoder
1939 * @brief append a string with characters having a special meaning in xml encoded to the buffer
1940 * @param OutBuf the output buffer
1941 * @param In Buffer to encode
1942 * @param PlainIn way in from plain old c strings
1943 * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
1944 * @param OverrideLowChars should chars < 0x20 be replaced by _ or escaped as xml entity?
1946 void StrBufXMLEscAppend(StrBuf *OutBuf,
1948 const char *PlainIn,
1950 int OverrideLowChars)
1952 const char *pch, *pche;
1957 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
1959 if (PlainIn != NULL) {
1961 len = strlen((const char*)PlainIn);
1968 pch = (const char*)In->buf;
1969 pche = pch + In->BufUsed;
1976 pt = OutBuf->buf + OutBuf->BufUsed;
1977 /**< we max append 6 chars at once plus the \0 */
1978 pte = OutBuf->buf + OutBuf->BufSize - 6;
1980 while (pch < pche) {
1982 OutBuf->BufUsed = pt - OutBuf->buf;
1983 IncreaseBuf(OutBuf, 1, -1);
1984 pte = OutBuf->buf + OutBuf->BufSize - 6;
1985 /**< we max append 3 chars at once plus the \0 */
1987 pt = OutBuf->buf + OutBuf->BufUsed;
1991 memcpy(pt, HKEY("<"));
1995 else if (*pch == '>') {
1996 memcpy(pt, HKEY(">"));
2000 else if (*pch == '&') {
2001 memcpy(pt, HKEY("&"));
2005 else if ((*pch >= 0x20) && (*pch <= 0x7F)) {
2009 else if (*pch < 0x20) {
2010 /* we probably shouldn't be doing this */
2011 if (OverrideLowChars)
2021 *pt = HexList[*(unsigned char*)pch][0];
2023 *pt = HexList[*(unsigned char*)pch][1];
2031 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(pch, pche);
2034 while (IsUtf8Sequence > 0){
2045 *pt = HexList[*(unsigned char*)pch][0];
2047 *pt = HexList[*(unsigned char*)pch][1];
2056 OutBuf->BufUsed = pt - OutBuf->buf;
2061 * @ingroup StrBuf_DeEnCoder
2062 * @brief append a string in hex encoding to the buffer
2063 * @param OutBuf the output buffer
2064 * @param In Buffer to encode
2065 * @param PlainIn way in from plain old c strings
2066 * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
2068 void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
2070 const unsigned char *pch, *pche;
2074 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
2076 if (PlainIn != NULL) {
2078 len = strlen((const char*)PlainIn);
2085 pch = (const unsigned char*)In->buf;
2086 pche = pch + In->BufUsed;
2093 pt = OutBuf->buf + OutBuf->BufUsed;
2094 pte = OutBuf->buf + OutBuf->BufSize - 3; /**< we max append 3 chars at once plus the \0 */
2096 while (pch < pche) {
2098 IncreaseBuf(OutBuf, 1, -1);
2099 pte = OutBuf->buf + OutBuf->BufSize - 3; /**< we max append 3 chars at once plus the \0 */
2100 pt = OutBuf->buf + OutBuf->BufUsed;
2103 *pt = HexList[*pch][0];
2105 *pt = HexList[*pch][1];
2106 pt ++; pch ++; OutBuf->BufUsed += 2;
2111 void StrBufBase64Append(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn, long PlainInLen, int linebreaks)
2118 if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
2120 if (PlainIn != NULL) {
2122 len = strlen(PlainIn);
2135 ExpectLen = ((len * 134) / 100) + OutBuf->BufUsed;
2137 if (ExpectLen > OutBuf->BufSize)
2138 if (IncreaseBuf(OutBuf, 1, ExpectLen) < ExpectLen)
2141 pt = OutBuf->buf + OutBuf->BufUsed;
2143 len = CtdlEncodeBase64(pt, pch, len, linebreaks);
2146 OutBuf->BufUsed += len;
2151 * @ingroup StrBuf_DeEnCoder
2152 * @brief append a string in hex encoding to the buffer
2153 * @param OutBuf the output buffer
2154 * @param In Buffer to encode
2155 * @param PlainIn way in from plain old c strings
2157 void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
2159 StrBufHexEscAppend(OutBuf, In, (const unsigned char*) PlainIn, -1);
2163 * @ingroup StrBuf_DeEnCoder
2164 * @brief Append a string, escaping characters which have meaning in HTML.
2166 * @param Target target buffer
2167 * @param Source source buffer; set to NULL if you just have a C-String
2168 * @param PlainIn Plain-C string to append; set to NULL if unused
2169 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
2170 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
2171 * if set to 2, linebreaks are replaced by <br/>
2173 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
2175 const char *aptr, *eiptr;
2179 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2182 if (PlainIn != NULL) {
2184 len = strlen(PlainIn);
2189 eiptr = aptr + Source->BufUsed;
2190 len = Source->BufUsed;
2196 bptr = Target->buf + Target->BufUsed;
2197 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2199 while (aptr < eiptr){
2201 IncreaseBuf(Target, 1, -1);
2202 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2203 bptr = Target->buf + Target->BufUsed;
2206 memcpy(bptr, "<", 4);
2208 Target->BufUsed += 4;
2210 else if (*aptr == '>') {
2211 memcpy(bptr, ">", 4);
2213 Target->BufUsed += 4;
2215 else if (*aptr == '&') {
2216 memcpy(bptr, "&", 5);
2218 Target->BufUsed += 5;
2220 else if (*aptr == '"') {
2221 memcpy(bptr, """, 6);
2223 Target->BufUsed += 6;
2225 else if (*aptr == '\'') {
2226 memcpy(bptr, "'", 5);
2228 Target->BufUsed += 5;
2230 else if (*aptr == LB) {
2235 else if (*aptr == RB) {
2240 else if (*aptr == QU) {
2245 else if ((*aptr == 32) && (nbsp == 1)) {
2246 memcpy(bptr, " ", 6);
2248 Target->BufUsed += 6;
2250 else if ((*aptr == '\n') && (nolinebreaks == 1)) {
2251 *bptr='\0'; /* nothing */
2253 else if ((*aptr == '\n') && (nolinebreaks == 2)) {
2254 memcpy(bptr, "<br/>", 11);
2256 Target->BufUsed += 11;
2260 else if ((*aptr == '\r') && (nolinebreaks != 0)) {
2261 *bptr='\0'; /* nothing */
2271 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
2273 return Target->BufUsed;
2277 * @ingroup StrBuf_DeEnCoder
2278 * @brief Append a string, escaping characters which have meaning in HTML.
2279 * Converts linebreaks into blanks; escapes single quotes
2280 * @param Target target buffer
2281 * @param Source source buffer; set to NULL if you just have a C-String
2282 * @param PlainIn Plain-C string to append; set to NULL if unused
2284 void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
2286 const char *aptr, *eiptr;
2290 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2293 if (PlainIn != NULL) {
2295 len = strlen(PlainIn);
2300 eiptr = aptr + Source->BufUsed;
2301 len = Source->BufUsed;
2307 eptr = Target->buf + Target->BufSize - 8;
2308 tptr = Target->buf + Target->BufUsed;
2310 while (aptr < eiptr){
2312 IncreaseBuf(Target, 1, -1);
2313 eptr = Target->buf + Target->BufSize - 8;
2314 tptr = Target->buf + Target->BufUsed;
2317 if (*aptr == '\n') {
2321 else if (*aptr == '\r') {
2325 else if (*aptr == '\'') {
2331 Target->BufUsed += 5;
2344 * @ingroup StrBuf_DeEnCoder
2345 * @brief Append a string, escaping characters which have meaning in ICAL.
2347 * @param Target target buffer
2348 * @param Source source buffer; set to NULL if you just have a C-String
2349 * @param PlainIn Plain-C string to append; set to NULL if unused
2351 void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
2353 const char *aptr, *eiptr;
2357 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2360 if (PlainIn != NULL) {
2362 len = strlen(PlainIn);
2367 eiptr = aptr + Source->BufUsed;
2368 len = Source->BufUsed;
2374 eptr = Target->buf + Target->BufSize - 8;
2375 tptr = Target->buf + Target->BufUsed;
2377 while (aptr < eiptr){
2378 if(tptr + 3 >= eptr) {
2379 IncreaseBuf(Target, 1, -1);
2380 eptr = Target->buf + Target->BufSize - 8;
2381 tptr = Target->buf + Target->BufUsed;
2384 if (*aptr == '\n') {
2391 else if (*aptr == '\r') {
2398 else if (*aptr == ',') {
2414 * @ingroup StrBuf_DeEnCoder
2415 * @brief Append a string, escaping characters which have meaning in JavaScript strings .
2417 * @param Target target buffer
2418 * @param Source source buffer; set to NULL if you just have a C-String
2419 * @param PlainIn Plain-C string to append; set to NULL if unused
2420 * @returns size of result or -1
2422 long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
2424 const char *aptr, *eiptr;
2429 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2432 if (PlainIn != NULL) {
2434 len = strlen(PlainIn);
2439 eiptr = aptr + Source->BufUsed;
2440 len = Source->BufUsed;
2446 bptr = Target->buf + Target->BufUsed;
2447 eptr = Target->buf + Target->BufSize - 7; /* our biggest unit to put in... */
2449 while (aptr < eiptr){
2451 IncreaseBuf(Target, 1, -1);
2452 eptr = Target->buf + Target->BufSize - 7; /* our biggest unit to put in... */
2453 bptr = Target->buf + Target->BufUsed;
2457 memcpy(bptr, HKEY("\\n"));
2459 Target->BufUsed += 2;
2462 memcpy(bptr, HKEY("\\r"));
2464 Target->BufUsed += 2;
2471 Target->BufUsed += 2;
2474 if ((*(aptr + 1) == 'u') &&
2475 isxdigit(*(aptr + 2)) &&
2476 isxdigit(*(aptr + 3)) &&
2477 isxdigit(*(aptr + 4)) &&
2478 isxdigit(*(aptr + 5)))
2479 { /* oh, a unicode escaper. let it pass through. */
2480 memcpy(bptr, aptr, 6);
2483 Target->BufUsed += 6;
2491 Target->BufUsed += 2;
2499 Target->BufUsed += 2;
2506 Target->BufUsed += 2;
2513 Target->BufUsed += 2;
2516 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
2517 while (IsUtf8Sequence > 0){
2520 if (--IsUtf8Sequence)
2528 if ((bptr == eptr - 1 ) && !IsEmptyStr(aptr) )
2530 return Target->BufUsed;
2534 * @ingroup StrBuf_DeEnCoder
2535 * @brief Append a string, escaping characters which have meaning in HTML + json.
2537 * @param Target target buffer
2538 * @param Source source buffer; set to NULL if you just have a C-String
2539 * @param PlainIn Plain-C string to append; set to NULL if unused
2540 * @param nbsp If nonzero, spaces are converted to non-breaking spaces.
2541 * @param nolinebreaks if set to 1, linebreaks are removed from the string.
2542 * if set to 2, linebreaks are replaced by <br/>
2544 long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
2546 const char *aptr, *eiptr;
2549 int IsUtf8Sequence = 0;
2551 if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
2554 if (PlainIn != NULL) {
2556 len = strlen(PlainIn);
2561 eiptr = aptr + Source->BufUsed;
2562 len = Source->BufUsed;
2568 bptr = Target->buf + Target->BufUsed;
2569 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2571 while (aptr < eiptr){
2573 IncreaseBuf(Target, 1, -1);
2574 eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
2575 bptr = Target->buf + Target->BufUsed;
2579 memcpy(bptr, HKEY("<"));
2581 Target->BufUsed += 4;
2584 memcpy(bptr, HKEY(">"));
2586 Target->BufUsed += 4;
2589 memcpy(bptr, HKEY("&"));
2591 Target->BufUsed += 5;
2604 switch (nolinebreaks) {
2606 *bptr='\0'; /* nothing */
2609 memcpy(bptr, HKEY("<br/>"));
2611 Target->BufUsed += 11;
2614 memcpy(bptr, HKEY("\\n"));
2616 Target->BufUsed += 2;
2620 switch (nolinebreaks) {
2623 *bptr='\0'; /* nothing */
2626 memcpy(bptr, HKEY("\\r"));
2628 Target->BufUsed += 2;
2638 Target->BufUsed += 2;
2641 if ((*(aptr + 1) == 'u') &&
2642 isxdigit(*(aptr + 2)) &&
2643 isxdigit(*(aptr + 3)) &&
2644 isxdigit(*(aptr + 4)) &&
2645 isxdigit(*(aptr + 5)))
2646 { /* oh, a unicode escaper. let it pass through. */
2647 memcpy(bptr, aptr, 6);
2650 Target->BufUsed += 6;
2658 Target->BufUsed += 2;
2666 Target->BufUsed += 2;
2673 Target->BufUsed += 2;
2680 Target->BufUsed += 2;
2684 memcpy(bptr, HKEY(" "));
2686 Target->BufUsed += 6;
2690 IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
2691 while (IsUtf8Sequence > 0){
2694 if (--IsUtf8Sequence)
2702 if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
2704 return Target->BufUsed;
2709 * @ingroup StrBuf_DeEnCoder
2710 * @brief replace all non-Ascii characters by another
2711 * @param Buf buffer to inspect
2712 * @param repl charater to stamp over non ascii chars
2714 void StrBufAsciify(StrBuf *Buf, const char repl)
2718 for (offset = 0; offset < Buf->BufUsed; offset ++)
2719 if (!isascii(Buf->buf[offset]))
2720 Buf->buf[offset] = repl;
2725 * @ingroup StrBuf_DeEnCoder
2726 * @brief unhide special chars hidden to the HTML escaper
2727 * @param target buffer to put the unescaped string in
2728 * @param source buffer to unescape
2730 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source)
2735 if ((source == NULL) || (target == NULL) || (target->buf == NULL))
2741 FlushStrBuf(target);
2743 len = source->BufUsed;
2744 for (a = 0; a < len; ++a) {
2745 if (target->BufUsed >= target->BufSize)
2746 IncreaseBuf(target, 1, -1);
2748 if (source->buf[a] == '=') {
2749 hex[0] = source->buf[a + 1];
2750 hex[1] = source->buf[a + 2];
2753 sscanf(hex, "%02x", &b);
2754 target->buf[target->BufUsed] = b;
2755 target->buf[++target->BufUsed] = 0;
2759 target->buf[target->BufUsed] = source->buf[a];
2760 target->buf[++target->BufUsed] = 0;
2767 * @ingroup StrBuf_DeEnCoder
2768 * @brief hide special chars from the HTML escapers and friends
2769 * @param target buffer to put the escaped string in
2770 * @param source buffer to escape
2772 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source)
2777 FlushStrBuf(target);
2779 if ((source == NULL) || (target == NULL) || (target->buf == NULL))
2784 len = source->BufUsed;
2785 for (i=0; i<len; ++i) {
2786 if (target->BufUsed + 4 >= target->BufSize)
2787 IncreaseBuf(target, 1, -1);
2788 if ( (isalnum(source->buf[i])) ||
2789 (source->buf[i]=='-') ||
2790 (source->buf[i]=='_') ) {
2791 target->buf[target->BufUsed++] = source->buf[i];
2794 sprintf(&target->buf[target->BufUsed],
2796 (0xFF &source->buf[i]));
2797 target->BufUsed += 3;
2800 target->buf[target->BufUsed + 1] = '\0';
2804 /*******************************************************************************
2805 * Quoted Printable de/encoding *
2806 *******************************************************************************/
2809 * @ingroup StrBuf_DeEnCoder
2810 * @brief decode a buffer from base 64 encoding; destroys original
2811 * @param Buf Buffor to transform
2813 int StrBufDecodeBase64(StrBuf *Buf)
2821 xferbuf = (char*) malloc(Buf->BufSize);
2822 if (xferbuf == NULL)
2826 siz = CtdlDecodeBase64(xferbuf,
2833 Buf->buf[Buf->BufUsed] = '\0';
2838 * @ingroup StrBuf_DeEnCoder
2839 * @brief decode a buffer from base 64 encoding; expects targetbuffer
2840 * @param BufIn Buffor to transform
2841 * @param BufOut Buffer to put result into
2843 int StrBufDecodeBase64To(const StrBuf *BufIn, StrBuf *BufOut)
2845 if ((BufIn == NULL) || (BufOut == NULL))
2848 if (BufOut->BufSize < BufIn->BufUsed)
2849 IncreaseBuf(BufOut, BufIn->BufUsed, 0);
2851 BufOut->BufUsed = CtdlDecodeBase64(BufOut->buf,
2854 return BufOut->BufUsed;
2857 typedef struct __z_enc_stream {
2862 void *StrBufNewStreamContext(eStreamType type)
2864 base64_decodestate *state;;
2870 state = (base64_decodestate*) malloc(sizeof(base64_decodestate));
2871 base64_init_decodestate(state);
2877 z_enc_stream *stream;
2880 stream = (z_enc_stream *) malloc(sizeof(z_enc_stream));
2881 memset(stream, 0, sizeof(z_enc_stream));
2882 stream->OutBuf.BufSize = 4*SIZ; /// TODO 64
2883 stream->OutBuf.buf = (char*)malloc(stream->OutBuf.BufSize);
2885 err = inflateInit(&stream->zstream);
2888 return NULL;/// tODO cleanup
2894 z_enc_stream *stream;
2897 stream = (z_enc_stream *) malloc(sizeof(z_enc_stream));
2898 memset(stream, 0, sizeof(z_enc_stream));
2899 stream->OutBuf.BufSize = 4*SIZ; /// todo 64
2900 stream->OutBuf.buf = (char*)malloc(stream->OutBuf.BufSize);
2901 /* write gzip header */
2902 stream->OutBuf.BufUsed = snprintf
2903 (stream->OutBuf.buf,
2904 stream->OutBuf.BufSize,
2905 "%c%c%c%c%c%c%c%c%c%c",
2906 gz_magic[0], gz_magic[1], Z_DEFLATED,
2907 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
2910 err = deflateInit2(&stream->zstream,
2911 ZLibCompressionRatio,
2915 Z_DEFAULT_STRATEGY);
2917 return NULL;/// tODO cleanup
2928 void StrBufDestroyStreamContext(eStreamType type, void **vStream)
2939 z_enc_stream *stream = (z_enc_stream *)*vStream;
2940 (void)inflateEnd(&stream->zstream);
2941 free(stream->OutBuf.buf);
2946 z_enc_stream *stream = (z_enc_stream *)*vStream;
2947 free(stream->OutBuf.buf);
2958 void StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *vStream, int LastChunk)
2967 // base64_decodestate *state = (base64_decodestate*)vStream;
2973 pInLen = In->BufUsed;
2975 if ((In == NULL) || (vStream == NULL))
2978 ExpectLen = (pInLen / 4) * 3;
2980 if (Target->BufSize - Target->BufUsed < ExpectLen)
2982 IncreaseBuf(Target, 1, Target->BufUsed + ExpectLen + 1);
2985 //// ExpectLen = base64_encode_block(pIn, pInLen, Target->buf + Target->BufUsed, state);
2986 Target->BufUsed += ExpectLen;
2987 Target->buf[Target->BufUsed] = '\0';
2994 base64_decodestate *state = (base64_decodestate *)vStream;
3000 pInLen = In->BufUsed;
3002 if ((pIn == NULL) || (vStream == NULL))
3005 ExpectLen = (pInLen / 4) * 3;
3007 if (Target->BufSize - Target->BufUsed < ExpectLen)
3009 IncreaseBuf(Target, 1, Target->BufUsed + ExpectLen + 1);
3012 ExpectLen = base64_decode_block(pIn, pInLen, Target->buf + Target->BufUsed, state);
3013 Target->BufUsed += ExpectLen;
3014 Target->buf[Target->BufUsed] = '\0';
3020 z_enc_stream *stream = (z_enc_stream *)vStream;
3021 int org_outbuf_len = stream->zstream.total_out;
3023 unsigned int chunkavail;
3025 if (In->ReadWritePointer != NULL)
3027 stream->zstream.next_in = (Bytef *) In->ReadWritePointer;
3028 stream->zstream.avail_in = (uInt) In->Buf->BufUsed -
3029 (In->ReadWritePointer - In->Buf->buf);
3033 stream->zstream.next_in = (Bytef *) In->Buf->buf;
3034 stream->zstream.avail_in = (uInt) In->Buf->BufUsed;
3037 stream->zstream.next_out = (unsigned char*)stream->OutBuf.buf + stream->OutBuf.BufUsed;
3038 stream->zstream.avail_out = chunkavail = (uInt) stream->OutBuf.BufSize - stream->OutBuf.BufUsed;
3040 err = deflate(&stream->zstream, (LastChunk) ? Z_FINISH : Z_NO_FLUSH);
3042 stream->OutBuf.BufUsed += (chunkavail - stream->zstream.avail_out);
3043 /// todo + org_outbuf_len;
3045 if (Target) SwapBuffers(Target->Buf, &stream->OutBuf);
3047 if (stream->zstream.avail_in == 0)
3049 FlushStrBuf(In->Buf);
3050 In->ReadWritePointer = NULL;
3054 if (stream->zstream.avail_in < 64)
3056 memmove(In->Buf->buf,
3057 In->Buf->buf + In->Buf->BufUsed - stream->zstream.avail_in,
3058 stream->zstream.avail_in);
3060 In->Buf->BufUsed = stream->zstream.avail_in;
3061 In->Buf->buf[In->Buf->BufUsed] = '\0';
3066 In->ReadWritePointer = In->Buf->buf +
3067 (In->Buf->BufUsed - stream->zstream.avail_in);
3074 z_enc_stream *stream = (z_enc_stream *)vStream;
3075 int org_outbuf_len = stream->zstream.total_out;
3078 if ((stream->zstream.avail_out != 0) && (stream->zstream.next_in != NULL)) {
3079 if (In->ReadWritePointer != NULL)
3081 stream->zstream.next_in = (Bytef *) In->ReadWritePointer;
3082 stream->zstream.avail_in = (uInt) In->Buf->BufUsed -
3083 (In->ReadWritePointer - In->Buf->buf);
3087 stream->zstream.next_in = (Bytef *) In->Buf->buf;
3088 stream->zstream.avail_in = (uInt) In->Buf->BufUsed;
3092 stream->zstream.next_out = (unsigned char*)stream->OutBuf.buf + stream->OutBuf.BufUsed;
3093 stream->zstream.avail_out = (uInt) stream->OutBuf.BufSize - stream->OutBuf.BufUsed;
3095 err = inflate(&stream->zstream, Z_NO_FLUSH);
3097 ///assert(ret != Z_STREAM_ERROR); /* state not clobbered * /
3100 err = Z_DATA_ERROR; /* and fall through */
3103 fprintf(stderr, "sanoteuh\n");
3105 (void)inflateEnd(&stream->zstream);
3109 stream->OutBuf.BufUsed += stream->zstream.total_out + org_outbuf_len;
3111 if (Target) SwapBuffers(Target->Buf, &stream->OutBuf);
3113 if (stream->zstream.avail_in == 0)
3115 FlushStrBuf(In->Buf);
3116 In->ReadWritePointer = NULL;
3120 if (stream->zstream.avail_in < 64)
3122 memmove(In->Buf->buf,
3123 In->Buf->buf + In->Buf->BufUsed - stream->zstream.avail_in,
3124 stream->zstream.avail_in);
3126 In->Buf->BufUsed = stream->zstream.avail_in;
3127 In->Buf->buf[In->Buf->BufUsed] = '\0';
3132 In->ReadWritePointer = In->Buf->buf +
3133 (In->Buf->BufUsed - stream->zstream.avail_in);
3146 * @ingroup StrBuf_DeEnCoder
3147 * @brief decode a buffer from base 64 encoding; destroys original
3148 * @param Buf Buffor to transform
3150 int StrBufDecodeHex(StrBuf *Buf)
3153 char *pch, *pche, *pchi;
3155 if (Buf == NULL) return -1;
3157 pch = pchi = Buf->buf;
3158 pche = pch + Buf->BufUsed;
3160 while (pchi < pche){
3161 ch = decode_hex(pchi);
3168 Buf->BufUsed = pch - Buf->buf;
3169 return Buf->BufUsed;
3173 * @ingroup StrBuf_DeEnCoder
3174 * @brief replace all chars >0x20 && < 0x7F with Mute
3175 * @param Mute char to put over invalid chars
3176 * @param Buf Buffor to transform
3178 int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
3182 if (Buf == NULL) return -1;
3183 pch = (unsigned char *)Buf->buf;
3184 while (pch < (unsigned char *)Buf->buf + Buf->BufUsed) {
3185 if ((*pch < 0x20) || (*pch > 0x7F))
3189 return Buf->BufUsed;
3194 * @ingroup StrBuf_DeEnCoder
3195 * @brief remove escaped strings from i.e. the url string (like %20 for blanks)
3196 * @param Buf Buffer to translate
3197 * @param StripBlanks Reduce several blanks to one?
3199 long StrBufUnescape(StrBuf *Buf, int StripBlanks)
3208 while ((Buf->BufUsed > 0) && (isspace(Buf->buf[Buf->BufUsed - 1]))){
3209 Buf->buf[Buf->BufUsed - 1] = '\0';
3214 while (a < Buf->BufUsed) {
3215 if (Buf->buf[a] == '+')
3217 else if (Buf->buf[a] == '%') {
3218 /* don't let % chars through, rather truncate the input. */
3219 if (a + 2 > Buf->BufUsed) {
3224 hex[0] = Buf->buf[a + 1];
3225 hex[1] = Buf->buf[a + 2];
3228 sscanf(hex, "%02x", &b);
3229 Buf->buf[a] = (char) b;
3230 len = Buf->BufUsed - a - 2;
3232 memmove(&Buf->buf[a + 1], &Buf->buf[a + 3], len);
3244 * @ingroup StrBuf_DeEnCoder
3245 * @brief RFC2047-encode a header field if necessary.
3246 * If no non-ASCII characters are found, the string
3247 * will be copied verbatim without encoding.
3249 * @param target Target buffer.
3250 * @param source Source string to be encoded.
3251 * @returns encoded length; -1 if non success.
3253 int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
3255 const char headerStr[] = "=?UTF-8?Q?";
3256 int need_to_encode = 0;
3260 if ((source == NULL) ||
3264 while ((i < source->BufUsed) &&
3265 (!IsEmptyStr (&source->buf[i])) &&
3266 (need_to_encode == 0)) {
3267 if (((unsigned char) source->buf[i] < 32) ||
3268 ((unsigned char) source->buf[i] > 126)) {
3274 if (!need_to_encode) {
3275 if (*target == NULL) {
3276 *target = NewStrBufPlain(source->buf, source->BufUsed);
3279 FlushStrBuf(*target);
3280 StrBufAppendBuf(*target, source, 0);
3283 return (*target)->BufUsed;
3287 if (*target == NULL)
3288 *target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
3289 else if (sizeof(headerStr) + source->BufUsed >= (*target)->BufSize)
3290 IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
3291 memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
3292 (*target)->BufUsed = sizeof(headerStr) - 1;
3293 for (i=0; (i < source->BufUsed); ++i) {
3294 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
3295 IncreaseBuf(*target, 1, 0);
3296 ch = (unsigned char) source->buf[i];
3305 sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
3306 (*target)->BufUsed += 3;
3310 (*target)->buf[(*target)->BufUsed] = '_';
3312 (*target)->buf[(*target)->BufUsed] = ch;
3313 (*target)->BufUsed++;
3317 if ((*target)->BufUsed + 4 >= (*target)->BufSize)
3318 IncreaseBuf(*target, 1, 0);
3320 (*target)->buf[(*target)->BufUsed++] = '?';
3321 (*target)->buf[(*target)->BufUsed++] = '=';
3322 (*target)->buf[(*target)->BufUsed] = '\0';
3323 return (*target)->BufUsed;;
3327 * @ingroup StrBuf_DeEnCoder
3328 * @brief Quoted-Printable encode a message; make it < 80 columns width.
3329 * @param source Source string to be encoded.
3330 * @returns buffer with encoded message.
3332 StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
3336 const char *ptr, *eptr;
3340 OutBuf = NewStrBufPlain(NULL, StrLength(EncodeMe) * 4);
3342 OEptr = OutBuf->buf + OutBuf->BufSize;
3343 ptr = EncodeMe->buf;
3344 eptr = EncodeMe->buf + EncodeMe->BufUsed;
3349 if (Optr + 4 >= OEptr)
3352 Offset = Optr - OutBuf->buf;
3353 OutBuf->BufUsed = Optr - OutBuf->buf;
3354 IncreaseBuf(OutBuf, 1, 0);
3355 Optr = OutBuf->buf + Offset;
3356 OEptr = OutBuf->buf + OutBuf->BufSize;
3360 /* ignore carriage returns */
3363 else if (*ptr == '\n') {
3364 /* hard line break */
3365 memcpy(Optr, HKEY("=0A"));
3370 else if (( (*ptr >= 32) && (*ptr <= 60) ) ||
3371 ( (*ptr >= 62) && (*ptr <= 126) ))
3382 *Optr = HexList[ch][0];
3384 *Optr = HexList[ch][1];
3391 /* soft line break */
3392 if (isspace(*(Optr - 1))) {
3397 *Optr = HexList[ch][0];
3399 *Optr = HexList[ch][1];
3411 OutBuf->BufUsed = Optr - OutBuf->buf;
3417 static void AddRecipient(StrBuf *Target,
3419 StrBuf *EmailAddress,
3424 if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
3425 if (strchr(ChrPtr(UserName), ',') != NULL) QuoteMe = 1;
3427 if (QuoteMe) StrBufAppendBufPlain(Target, HKEY("\""), 0);
3428 StrBufRFC2047encode(&EncBuf, UserName);
3429 StrBufAppendBuf(Target, EncBuf, 0);
3430 if (QuoteMe) StrBufAppendBufPlain(Target, HKEY("\" "), 0);
3431 else StrBufAppendBufPlain(Target, HKEY(" "), 0);
3433 if (StrLength(EmailAddress) > 0){
3434 StrBufAppendBufPlain(Target, HKEY("<"), 0);
3435 StrBufAppendBuf(Target, EmailAddress, 0); /* TODO: what about IDN???? */
3436 StrBufAppendBufPlain(Target, HKEY(">"), 0);
3442 * \brief QP encode parts of an email TO/CC/BCC vector, and strip/filter invalid parts
3443 * \param Recp Source list of email recipients
3444 * \param UserName Temporary buffer for internal use; Please provide valid buffer.
3445 * \param EmailAddress Temporary buffer for internal use; Please provide valid buffer.
3446 * \param EncBuf Temporary buffer for internal use; Please provide valid buffer.
3447 * \returns encoded & sanitized buffer with the contents of Recp; Caller owns this memory.
3449 StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
3451 StrBuf *EmailAddress,
3455 const char *pch, *pche;
3456 const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
3458 if ((Recp == NULL) || (StrLength(Recp) == 0))
3462 pche = pch + StrLength(Recp);
3464 if (!CheckEncode(pch, -1, pche))
3465 return NewStrBufDup(Recp);
3467 Target = NewStrBufPlain(NULL, StrLength(Recp));
3469 while ((pch != NULL) && (pch < pche))
3471 while (isspace(*pch)) pch++;
3472 UserEnd = EmailStart = EmailEnd = NULL;
3474 if ((*pch == '"') || (*pch == '\'')) {
3475 UserStart = pch + 1;
3477 UserEnd = strchr(UserStart, *pch);
3478 if (UserEnd == NULL)
3479 break; ///TODO: Userfeedback??
3480 EmailStart = UserEnd + 1;
3481 while (isspace(*EmailStart))
3483 if (UserEnd == UserStart) {
3484 UserStart = UserEnd = NULL;
3487 if (*EmailStart == '<') {
3489 EmailEnd = strchr(EmailStart, '>');
3490 if (EmailEnd == NULL)
3491 EmailEnd = strchr(EmailStart, ',');
3495 EmailEnd = strchr(EmailStart, ',');
3497 if (EmailEnd == NULL)
3504 EmailEnd = strchr(UserStart, ',');
3505 if (EmailEnd == NULL) {
3506 EmailEnd = strchr(pch, '>');
3508 if (EmailEnd != NULL) {
3518 while ((EmailEnd > UserStart) && !gt &&
3519 ((*EmailEnd == ',') ||
3520 (*EmailEnd == '>') ||
3521 (isspace(*EmailEnd))))
3523 if (*EmailEnd == '>')
3528 if (EmailEnd == UserStart)
3532 EmailStart = strchr(UserStart, '<');
3533 if ((EmailStart == NULL) || (EmailStart > EmailEnd))
3535 UserEnd = EmailStart;
3537 while ((UserEnd > UserStart) &&
3538 isspace (*(UserEnd - 1)))
3541 if (UserStart >= UserEnd)
3542 UserStart = UserEnd = NULL;
3544 else { /* this is a local recipient... no domain, just a realname */
3545 EmailStart = UserStart;
3546 At = strchr(EmailStart, '@');
3552 EmailStart = UserStart;
3558 if ((UserStart != NULL) && (UserEnd != NULL))
3559 StrBufPlain(UserName, UserStart, UserEnd - UserStart);
3560 else if ((UserStart != NULL) && (UserEnd == NULL))
3561 StrBufPlain(UserName, UserStart, UserEnd - UserStart);
3563 FlushStrBuf(UserName);
3565 if ((EmailStart != NULL) && (EmailEnd != NULL))
3566 StrBufPlain(EmailAddress, EmailStart, EmailEnd - EmailStart);
3567 else if ((EmailStart != NULL) && (EmailEnd == NULL))
3568 StrBufPlain(EmailAddress, EmailStart, EmailEnd - pche);
3570 FlushStrBuf(EmailAddress);
3572 AddRecipient(Target, UserName, EmailAddress, EncBuf);
3577 if ((pch != NULL) && (*pch == ','))
3579 if (pch != NULL) while (isspace(*pch))
3588 * @brief replaces all occurances of 'search' by 'replace'
3589 * @param buf Buffer to modify
3590 * @param search character to search
3591 * @param replace character to replace search by
3593 void StrBufReplaceChars(StrBuf *buf, char search, char replace)
3598 for (i=0; i<buf->BufUsed; i++)
3599 if (buf->buf[i] == search)
3600 buf->buf[i] = replace;
3606 * @brief removes all \\r s from the string, or replaces them with \n if its not a combination of both.
3607 * @param buf Buffer to modify
3609 void StrBufToUnixLF(StrBuf *buf)
3611 char *pche, *pchS, *pchT;
3615 pche = buf->buf + buf->BufUsed;
3616 pchS = pchT = buf->buf;
3622 if (*pchS != '\n') {
3631 buf->BufUsed = pchT - buf->buf;
3635 /*******************************************************************************
3636 * Iconv Wrapper; RFC822 de/encoding *
3637 *******************************************************************************/
3640 * @ingroup StrBuf_DeEnCoder
3641 * @brief Wrapper around iconv_open()
3642 * Our version adds aliases for non-standard Microsoft charsets
3643 * such as 'MS950', aliasing them to names like 'CP950'
3645 * @param tocode Target encoding
3646 * @param fromcode Source encoding
3647 * @param pic anonimized pointer to iconv struct
3649 void ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
3652 iconv_t ic = (iconv_t)(-1) ;
3653 ic = iconv_open(tocode, fromcode);
3654 if (ic == (iconv_t)(-1) ) {
3655 char alias_fromcode[64];
3656 if ( (strlen(fromcode) == 5) && (!strncasecmp(fromcode, "MS", 2)) ) {
3657 safestrncpy(alias_fromcode, fromcode, sizeof alias_fromcode);
3658 alias_fromcode[0] = 'C';
3659 alias_fromcode[1] = 'P';
3660 ic = iconv_open(tocode, alias_fromcode);
3663 *(iconv_t *)pic = ic;
3669 * @ingroup StrBuf_DeEnCoder
3670 * @brief find one chunk of a RFC822 encoded string
3671 * @param Buffer where to search
3672 * @param bptr where to start searching
3673 * @returns found position, NULL if none.
3675 static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr)
3678 /* Find the next ?Q? */
3679 if (Buf->BufUsed - (bptr - Buf->buf) < 6)
3682 end = strchr(bptr + 2, '?');
3687 if ((Buf->BufUsed - (end - Buf->buf) > 3) &&
3688 (((*(end + 1) == 'B') || (*(end + 1) == 'Q')) ||
3689 ((*(end + 1) == 'b') || (*(end + 1) == 'q'))) &&
3690 (*(end + 2) == '?')) {
3691 /* skip on to the end of the cluster, the next ?= */
3692 end = strstr(end + 3, "?=");
3695 /* sort of half valid encoding, try to find an end. */
3696 end = strstr(bptr, "?=");
3703 * @ingroup StrBuf_DeEnCoder
3704 * @brief convert one buffer according to the preselected iconv pointer PIC
3705 * @param ConvertBuf buffer we need to translate
3706 * @param TmpBuf To share a workbuffer over several iterations. prepare to have it filled with useless stuff afterwards.
3707 * @param pic Pointer to the iconv-session Object
3709 void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
3715 char *ibuf; /**< Buffer of characters to be converted */
3716 char *obuf; /**< Buffer for converted characters */
3717 size_t ibuflen; /**< Length of input buffer */
3718 size_t obuflen; /**< Length of output buffer */
3721 if ((ConvertBuf == NULL) || (TmpBuf == NULL))
3724 /* since we're converting to utf-8, one glyph may take up to 6 bytes */
3725 if (ConvertBuf->BufUsed * 6 >= TmpBuf->BufSize)
3726 IncreaseBuf(TmpBuf, 0, ConvertBuf->BufUsed * 6);
3728 ic = *(iconv_t*)pic;
3729 ibuf = ConvertBuf->buf;
3730 ibuflen = ConvertBuf->BufUsed;
3732 obuflen = TmpBuf->BufSize;
3734 siz = iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
3737 if (errno == E2BIG) {
3739 IncreaseBuf(TmpBuf, 0, 0);
3744 else if (errno == EILSEQ){
3745 /* hm, invalid utf8 sequence... what to do now? */
3746 /* An invalid multibyte sequence has been encountered in the input */
3748 else if (errno == EINVAL) {
3749 /* An incomplete multibyte sequence has been encountered in the input. */
3752 FlushStrBuf(TmpBuf);
3755 TmpBuf->BufUsed = TmpBuf->BufSize - obuflen;
3756 TmpBuf->buf[TmpBuf->BufUsed] = '\0';
3758 /* little card game: wheres the red lady? */
3759 SwapBuffers(ConvertBuf, TmpBuf);
3760 FlushStrBuf(TmpBuf);
3767 * @ingroup StrBuf_DeEnCoder
3768 * @brief catches one RFC822 encoded segment, and decodes it.
3769 * @param Target buffer to fill with result
3770 * @param DecodeMe buffer with stuff to process
3771 * @param SegmentStart points to our current segment in DecodeMe
3772 * @param SegmentEnd Points to the end of our current segment in DecodeMe
3773 * @param ConvertBuf Workbuffer shared between several iterations. Random content; needs to be valid
3774 * @param ConvertBuf2 Workbuffer shared between several iterations. Random content; needs to be valid
3775 * @param FoundCharset Characterset to default decoding to; if we find another we will overwrite it.
3777 inline static void DecodeSegment(StrBuf *Target,
3778 const StrBuf *DecodeMe,
3779 const char *SegmentStart,
3780 const char *SegmentEnd,
3782 StrBuf *ConvertBuf2,
3783 StrBuf *FoundCharset)
3789 iconv_t ic = (iconv_t)(-1);
3793 /* Now we handle foreign character sets properly encoded
3794 * in RFC2047 format.
3796 StaticBuf.buf = (char*) SegmentStart; /*< it will just be read there... */
3797 StaticBuf.BufUsed = SegmentEnd - SegmentStart;
3798 StaticBuf.BufSize = DecodeMe->BufSize - (SegmentStart - DecodeMe->buf);
3799 extract_token(charset, SegmentStart, 1, '?', sizeof charset);
3800 if (FoundCharset != NULL) {
3801 FlushStrBuf(FoundCharset);
3802 StrBufAppendBufPlain(FoundCharset, charset, -1, 0);
3804 extract_token(encoding, SegmentStart, 2, '?', sizeof encoding);
3805 StrBufExtract_token(ConvertBuf, &StaticBuf, 3, '?');
3807 *encoding = toupper(*encoding);
3808 if (*encoding == 'B') { /**< base64 */
3809 if (ConvertBuf2->BufSize < ConvertBuf->BufUsed)
3810 IncreaseBuf(ConvertBuf2, 0, ConvertBuf->BufUsed);
3811 ConvertBuf2->BufUsed = CtdlDecodeBase64(ConvertBuf2->buf,
3813 ConvertBuf->BufUsed);
3815 else if (*encoding == 'Q') { /**< quoted-printable */
3819 while (pos < ConvertBuf->BufUsed)
3821 if (ConvertBuf->buf[pos] == '_')
3822 ConvertBuf->buf[pos] = ' ';
3826 if (ConvertBuf2->BufSize < ConvertBuf->BufUsed)
3827 IncreaseBuf(ConvertBuf2, 0, ConvertBuf->BufUsed);
3829 ConvertBuf2->BufUsed = CtdlDecodeQuotedPrintable(
3832 ConvertBuf->BufUsed);
3835 StrBufAppendBuf(ConvertBuf2, ConvertBuf, 0);
3838 ctdl_iconv_open("UTF-8", charset, &ic);
3839 if (ic != (iconv_t)(-1) ) {
3841 StrBufConvert(ConvertBuf2, ConvertBuf, &ic);
3842 StrBufAppendBuf(Target, ConvertBuf2, 0);
3847 StrBufAppendBufPlain(Target, HKEY("(unreadable)"), 0);
3853 * @ingroup StrBuf_DeEnCoder
3854 * @brief Handle subjects with RFC2047 encoding such as: [deprecated old syntax!]
3855 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
3856 * @param Target where to put the decoded string to
3857 * @param DecodeMe buffer with encoded string
3858 * @param DefaultCharset if we don't find one, which should we use?
3859 * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string,
3860 * put it here for later use where no string might be known.
3862 void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset)
3865 StrBuf *ConvertBuf2;
3866 ConvertBuf = NewStrBufPlain(NULL, StrLength(DecodeMe));
3867 ConvertBuf2 = NewStrBufPlain(NULL, StrLength(DecodeMe));
3869 StrBuf_RFC822_2_Utf8(Target,
3875 FreeStrBuf(&ConvertBuf);
3876 FreeStrBuf(&ConvertBuf2);
3880 * @ingroup StrBuf_DeEnCoder
3881 * @brief Handle subjects with RFC2047 encoding such as:
3882 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
3883 * @param Target where to put the decoded string to
3884 * @param DecodeMe buffer with encoded string
3885 * @param DefaultCharset if we don't find one, which should we use?
3886 * @param FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string,
3887 * put it here for later use where no string might be known.
3888 * @param ConvertBuf workbuffer. feed in, you shouldn't care about its content.
3889 * @param ConvertBuf2 workbuffer. feed in, you shouldn't care about its content.
3891 void StrBuf_RFC822_2_Utf8(StrBuf *Target,
3892 const StrBuf *DecodeMe,
3893 const StrBuf* DefaultCharset,
3894 StrBuf *FoundCharset,
3896 StrBuf *ConvertBuf2)
3898 StrBuf *DecodedInvalidBuf = NULL;
3899 const StrBuf *DecodeMee = DecodeMe;
3900 const char *start, *end, *next, *nextend, *ptr = NULL;
3902 iconv_t ic = (iconv_t)(-1) ;
3907 int illegal_non_rfc2047_encoding = 0;
3910 if (DecodeMe == NULL)
3912 /* Sometimes, badly formed messages contain strings which were simply
3913 * written out directly in some foreign character set instead of
3914 * using RFC2047 encoding. This is illegal but we will attempt to
3915 * handle it anyway by converting from a user-specified default
3916 * charset to UTF-8 if we see any nonprintable characters.
3919 for (i=0; i<DecodeMe->BufUsed; ++i) {
3920 if ((DecodeMe->buf[i] < 32) || (DecodeMe->buf[i] > 126)) {
3921 illegal_non_rfc2047_encoding = 1;
3926 if ((illegal_non_rfc2047_encoding) &&
3927 (strcasecmp(ChrPtr(DefaultCharset), "UTF-8")) &&
3928 (strcasecmp(ChrPtr(DefaultCharset), "us-ascii")) )
3931 ctdl_iconv_open("UTF-8", ChrPtr(DefaultCharset), &ic);
3932 if (ic != (iconv_t)(-1) ) {
3933 DecodedInvalidBuf = NewStrBufDup(DecodeMe);
3934 StrBufConvert(DecodedInvalidBuf, ConvertBuf, &ic);///TODO: don't void const?
3935 DecodeMee = DecodedInvalidBuf;
3941 /* pre evaluate the first pair */
3943 start = strstr(DecodeMee->buf, "=?");
3944 eptr = DecodeMee->buf + DecodeMee->BufUsed;
3946 end = FindNextEnd (DecodeMee, start + 2);
3948 StrBufAppendBuf(Target, DecodeMee, 0);
3949 FreeStrBuf(&DecodedInvalidBuf);
3954 if (start != DecodeMee->buf) {
3957 nFront = start - DecodeMee->buf;
3958 StrBufAppendBufPlain(Target, DecodeMee->buf, nFront, 0);
3961 * Since spammers will go to all sorts of absurd lengths to get their
3962 * messages through, there are LOTS of corrupt headers out there.
3963 * So, prevent a really badly formed RFC2047 header from throwing
3964 * this function into an infinite loop.
3966 while ((start != NULL) &&
3973 DecodeSegment(Target,
3981 next = strstr(end, "=?");
3983 if ((next != NULL) &&
3985 nextend = FindNextEnd(DecodeMee, next);
3986 if (nextend == NULL)
3989 /* did we find two partitions */
3990 if ((next != NULL) &&
3994 while ((ptr < next) &&
4001 * did we find a gab just filled with blanks?
4002 * if not, copy its stuff over.
4006 StrBufAppendBufPlain(Target,
4012 /* our next-pair is our new first pair now. */
4018 nextend = DecodeMee->buf + DecodeMee->BufUsed;
4019 if ((end != NULL) && (end < nextend)) {
4021 while ( (ptr < nextend) &&
4028 StrBufAppendBufPlain(Target, end, nextend - end, 0);
4030 FreeStrBuf(&DecodedInvalidBuf);
4033 /*******************************************************************************
4034 * Manipulating UTF-8 Strings *
4035 *******************************************************************************/
4039 * @brief evaluate the length of an utf8 special character sequence
4040 * @param Char the character to examine
4041 * @returns width of utf8 chars in bytes; if the sequence is broken 0 is returned; 1 if its simply ASCII.
4043 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
4046 unsigned char test = (1<<7);
4048 if ((*CharS & 0xC0) != 0xC0)
4052 ((test & ((unsigned char)*CharS)) != 0))
4057 if ((n > 6) || ((CharE - CharS) < n))
4064 * @brief detect whether this char starts an utf-8 encoded char
4065 * @param Char character to inspect
4066 * @returns yes or no
4068 static inline int Ctdl_IsUtf8SequenceStart(const char Char)
4070 /** 11??.???? indicates an UTF8 Sequence. */
4071 return ((Char & 0xC0) == 0xC0);
4076 * @brief measure the number of glyphs in an UTF8 string...
4077 * @param Buf string to measure
4078 * @returns the number of glyphs in Buf
4080 long StrBuf_Utf8StrLen(StrBuf *Buf)
4086 if ((Buf == NULL) || (Buf->BufUsed == 0))
4089 eptr = Buf->buf + Buf->BufUsed;
4090 while ((aptr < eptr) && (*aptr != '\0')) {
4091 if (Ctdl_IsUtf8SequenceStart(*aptr)){
4092 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
4093 while ((aptr < eptr) && (*aptr++ != '\0')&& (m-- > 0) );
4106 * @brief cuts a string after maxlen glyphs
4107 * @param Buf string to cut to maxlen glyphs
4108 * @param maxlen how long may the string become?
4109 * @returns current length of the string
4111 long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
4117 eptr = Buf->buf + Buf->BufUsed;
4118 while ((aptr < eptr) && (*aptr != '\0')) {
4119 if (Ctdl_IsUtf8SequenceStart(*aptr)){
4120 m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
4121 while ((*aptr++ != '\0') && (m-- > 0));
4130 Buf->BufUsed = aptr - Buf->buf;
4131 return Buf->BufUsed;
4134 return Buf->BufUsed;
4142 /*******************************************************************************
4144 *******************************************************************************/
4147 * @ingroup StrBuf_DeEnCoder
4148 * @brief uses the same calling syntax as compress2(), but it
4149 * creates a stream compatible with HTTP "Content-encoding: gzip"
4150 * @param dest compressed buffer
4151 * @param destLen length of the compresed data
4152 * @param source source to encode
4153 * @param sourceLen length of source to encode
4154 * @param level compression level
4157 int ZEXPORT compress_gzip(Bytef * dest,
4159 const Bytef * source,
4163 /* write gzip header */
4164 snprintf((char *) dest, *destLen,
4165 "%c%c%c%c%c%c%c%c%c%c",
4166 gz_magic[0], gz_magic[1], Z_DEFLATED,
4167 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
4170 /* normal deflate */
4173 stream.next_in = (Bytef *) source;
4174 stream.avail_in = (uInt) sourceLen;
4175 stream.next_out = dest + 10L; // after header
4176 stream.avail_out = (uInt) * destLen;
4177 if ((uLong) stream.avail_out != *destLen)
4180 stream.zalloc = (alloc_func) 0;
4181 stream.zfree = (free_func) 0;
4182 stream.opaque = (voidpf) 0;
4184 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
4185 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
4189 err = deflate(&stream, Z_FINISH);
4190 if (err != Z_STREAM_END) {
4191 deflateEnd(&stream);
4192 return err == Z_OK ? Z_BUF_ERROR : err;
4194 *destLen = stream.total_out + 10L;
4196 /* write CRC and Length */
4197 uLong crc = crc32(0L, source, sourceLen);
4199 for (n = 0; n < 4; ++n, ++*destLen) {
4200 dest[*destLen] = (int) (crc & 0xff);
4203 uLong len = stream.total_in;
4204 for (n = 0; n < 4; ++n, ++*destLen) {
4205 dest[*destLen] = (int) (len & 0xff);
4208 err = deflateEnd(&stream);
4215 * @ingroup StrBuf_DeEnCoder
4216 * @brief compress the buffer with gzip
4217 * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
4218 * @param Buf buffer whose content is to be gzipped
4220 int CompressBuffer(StrBuf *Buf)
4223 char *compressed_data = NULL;
4224 size_t compressed_len, bufsize;
4227 bufsize = compressed_len = Buf->BufUsed + (Buf->BufUsed / 100) + 100;
4228 compressed_data = malloc(compressed_len);
4230 if (compressed_data == NULL)
4232 /* Flush some space after the used payload so valgrind shuts up... */
4233 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
4234 Buf->buf[Buf->BufUsed + i++] = '\0';
4235 if (compress_gzip((Bytef *) compressed_data,
4238 (uLongf) Buf->BufUsed, Z_BEST_SPEED) == Z_OK) {
4241 Buf->buf = compressed_data;
4242 Buf->BufUsed = compressed_len;
4243 Buf->BufSize = bufsize;
4244 /* Flush some space after the used payload so valgrind shuts up... */
4246 while ((i < 10) && (Buf->BufUsed + i < Buf->BufSize))
4247 Buf->buf[Buf->BufUsed + i++] = '\0';
4250 free(compressed_data);
4252 #endif /* HAVE_ZLIB */
4256 /*******************************************************************************
4257 * File I/O; Callbacks to libevent *
4258 *******************************************************************************/
4260 long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
4265 if ((FB == NULL) || (FB->Buf == NULL))
4269 * check whether the read pointer is somewhere in a range
4270 * where a cut left is inexpensive
4273 if (FB->ReadWritePointer != NULL)
4277 already_read = FB->ReadWritePointer - FB->Buf->buf;
4278 bufremain = FB->Buf->BufSize - FB->Buf->BufUsed - 1;
4280 if (already_read != 0) {
4283 unread = FB->Buf->BufUsed - already_read;
4285 /* else nothing to compact... */
4287 FB->ReadWritePointer = FB->Buf->buf;
4288 bufremain = FB->Buf->BufSize;
4290 else if ((unread < 64) ||
4291 (bufremain < already_read))
4294 * if its just a tiny bit remaining, or we run out of space...
4297 FB->Buf->BufUsed = unread;
4298 if (unread < already_read)
4299 memcpy(FB->Buf->buf, FB->ReadWritePointer, unread);
4301 memmove(FB->Buf->buf, FB->ReadWritePointer, unread);
4302 FB->ReadWritePointer = FB->Buf->buf;
4303 bufremain = FB->Buf->BufSize - unread - 1;
4305 else if (bufremain < (FB->Buf->BufSize / 10))
4307 /* get a bigger buffer */
4309 IncreaseBuf(FB->Buf, 0, FB->Buf->BufUsed + 1);
4311 FB->ReadWritePointer = FB->Buf->buf + unread;
4313 bufremain = FB->Buf->BufSize - unread - 1;
4314 /*TODO: special increase function that won't copy the already read! */
4317 else if (bufremain < 10) {
4318 IncreaseBuf(FB->Buf, 1, FB->Buf->BufUsed + 10);
4320 FB->ReadWritePointer = FB->Buf->buf;
4322 bufremain = FB->Buf->BufSize - FB->Buf->BufUsed - 1;
4327 FB->ReadWritePointer = FB->Buf->buf;
4328 bufremain = FB->Buf->BufSize - 1;
4331 n = read(fd, FB->Buf->buf + FB->Buf->BufUsed, bufremain);
4334 FB->Buf->BufUsed += n;
4335 FB->Buf->buf[FB->Buf->BufUsed] = '\0';
4340 int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB)
4345 if ((FB == NULL) || (FB->Buf == NULL))
4348 if (FB->ReadWritePointer != NULL)
4350 WriteRemain = FB->Buf->BufUsed -
4351 (FB->ReadWritePointer -
4355 FB->ReadWritePointer = FB->Buf->buf;
4356 WriteRemain = FB->Buf->BufUsed;
4359 n = write(fd, FB->ReadWritePointer, WriteRemain);
4361 FB->ReadWritePointer += n;
4363 if (FB->ReadWritePointer ==
4364 FB->Buf->buf + FB->Buf->BufUsed)
4366 FlushStrBuf(FB->Buf);
4367 FB->ReadWritePointer = NULL;
4370 // check whether we've got something to write
4371 // get the maximum chunk plus the pointer we can send
4372 // write whats there
4373 // if not all was sent, remember the send pointer for the next time
4374 return FB->ReadWritePointer - FB->Buf->buf + FB->Buf->BufUsed;
4380 * @ingroup StrBuf_IO
4381 * @brief extract a "next line" from Buf; Ptr to persist across several iterations
4382 * @param LineBuf your line will be copied here.
4383 * @param FB BLOB with lines of text...
4384 * @param Ptr moved arround to keep the next-line across several iterations
4385 * has to be &NULL on start; will be &NotNULL on end of buffer
4386 * @returns size of copied buffer
4388 eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB)
4390 const char *aptr, *ptr, *eptr;
4393 if ((FB == NULL) || (LineBuf == NULL) || (LineBuf->buf == NULL))
4397 if ((FB->Buf == NULL) || (FB->ReadWritePointer == StrBufNOTNULL)) {
4398 FB->ReadWritePointer = StrBufNOTNULL;
4402 FlushStrBuf(LineBuf);
4403 if (FB->ReadWritePointer == NULL)
4404 ptr = aptr = FB->Buf->buf;
4406 ptr = aptr = FB->ReadWritePointer;
4408 optr = LineBuf->buf;
4409 eptr = FB->Buf->buf + FB->Buf->BufUsed;
4410 xptr = LineBuf->buf + LineBuf->BufSize - 1;
4412 while ((ptr <= eptr) &&
4419 LineBuf->BufUsed = optr - LineBuf->buf;
4420 IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
4421 optr = LineBuf->buf + LineBuf->BufUsed;
4422 xptr = LineBuf->buf + LineBuf->BufSize - 1;
4427 if (optr > LineBuf->buf)
4429 if ((*(ptr - 1) != '\r') && (*(ptr - 1) != '\n')) {
4430 LineBuf->BufUsed = optr - LineBuf->buf;
4432 if ((FB->ReadWritePointer != NULL) &&
4433 (FB->ReadWritePointer != FB->Buf->buf))
4435 /* Ok, the client application read all the data
4436 it was interested in so far. Since there is more to read,
4437 we now shrink the buffer, and move the rest over.
4439 StrBufCutLeft(FB->Buf,
4440 FB->ReadWritePointer - FB->Buf->buf);
4441 FB->ReadWritePointer = FB->Buf->buf;
4443 return eMustReadMore;
4446 LineBuf->BufUsed = optr - LineBuf->buf;
4448 if ((ptr <= eptr) && (*ptr == '\r'))
4450 if ((ptr <= eptr) && (*ptr == '\n'))
4454 FB->ReadWritePointer = ptr;
4457 FlushStrBuf(FB->Buf);
4458 FB->ReadWritePointer = NULL;
4461 return eReadSuccess;
4465 * @ingroup StrBuf_CHUNKED_IO
4466 * @brief check whether the chunk-buffer has more data waiting or not.
4467 * @param FB Chunk-Buffer to inspect
4469 eReadState StrBufCheckBuffer(IOBuffer *FB)
4473 if (FB->Buf->BufUsed == 0)
4474 return eReadSuccess;
4475 if (FB->ReadWritePointer == NULL)
4476 return eBufferNotEmpty;
4477 if (FB->Buf->buf + FB->Buf->BufUsed > FB->ReadWritePointer)
4478 return eBufferNotEmpty;
4479 return eReadSuccess;
4482 long IOBufferStrLength(IOBuffer *FB)
4484 if ((FB == NULL) || (FB->Buf == NULL))
4486 if (FB->ReadWritePointer == NULL)
4487 return StrLength(FB->Buf);
4489 return StrLength(FB->Buf) - (FB->ReadWritePointer - FB->Buf->buf);
4492 inline static void FDIOBufferFlush(FDIOBuffer *FDB)
4494 memset(FDB, 0, sizeof(FDIOBuffer));
4496 FDB->SplicePipe[0] = -1;
4497 FDB->SplicePipe[1] = -1;
4500 void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
4502 FDIOBufferFlush(FDB);
4504 FDB->TotalSendSize = TotalSendSize;
4505 if (TotalSendSize > 0)
4506 FDB->ChunkSize = TotalSendSize;
4509 TotalSendSize = SIZ * 10;
4510 FDB->ChunkSize = TotalSendSize;
4516 pipe(FDB->SplicePipe);
4519 FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize+ 1);
4524 void FDIOBufferDelete(FDIOBuffer *FDB)
4529 if (FDB->SplicePipe[0] > 0)
4530 close(FDB->SplicePipe[0]);
4531 if (FDB->SplicePipe[1] > 0)
4532 close(FDB->SplicePipe[1]);
4536 FreeStrBuf(&FDB->ChunkBuffer);
4538 if (FDB->OtherFD > 0)
4539 close(FDB->OtherFD);
4540 FDIOBufferFlush(FDB);
4543 int FileSendChunked(FDIOBuffer *FDB, const char **Err)
4545 ssize_t sent, pipesize;
4547 if (FDB->TotalSendSize > 0)
4552 if (FDB->PipeSize == 0)
4554 pipesize = splice(FDB->OtherFD,
4555 &FDB->TotalSentAlready,
4558 FDB->ChunkSendRemain,
4563 *Err = strerror(errno);
4566 FDB->PipeSize = pipesize;
4568 sent = splice(FDB->SplicePipe[0],
4573 SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
4576 *Err = strerror(errno);
4579 FDB->PipeSize -= sent;
4580 FDB->ChunkSendRemain -= sent;
4589 pRead = FDB->ChunkBuffer->buf;
4590 while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0))
4592 nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed);
4594 FDB->ChunkBuffer->BufUsed += nRead;
4595 FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0';
4597 else if (nRead == 0) {}
4601 nRead = write(FDB->IOB->fd,
4602 FDB->ChunkBuffer->buf + FDB->TotalSentAlready,
4603 FDB->ChunkBuffer->BufUsed - FDB->TotalSentAlready);
4606 FDB->TotalSentAlready += nRead;
4607 FDB->ChunkSendRemain -= nRead;
4608 return FDB->ChunkSendRemain;
4620 if (FDB->PipeSize == 0)
4622 pipesize = splice(FDB->OtherFD,
4623 &FDB->TotalSentAlready,
4631 *Err = strerror(errno);
4634 FDB->PipeSize = pipesize;
4638 sent = splice(FDB->SplicePipe[0],
4643 SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
4646 *Err = strerror(errno);
4649 FDB->PipeSize -= sent;
4650 FDB->ChunkSendRemain -= sent;
4659 pRead = FDB->ChunkBuffer->buf;
4660 while ((FDB->ChunkSendRemain == 0) &&
4661 (FDB->ChunkBuffer->BufUsed < FDB->ChunkBuffer->BufSize) &&
4664 FDB->TotalSentAlready = 0;
4665 nRead = read(FDB->OtherFD, pRead, FDB->ChunkBuffer->BufSize - FDB->ChunkBuffer->BufUsed);
4667 FDB->ChunkBuffer->BufUsed += nRead;
4668 FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0';
4669 FDB->ChunkSendRemain += nRead;
4671 else if (nRead == 0)
4677 *Err = strerror(errno);
4682 nRead = write(FDB->IOB->fd,
4683 FDB->ChunkBuffer->buf + FDB->TotalSentAlready,
4684 FDB->ChunkBuffer->BufUsed - FDB->TotalSentAlready);
4687 FDB->TotalSentAlready += nRead;
4688 FDB->ChunkSendRemain -= nRead;
4689 if (FDB->ChunkSendRemain == 0)
4691 FDB->ChunkBuffer->BufUsed = 0;
4692 FDB->TotalSentAlready = 0;
4694 return FDB->ChunkSendRemain;
4703 int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
4705 ssize_t sent, pipesize;
4710 if (FDB->PipeSize == 0)
4712 pipesize = splice(FDB->IOB->fd,
4716 FDB->ChunkSendRemain,
4717 SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
4721 *Err = strerror(errno);
4724 FDB->PipeSize = pipesize;
4727 sent = splice(FDB->SplicePipe[0],
4730 &FDB->TotalSentAlready,
4732 SPLICE_F_MORE | SPLICE_F_MOVE);
4736 *Err = strerror(errno);
4739 FDB->PipeSize -= sent;
4740 FDB->ChunkSendRemain -= sent;
4746 sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
4751 FDB->ChunkBuffer->BufUsed = sent;
4753 while (nWritten < FDB->ChunkBuffer->BufUsed) {
4754 rc = write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
4756 *Err = strerror(errno);
4762 FDB->ChunkBuffer->BufUsed = 0;
4763 FDB->TotalSentAlready += sent;
4764 FDB->ChunkSendRemain -= sent;
4765 return FDB->ChunkSendRemain;
4767 else if (sent < 0) {
4768 *Err = strerror(errno);
4775 int FileMoveChunked(FDIOBuffer *FDB, const char **Err)
4777 ssize_t sent, pipesize;
4782 if (FDB->PipeSize == 0)
4784 pipesize = splice(FDB->IOB->fd,
4785 &FDB->TotalReadAlready,
4788 FDB->ChunkSendRemain,
4789 SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
4793 *Err = strerror(errno);
4796 FDB->PipeSize = pipesize;
4799 sent = splice(FDB->SplicePipe[0],
4802 &FDB->TotalSentAlready,
4804 SPLICE_F_MORE | SPLICE_F_MOVE);
4808 *Err = strerror(errno);
4811 FDB->PipeSize -= sent;
4812 FDB->ChunkSendRemain -= sent;
4818 sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
4823 FDB->ChunkBuffer->BufUsed = sent;
4825 while (nWritten < FDB->ChunkBuffer->BufUsed) {
4826 rc = write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
4828 *Err = strerror(errno);
4834 FDB->ChunkBuffer->BufUsed = 0;
4835 FDB->TotalSentAlready += sent;
4836 FDB->ChunkSendRemain -= sent;
4837 return FDB->ChunkSendRemain;
4839 else if (sent < 0) {
4840 *Err = strerror(errno);
4847 eReadState WriteIOBAlreadyRead(FDIOBuffer *FDB, const char **Error)
4853 int nSuccessLess = 0;
4857 fdflags = fcntl(FDB->OtherFD, F_GETFL);
4858 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
4860 while ((FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf < FDB->IOB->Buf->BufUsed) &&
4861 (FDB->ChunkSendRemain > 0))
4864 tv.tv_sec = 1; /* selectresolution; */
4868 FD_SET(FDB->OtherFD, &rfds);
4869 if (select(FDB->OtherFD + 1, NULL, &rfds, NULL, &tv) == -1) {
4870 *Error = strerror(errno);
4874 if (IsNonBlock && ! FD_ISSET(FDB->OtherFD, &rfds)) {
4879 should_write = FDB->IOB->Buf->BufUsed -
4880 (FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf);
4881 if (should_write > FDB->ChunkSendRemain)
4882 should_write = FDB->ChunkSendRemain;
4884 rlen = write(FDB->OtherFD,
4885 FDB->IOB->ReadWritePointer,
4888 *Error = strerror(errno);
4892 FDB->TotalSentAlready += rlen;
4893 FDB->IOB->ReadWritePointer += rlen;
4894 FDB->ChunkSendRemain -= rlen;
4896 if (FDB->IOB->ReadWritePointer >= FDB->IOB->Buf->buf + FDB->IOB->Buf->BufUsed)
4898 FlushStrBuf(FDB->IOB->Buf);
4899 FDB->IOB->ReadWritePointer = NULL;
4902 if (FDB->ChunkSendRemain == 0)
4903 return eReadSuccess;
4905 return eMustReadMore;
4908 /*******************************************************************************
4909 * File I/O; Prefer buffered read since its faster! *
4910 *******************************************************************************/
4913 * @ingroup StrBuf_IO
4914 * @brief Read a line from socket
4915 * flushes and closes the FD on error
4916 * @param buf the buffer to get the input to
4917 * @param fd pointer to the filedescriptor to read
4918 * @param append Append to an existing string or replace?
4919 * @param Error strerror() on error
4920 * @returns numbers of chars read
4922 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
4924 int len, rlen, slen;
4926 if ((buf == NULL) || (buf->buf == NULL)) {
4927 *Error = strerror(EINVAL);
4934 slen = len = buf->BufUsed;
4936 rlen = read(*fd, &buf->buf[len], 1);
4938 *Error = strerror(errno);
4945 if (buf->buf[len] == '\n')
4947 if (buf->buf[len] != '\r')
4949 if (len + 2 >= buf->BufSize) {
4951 buf->buf[len+1] = '\0';
4952 IncreaseBuf(buf, 1, -1);
4956 buf->buf[len] = '\0';
4961 * @ingroup StrBuf_BufferedIO
4962 * @brief Read a line from socket
4963 * flushes and closes the FD on error
4964 * @param Line the line to read from the fd / I/O Buffer
4965 * @param buf the buffer to get the input to
4966 * @param fd pointer to the filedescriptor to read
4967 * @param timeout number of successless selects until we bail out
4968 * @param selectresolution how long to wait on each select
4969 * @param Error strerror() on error
4970 * @returns numbers of chars read
4972 int StrBufTCP_read_buffered_line(StrBuf *Line,
4976 int selectresolution,
4980 int nSuccessLess = 0;
4987 if (buf->BufUsed > 0) {
4988 pch = strchr(buf->buf, '\n');
4991 len = pch - buf->buf;
4992 if (len > 0 && (*(pch - 1) == '\r') )
4994 StrBufSub(Line, buf, 0, len - rlen);
4995 StrBufCutLeft(buf, len + 1);
5000 if (buf->BufSize - buf->BufUsed < 10)
5001 IncreaseBuf(buf, 1, -1);
5003 fdflags = fcntl(*fd, F_GETFL);
5004 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
5006 while ((nSuccessLess < timeout) && (pch == NULL)) {
5008 tv.tv_sec = selectresolution;
5013 if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
5014 *Error = strerror(errno);
5020 if (IsNonBlock && ! FD_ISSET(*fd, &rfds)) {
5025 &buf->buf[buf->BufUsed],
5026 buf->BufSize - buf->BufUsed - 1);
5028 *Error = strerror(errno);
5033 else if (rlen > 0) {
5035 buf->BufUsed += rlen;
5036 buf->buf[buf->BufUsed] = '\0';
5037 pch = strchr(buf->buf, '\n');
5038 if ((pch == NULL) &&
5039 (buf->BufUsed + 10 > buf->BufSize) &&
5040 (IncreaseBuf(buf, 1, -1) == -1))
5048 len = pch - buf->buf;
5049 if (len > 0 && (*(pch - 1) == '\r') )
5051 StrBufSub(Line, buf, 0, len - rlen);
5052 StrBufCutLeft(buf, len + 1);
5059 static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
5060 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
5061 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
5063 * @ingroup StrBuf_BufferedIO
5064 * @brief Read a line from socket
5065 * flushes and closes the FD on error
5066 * @param Line where to append our Line read from the fd / I/O Buffer;
5067 * @param IOBuf the buffer to get the input to; lifetime pair to FD
5068 * @param Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
5069 * @param fd pointer to the filedescriptor to read
5070 * @param timeout number of successless selects until we bail out
5071 * @param selectresolution how long to wait on each select
5072 * @param Error strerror() on error
5073 * @returns numbers of chars read or -1 in case of error. "\n" will become 0
5075 int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
5080 int selectresolution,
5083 const char *pche = NULL;
5084 const char *pos = NULL;
5086 int len, rlen, retlen;
5087 int nSuccessLess = 0;
5089 const char *pch = NULL;
5095 if ((Line == NULL) ||
5102 *Error = ErrRBLF_PreConditionFailed;
5107 if ((IOBuf->BufUsed > 0) &&
5109 (pos < IOBuf->buf + IOBuf->BufUsed))
5113 pche = IOBuf->buf + IOBuf->BufUsed;
5117 while ((pch < pche) && (*pch != '\n'))
5119 if (Line->BufUsed + 10 > Line->BufSize)
5122 apos = pcht - Line->buf;
5124 IncreaseBuf(Line, 1, -1);
5125 pcht = Line->buf + apos;
5133 if (len > 0 && (*(pch - 1) == '\r') )
5142 if ((pch >= pche) || (*pch == '\0'))
5150 if ((pch != NULL) &&
5153 if (pch + 1 >= pche) {
5166 /* If we come here, Pos is Unset since we read everything into Line, and now go for more. */
5168 if (IOBuf->BufSize - IOBuf->BufUsed < 10)
5169 IncreaseBuf(IOBuf, 1, -1);
5171 fdflags = fcntl(*fd, F_GETFL);
5172 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
5175 while ((nSuccessLess < timeout) &&
5185 if (select((*fd) + 1, &rfds, NULL, NULL, &tv) == -1) {
5186 *Error = strerror(errno);
5190 *Error = ErrRBLF_SelectFailed;
5193 if (! FD_ISSET(*fd, &rfds) != 0) {
5199 &IOBuf->buf[IOBuf->BufUsed],
5200 IOBuf->BufSize - IOBuf->BufUsed - 1);
5202 *Error = strerror(errno);
5207 else if (rlen > 0) {
5209 pLF = IOBuf->buf + IOBuf->BufUsed;
5210 IOBuf->BufUsed += rlen;
5211 IOBuf->buf[IOBuf->BufUsed] = '\0';
5213 pche = IOBuf->buf + IOBuf->BufUsed;
5215 while ((pLF < pche) && (*pLF != '\n'))
5217 if ((pLF >= pche) || (*pLF == '\0'))
5220 if (IOBuf->BufUsed + 10 > IOBuf->BufSize)
5224 if (pLF != NULL) apos = pLF - IOBuf->buf;
5225 IncreaseBuf(IOBuf, 1, -1);
5226 if (pLF != NULL) pLF = IOBuf->buf + apos;
5240 if (len > 0 && (*(pLF - 1) == '\r') )
5242 StrBufAppendBufPlain(Line, ChrPtr(IOBuf), len, 0);
5243 if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed)
5249 return retlen + len;
5251 *Error = ErrRBLF_NotEnoughSentFromServer;
5256 static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong arguments or invalid Filedescriptor";
5258 * @ingroup StrBuf_IO
5259 * @brief Input binary data from socket
5260 * flushes and closes the FD on error
5261 * @param Buf the buffer to get the input to
5262 * @param fd pointer to the filedescriptor to read
5263 * @param append Append to an existing string or replace?
5264 * @param nBytes the maximal number of bytes to read
5265 * @param Error strerror() on error
5266 * @returns numbers of chars read
5268 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
5279 if ((Buf == NULL) || (Buf->buf == NULL) || (*fd == -1))
5281 *Error = ErrRBLF_BLOBPreConditionFailed;
5286 if (Buf->BufUsed + nBytes >= Buf->BufSize)
5287 IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
5289 ptr = Buf->buf + Buf->BufUsed;
5291 fdflags = fcntl(*fd, F_GETFL);
5292 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
5294 while ((nRead < nBytes) &&
5304 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
5305 *Error = strerror(errno);
5309 *Error = ErrRBLF_SelectFailed;
5312 if (! FD_ISSET(*fd, &rfds) != 0) {
5318 if ((rlen = read(*fd,
5320 nBytes - nRead)) == -1) {
5323 *Error = strerror(errno);
5328 Buf->BufUsed += rlen;
5330 Buf->buf[Buf->BufUsed] = '\0';
5334 const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
5335 const char *ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting.";
5337 * @ingroup StrBuf_BufferedIO
5338 * @brief Input binary data from socket
5339 * flushes and closes the FD on error
5340 * @param Blob put binary thing here
5341 * @param IOBuf the buffer to get the input to
5342 * @param Pos offset inside of IOBuf
5343 * @param fd pointer to the filedescriptor to read
5344 * @param append Append to an existing string or replace?
5345 * @param nBytes the maximal number of bytes to read
5346 * @param check whether we should search for '000\n' terminators in case of timeouts
5347 * @param Error strerror() on error
5348 * @returns numbers of chars read
5350 int StrBufReadBLOBBuffered(StrBuf *Blob,
5363 int nAlreadyRead = 0;
5368 int nSuccessLess = 0;
5371 if ((Blob == NULL) ||
5378 *Error = ErrRBB_BLOBFPreConditionFailed;
5384 if (Blob->BufUsed + nBytes >= Blob->BufSize)
5385 IncreaseBuf(Blob, append, Blob->BufUsed + nBytes);
5390 rlen = pos - IOBuf->buf;
5391 rlen = IOBuf->BufUsed - rlen;
5394 if ((IOBuf->BufUsed > 0) &&
5396 (pos < IOBuf->buf + IOBuf->BufUsed))
5398 if (rlen < nBytes) {
5399 memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
5400 Blob->BufUsed += rlen;
5401 Blob->buf[Blob->BufUsed] = '\0';
5402 nAlreadyRead = nRead = rlen;
5405 if (rlen >= nBytes) {
5406 memcpy(Blob->buf + Blob->BufUsed, pos, nBytes);
5407 Blob->BufUsed += nBytes;
5408 Blob->buf[Blob->BufUsed] = '\0';
5409 if (rlen == nBytes) {
5421 if (IOBuf->BufSize < nBytes - nRead)
5422 IncreaseBuf(IOBuf, 0, nBytes - nRead);
5425 fdflags = fcntl(*fd, F_GETFL);
5426 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
5434 while ((nSuccessLess < MaxTries) &&
5444 if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
5445 *Error = strerror(errno);
5449 *Error = ErrRBLF_SelectFailed;
5452 if (! FD_ISSET(*fd, &rfds) != 0) {
5459 IOBuf->BufSize - (ptr - IOBuf->buf));
5463 *Error = strerror(errno);
5466 else if (rlen == 0){
5467 if ((check == NNN_TERM) &&
5469 (strncmp(IOBuf->buf + IOBuf->BufUsed - 5, "\n000\n", 5) == 0))
5471 StrBufPlain(Blob, HKEY("\n000\n"));
5472 StrBufCutRight(Blob, 5);
5473 return Blob->BufUsed;
5475 else if (!IsNonBlock)
5477 else if (nSuccessLess > MaxTries) {
5479 *Error = ErrRBB_too_many_selects;
5483 else if (rlen > 0) {
5487 IOBuf->BufUsed += rlen;
5490 if (nSuccessLess >= MaxTries) {
5492 *Error = ErrRBB_too_many_selects;
5496 if (nRead > nBytes) {
5497 *Pos = IOBuf->buf + nBytes;
5499 Blob->buf[Blob->BufUsed] = '\0';
5500 StrBufAppendBufPlain(Blob, IOBuf->buf, nBytes, 0);
5504 return nRead + nAlreadyRead;
5508 * @ingroup StrBuf_IO
5509 * @brief extract a "next line" from Buf; Ptr to persist across several iterations
5510 * @param LineBuf your line will be copied here.
5511 * @param Buf BLOB with lines of text...
5512 * @param Ptr moved arround to keep the next-line across several iterations
5513 * has to be &NULL on start; will be &NotNULL on end of buffer
5514 * @returns size of remaining buffer
5516 int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
5518 const char *aptr, *ptr, *eptr;
5521 if ((Buf == NULL) ||
5522 (*Ptr == StrBufNOTNULL) ||
5524 (LineBuf->buf == NULL))
5526 *Ptr = StrBufNOTNULL;
5530 FlushStrBuf(LineBuf);
5532 ptr = aptr = Buf->buf;
5536 optr = LineBuf->buf;
5537 eptr = Buf->buf + Buf->BufUsed;
5538 xptr = LineBuf->buf + LineBuf->BufSize - 1;
5540 while ((ptr <= eptr) &&
5547 LineBuf->BufUsed = optr - LineBuf->buf;
5548 IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
5549 optr = LineBuf->buf + LineBuf->BufUsed;
5550 xptr = LineBuf->buf + LineBuf->BufSize - 1;
5554 if ((ptr >= eptr) && (optr > LineBuf->buf))
5556 LineBuf->BufUsed = optr - LineBuf->buf;
5558 if ((ptr <= eptr) && (*ptr == '\r'))
5560 if ((ptr <= eptr) && (*ptr == '\n'))
5567 *Ptr = StrBufNOTNULL;
5570 return Buf->BufUsed - (ptr - Buf->buf);
5575 * @ingroup StrBuf_IO
5576 * @brief removes double slashes from pathnames
5577 * @param Dir directory string to filter
5578 * @param RemoveTrailingSlash allows / disallows trailing slashes
5580 void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
5586 while (!IsEmptyStr(a)) {
5598 if ((RemoveTrailingSlash) &&
5604 Dir->BufUsed = b - Dir->buf;