ref: a8e09ce5a8a2818a11f926962f1cbd71d469d0bf
parent: c5ba431f0810f439854ec48f4c8e0fb5d5a46d01
author: rrt <rrt>
date: Mon Dec 11 18:10:05 EST 2006
Move little-endian memory reading and writing functions into misc.c as directed.
--- a/src/cvsd.c
+++ b/src/cvsd.c
@@ -373,38 +373,7 @@
#define DVMS_HEADER_LEN 120
/* ---------------------------------------------------------------------- */
-/* FIXME: Move these to misc.c */
-static uint32_t get32(unsigned char **p)
-{
- uint32_t val = (((*p)[3]) << 24) | (((*p)[2]) << 16) |
- (((*p)[1]) << 8) | (**p);
- (*p) += 4;
- return val;
-}
-static uint16_t get16(unsigned char **p)
-{
- unsigned val = (((*p)[1]) << 8) | (**p);
- (*p) += 2;
- return val;
-}
-
-static void put32(unsigned char **p, uint32_t val)
-{
- *(*p)++ = val & 0xff;
- *(*p)++ = (val >> 8) & 0xff;
- *(*p)++ = (val >> 16) & 0xff;
- *(*p)++ = (val >> 24) & 0xff;
-}
-
-static void put16(unsigned char **p, int16_t val)
-{
- *(*p)++ = val & 0xff;
- *(*p)++ = (val >> 8) & 0xff;
-}
-
-/* ---------------------------------------------------------------------- */
-
static int dvms_read_header(ft_t ft, struct dvms_header *hdr)
{
unsigned char hdrbuf[DVMS_HEADER_LEN];
@@ -421,21 +390,21 @@
pch = hdrbuf;
memcpy(hdr->Filename, pch, sizeof(hdr->Filename));
pch += sizeof(hdr->Filename);
- hdr->Id = get16(&pch);
- hdr->State = get16(&pch);
- hdr->Unixtime = get32(&pch);
- hdr->Usender = get16(&pch);
- hdr->Ureceiver = get16(&pch);
- hdr->Length = get32(&pch);
- hdr->Srate = get16(&pch);
- hdr->Days = get16(&pch);
- hdr->Custom1 = get16(&pch);
- hdr->Custom2 = get16(&pch);
+ hdr->Id = get16_le(&pch);
+ hdr->State = get16_le(&pch);
+ hdr->Unixtime = get32_le(&pch);
+ hdr->Usender = get16_le(&pch);
+ hdr->Ureceiver = get16_le(&pch);
+ hdr->Length = get32_le(&pch);
+ hdr->Srate = get16_le(&pch);
+ hdr->Days = get16_le(&pch);
+ hdr->Custom1 = get16_le(&pch);
+ hdr->Custom2 = get16_le(&pch);
memcpy(hdr->Info, pch, sizeof(hdr->Info));
pch += sizeof(hdr->Info);
memcpy(hdr->extend, pch, sizeof(hdr->extend));
pch += sizeof(hdr->extend);
- hdr->Crc = get16(&pch);
+ hdr->Crc = get16_le(&pch);
if (sum != hdr->Crc)
{
st_report("DVMS header checksum error, read %u, calculated %u",
@@ -460,16 +429,16 @@
memcpy(pch, hdr->Filename, sizeof(hdr->Filename));
pch += sizeof(hdr->Filename);
- put16(&pch, hdr->Id);
- put16(&pch, hdr->State);
- put32(&pch, hdr->Unixtime);
- put16(&pch, hdr->Usender);
- put16(&pch, hdr->Ureceiver);
- put32(&pch, hdr->Length);
- put16(&pch, hdr->Srate);
- put16(&pch, hdr->Days);
- put16(&pch, hdr->Custom1);
- put16(&pch, hdr->Custom2);
+ put16_le(&pch, hdr->Id);
+ put16_le(&pch, hdr->State);
+ put32_le(&pch, hdr->Unixtime);
+ put16_le(&pch, hdr->Usender);
+ put16_le(&pch, hdr->Ureceiver);
+ put32_le(&pch, hdr->Length);
+ put16_le(&pch, hdr->Srate);
+ put16_le(&pch, hdr->Days);
+ put16_le(&pch, hdr->Custom1);
+ put16_le(&pch, hdr->Custom2);
memcpy(pch, hdr->Info, sizeof(hdr->Info));
pch += sizeof(hdr->Info);
memcpy(pch, hdr->extend, sizeof(hdr->extend));
@@ -477,7 +446,7 @@
for(i = sizeof(hdrbuf), sum = 0; i > /*2*/3; i--) /* Deti bug */
sum += *pchs++;
hdr->Crc = sum;
- put16(&pch, hdr->Crc);
+ put16_le(&pch, hdr->Crc);
if (st_seeki(ft, 0, SEEK_SET) < 0)
{
st_report("seek failed\n: %s",strerror(errno));
--- a/src/misc.c
+++ b/src/misc.c
@@ -332,6 +332,36 @@
return (ST_SUCCESS);
}
+
+uint32_t get32_le(unsigned char **p)
+{
+ uint32_t val = (((*p)[3]) << 24) | (((*p)[2]) << 16) |
+ (((*p)[1]) << 8) | (**p);
+ (*p) += 4;
+ return val;
+}
+
+uint16_t get16_le(unsigned char **p)
+{
+ unsigned val = (((*p)[1]) << 8) | (**p);
+ (*p) += 2;
+ return val;
+}
+
+void put32_le(unsigned char **p, uint32_t val)
+{
+ *(*p)++ = val & 0xff;
+ *(*p)++ = (val >> 8) & 0xff;
+ *(*p)++ = (val >> 16) & 0xff;
+ *(*p)++ = (val >> 24) & 0xff;
+}
+
+void put16_le(unsigned char **p, int16_t val)
+{
+ *(*p)++ = val & 0xff;
+ *(*p)++ = (val >> 8) & 0xff;
+}
+
/* generic swap routine. Swap l and place in to f (datatype length = n) */
static void st_swapb(char *l, char *f, int n)
{
--- a/src/st_i.h
+++ b/src/st_i.h
@@ -85,6 +85,12 @@
void st_rewind(ft_t ft);
void st_clearerr(ft_t ft);
+/* Utilities to read/write values endianness-independently */
+uint32_t get32_le(unsigned char **p);
+uint16_t get16_le(unsigned char **p);
+void put32_le(unsigned char **p, uint32_t val);
+void put16_le(unsigned char **p, int16_t val);
+
/* Utilities to byte-swap values, use libc optimized macros if possible */
#ifdef HAVE_BYTESWAP_H
#define st_swapw(x) bswap_16(x)