src/buffer.c

Sat, 16 Apr 2022 14:47:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Apr 2022 14:47:27 +0200
changeset 515
6d3909bf1609
parent 501
9a08f5e515cc
child 529
814d51173f20
permissions
-rw-r--r--

migrate tree tests to gtest

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "cx/buffer.h"
#include "cx/utils.h"

#include <stdlib.h>
#include <string.h>

int cxBufferInit(
        CxBuffer *buffer,
        void *space,
        size_t capacity,
        CxAllocator *allocator,
        int flags
) {
    buffer->allocator = allocator;
    buffer->flags = flags;
    if (!space) {
        buffer->bytes = cxMalloc(allocator, capacity);
        if (buffer->bytes == NULL) {
            return 1;
        }
        memset(buffer->bytes, 0, capacity);
        buffer->flags |= CX_BUFFER_FREE_CONTENTS;
    } else {
        buffer->bytes = space;
    }
    buffer->capacity = capacity;
    buffer->size = 0;

    buffer->pos = 0;

    return 0;
}

void cxBufferDestroy(CxBuffer *buffer) {
    if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
        cxFree(buffer->allocator, buffer->bytes);
    }
}

CxBuffer *cxBufferExtract(
        CxBuffer *src,
        size_t start,
        size_t length,
        int flags
) {
    if (src->size == 0 || length == 0 ||
        ((size_t) -1) - start < length || start + length > src->capacity) {
        return NULL;
    }

    CxBuffer *dst = (CxBuffer *) malloc(sizeof(cx_buffer_s));
    if (dst) {
        dst->bytes = malloc(length);
        if (!dst->bytes) {
            free(dst);
            return NULL;
        }
        dst->capacity = length;
        dst->size = length;
        dst->flags = flags | CX_BUFFER_FREE_CONTENTS;
        dst->pos = 0;
        memcpy(dst->bytes, src->bytes + start, length);
    }
    return dst;
}

int cxBufferSeek(
        CxBuffer *buffer,
        off_t offset,
        int whence
) {
    size_t npos;
    switch (whence) {
        case SEEK_CUR:
            npos = buffer->pos;
            break;
        case SEEK_END:
            npos = buffer->size;
            break;
        case SEEK_SET:
            npos = 0;
            break;
        default:
            return -1;
    }

    size_t opos = npos;
    npos += offset;

    if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
        return -1;
    }

    if (npos >= buffer->size) {
        return -1;
    } else {
        buffer->pos = npos;
        return 0;
    }

}

int cxBufferEof(CxBuffer *buffer) {
    return buffer->pos >= buffer->size;
}

int cxBufferMinimumCapacity(
        CxBuffer *buffer,
        size_t additional_bytes
) {
    size_t newcap = buffer->capacity + additional_bytes;

    // overflow protection
    if (newcap < buffer->capacity) {
        return -1;
    }

    unsigned char *newspace = realloc(buffer->bytes, newcap);
    if (newspace) {
        memset(newspace + buffer->size, 0, newcap - buffer->size);
        buffer->bytes = newspace;
        buffer->capacity = newcap;
    } else {
        return -1;
    }

    return 0;
}

size_t cxBufferWrite(
        void const *ptr,
        size_t size,
        size_t nitems,
        CxBuffer *buffer
) {
    size_t len;
    if (cx_szmul(size, nitems, &len)) {
        return 0;
    }
    size_t required = buffer->pos + len;
    if (buffer->pos > required) {
        return 0;
    }

    if (required > buffer->capacity) {
        if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
            if (cxBufferMinimumCapacity(buffer, required)) {
                return 0;
            }
        } else {
            len = buffer->capacity - buffer->pos;
            if (size > 1) {
                len -= len % size;
            }
        }
    }

    if (len == 0) {
        return len;
    }

    memcpy(buffer->bytes + buffer->pos, ptr, len);
    buffer->pos += len;
    if (buffer->pos > buffer->size) {
        buffer->size = buffer->pos;
    }

    return len / size;
}

size_t cxBufferRead(
        void *ptr,
        size_t size,
        size_t nitems,
        CxBuffer *buffer
) {
    size_t len;
    if (cx_szmul(size, nitems, &len)) {
        return 0;
    }
    if (buffer->pos + len > buffer->size) {
        len = buffer->size - buffer->pos;
        if (size > 1) len -= len % size;
    }

    if (len <= 0) {
        return len;
    }

    memcpy(ptr, buffer->bytes + buffer->pos, len);
    buffer->pos += len;

    return len / size;
}

int cxBufferPut(
        CxBuffer *buffer,
        int c
) {
    if (buffer->pos >= buffer->capacity) {
        if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
            if (cxBufferMinimumCapacity(buffer, buffer->capacity + 1)) {
                return EOF;
            }
        } else {
            return EOF;
        }
    }

    c &= 0xFF;
    buffer->bytes[buffer->pos] = (unsigned char) c;
    buffer->pos++;
    if (buffer->pos > buffer->size) {
        buffer->size = buffer->pos;
    }
    return c;
}

int cxBufferGet(CxBuffer *buffer) {
    if (cxBufferEof(buffer)) {
        return EOF;
    } else {
        int c = buffer->bytes[buffer->pos];
        buffer->pos++;
        return c;
    }
}

size_t cxBufferPutString(
        CxBuffer *buffer,
        const char *str
) {
    return cxBufferWrite(str, 1, strlen(str), buffer);
}

int cxBufferShiftLeft(
        CxBuffer *buffer,
        size_t shift
) {
    if (shift >= buffer->size) {
        buffer->pos = buffer->size = 0;
    } else {
        memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
        buffer->size -= shift;

        if (buffer->pos >= shift) {
            buffer->pos -= shift;
        } else {
            buffer->pos = 0;
        }
    }
    return 0;
}

int cxBufferShiftRight(
        CxBuffer *buffer,
        size_t shift
) {
    size_t req_capacity = buffer->size + shift;
    size_t movebytes;

    // auto extend buffer, if required and enabled
    if (buffer->capacity < req_capacity) {
        if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
            if (cxBufferMinimumCapacity(buffer, req_capacity)) {
                return 1;
            }
            movebytes = buffer->size;
        } else {
            movebytes = buffer->capacity - shift;
        }
    } else {
        movebytes = buffer->size;
    }

    memmove(buffer->bytes + shift, buffer->bytes, movebytes);
    buffer->size = shift + movebytes;

    buffer->pos += shift;
    if (buffer->pos > buffer->size) {
        buffer->pos = buffer->size;
    }

    return 0;
}

int cxBufferShift(
        CxBuffer *buffer,
        off_t shift
) {
    if (shift < 0) {
        return cxBufferShiftLeft(buffer, (size_t) (-shift));
    } else if (shift > 0) {
        return cxBufferShiftRight(buffer, (size_t) shift);
    } else {
        return 0;
    }
}

mercurial