This repository has been archived on 2021-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
uIRCd/src/buffer.c

90 lines
2.8 KiB
C

/*
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
* Copyright (c) 2019, 2020 Alex-David Denes
*
* uIRCd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* uIRCd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
*/
#include "buffer.h"
#include "logging.h"
#include <assert.h> // assert()
#include <errno.h> // errno
#include <stdio.h> // fprintf()
#include <string.h> // strerror()
#include <sys/types.h> // size_t ssize_t
#include <syslog.h> // syslog()
#include <unistd.h> // write()
ssize_t
read_buffer(char* dest, int fd, size_t len)
{
assert(dest != NULL);
if (len == 0) return 0;
ssize_t brd;
if ((brd = read(fd, dest, len)) > 0) {
*(dest + brd) = '\0';
LOG(LOG_DEBUG, "Read %li bytes from fd buffer %i", brd, fd);
} else if (brd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return 0;
LOG(LOG_WARNING, "Failed to read buffer on fd %i: " ERRNOFMT, fd, strerror(errno), errno);
}
return brd;
}
ssize_t
write_buffer(const char* src, int fd, size_t len, bool fullflush)
{
assert(src != NULL);
if (len == 0) return 0;
ssize_t res;
size_t b_wr = 0;
do {
if ((res = write(fd, src + b_wr, len - b_wr)) > 0) {
LOG(LOG_DEBUG, "Wrote %lu bytes to fd %i", res, len);
b_wr += (size_t) res;
} else if (res == -1) {
if (!fullflush && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) return 0;
LOG(LOG_WARNING, "Couldn't flush buffer to fd %i: " ERRNOFMT, fd, strerror(errno), errno);
return res;
}
} while (fullflush && b_wr < len);
return (ssize_t) b_wr;
}
/*!
* Move a string to the begging of a buffer from offset
*
* This function moves string in a buffer to the begging in chunks.
* Chunks are either offset sized or b_left sized depending if the offset is bigger than how much is left.
* This is done because strcpy may be optimized and copy order is not defined, so destination might overwrite source on a rtl copy
*/
size_t
dequeue_bytes(char* str, size_t cnt, size_t len)
{
assert(str != NULL);
assert(cnt <= len);
size_t pos = 0;
do {
char* dest = str + pos;
size_t b_left = len - cnt - pos;
strncpy(dest, dest + cnt, (b_left < cnt) ? b_left : cnt);
LOG(LOG_DEBUG, "Moved %lu bytes from %lu to %lu", (b_left < cnt) ? b_left : cnt, cnt + pos, pos);
pos += cnt;
} while (cnt + pos < len);
return cnt;
}