optimize math (#5287)
[betaflight.git] / src / main / common / streambuf.c
blobad7db189d3147f0f5d2af91200d7925b273c495e
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <string.h>
19 #include <stdint.h>
21 #include "streambuf.h"
23 sbuf_t *sbufInit(sbuf_t *sbuf, uint8_t *ptr, uint8_t *end)
25 sbuf->ptr = ptr;
26 sbuf->end = end;
27 return sbuf;
30 void sbufWriteU8(sbuf_t *dst, uint8_t val)
32 *dst->ptr++ = val;
35 void sbufWriteU16(sbuf_t *dst, uint16_t val)
37 sbufWriteU8(dst, val >> 0);
38 sbufWriteU8(dst, val >> 8);
41 void sbufWriteU32(sbuf_t *dst, uint32_t val)
43 sbufWriteU8(dst, val >> 0);
44 sbufWriteU8(dst, val >> 8);
45 sbufWriteU8(dst, val >> 16);
46 sbufWriteU8(dst, val >> 24);
49 void sbufWriteU16BigEndian(sbuf_t *dst, uint16_t val)
51 sbufWriteU8(dst, val >> 8);
52 sbufWriteU8(dst, (uint8_t)val);
55 void sbufWriteU32BigEndian(sbuf_t *dst, uint32_t val)
57 sbufWriteU8(dst, val >> 24);
58 sbufWriteU8(dst, val >> 16);
59 sbufWriteU8(dst, val >> 8);
60 sbufWriteU8(dst, (uint8_t)val);
64 void sbufFill(sbuf_t *dst, uint8_t data, int len)
66 memset(dst->ptr, data, len);
67 dst->ptr += len;
70 void sbufWriteData(sbuf_t *dst, const void *data, int len)
72 memcpy(dst->ptr, data, len);
73 dst->ptr += len;
76 void sbufWriteString(sbuf_t *dst, const char *string)
78 sbufWriteData(dst, string, strlen(string));
81 void sbufWriteStringWithZeroTerminator(sbuf_t *dst, const char *string)
83 sbufWriteData(dst, string, strlen(string) + 1);
86 uint8_t sbufReadU8(sbuf_t *src)
88 return *src->ptr++;
91 uint16_t sbufReadU16(sbuf_t *src)
93 uint16_t ret;
94 ret = sbufReadU8(src);
95 ret |= sbufReadU8(src) << 8;
96 return ret;
99 uint32_t sbufReadU32(sbuf_t *src)
101 uint32_t ret;
102 ret = sbufReadU8(src);
103 ret |= sbufReadU8(src) << 8;
104 ret |= sbufReadU8(src) << 16;
105 ret |= sbufReadU8(src) << 24;
106 return ret;
109 void sbufReadData(sbuf_t *src, void *data, int len)
111 memcpy(data, src->ptr, len);
114 // reader - return bytes remaining in buffer
115 // writer - return available space
116 int sbufBytesRemaining(sbuf_t *buf)
118 return buf->end - buf->ptr;
121 uint8_t* sbufPtr(sbuf_t *buf)
123 return buf->ptr;
126 const uint8_t* sbufConstPtr(const sbuf_t *buf)
128 return buf->ptr;
131 // advance buffer pointer
132 // reader - skip data
133 // writer - commit written data
134 void sbufAdvance(sbuf_t *buf, int size)
136 buf->ptr += size;
139 // modifies streambuf so that written data are prepared for reading
140 void sbufSwitchToReader(sbuf_t *buf, uint8_t *base)
142 buf->end = buf->ptr;
143 buf->ptr = base;