/* * rb_linebuf_copy_raw * * Copy as much data as possible directly into a linebuf, * splitting at \r\n, but without altering any data. * */ static ssize_t rb_linebuf_copy_raw(rb_buf_head_t * bufhead, rb_buf_line_t * bufline, char *data, size_t len) { size_t cpylen = 0; /* how many bytes we've copied */ char *ch = data; /* Pointer to where we are in the read data */ char *bufch = bufline->buf + bufline->len; ssize_t clen = 0; /* how many bytes we've processed, and don't ever want to see again.. */ /* If its full or terminated, ignore it */ bufline->raw = true; lrb_assert(bufline->len < BUF_DATA_SIZE); if(bufline->terminated == true) return 0; clen = rb_linebuf_skip_crlf(ch, len); cpylen = (size_t) clen; if(clen == -1) return -1; /* This is the overflow case..This doesn't happen often.. */ if(cpylen > (BUF_DATA_SIZE - bufline->len - 1)) { clen = BUF_DATA_SIZE - (ssize_t)bufline->len - 1; memcpy(bufch, ch, (size_t)clen); bufline->buf[BUF_DATA_SIZE - 1] = '\0'; bufch = bufline->buf + BUF_DATA_SIZE - 2; bufline->terminated = true; bufline->len = BUF_DATA_SIZE - 1; bufhead->len += BUF_DATA_SIZE - 1; return clen; } memcpy(bufch, ch, cpylen); bufch += cpylen; *bufch = '\0'; bufch--; if(*bufch != '\r' && *bufch != '\n') { /* No linefeed, bail for the next time */ bufhead->len += cpylen; bufline->len += cpylen; bufline->terminated = false; return clen; } bufline->terminated = true; bufhead->len += cpylen; bufline->len += cpylen; return clen; }
/* * rb_linebuf_copy_raw * * Copy as much data as possible directly into a linebuf, * splitting at \r\n, but without altering any data. * */ static int rb_linebuf_copy_raw(buf_head_t * bufhead, buf_line_t * bufline, char *data, int len) { int cpylen = 0; /* how many bytes we've copied */ char *ch = data; /* Pointer to where we are in the read data */ char *bufch = bufline->buf + bufline->len; int clen = 0; /* how many bytes we've processed, and don't ever want to see again.. */ /* If its full or terminated, ignore it */ bufline->raw = 1; lrb_assert(bufline->len <= LINEBUF_SIZE); if(bufline->terminated == 1) return 0; clen = cpylen = rb_linebuf_skip_crlf(ch, len); if(clen == -1) return -1; /* This is the overflow case..This doesn't happen often.. */ if(cpylen > (LINEBUF_SIZE - bufline->len)) { clen = LINEBUF_SIZE - bufline->len; memcpy(bufch, ch, clen); bufline->buf[LINEBUF_SIZE] = '\0'; bufline->terminated = 1; bufline->len = LINEBUF_SIZE; bufhead->len += LINEBUF_SIZE; return clen; } memcpy(bufch, ch, cpylen); bufch += cpylen; *bufch = '\0'; bufch--; if(*bufch != '\r' && *bufch != '\n') { /* No linefeed, bail for the next time */ bufhead->len += cpylen; bufline->len += cpylen; bufline->terminated = 0; return clen; } bufline->terminated = 1; bufhead->len += cpylen; bufline->len += cpylen; return clen; }
/* * rb_linebuf_copy_line * * Okay..this functions comments made absolutely no sense. * * Basically what we do is this. Find the first chunk of text * and then scan for a CRLF. If we didn't find it, but we didn't * overflow our buffer..we wait for some more data. * If we found a CRLF, we replace them with a \0 character. * If we overflowed, we copy the most our buffer can handle, terminate * it with a \0 and return. * * The return value is the amount of data we consumed. This could * be different than the size of the linebuffer, as when we discard * the overflow, we don't want to process it again. * * This still sucks in my opinion, but it seems to work. * * -Aaron */ static int rb_linebuf_copy_line(buf_head_t * bufhead, buf_line_t * bufline, char *data, int len) { int cpylen = 0; /* how many bytes we've copied */ char *ch = data; /* Pointer to where we are in the read data */ char *bufch = bufline->buf + bufline->len; int clen = 0; /* how many bytes we've processed, and don't ever want to see again.. */ /* If its full or terminated, ignore it */ bufline->raw = 0; lrb_assert(bufline->len < BUF_DATA_SIZE); if(bufline->terminated == 1) return 0; clen = cpylen = rb_linebuf_skip_crlf(ch, len); if(clen == -1) return -1; /* This is the ~overflow case..This doesn't happen often.. */ if(cpylen > (BUF_DATA_SIZE - bufline->len - 1)) { memcpy(bufch, ch, (BUF_DATA_SIZE - bufline->len - 1)); bufline->buf[BUF_DATA_SIZE - 1] = '\0'; bufch = bufline->buf + BUF_DATA_SIZE - 2; while(cpylen && (*bufch == '\r' || *bufch == '\n')) { *bufch = '\0'; cpylen--; bufch--; } bufline->terminated = 1; bufline->len = BUF_DATA_SIZE - 1; bufhead->len += BUF_DATA_SIZE - 1; return clen; } memcpy(bufch, ch, cpylen); bufch += cpylen; *bufch = '\0'; bufch--; if(*bufch != '\r' && *bufch != '\n') { /* No linefeed, bail for the next time */ bufhead->len += cpylen; bufline->len += cpylen; bufline->terminated = 0; return clen; } /* Yank the CRLF off this, replace with a \0 */ while(cpylen && (*bufch == '\r' || *bufch == '\n')) { *bufch = '\0'; cpylen--; bufch--; } bufline->terminated = 1; bufhead->len += cpylen; bufline->len += cpylen; return clen; }