1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-10-26 00:00:09 +00:00

lsocket.[ch]: refactored writeInt && readInt

- switched to laikaS_readu* && laikaS_writeu*
- this gets rid of the ugly malloc() for platforms that don't support VLAs
This commit is contained in:
2022-09-07 16:46:18 -05:00
parent dc91a207b1
commit 6ab280d010
12 changed files with 95 additions and 79 deletions

View File

@@ -58,4 +58,28 @@
void *laikaM_realloc(void *buf, size_t sz);
inline bool laikaM_isBigEndian(void)
{
union
{
uint32_t i;
uint8_t c[4];
} _indxint = {0xDEADB33F};
return _indxint.c[0] == 0xDE;
}
inline void laikaM_reverse(uint8_t *buf, size_t sz)
{
int k;
/* swap bytes, reversing the buffer */
for (k = 0; k < (sz / 2); k++) {
uint8_t tmp = buf[k];
buf[k] = buf[sz - k - 1];
buf[sz - k - 1] = tmp;
}
}
#endif

View File

@@ -88,8 +88,6 @@ struct sLaika_socket
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
bool laikaS_isBigEndian(void);
void laikaS_init(void);
void laikaS_cleanUp(void);
@@ -114,10 +112,10 @@ void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uin
uint8_t *priv); /* decrypts & reads to buf using pub & priv key*/
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data);
uint8_t laikaS_readByte(struct sLaika_socket *sock);
void laikaS_readInt(struct sLaika_socket *sock, void *buf,
size_t sz); /* reads INT, respecting endianness */
void laikaS_writeInt(struct sLaika_socket *sock, void *buf,
size_t sz); /* writes INT, respecting endianness */
void laikaS_writeu16(struct sLaika_socket *sock, uint16_t i); /* writes UINT16, respecting endianness */
uint16_t laikaS_readu16(struct sLaika_socket *sock); /* reads UINT16, respecting endianness */
void laikaS_writeu32(struct sLaika_socket *sock, uint32_t i); /* writes UINT32, respecting endianness */
uint32_t laikaS_readu32(struct sLaika_socket *sock); /* reads UINT32, respecting endianness */
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed);
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed);

View File

@@ -232,7 +232,7 @@ bool laikaS_handlePeerIn(struct sLaika_socket *sock)
LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT\n");
/* read packet size */
laikaS_readInt(&peer->sock, (void *)&peer->pktSize, sizeof(LAIKAPKT_SIZE));
peer->pktSize = laikaS_readu16(&peer->sock);
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
LAIKA_ERROR("variable packet too large!\n");

View File

@@ -8,17 +8,6 @@
static int _LNSetup = 0;
bool laikaS_isBigEndian(void)
{
union
{
uint32_t i;
uint8_t c[4];
} _indxint = {0xDEADB33F};
return _indxint.c[0] == 0xDE;
}
void laikaS_init(void)
{
if (_LNSetup++ > 0)
@@ -258,41 +247,46 @@ uint8_t laikaS_readByte(struct sLaika_socket *sock)
return tmp;
}
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz)
void laikaS_writeu16(struct sLaika_socket *sock, uint16_t i)
{
if (sock->flipEndian) {
VLA(uint8_t, tmp, sz); /* allocate tmp buffer to hold data while we switch endianness */
int k;
uint16_t tmp = i; /* copy int to buffer (which we can reverse if need-be) */
laikaS_read(sock, (void *)tmp, sz);
if (sock->flipEndian)
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
/* copy tmp buffer to user buffer, flipping endianness */
for (k = 0; k < sz; k++)
*((uint8_t *)buf + k) = tmp[sz - k - 1];
ENDVLA(tmp);
} else {
/* just a wrapper for laikaS_read */
laikaS_read(sock, buf, sz);
}
laikaS_write(sock, (void *)&tmp, sizeof(tmp));
}
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz)
uint16_t laikaS_readu16(struct sLaika_socket *sock)
{
if (sock->flipEndian) {
VLA(uint8_t, tmp, sz); /* allocate tmp buffer to hold data while we switch endianness */
int k;
uint16_t tmp;
laikaS_read(sock, (void *)&tmp, sizeof(tmp));
/* copy user buffer to tmp buffer, flipping endianness */
for (k = 0; k < sz; k++)
tmp[k] = *((uint8_t *)buf + (sz - k - 1));
if (sock->flipEndian)
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
laikaS_write(sock, (void *)tmp, sz);
ENDVLA(tmp);
} else {
/* just a wrapper for laikaS_write */
laikaS_write(sock, buf, sz);
}
return tmp;
}
void laikaS_writeu32(struct sLaika_socket *sock, uint32_t i)
{
uint32_t tmp = i; /* copy int to buffer (which we can reverse if need-be) */
if (sock->flipEndian)
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
laikaS_write(sock, (void *)&tmp, sizeof(tmp));
}
uint32_t laikaS_readu32(struct sLaika_socket *sock)
{
uint32_t tmp;
laikaS_read(sock, (void *)&tmp, sizeof(tmp));
if (sock->flipEndian)
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
return tmp;
}
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed)