From 6153a713e13120f8d1c1fa83105fd67e6a900f0a Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Sat, 7 Jan 2012 00:03:23 +0100 Subject: [PATCH] Readability fixes --- fr60.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/fr60.c b/fr60.c index e0d6a1c..17564f5 100644 --- a/fr60.c +++ b/fr60.c @@ -78,6 +78,14 @@ static uchar *blast; static int blsize = 0; static int clock_offset = 0; +u_int32_t le32_read(const unsigned char *i) { + return le32toh(*(u_int32_t*)i); +} + +u_int16_t le16_read(const unsigned char *i) { + return le16toh(*(int16_t*)i); +} + uint randno(void) { @@ -363,9 +371,9 @@ save_antfs_file(const uchar chan, const uchar* blast, uint blsize) fprintf(stderr, "didn't receive full ant-fs header\n"); exit(1); } - remaining = blast[12] + blast[13]*256 + blast[14]*256*256 + blast[15]*256*256*256; - offset = blast[16] + blast[17]*256 + blast[18]*256*256 + blast[19]*256*256*256; - size = blast[20] + blast[21]*256 + blast[22]*256*256 + blast[23]*256*256*256; + remaining = le32_read(blast+12); + offset = le32_read(blast+16); + size = le32_read(blast+20); printf("save_antfs_file: remaining=%d offset=%d size=%d\n", remaining, offset, size); if (remaining > blsize - 24 || remaining > blsize - 24 + 16) { fprintf(stderr, "unexpected ant-fs packet: header says %d bytes, but received %d\n", size, blsize - 24); @@ -393,8 +401,10 @@ save_antfs_file(const uchar chan, const uchar* blast, uint blsize) if (verbose) printf("%s (%d bytes)\n", filename, size); ANT_RequestMessage(chan, MESG_CHANNEL_STATUS_ID); //informative if (offset+remaining < size) { + int16_t crc = le16_read(blast + blsize - 1); + printf("=> Starting download: %d %d %d\n", offset, remaining, size); - start_download(chan, antfsdir[downloadingfilenum].fileno,offset+remaining, blast[blsize-1] * 256 + blast[blsize-2], 0); + start_download(chan, antfsdir[downloadingfilenum].fileno,offset+remaining, crc, 0); return 1; } return 0; @@ -431,7 +441,7 @@ chevent(uchar chan, uchar event) if (event == EVENT_RX_BROADCAST) { } - cid = cbuf[4]+cbuf[5]*256+cbuf[6]*256*256+cbuf[7]*256*256*256; + cid = le32_read(cbuf+4); memcpy((void *)&id, cbuf+4, 4); D("cid %08x myid %08x\n", cid, myid); @@ -631,14 +641,15 @@ chevent(uchar chan, uchar event) uint offset, remaining, size; D("received ant-fs directory\n"); - remaining = blast[12] + blast[13]*256 + blast[14]*256*256 + blast[15]*256*256*256; - offset = blast[16] + blast[17]*256 + blast[18]*256*256 + blast[19]*256*256*256; - size = blast[20] + blast[21]*256 + blast[22]*256*256 + blast[23]*256*256*256; + remaining = le32_read(blast+12); + offset = le32_read(blast+16); + size = le32_read(blast+20); decode_antfs_dir(blast, blsize); if (offset+remaining < size) { - start_download(chan, 0, offset+remaining, blast[blsize-1] * 256 + blast[blsize-2], 0); + int16_t crc = le16_read(blast + blsize - 1); + start_download(chan, 0, offset+remaining, crc, 0); return 1; } @@ -816,7 +827,7 @@ revent(uchar chan, uchar event) } break; case MESG_CHANNEL_ID_ID: - devid = ebuf[1]+ebuf[2]*256; + devid = le16_read(ebuf+1); if (mydev == 0 || devid == mydev%65536) { D("devid %08x myid %08x\n", devid, myid); } else { -- 2.39.5