123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #include <string.h>
- #include "hal.h"
- #include "memstreams.h"
- static size_t _writes(void *ip, const uint8_t *bp, size_t n) {
- MemoryStream *msp = ip;
- if (msp->size - msp->eos < n)
- n = msp->size - msp->eos;
- memcpy(msp->buffer + msp->eos, bp, n);
- msp->eos += n;
- return n;
- }
- static size_t _reads(void *ip, uint8_t *bp, size_t n) {
- MemoryStream *msp = ip;
- if (msp->eos - msp->offset < n)
- n = msp->eos - msp->offset;
- memcpy(bp, msp->buffer + msp->offset, n);
- msp->offset += n;
- return n;
- }
- static msg_t _put(void *ip, uint8_t b) {
- MemoryStream *msp = ip;
- if (msp->size - msp->eos <= 0)
- return MSG_RESET;
- *(msp->buffer + msp->eos) = b;
- msp->eos += 1;
- return MSG_OK;
- }
- static msg_t _get(void *ip) {
- uint8_t b;
- MemoryStream *msp = ip;
- if (msp->eos - msp->offset <= 0)
- return MSG_RESET;
- b = *(msp->buffer + msp->offset);
- msp->offset += 1;
- return b;
- }
- static const struct MemStreamVMT vmt = {(size_t)0, _writes, _reads, _put, _get};
- void msObjectInit(MemoryStream *msp, uint8_t *buffer,
- size_t size, size_t eos) {
- msp->vmt = &vmt;
- msp->buffer = buffer;
- msp->size = size;
- msp->eos = eos;
- msp->offset = 0;
- }
|