*/
void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
{
- if ((AppendBuf == NULL) || (Buf == NULL) || (AppendBuf->buf == NULL))
+ if ((AppendBuf == NULL) || (Buf == NULL) || (AppendBuf->buf == NULL))
return;
- if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed)
+ if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1)
IncreaseBuf(Buf,
(Buf->BufUsed > 0),
AppendBuf->BufUsed + Buf->BufUsed);
if (len == 0)
return;
- eptr = Target->buf + Target->BufSize - 6;
+ eptr = Target->buf + Target->BufSize - 8;
tptr = Target->buf + Target->BufUsed;
while (aptr < eiptr){
if(tptr >= eptr) {
IncreaseBuf(Target, 1, -1);
- eptr = Target->buf + Target->BufSize - 6;
+ eptr = Target->buf + Target->BufSize - 8;
tptr = Target->buf + Target->BufUsed;
}
return -1;
bptr = Target->buf + Target->BufUsed;
- eptr = Target->buf + Target->BufSize - 2; /* our biggest unit to put in... */
+ eptr = Target->buf + Target->BufSize - 3; /* our biggest unit to put in... */
while (aptr < eiptr){
if(bptr >= eptr) {
IncreaseBuf(Target, 1, -1);
- eptr = Target->buf + Target->BufSize - 2;
+ eptr = Target->buf + Target->BufSize - 3;
bptr = Target->buf + Target->BufUsed;
}
else if (*aptr == '"') {
}
if (Offset + nChars < Source->BufUsed)
{
- if (nChars > dest->BufSize)
+ if (nChars >= dest->BufSize)
IncreaseBuf(dest, 0, nChars + 1);
memcpy(dest->buf, Source->buf + Offset, nChars);
dest->BufUsed = nChars;
return nChars;
}
NCharsRemain = Source->BufUsed - Offset;
- if (NCharsRemain > dest->BufSize)
+ if (NCharsRemain >= dest->BufSize)
IncreaseBuf(dest, 0, NCharsRemain + 1);
memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
dest->BufUsed = NCharsRemain;
* \param tok Tokenizer char to count
* \returns numbers of tokenizer chars found
*/
-inline int StrBufNum_tokens(const StrBuf *source, char tok)
+int StrBufNum_tokens(const StrBuf *source, char tok)
{
if (source == NULL)
return 0;
break;
if (buf->buf[len] != '\r')
len ++;
- if (!(len < buf->BufSize)) {
+ if (len >= buf->BufSize) {
buf->BufUsed = len;
buf->buf[len+1] = '\0';
IncreaseBuf(buf, 1, -1);
}
+/**
+ * \brief Read a line from socket
+ * flushes and closes the FD on error
+ * \param buf the buffer to get the input to
+ * \param Pos pointer to the current read position, should be NULL initialized!
+ * \param fd pointer to the filedescriptor to read
+ * \param append Append to an existing string or replace?
+ * \param Error strerror() on error
+ * \returns numbers of chars read
+ */
+int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
+ StrBuf *buf,
+ const char **Pos,
+ int *fd,
+ int timeout,
+ int selectresolution,
+ const char **Error)
+{
+ int len, rlen;
+ int nSuccessLess = 0;
+ fd_set rfds;
+ const char *pch = NULL;
+ int fdflags;
+ struct timeval tv;
+
+ if ((buf->BufUsed > 0) && (Pos != NULL)) {
+ if (*Pos == NULL)
+ *Pos = buf->buf;
+ pch = strchr(*Pos, '\n');
+ if (pch != NULL) {
+ rlen = 0;
+ len = pch - *Pos;
+ if (len > 0 && (*(pch - 1) == '\r') )
+ rlen ++;
+ StrBufSub(Line, buf, (*Pos - buf->buf), len - rlen);
+ *Pos = pch + 1;
+ return len - rlen;
+ }
+ }
+
+ if (*Pos != NULL) {
+ StrBufCutLeft(buf, (*Pos - buf->buf));
+ *Pos = NULL;
+ }
+
+ if (buf->BufSize - buf->BufUsed < 10)
+ IncreaseBuf(buf, 1, -1);
+
+ fdflags = fcntl(*fd, F_GETFL);
+ if ((fdflags & O_NONBLOCK) == O_NONBLOCK)
+ return -1;
+
+ while ((nSuccessLess < timeout) && (pch == NULL)) {
+ tv.tv_sec = selectresolution;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&rfds);
+ FD_SET(*fd, &rfds);
+ if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
+ *Error = strerror(errno);
+ close (*fd);
+ *fd = -1;
+ return -1;
+ }
+ if (FD_ISSET(*fd, &rfds)) {
+ rlen = read(*fd,
+ &buf->buf[buf->BufUsed],
+ buf->BufSize - buf->BufUsed - 1);
+ if (rlen < 1) {
+ *Error = strerror(errno);
+ close(*fd);
+ *fd = -1;
+ return -1;
+ }
+ else if (rlen > 0) {
+ nSuccessLess = 0;
+ buf->BufUsed += rlen;
+ buf->buf[buf->BufUsed] = '\0';
+ if (buf->BufUsed + 10 > buf->BufSize) {
+ IncreaseBuf(buf, 1, -1);
+ }
+ pch = strchr(buf->buf, '\n');
+ continue;
+ }
+ }
+ nSuccessLess ++;
+ }
+ if (pch != NULL) {
+ *Pos = buf->buf;
+ rlen = 0;
+ len = pch - *Pos;
+ if (len > 0 && (*(pch - 1) == '\r') )
+ rlen ++;
+ StrBufSub(Line, buf, 0, len - rlen);
+ *Pos = *Pos + len + 1;
+ return len - rlen;
+ }
+ return -1;
+
+}
+
/**
* \brief Input binary data from socket
* flushes and closes the FD on error
return -1;
if (!append)
FlushStrBuf(Buf);
- if (Buf->BufUsed + nBytes > Buf->BufSize)
+ if (Buf->BufUsed + nBytes >= Buf->BufSize)
IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
ptr = Buf->buf + Buf->BufUsed;
*/
int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
{
- char *pch;
+ unsigned char *pch;
if (Buf == NULL) return -1;
- pch = Buf->buf;
- while (pch < Buf->buf + Buf->BufUsed) {
+ pch = (unsigned char *)Buf->buf;
+ while (pch < (unsigned char *)Buf->buf + Buf->BufUsed) {
if ((*pch < 0x20) || (*pch > 0x7F))
*pch = Mute;
pch ++;
}
if (*target == NULL)
*target = NewStrBufPlain(NULL, sizeof(headerStr) + source->BufUsed * 2);
- else if (sizeof(headerStr) + source->BufUsed > (*target)->BufSize)
+ else if (sizeof(headerStr) + source->BufUsed >= (*target)->BufSize)
IncreaseBuf(*target, sizeof(headerStr) + source->BufUsed, 0);
memcpy ((*target)->buf, headerStr, sizeof(headerStr) - 1);
(*target)->BufUsed = sizeof(headerStr) - 1;
for (i=0; (i < source->BufUsed); ++i) {
- if ((*target)->BufUsed + 4 > (*target)->BufSize)
+ if ((*target)->BufUsed + 4 >= (*target)->BufSize)
IncreaseBuf(*target, 1, 0);
ch = (unsigned char) source->buf[i];
if ((ch < 32) || (ch > 126) || (ch == 61)) {
}
}
- if ((*target)->BufUsed + 4 > (*target)->BufSize)
+ if ((*target)->BufUsed + 4 >= (*target)->BufSize)
IncreaseBuf(*target, 1, 0);
(*target)->buf[(*target)->BufUsed++] = '?';
size_t obuflen; /**< Length of output buffer */
- if (ConvertBuf->BufUsed > TmpBuf->BufSize)
+ if (ConvertBuf->BufUsed >= TmpBuf->BufSize)
IncreaseBuf(TmpBuf, 0, ConvertBuf->BufUsed);
ic = *(iconv_t*)pic;
optr = LineBuf->buf;
eptr = Buf->buf + Buf->BufUsed;
- xptr = LineBuf->buf + LineBuf->BufSize;
+ xptr = LineBuf->buf + LineBuf->BufSize - 1;
while ((*ptr != '\n') &&
(*ptr != '\r') &&
LineBuf->BufUsed = optr - LineBuf->buf;
IncreaseBuf(LineBuf, 1, LineBuf->BufUsed + 1);
optr = LineBuf->buf + LineBuf->BufUsed;
- xptr = LineBuf->buf + LineBuf->BufSize;
+ xptr = LineBuf->buf + LineBuf->BufSize - 1;
}
}
LineBuf->BufUsed = optr - LineBuf->buf;