From: Wilfried Goesgens Date: Mon, 5 Sep 2011 08:56:58 +0000 (+0000) Subject: Sync libcitadel with the one from the libevent-branch X-Git-Tag: v8.01~61 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=1c151ce69fccc967650f3258d4ec2ec255df1baf Sync libcitadel with the one from the libevent-branch --- diff --git a/libcitadel/Makefile.in b/libcitadel/Makefile.in index 84234628e..a178ccd76 100755 --- a/libcitadel/Makefile.in +++ b/libcitadel/Makefile.in @@ -113,6 +113,7 @@ LIB_OBJS = lib/libcitadel.lo \ lib/stringbuf.lo \ lib/json.lo \ lib/wildfire.lo \ + lib/urlhandling.lo \ lib/xdgmime/xdgmime.lo \ lib/xdgmime/xdgmimeglob.lo \ lib/xdgmime/xdgmimeint.lo \ diff --git a/libcitadel/lib/hash.c b/libcitadel/lib/hash.c index e67c2f0af..d315358be 100644 --- a/libcitadel/lib/hash.c +++ b/libcitadel/lib/hash.c @@ -28,6 +28,50 @@ typedef struct Payload Payload; /** + * @defgroup HashList Hashlist Key Value list implementation; + * Hashlist is a simple implementation of key value pairs. It doesn't implement collision handling. + * the Hashingalgorythm is pluggeable on creation. + * items are added with a functionpointer destructs them; that way complex structures can be added. + * if no pointer is given, simply free is used. Use @ref reference_free_handler if you don't want us to free you rmemory. + */ + +/** + * @defgroup HashListData Datastructures used for the internals of HashList + * @ingroup HashList + */ + +/** + * @defgroup HashListDebug Hashlist debugging functions + * @ingroup HashList + */ + +/** + * @defgroup HashListPrivate Hashlist internal functions + * @ingroup HashList + */ + +/** + * @defgroup HashListSort Hashlist sorting functions + * @ingroup HashList + */ + +/** + * @defgroup HashListAccess Hashlist functions to access / put / delete items in(to) the list + * @ingroup HashList + */ + +/** + * @defgroup HashListAlgorithm functions to condense Key to an integer. + * @ingroup HashList + */ + +/** + * @defgroup HashListMset MSet is sort of a derived hashlist, its special for treating Messagesets as Citadel uses them to store access rangesx + * @ingroup HashList + */ + +/** + * @ingroup HashListData * @brief Hash Payload storage Structure; filled in linear. */ struct Payload { @@ -37,6 +81,7 @@ struct Payload { /** + * @ingroup HashListData * @brief Hash key element; sorted by key */ struct HashKey { @@ -48,6 +93,7 @@ struct HashKey { }; /** + * @ingroup HashListData * @brief Hash structure; holds arrays of Hashkey and Payload. */ struct HashList { @@ -63,6 +109,7 @@ struct HashList { }; /** + * @ingroup HashListData * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside */ struct HashPos { @@ -72,12 +119,13 @@ struct HashPos { /** + * @ingroup HashListDebug * @brief Iterate over the hash and call PrintEntry. * @param Hash your Hashlist structure * @param Trans is called so you could for example print 'A:' if the next entries are like that. * Must be aware to receive NULL in both pointers. * @param PrintEntry print entry one by one - * \returns the number of items printed + * @return the number of items printed */ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry) { @@ -116,11 +164,12 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry /** + * @ingroup HashListDebug * @brief verify the contents of a hash list; here for debugging purposes. * @param Hash your Hashlist structure * @param First Functionpointer to allow you to print your payload * @param Second Functionpointer to allow you to print your payload - * \returns 0 + * @return 0 */ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second) { @@ -172,9 +221,34 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon } +int TestValidateHash(HashList *TestHash) +{ + long i; + + if (TestHash->nMembersUsed != TestHash->nLookupTableItems) + return 1; + + if (TestHash->nMembersUsed > TestHash->MemberSize) + return 2; + + for (i=0; i < TestHash->nMembersUsed; i++) + { + + if (TestHash->LookupTable[i]->Position > TestHash->nMembersUsed) + return 3; + + if (TestHash->Members[TestHash->LookupTable[i]->Position] == NULL) + return 4; + if (TestHash->Members[TestHash->LookupTable[i]->Position]->Data == NULL) + return 5; + } + return 0; +} + /** + * @ingroup HashListAccess * @brief instanciate a new hashlist - * \returns the newly allocated list. + * @return the newly allocated list. */ HashList *NewHash(int Uniq, HashFunc F) { @@ -204,6 +278,7 @@ int GetCount(HashList *Hash) /** + * @ingroup HashListPrivate * @brief private destructor for one hash element. * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]' * @param Data an element to free using the user provided destructor, or just plain free @@ -218,6 +293,7 @@ static void DeleteHashPayload (Payload *Data) } /** + * @ingroup HashListPrivate * @brief Destructor for nested hashes */ void HDeleteHash(void *vHash) @@ -227,11 +303,12 @@ void HDeleteHash(void *vHash) } /** - * @brief destroy a hashlist and all of its members + * @ingroup HashListAccess + * @brief flush the members of a hashlist * Crashing? do 'print *FreeMe->LookupTable[i]' * @param Hash Hash to destroy. Is NULL'ed so you are shure its done. */ -void DeleteHash(HashList **Hash) +void DeleteHashContent(HashList **Hash) { int i; HashList *FreeMe; @@ -255,18 +332,42 @@ void DeleteHash(HashList **Hash) free(FreeMe->LookupTable[i]); } } - /** now, free our arrays... */ - free(FreeMe->LookupTable); - free(FreeMe->Members); + FreeMe->nMembersUsed = 0; + FreeMe->tainted = 0; + FreeMe->nLookupTableItems = 0; + memset(FreeMe->Members, 0, sizeof(Payload*) * FreeMe->MemberSize); + memset(FreeMe->LookupTable, 0, sizeof(HashKey*) * FreeMe->MemberSize); + /** did s.b. want an array of our keys? free them. */ if (FreeMe->MyKeys != NULL) free(FreeMe->MyKeys); +} + +/** + * @ingroup HashListAccess + * @brief destroy a hashlist and all of its members + * Crashing? do 'print *FreeMe->LookupTable[i]' + * @param Hash Hash to destroy. Is NULL'ed so you are shure its done. + */ +void DeleteHash(HashList **Hash) +{ + HashList *FreeMe; + + FreeMe = *Hash; + if (FreeMe == NULL) + return; + DeleteHashContent(Hash); + /** now, free our arrays... */ + free(FreeMe->LookupTable); + free(FreeMe->Members); + /** buye bye cruel world. */ free (FreeMe); *Hash = NULL; } /** + * @ingroup HashListPrivate * @brief Private function to increase the hash size. * @param Hash the Hasharray to increase */ @@ -308,9 +409,10 @@ static void IncreaseHashSize(HashList *Hash) /** + * @ingroup HashListPrivate * @brief private function to add a new item to / replace an existing in - the hashlist * if the hash list is full, its re-alloced with double size. - * \parame Hash our hashlist to manipulate + * @param Hash our hashlist to manipulate * @param HashPos where should we insert / replace? * @param HashKeyStr the Hash-String * @param HKLen length of HashKeyStr @@ -369,11 +471,12 @@ static void InsertHashItem(HashList *Hash, } /** + * @ingroup HashListSort * @brief if the user has tainted the hash, but wants to insert / search items by their key * we need to search linear through the array. You have been warned that this will take more time! * @param Hash Our Hash to manipulate * @param HashBinKey the Hash-Number to lookup. - * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! ) + * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! ) */ static long FindInTaintedHash(HashList *Hash, long HashBinKey) { @@ -391,10 +494,11 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey) } /** + * @ingroup HashListPrivate * @brief Private function to lookup the Item / the closest position to put it in * @param Hash Our Hash to manipulate * @param HashBinKey the Hash-Number to lookup. - * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! ) + * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! ) */ static long FindInHash(HashList *Hash, long HashBinKey) { @@ -445,10 +549,11 @@ static long FindInHash(HashList *Hash, long HashBinKey) /** + * @ingroup HashListAlgorithm * @brief another hashing algorithm; treat it as just a pointer to int. * @param str Our pointer to the int value * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it! - * \returns the calculated hash value + * @return the calculated hash value */ long Flathash(const char *str, long len) { @@ -458,10 +563,11 @@ long Flathash(const char *str, long len) } /** + * @ingroup HashListAlgorithm * @brief another hashing algorithm; treat it as just a pointer to long. * @param str Our pointer to the long value * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it! - * \returns the calculated hash value + * @return the calculated hash value */ long lFlathash(const char *str, long len) { @@ -471,10 +577,11 @@ long lFlathash(const char *str, long len) } /** + * @ingroup HashListPrivate * @brief private abstract wrapper around the hashing algorithm * @param HKey the hash string * @param HKLen length of HKey - * \returns the calculated hash value + * @return the calculated hash value */ inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen) { @@ -489,10 +596,11 @@ inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen) /** + * @ingroup HashListAccess * @brief Add a new / Replace an existing item in the Hash - * @param HashList the list to manipulate + * @param Hash the list to manipulate * @param HKey the hash-string to store Data under - * @param HKeyLen Length of HKey + * @param HKLen Length of HKey * @param Data the payload you want to associate with HKey * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used. */ @@ -537,12 +645,13 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat } /** + * @ingroup HashListAccess * @brief Lookup the Data associated with HKey * @param Hash the Hashlist to search in * @param HKey the hashkey to look up * @param HKLen length of HKey * @param Data returns the Data associated with HKey - * \returns 0 if not found, 1 if. + * @return 0 if not found, 1 if. */ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data) { @@ -581,6 +690,7 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload) } /** + * @ingroup HashListAccess * @brief get the Keys present in this hash, simila to array_keys() in PHP * Attention: List remains to Hash! don't modify or free it! * @param Hash Your Hashlist to extract the keys from @@ -604,12 +714,13 @@ int GetHashKeys(HashList *Hash, char ***List) } /** + * @ingroup HashListAccess * @brief creates a hash-linear iterator object * @param Hash the list we reference - * @param in which step width should we iterate? + * @param StepWidth in which step width should we iterate? * If negative, the last position matching the * step-raster is provided. - * \returns the hash iterator + * @return the hash iterator */ HashPos *GetNewHashPos(HashList *Hash, int StepWidth) { @@ -630,12 +741,13 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth) } /** + * @ingroup HashListAccess * @brief Set iterator object to point to key. If not found, don't change iterator * @param Hash the list we reference * @param HKey key to search for * @param HKLen length of key * @param At HashPos to update - * \returns 0 if not found + * @return 0 if not found */ int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At) { @@ -662,10 +774,11 @@ int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At) } /** + * @ingroup HashListAccess * @brief Delete from the Hash the entry at Position * @param Hash the list we reference * @param At the position within the Hash - * \returns 0 if not found + * @return 0 if not found */ int DeleteEntryFromHash(HashList *Hash, HashPos *At) { @@ -718,9 +831,11 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At) } /** + * @ingroup HashListAccess * @brief retrieve the counter from the itteratoor - * @param the Iterator to analyze - * \returns the n'th hashposition we point at + * @param Hash which + * @param At the Iterator to analyze + * @return the n'th hashposition we point at */ int GetHashPosCounter(HashList *Hash, HashPos *At) { @@ -733,6 +848,7 @@ int GetHashPosCounter(HashList *Hash, HashPos *At) } /** + * @ingroup HashListAccess * @brief frees a linear hash iterator */ void DeleteHashPos(HashPos **DelMe) @@ -746,13 +862,14 @@ void DeleteHashPos(HashPos **DelMe) /** + * @ingroup HashListAccess * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward * @param Hash your Hashlist to follow * @param At the position to retrieve the Item from and move forward afterwards * @param HKLen returns Length of Hashkey Returned * @param HashKey returns the Hashkey corrosponding to HashPos * @param Data returns the Data found at HashPos - * \returns whether the item was found or not. + * @return whether the item was found or not. */ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) { @@ -778,13 +895,14 @@ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKe } /** + * @ingroup HashListAccess * @brief Get the data located where HashPos Iterator points at * @param Hash your Hashlist to follow * @param At the position retrieve the data from * @param HKLen returns Length of Hashkey Returned * @param HashKey returns the Hashkey corrosponding to HashPos * @param Data returns the Data found at HashPos - * \returns whether the item was found or not. + * @return whether the item was found or not. */ int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) { @@ -804,10 +922,11 @@ int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, v } /** + * @ingroup HashListAccess * @brief Move HashPos one forward * @param Hash your Hashlist to follow * @param At the position to move forward - * \returns whether there is a next item or not. + * @return whether there is a next item or not. */ int NextHashPos(HashList *Hash, HashPos *At) { @@ -829,13 +948,15 @@ int NextHashPos(HashList *Hash, HashPos *At) } /** + * @ingroup HashListAccess * @brief Get the data located where At points to * note: you should prefer iterator operations instead of using me. * @param Hash your Hashlist peek from + * @param At get the item in the position At. * @param HKLen returns Length of Hashkey Returned * @param HashKey returns the Hashkey corrosponding to HashPos * @param Data returns the Data found at HashPos - * \returns whether the item was found or not. + * @return whether the item was found or not. */ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data) { @@ -853,13 +974,14 @@ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void ** } /** + * @ingroup HashListSort * @brief Get the data located where At points to * note: you should prefer iterator operations instead of using me. * @param Hash your Hashlist peek from * @param HKLen returns Length of Hashkey Returned * @param HashKey returns the Hashkey corrosponding to HashPos * @param Data returns the Data found at HashPos - * \returns whether the item was found or not. + * @return whether the item was found or not. */ /* long GetHashIDAt(HashList *Hash,long At) @@ -875,6 +997,7 @@ long GetHashIDAt(HashList *Hash,long At) /** + * @ingroup HashListSort * @brief sorting function for sorting the Hash alphabeticaly by their strings * @param Key1 first item * @param Key2 second item @@ -889,6 +1012,7 @@ static int SortByKeys(const void *Key1, const void* Key2) } /** + * @ingroup HashListSort * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings * @param Key1 first item * @param Key2 second item @@ -903,6 +1027,7 @@ static int SortByKeysRev(const void *Key1, const void* Key2) } /** + * @ingroup HashListSort * @brief sorting function to regain hash-sequence and revert tainted status * @param Key1 first item * @param Key2 second item @@ -918,6 +1043,7 @@ static int SortByHashKeys(const void *Key1, const void* Key2) /** + * @ingroup HashListSort * @brief sort the hash alphabeticaly by their keys. * Caution: This taints the hashlist, so accessing it later * will be significantly slower! You can un-taint it by SortByHashKeyStr @@ -934,6 +1060,7 @@ void SortByHashKey(HashList *Hash, int Order) } /** + * @ingroup HashListSort * @brief sort the hash by their keys (so it regains untainted state). * this will result in the sequence the hashing allgorithm produces it by default. * @param Hash the list to sort @@ -948,9 +1075,10 @@ void SortByHashKeyStr(HashList *Hash) /** + * @ingroup HashListSort * @brief gives user sort routines access to the hash payload - * @param Searchentry to retrieve Data to - * \returns Data belonging to HashVoid + * @param HashVoid to retrieve Data to + * @return Data belonging to HashVoid */ const void *GetSearchPayload(const void *HashVoid) { @@ -958,6 +1086,7 @@ const void *GetSearchPayload(const void *HashVoid) } /** + * @ingroup HashListSort * @brief sort the hash by your sort function. see the following sample. * this will result in the sequence the hashing allgorithm produces it by default. * @param Hash the list to sort @@ -987,8 +1116,9 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy) */ -/* - * Generic function to free a reference. +/** + * @ingroup HashListAccess + * @brief Generic function to free a reference. * since a reference actualy isn't needed to be freed, do nothing. */ void reference_free_handler(void *ptr) @@ -997,7 +1127,8 @@ void reference_free_handler(void *ptr) } -/* +/** + * @ingroup HashListAlgorithm * This exposes the hashlittle() function to consumers. */ int HashLittle(const void *key, size_t length) { @@ -1006,9 +1137,10 @@ int HashLittle(const void *key, size_t length) { /** - * \brief parses an MSet string into a list for later use - * \param MSetList List to be read from MSetStr - * \param MSetStr String containing the list + * @ingroup HashListMset + * @brief parses an MSet string into a list for later use + * @param MSetList List to be read from MSetStr + * @param MSetStr String containing the list */ int ParseMSet(MSet **MSetList, StrBuf *MSetStr) { @@ -1061,9 +1193,10 @@ int ParseMSet(MSet **MSetList, StrBuf *MSetStr) } /** - * \brief checks whether a message is inside a mset - * \param MSetList List to search for MsgNo - * \param MsgNo number to search in mset + * @ingroup HashListMset + * @brief checks whether a message is inside a mset + * @param MSetList List to search for MsgNo + * @param MsgNo number to search in mset */ int IsInMSetList(MSet *MSetList, long MsgNo) { @@ -1112,8 +1245,9 @@ int IsInMSetList(MSet *MSetList, long MsgNo) /** - * \brief frees a mset [redirects to @ref DeleteHash - * \param FreeMe to be free'd + * @ingroup HashListMset + * @brief frees a mset [redirects to @ref DeleteHash + * @param FreeMe to be free'd */ void DeleteMSet(MSet **FreeMe) { diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 96cb9f3ff..021caf8d0 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -14,6 +14,8 @@ #include #include #include +#include + #define LIBCITADEL_VERSION_NUMBER 800 /* @@ -219,6 +221,27 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, int selectresolution, const char **Error); +typedef enum _eReadState { + eReadFail, + eReadSuccess, + eMustReadMore, + eBufferNotEmpty +} eReadState; + +typedef struct _file_buffer { + StrBuf *Buf; + const char *ReadWritePointer; + int fd; + int LineCompleted; + int nBlobBytesWanted; +} IOBuffer; + +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); +eReadState StrBufCheckBuffer(IOBuffer *FB); + int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr); int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, const char *Repl, long ReplLen); int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator); @@ -310,6 +333,30 @@ int LoadIconDir(const char *DirName); const char *GetIconFilename(char *MimeType, size_t len); +/* URL parsing & connection data */ +typedef struct ParsedURL ParsedURL; +struct ParsedURL { + StrBuf *URL; + StrBuf *UrlWithoutCred; + StrBuf *CurlCreds; + unsigned Port; + const char *Host; + const char *User; + const char *Pass; + const char *LocalPart; + const char *PlainUrl; + int IsIP; + int IPv6; + int af; + struct hostent *HEnt; + struct sockaddr_in6 Addr; + ParsedURL *Next; +}; + +void FreeURL(ParsedURL** Url); +int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort); +void CurlPrepareURL(ParsedURL *Url); + /* tools */ @@ -410,8 +457,11 @@ long lFlathash(const char *str, long len); #define IKEY(a) (const char*) &a, sizeof(a) #define LKEY(a) (const char*) &a, sizeof(a) +int TestValidateHash(HashList *TestHash); + HashList *NewHash(int Uniq, HashFunc F); void DeleteHash(HashList **Hash); +void DeleteHashContent(HashList **Hash); void HDeleteHash(void *vHash); int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data); void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index d4b2e666f..2b0d2fe8e 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1536,6 +1536,8 @@ int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pSt (Source->BufUsed == 0) ) { *pStart = StrBufNOTNULL; + if (dest != NULL) + FlushStrBuf(dest); return -1; } @@ -2898,7 +2900,7 @@ void StrBufReplaceChars(StrBuf *buf, char search, char replace) /** * @ingroup StrBuf - * @brief removes all \r s from the string, or replaces them with \n if its not a combination of both. + * @brief removes all \\r s from the string, or replaces them with \n if its not a combination of both. * @param buf Buffer to modify */ void StrBufToUnixLF(StrBuf *buf) @@ -3550,7 +3552,227 @@ int CompressBuffer(StrBuf *Buf) return 0; } +/******************************************************************************* + * File I/O; Callbacks to libevent * + *******************************************************************************/ + +long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB) +{ + 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; +} + +int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB) +{ + 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; +} + +/** + * @ingroup StrBuf_IO + * @brief extract a "next line" from Buf; Ptr to persist across several iterations + * @param LineBuf your line will be copied here. + * @param FB BLOB with lines of text... + * @param Ptr moved arround to keep the next-line across several iterations + * has to be &NULL on start; will be &NotNULL on end of buffer + * @returns size of copied buffer + */ +eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB) +{ + const char *aptr, *ptr, *eptr; + char *optr, *xptr; + + if ((FB->Buf == NULL) || (FB->ReadWritePointer == StrBufNOTNULL)) { + FB->ReadWritePointer = StrBufNOTNULL; + return eReadFail; + } + + FlushStrBuf(LineBuf); + if (FB->ReadWritePointer == NULL) + ptr = aptr = FB->Buf->buf; + else + ptr = aptr = FB->ReadWritePointer; + + optr = LineBuf->buf; + eptr = FB->Buf->buf + FB->Buf->BufUsed; + xptr = LineBuf->buf + LineBuf->BufSize - 1; + + while ((ptr <= eptr) && + (*ptr != '\n') && + (*ptr != '\r') ) + { + *optr = *ptr; + optr++; ptr++; + if (optr == xptr) { + LineBuf->BufUsed = optr - LineBuf->buf; + IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1); + optr = LineBuf->buf + LineBuf->BufUsed; + xptr = LineBuf->buf + LineBuf->BufSize - 1; + } + } + + if (ptr >= eptr) { + if (optr > LineBuf->buf) + optr --; + if ((*(ptr - 1) != '\r') && (*(ptr - 1) != '\n')) { + LineBuf->BufUsed = optr - LineBuf->buf; + *optr = '\0'; + if ((FB->ReadWritePointer != NULL) && + (FB->ReadWritePointer != FB->Buf->buf)) + { + /* Ok, the client application read all the data + it was interested in so far. Since there is more to read, + we now shrink the buffer, and move the rest over. + */ + StrBufCutLeft(FB->Buf, + FB->ReadWritePointer - FB->Buf->buf); + FB->ReadWritePointer = FB->Buf->buf; + } + return eMustReadMore; + } + } + LineBuf->BufUsed = optr - LineBuf->buf; + *optr = '\0'; + if ((ptr <= eptr) && (*ptr == '\r')) + ptr ++; + if ((ptr <= eptr) && (*ptr == '\n')) + ptr ++; + + if (ptr < eptr) { + FB->ReadWritePointer = ptr; + } + else { + FlushStrBuf(FB->Buf); + FB->ReadWritePointer = NULL; + } + + return eReadSuccess; +} +/** + * @ingroup StrBuf_CHUNKED_IO + * @brief check whether the chunk-buffer has more data waiting or not. + * @param FB Chunk-Buffer to inspect + */ +eReadState StrBufCheckBuffer(IOBuffer *FB) +{ + 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; +} /******************************************************************************* * File I/O; Prefer buffered read since its faster! *