Back to index

libcitadel  8.12
Classes | Defines | Functions | Variables
stringbuf.c File Reference
#include "sysdep.h"
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdarg.h>
#include "libcitadel.h"

Go to the source code of this file.

Classes

struct  StrBuf
 Private Structure for the Stringbuffer. More...

Defines

#define _GNU_SOURCE
#define SHOW_ME_VAPPEND_PRINTF
#define dbg_FreeStrBuf(a, b)
#define dbg_IncreaseBuf(a)
#define dbg_Init(a)

Functions

static int Ctdl_GetUtf8SequenceLength (const char *CharS, const char *CharE)
 evaluate the length of an utf8 special character sequence
static int Ctdl_IsUtf8SequenceStart (const char Char)
 detect whether this char starts an utf-8 encoded char
static void SwapBuffers (StrBuf *A, StrBuf *B)
 swaps the contents of two StrBufs this is to be used to have cheap switched between a work-buffer and a target buffer
const char * ChrPtr (const StrBuf *Str)
 Cast operator to Plain String.
int StrLength (const StrBuf *Str)
 since we know strlen()'s result, provide it here.
static int IncreaseBuf (StrBuf *Buf, int KeepOriginal, int DestSize)
 local utility function to resize the buffer
void ReAdjustEmptyBuf (StrBuf *Buf, long ThreshHold, long NewSize)
 shrink / increase an EMPTY buffer to NewSize.
long StrBufShrinkToFit (StrBuf *Buf, int Force)
 shrink long term buffers to their real size so they don't waste memory
StrBufNewStrBuf (void)
 Allocate a new buffer with default buffer size.
StrBufNewStrBufDup (const StrBuf *CopyMe)
 Copy Constructor; returns a duplicate of CopyMe.
void NewStrBufDupAppendFlush (StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, const char *NoMe, int KeepOriginal)
 Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards.
StrBufNewStrBufPlain (const char *ptr, int nChars)
 create a new Buffer using an existing c-string this function should also be used if you want to pre-suggest the buffer size to allocate in conjunction with ptr == NULL
int StrBufPlain (StrBuf *Buf, const char *ptr, int nChars)
 Set an existing buffer from a c-string.
StrBuf_NewConstStrBuf (const char *StringConstant, size_t SizeOfStrConstant)
 use strbuf as wrapper for a string constant for easy handling
int FlushStrBuf (StrBuf *buf)
 flush the content of a Buf; keep its struct
int FLUSHStrBuf (StrBuf *buf)
 wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
void FreeStrBuf (StrBuf **FreeMe)
 Release a Buffer Its a double pointer, so it can NULL your pointer so fancy SIG11 appear instead of random results.
char * SmashStrBuf (StrBuf **SmashMe)
 flatten a Buffer to the Char * we return Its a double pointer, so it can NULL your pointer so fancy SIG11 appear instead of random results The Callee then owns the buffer and is responsible for freeing it.
void HFreeStrBuf (void *VFreeMe)
 Release the buffer If you want put your StrBuf into a Hash, use this as Destructor.
long StrTol (const StrBuf *Buf)
 Wrapper around atol.
int StrToi (const StrBuf *Buf)
 Wrapper around atoi.
int StrBufIsNumber (const StrBuf *Buf)
 Checks to see if the string is a pure number.
long StrBufPeek (StrBuf *Buf, const char *ptr, long nThChar, char PeekValue)
 modifies a Single char of the Buf You can point to it via char* or a zero-based integer
long StrBufPook (StrBuf *Buf, const char *ptr, long nThChar, long nChars, char PookValue)
 modifies a range of chars of the Buf You can point to it via char* or a zero-based integer
void StrBufAppendBuf (StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
 Append a StringBuffer to the buffer.
void StrBufAppendBufPlain (StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset)
 Append a C-String to the buffer.
void StrBufVAppendPrintf (StrBuf *Buf, const char *format, va_list ap)
 sprintf like function appending the formated string to the buffer vsnprintf version to wrap into own calls
void StrBufAppendPrintf (StrBuf *Buf, const char *format,...)
 sprintf like function appending the formated string to the buffer
void StrBufPrintf (StrBuf *Buf, const char *format,...)
 sprintf like function putting the formated string into the buffer
size_t CurlFillStrBuf_callback (void *ptr, size_t size, size_t nmemb, void *stream)
 Callback for cURL to append the webserver reply to a buffer.
int StrBufSub (StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
 extracts a substring from Source into dest
void StrBufCutLeft (StrBuf *Buf, int nChars)
 Cut nChars from the start of the string.
void StrBufCutRight (StrBuf *Buf, int nChars)
 Cut the trailing n Chars from the string.
void StrBufCutAt (StrBuf *Buf, int AfternChars, const char *At)
 Cut the string after n Chars.
void StrBufTrim (StrBuf *Buf)
 Strip leading and trailing spaces from a string; with premeasured and adjusted length.
void StrBufSpaceToBlank (StrBuf *Buf)
 changes all spaces in the string (tab, linefeed...) to Blank (0x20)
void StrBufStripAllBut (StrBuf *Buf, char leftboundary, char rightboundary)
void StrBufUpCase (StrBuf *Buf)
 uppercase the contents of a buffer
void StrBufLowerCase (StrBuf *Buf)
 lowercase the contents of a buffer
int StrBufReplaceToken (StrBuf *Buf, long where, long HowLong, const char *Repl, long ReplLen)
 Replace a token at a given place with a given length by another token with given length.
int StrBufNum_tokens (const StrBuf *source, char tok)
 Counts the numbmer of tokens in a buffer.
int StrBufRemove_token (StrBuf *Source, int parmnum, char separator)
 a string tokenizer
int StrBufExtract_tokenFromStr (StrBuf *dest, const char *Source, long SourceLen, int parmnum, char separator)
int StrBufExtract_token (StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
 a string tokenizer
int StrBufExtract_int (const StrBuf *Source, int parmnum, char separator)
 a string tokenizer to fetch an integer
long StrBufExtract_long (const StrBuf *Source, int parmnum, char separator)
 a string tokenizer to fetch a long integer
unsigned long StrBufExtract_unsigned_long (const StrBuf *Source, int parmnum, char separator)
 a string tokenizer to fetch an unsigned long
int StrBufHaveNextToken (const StrBuf *Source, const char **pStart)
 a string tokenizer; Bounds checker function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
int StrBufExtract_NextToken (StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer
int StrBufSkip_NTokenS (const StrBuf *Source, const char **pStart, char separator, int nTokens)
 a string tokenizer
int StrBufExtractNext_int (const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer to fetch an integer
long StrBufExtractNext_long (const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer to fetch a long integer
unsigned long StrBufExtractNext_unsigned_long (const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer to fetch an unsigned long
void StrBufUrlescAppend (StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
 Escape a string for feeding out as a URL while appending it to a Buffer.
void StrBufUrlescUPAppend (StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
 Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer.
void StrBufHexEscAppend (StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
 append a string in hex encoding to the buffer
void StrBufHexescAppend (StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
 append a string in hex encoding to the buffer
long StrEscAppend (StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
 Append a string, escaping characters which have meaning in HTML.
void StrMsgEscAppend (StrBuf *Target, const StrBuf *Source, const char *PlainIn)
 Append a string, escaping characters which have meaning in HTML.
void StrIcalEscAppend (StrBuf *Target, const StrBuf *Source, const char *PlainIn)
 Append a string, escaping characters which have meaning in ICAL.
long StrECMAEscAppend (StrBuf *Target, const StrBuf *Source, const char *PlainIn)
 Append a string, escaping characters which have meaning in JavaScript strings .
long StrHtmlEcmaEscAppend (StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
 Append a string, escaping characters which have meaning in HTML + json.
void StrBufEUid_unescapize (StrBuf *target, const StrBuf *source)
 unhide special chars hidden to the HTML escaper
void StrBufEUid_escapize (StrBuf *target, const StrBuf *source)
 hide special chars from the HTML escapers and friends
int StrBufDecodeBase64 (StrBuf *Buf)
 decode a buffer from base 64 encoding; destroys original
int StrBufDecodeHex (StrBuf *Buf)
 decode a buffer from base 64 encoding; destroys original
int StrBufSanitizeAscii (StrBuf *Buf, const char Mute)
 replace all chars >0x20 && < 0x7F with Mute
long StrBufUnescape (StrBuf *Buf, int StripBlanks)
 remove escaped strings from i.e.
int StrBufRFC2047encode (StrBuf **target, const StrBuf *source)
 RFC2047-encode a header field if necessary.
static void AddRecipient (StrBuf *Target, StrBuf *UserName, StrBuf *EmailAddress, StrBuf *EncBuf)
StrBufStrBufSanitizeEmailRecipientVector (const StrBuf *Recp, StrBuf *UserName, StrBuf *EmailAddress, StrBuf *EncBuf)
 QP encode parts of an email TO/CC/BCC vector, and strip/filter invalid parts.
void StrBufReplaceChars (StrBuf *buf, char search, char replace)
 replaces all occurances of 'search' by 'replace'
void StrBufToUnixLF (StrBuf *buf)
 removes all \r s from the string, or replaces them with
if its not a combination of both.
void ctdl_iconv_open (const char *tocode, const char *fromcode, void *pic)
 Wrapper around iconv_open() Our version adds aliases for non-standard Microsoft charsets such as 'MS950', aliasing them to names like 'CP950'.
static const char * FindNextEnd (const StrBuf *Buf, const char *bptr)
 find one chunk of a RFC822 encoded string
void StrBufConvert (StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
 convert one buffer according to the preselected iconv pointer PIC
static void DecodeSegment (StrBuf *Target, const StrBuf *DecodeMe, const char *SegmentStart, const char *SegmentEnd, StrBuf *ConvertBuf, StrBuf *ConvertBuf2, StrBuf *FoundCharset)
 catches one RFC822 encoded segment, and decodes it.
void StrBuf_RFC822_to_Utf8 (StrBuf *Target, const StrBuf *DecodeMe, const StrBuf *DefaultCharset, StrBuf *FoundCharset)
 Handle subjects with RFC2047 encoding such as: [deprecated old syntax!] =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=.
void StrBuf_RFC822_2_Utf8 (StrBuf *Target, const StrBuf *DecodeMe, const StrBuf *DefaultCharset, StrBuf *FoundCharset, StrBuf *ConvertBuf, StrBuf *ConvertBuf2)
 Handle subjects with RFC2047 encoding such as: =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=.
long StrBuf_Utf8StrLen (StrBuf *Buf)
 measure the number of glyphs in an UTF8 string...
long StrBuf_Utf8StrCut (StrBuf *Buf, int maxlen)
 cuts a string after maxlen glyphs
int CompressBuffer (StrBuf *Buf)
 compress the buffer with gzip Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
int StrBuf_write_one_chunk_callback (int fd, short event, IOBuffer *FB)
eReadState StrBufChunkSipLine (StrBuf *LineBuf, IOBuffer *FB)
 extract a "next line" from Buf; Ptr to persist across several iterations
eReadState StrBufCheckBuffer (IOBuffer *FB)
 check whether the chunk-buffer has more data waiting or not.
long IOBufferStrLength (IOBuffer *FB)
void FDIOBufferInit (FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
void FDIOBufferDelete (FDIOBuffer *FDB)
int FileSendChunked (FDIOBuffer *FDB, const char **Err)
int FileRecvChunked (FDIOBuffer *FDB, const char **Err)
int FileMoveChunked (FDIOBuffer *FDB, const char **Err)
eReadState WriteIOBAlreadyRead (FDIOBuffer *FDB, const char **Error)
int StrBufTCP_read_line (StrBuf *buf, int *fd, int append, const char **Error)
 Read a line from socket flushes and closes the FD on error.
int StrBufTCP_read_buffered_line (StrBuf *Line, StrBuf *buf, int *fd, int timeout, int selectresolution, const char **Error)
 Read a line from socket flushes and closes the FD on error.
int StrBufTCP_read_buffered_line_fast (StrBuf *Line, StrBuf *IOBuf, const char **Pos, int *fd, int timeout, int selectresolution, const char **Error)
 Read a line from socket flushes and closes the FD on error.
int StrBufReadBLOB (StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
 Input binary data from socket flushes and closes the FD on error.
int StrBufReadBLOBBuffered (StrBuf *Blob, StrBuf *IOBuf, const char **Pos, int *fd, int append, long nBytes, int check, const char **Error)
 Input binary data from socket flushes and closes the FD on error.
int StrBufSipLine (StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
 extract a "next line" from Buf; Ptr to persist across several iterations
void StrBufStripSlashes (StrBuf *Dir, int RemoveTrailingSlash)
 removes double slashes from pathnames

Variables

int BaseStrBufSize = 64
const char * StrBufNOTNULL = ((char*) NULL) - 1
const char HexList [256][3]
static const char * ErrRBLF_PreConditionFailed = "StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor"
static const char * ErrRBLF_SelectFailed = "StrBufTCP_read_buffered_line_fast: Select failed without reason"
static const char * ErrRBLF_NotEnoughSentFromServer = "StrBufTCP_read_buffered_line_fast: No complete line was sent from peer"
static const char * ErrRBLF_BLOBPreConditionFailed = "StrBufReadBLOB: Wrong arguments or invalid Filedescriptor"
const char * ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting."
const char * ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting."

Class Documentation

struct StrBuf

Private Structure for the Stringbuffer.

Definition at line 138 of file stringbuf.c.

Class Members
char * buf the pointer to the dynamic buffer
long BufSize how many spcae do we optain
long BufUsed StNumber of Chars used excluding the trailing \0.
int ConstBuf are we just a wrapper arround a static buffer and musn't we be changed?

Define Documentation

#define _GNU_SOURCE

Definition at line 19 of file stringbuf.c.

#define dbg_FreeStrBuf (   a,
 
)

Definition at line 236 of file stringbuf.c.

#define dbg_IncreaseBuf (   a)

Definition at line 237 of file stringbuf.c.

#define dbg_Init (   a)

Definition at line 238 of file stringbuf.c.

Definition at line 30 of file stringbuf.c.


Function Documentation

static void AddRecipient ( StrBuf Target,
StrBuf UserName,
StrBuf EmailAddress,
StrBuf EncBuf 
) [static]

Definition at line 2803 of file stringbuf.c.

{
       int QuoteMe = 0;

       if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
       if (strchr(ChrPtr(UserName), ',') != NULL) QuoteMe = 1;

       if (QuoteMe)  StrBufAppendBufPlain(Target, HKEY("\""), 0);
       StrBufRFC2047encode(&EncBuf, UserName);
       StrBufAppendBuf(Target, EncBuf, 0);
       if (QuoteMe)  StrBufAppendBufPlain(Target, HKEY("\" "), 0);
       else          StrBufAppendBufPlain(Target, HKEY(" "), 0);

       if (StrLength(EmailAddress) > 0){
              StrBufAppendBufPlain(Target, HKEY("<"), 0);
              StrBufAppendBuf(Target, EmailAddress, 0); /* TODO: what about IDN???? */
              StrBufAppendBufPlain(Target, HKEY(">"), 0);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void FDIOBufferDelete ( FDIOBuffer FDB)

Definition at line 3897 of file stringbuf.c.

{
#ifndef LINUX_SPLICE
       FreeStrBuf(&FDB->ChunkBuffer);
#else
       close(FDB->SplicePipe[0]);
       close(FDB->SplicePipe[1]);
       
#endif
       close(FDB->OtherFD);
       memset(FDB, 0, sizeof(FDIOBuffer));       
}

Here is the call graph for this function:

void FDIOBufferInit ( FDIOBuffer FDB,
IOBuffer IO,
int  FD,
long  TotalSendSize 
)

Definition at line 3883 of file stringbuf.c.

{
       memset(FDB, 0, sizeof(FDIOBuffer));
       FDB->ChunkSize = 
              FDB->TotalSendSize = TotalSendSize;
       FDB->IOB = IO;
#ifndef LINUX_SPLICE
       FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize + 1);
#else
       pipe(FDB->SplicePipe);
#endif
       FDB->OtherFD = FD;
}

Here is the call graph for this function:

int FileMoveChunked ( FDIOBuffer FDB,
const char **  Err 
)

Definition at line 4044 of file stringbuf.c.

{
       ssize_t sent, pipesize;

#ifdef LINUX_SPLICE
       if (FDB->PipeSize == 0)
       {
              pipesize = splice(FDB->IOB->fd,
                              &FDB->TotalReadAlready, 
                              FDB->SplicePipe[1],
                              NULL, 
                              FDB->ChunkSendRemain, 
                              SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);

              if (pipesize == -1)
              {
                     *Err = strerror(errno);
                     return pipesize;
              }
              FDB->PipeSize = pipesize;
       }
       
       sent = splice(FDB->SplicePipe[0],
                    NULL, 
                    FDB->OtherFD,
                    &FDB->TotalSentAlready, 
                    FDB->PipeSize,
                    SPLICE_F_MORE | SPLICE_F_MOVE);

       if (sent == -1)
       {
              *Err = strerror(errno);
              return sent;
       }
       FDB->PipeSize -= sent;
       FDB->ChunkSendRemain -= sent;
       return sent;
#else
       
       sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
       if (sent > 0) {
              int nWritten = 0;
              int rc; 
              
              FDB->ChunkBuffer->BufUsed = sent;

              while (nWritten < FDB->ChunkBuffer->BufUsed) {
                     rc =  write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
                     if (rc < 0) {
                            *Err = strerror(errno);
                            return rc;
                     }
                     nWritten += rc;

              }
              FDB->ChunkBuffer->BufUsed = 0;
              FDB->TotalSentAlready += sent;
              FDB->ChunkSendRemain -= sent;
              return FDB->ChunkSendRemain;
       }
       else if (sent < 0) {
              *Err = strerror(errno);
              return sent;
       }

#endif
       return 0;
}
int FileRecvChunked ( FDIOBuffer FDB,
const char **  Err 
)

Definition at line 3975 of file stringbuf.c.

{
       ssize_t sent, pipesize;

#ifdef LINUX_SPLICE
       if (FDB->PipeSize == 0)
       {
              pipesize = splice(FDB->IOB->fd,
                              NULL, 
                              FDB->SplicePipe[1],
                              NULL, 
                              FDB->ChunkSendRemain, 
                              SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);

              if (pipesize == -1)
              {
                     *Err = strerror(errno);
                     return pipesize;
              }
              FDB->PipeSize = pipesize;
       }
       
       sent = splice(FDB->SplicePipe[0],
                    NULL, 
                    FDB->OtherFD,
                    &FDB->TotalSentAlready, 
                    FDB->PipeSize,
                    SPLICE_F_MORE | SPLICE_F_MOVE);

       if (sent == -1)
       {
              *Err = strerror(errno);
              return sent;
       }
       FDB->PipeSize -= sent;
       FDB->ChunkSendRemain -= sent;
       return sent;
#else
       
       sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
       if (sent > 0) {
              int nWritten = 0;
              int rc; 
              
              FDB->ChunkBuffer->BufUsed = sent;

              while (nWritten < FDB->ChunkBuffer->BufUsed) {
                     rc =  write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
                     if (rc < 0) {
                            *Err = strerror(errno);
                            return rc;
                     }
                     nWritten += rc;

              }
              FDB->ChunkBuffer->BufUsed = 0;
              FDB->TotalSentAlready += sent;
              FDB->ChunkSendRemain -= sent;
              return FDB->ChunkSendRemain;
       }
       else if (sent < 0) {
              *Err = strerror(errno);
              return sent;
       }

#endif
       return 0;
}
int FileSendChunked ( FDIOBuffer FDB,
const char **  Err 
)

Definition at line 3910 of file stringbuf.c.

{
       ssize_t sent, pipesize;
#ifdef LINUX_SPLICE
       if (FDB->PipeSize == 0)
       {
              pipesize = splice(FDB->OtherFD,
                              &FDB->TotalSentAlready, 
                              FDB->SplicePipe[1],
                              NULL, 
                              FDB->ChunkSendRemain, 
                              SPLICE_F_MOVE);
       
              if (pipesize == -1)
              {
                     *Err = strerror(errno);
                     return pipesize;
              }
              FDB->PipeSize = pipesize;
       }
       sent =  splice(FDB->SplicePipe[0],
                     NULL, 
                     FDB->IOB->fd,
                     NULL, 
                     FDB->PipeSize,
                     SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
       if (sent == -1)
       {
              *Err = strerror(errno);
              return sent;
       }
       FDB->PipeSize -= sent;
       FDB->ChunkSendRemain -= sent;
       return sent;
#else

       char *pRead;
       long nRead = 0;

       pRead = FDB->ChunkBuffer->buf;
       while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0))
       {
              nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed);
              if (nRead > 0) {
                     FDB->ChunkBuffer->BufUsed += nRead;
                     FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0';
              }
              else if (nRead == 0) {}
              else return nRead;
              
       }

       nRead = write(FDB->IOB->fd, FDB->ChunkBuffer->buf + FDB->TotalSentAlready, FDB->ChunkSendRemain);

       if (nRead >= 0) {
              FDB->TotalSentAlready += nRead;
              FDB->ChunkSendRemain -= nRead;
              return FDB->ChunkSendRemain;
       }
       else {
              return nRead;
       }
#endif
}
long IOBufferStrLength ( IOBuffer FB)

Definition at line 3873 of file stringbuf.c.

{
       if ((FB == NULL) || (FB->Buf == NULL))
              return 0;
       if (FB->ReadWritePointer == NULL)
              return StrLength(FB->Buf);
       
       return StrLength(FB->Buf) - (FB->ReadWritePointer - FB->Buf->buf);
}

Here is the call graph for this function:

long StrBuf_read_one_chunk_callback ( int  fd,
short  event,
IOBuffer FB 
)

Definition at line 3651 of file stringbuf.c.

{
       long bufremain = 0;
       int n;
       
       if ((FB == NULL) || (FB->Buf == NULL))
              return -1;

       /*
        * check whether the read pointer is somewhere in a range 
        * where a cut left is inexpensive
        */

       if (FB->ReadWritePointer != NULL)
       {
              long already_read;
              
              already_read = FB->ReadWritePointer - FB->Buf->buf;
              bufremain = FB->Buf->BufSize - FB->Buf->BufUsed - 1;

              if (already_read != 0) {
                     long unread;
                     
                     unread = FB->Buf->BufUsed - already_read;

                     /* else nothing to compact... */
                     if (unread == 0) {
                            FB->ReadWritePointer = FB->Buf->buf;
                            bufremain = FB->Buf->BufSize;                    
                     }
                     else if ((unread < 64) || 
                             (bufremain < already_read))
                     {
                            /* 
                             * if its just a tiny bit remaining, or we run out of space... 
                             * lets tidy up.
                             */
                            FB->Buf->BufUsed = unread;
                            if (unread < already_read)
                                   memcpy(FB->Buf->buf, FB->ReadWritePointer, unread);
                            else
                                   memmove(FB->Buf->buf, FB->ReadWritePointer, unread);
                            FB->ReadWritePointer = FB->Buf->buf;
                            bufremain = FB->Buf->BufSize - unread - 1;
                     }
                     else if (bufremain < (FB->Buf->BufSize / 10))
                     {
                            /* get a bigger buffer */ 

                            IncreaseBuf(FB->Buf, 0, FB->Buf->BufUsed + 1);

                            FB->ReadWritePointer = FB->Buf->buf + unread;

                            bufremain = FB->Buf->BufSize - unread - 1;
/*TODO: special increase function that won't copy the already read! */
                     }
              }
              else if (bufremain < 10) {
                     IncreaseBuf(FB->Buf, 1, FB->Buf->BufUsed + 10);
                     
                     FB->ReadWritePointer = FB->Buf->buf;
                     
                     bufremain = FB->Buf->BufSize - FB->Buf->BufUsed - 1;
              }
              
       }
       else {
              FB->ReadWritePointer = FB->Buf->buf;
              bufremain = FB->Buf->BufSize - 1;
       }

       n = read(fd, FB->Buf->buf + FB->Buf->BufUsed, bufremain);

       if (n > 0) {
              FB->Buf->BufUsed += n;
              FB->Buf->buf[FB->Buf->BufUsed] = '\0';
       }
       return n;
}

Here is the call graph for this function:

int StrBuf_write_one_chunk_callback ( int  fd,
short  event,
IOBuffer FB 
)

Definition at line 3731 of file stringbuf.c.

{
       long WriteRemain;
       int n;

       if ((FB == NULL) || (FB->Buf == NULL))
              return -1;

       if (FB->ReadWritePointer != NULL)
       {
              WriteRemain = FB->Buf->BufUsed - 
                     (FB->ReadWritePointer - 
                      FB->Buf->buf);
       }
       else {
              FB->ReadWritePointer = FB->Buf->buf;
              WriteRemain = FB->Buf->BufUsed;
       }

       n = write(fd, FB->ReadWritePointer, WriteRemain);
       if (n > 0) {
              FB->ReadWritePointer += n;

              if (FB->ReadWritePointer == 
                  FB->Buf->buf + FB->Buf->BufUsed)
              {
                     FlushStrBuf(FB->Buf);
                     FB->ReadWritePointer = NULL;
                     return 0;
              }
       // check whether we've got something to write
       // get the maximum chunk plus the pointer we can send
       // write whats there
       // if not all was sent, remember the send pointer for the next time
              return FB->ReadWritePointer - FB->Buf->buf + FB->Buf->BufUsed;
       }
       return n;
}

Here is the call graph for this function:

check whether the chunk-buffer has more data waiting or not.

Parameters:
FBChunk-Buffer to inspect

Definition at line 3860 of file stringbuf.c.

{
       if (FB == NULL)
              return eReadFail;
       if (FB->Buf->BufUsed == 0)
              return eReadSuccess;
       if (FB->ReadWritePointer == NULL)
              return eBufferNotEmpty;
       if (FB->Buf->buf + FB->Buf->BufUsed > FB->ReadWritePointer)
              return eBufferNotEmpty;
       return eReadSuccess;
}
int StrBufExtract_tokenFromStr ( StrBuf dest,
const char *  Source,
long  SourceLen,
int  parmnum,
char  separator 
)

Definition at line 1348 of file stringbuf.c.

{
       const StrBuf Temp = {
              (char*)Source,
              SourceLen,
              SourceLen,
              1
#ifdef SIZE_DEBUG
              ,
              0,
              "",
              ""
#endif
       };

       return StrBufExtract_token(dest, &Temp, parmnum, separator);
}

Here is the call graph for this function:

StrBuf* StrBufSanitizeEmailRecipientVector ( const StrBuf Recp,
StrBuf UserName,
StrBuf EmailAddress,
StrBuf EncBuf 
)

QP encode parts of an email TO/CC/BCC vector, and strip/filter invalid parts.

Parameters:
RecpSource list of email recipients
UserNameTemporary buffer for internal use; Please provide valid buffer.
EmailAddressTemporary buffer for internal use; Please provide valid buffer.
EncBufTemporary buffer for internal use; Please provide valid buffer.
Returns:
encoded & sanitized buffer with the contents of Recp; Caller owns this memory.

TODO: Userfeedback??

Definition at line 2835 of file stringbuf.c.

{
       StrBuf *Target;
       const char *pch, *pche;
       const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;

       if ((Recp == NULL) || (StrLength(Recp) == 0))
              return NULL;

       pch = ChrPtr(Recp);
       pche = pch + StrLength(Recp);

       if (!CheckEncode(pch, -1, pche))
              return NewStrBufDup(Recp);

       Target = NewStrBufPlain(NULL, StrLength(Recp));

       while ((pch != NULL) && (pch < pche))
       {
              while (isspace(*pch)) pch++;
              UserEnd = EmailStart = EmailEnd = NULL;
              
              if ((*pch == '"') || (*pch == '\'')) {
                     UserStart = pch + 1;
                     
                     UserEnd = strchr(UserStart, *pch);
                     if (UserEnd == NULL) 
                            break; 
                     EmailStart = UserEnd + 1;
                     while (isspace(*EmailStart))
                            EmailStart++;
                     if (UserEnd == UserStart) {
                            UserStart = UserEnd = NULL;
                     }
                     
                     if (*EmailStart == '<') {
                            EmailStart++;
                            EmailEnd = strchr(EmailStart, '>');
                            if (EmailEnd == NULL)
                                   EmailEnd = strchr(EmailStart, ',');
                            
                     }
                     else {
                            EmailEnd = strchr(EmailStart, ',');
                     }
                     if (EmailEnd == NULL)
                            EmailEnd = pche;
                     pch = EmailEnd + 1;
              }
              else {
                     int gt = 0;
                     UserStart = pch;
                     EmailEnd = strchr(UserStart, ',');
                     if (EmailEnd == NULL) {
                            EmailEnd = strchr(pch, '>');
                            pch = NULL;
                            if (EmailEnd != NULL) {
                                   gt = 1;
                            }
                            else {
                                   EmailEnd = pche;
                            }
                     }
                     else {

                            pch = EmailEnd + 1;
                            while ((EmailEnd > UserStart) && !gt &&
                                   ((*EmailEnd == ',') ||
                                   (*EmailEnd == '>') ||
                                   (isspace(*EmailEnd))))
                            {
                                   if (*EmailEnd == '>')
                                          gt = 1;
                                   else 
                                          EmailEnd--;
                            }
                            if (EmailEnd == UserStart)
                                   break;
                     }
                     if (gt) {
                            EmailStart = strchr(UserStart, '<');
                            if ((EmailStart == NULL) || (EmailStart > EmailEnd))
                                   break;
                            UserEnd = EmailStart;

                            while ((UserEnd > UserStart) && 
                                   isspace (*(UserEnd - 1)))
                                   UserEnd --;
                            EmailStart ++;
                            if (UserStart >= UserEnd)
                                   UserStart = UserEnd = NULL;
                     }
                     else { /* this is a local recipient... no domain, just a realname */
                            EmailStart = UserStart;
                            At = strchr(EmailStart, '@');
                            if (At == NULL) {
                                   UserEnd = EmailEnd;
                                   EmailEnd = NULL;
                            }
                            else {
                                   EmailStart = UserStart;
                                   UserStart = NULL;
                            }
                     }
              }

              if ((UserStart != NULL) && (UserEnd != NULL))
                     StrBufPlain(UserName, UserStart, UserEnd - UserStart);
              else if ((UserStart != NULL) && (UserEnd == NULL))
                     StrBufPlain(UserName, UserStart, UserEnd - UserStart);
              else
                     FlushStrBuf(UserName);

              if ((EmailStart != NULL) && (EmailEnd != NULL))
                     StrBufPlain(EmailAddress, EmailStart, EmailEnd - EmailStart);
              else if ((EmailStart != NULL) && (EmailEnd == NULL))
                     StrBufPlain(EmailAddress, EmailStart, EmailEnd - pche);
              else 
                     FlushStrBuf(EmailAddress);

              AddRecipient(Target, UserName, EmailAddress, EncBuf);

              if (pch == NULL)
                     break;
              
              if ((pch != NULL) && (*pch == ','))
                     pch ++;
              if (pch != NULL) while (isspace(*pch))
                     pch ++;
       }
       return Target;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void StrBufStripAllBut ( StrBuf Buf,
char  leftboundary,
char  rightboundary 
)

Definition at line 1144 of file stringbuf.c.

{
       const char *pBuff;
       const char *pLeft;
       const char *pRight;

       if ((Buf == NULL) || (Buf->buf == NULL))
              return;
       pLeft = pBuff = Buf->buf;
       while (pBuff != NULL) {
              pLeft = pBuff;
              pBuff = strchr(pBuff, leftboundary);
              if (pBuff != NULL)
                     pBuff++;
       }
              
       if (pLeft != NULL)
              pBuff = pLeft;
       else
              pBuff = Buf->buf;
       pRight = strchr(pBuff, rightboundary);
       if (pRight != NULL)
              StrBufCutAt(Buf, 0, pRight);
       if (pLeft != NULL)
              StrBufCutLeft(Buf, pLeft - Buf->buf);
}

Here is the call graph for this function:

eReadState WriteIOBAlreadyRead ( FDIOBuffer FDB,
const char **  Error 
)

Definition at line 4113 of file stringbuf.c.

{
       int IsNonBlock;
       int fdflags;
       long rlen;
       long should_write;
       int nSuccessLess = 0;
       struct timeval tv;
       fd_set rfds;

       fdflags = fcntl(FDB->OtherFD, F_GETFL);
       IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;

       while ((FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf < FDB->IOB->Buf->BufUsed) &&
              (FDB->ChunkSendRemain > 0))
       {
              if (IsNonBlock){
                     tv.tv_sec = 1; /* selectresolution; */
                     tv.tv_usec = 0;
                     
                     FD_ZERO(&rfds);
                     FD_SET(FDB->OtherFD, &rfds);
                     if (select(FDB->OtherFD + 1, NULL, &rfds, NULL, &tv) == -1) {
                            *Error = strerror(errno);
                            return eReadFail;
                     }
              }
              if (IsNonBlock && !  FD_ISSET(FDB->OtherFD, &rfds)) {
                     nSuccessLess ++;
                     continue;
              }

              should_write = FDB->IOB->Buf->BufUsed - 
                     (FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf);
              if (should_write > FDB->ChunkSendRemain)
                     should_write = FDB->ChunkSendRemain;

              rlen = write(FDB->OtherFD, 
                          FDB->IOB->ReadWritePointer, 
                          should_write);
              if (rlen < 1) {
                     *Error = strerror(errno);
                                          
                     return eReadFail;
              }
              FDB->TotalSentAlready += rlen;
              FDB->IOB->ReadWritePointer += rlen;
              FDB->ChunkSendRemain -= rlen;
       }
       if (FDB->IOB->ReadWritePointer >= FDB->IOB->Buf->buf + FDB->IOB->Buf->BufUsed)
       {
              FlushStrBuf(FDB->IOB->Buf);
              FDB->IOB->ReadWritePointer = NULL;
       }

       if (FDB->ChunkSendRemain == 0)
              return eReadSuccess;
       else 
              return eMustReadMore;
}

Here is the call graph for this function:


Variable Documentation

int BaseStrBufSize = 64

Definition at line 52 of file stringbuf.c.

const char* ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting."

Definition at line 4595 of file stringbuf.c.

const char* ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting."

Definition at line 4596 of file stringbuf.c.

const char* ErrRBLF_BLOBPreConditionFailed = "StrBufReadBLOB: Wrong arguments or invalid Filedescriptor" [static]

Definition at line 4517 of file stringbuf.c.

const char* ErrRBLF_NotEnoughSentFromServer = "StrBufTCP_read_buffered_line_fast: No complete line was sent from peer" [static]

Definition at line 4326 of file stringbuf.c.

const char* ErrRBLF_PreConditionFailed = "StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor" [static]

Definition at line 4324 of file stringbuf.c.

const char* ErrRBLF_SelectFailed = "StrBufTCP_read_buffered_line_fast: Select failed without reason" [static]

Definition at line 4325 of file stringbuf.c.

const char HexList[256][3]
Initial value:
 {
       "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
       "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
       "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
       "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
       "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
       "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
       "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
       "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
       "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
       "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
       "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
       "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
       "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
       "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
       "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
       "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"}

Definition at line 56 of file stringbuf.c.

const char* StrBufNOTNULL = ((char*) NULL) - 1

Definition at line 54 of file stringbuf.c.