#include #include "rtv.h" #define expect(x) do { \ if (!(x)) { \ fprintf(stderr, "Unexpected %s %d\n ", #x, __LINE__); \ exit(1); \ } \ } while(0) u16 rtv_to_u16(unsigned char ** pp) { unsigned char * p; u16 r; p = *pp; r = *p++; r <<= 8; r |= *p++; *pp = p; return r; } u32 rtv_to_u32(unsigned char ** pp) { unsigned char * p; u32 r; p = *pp; r = rtv_to_u16(&p); r <<= 16; r |= rtv_to_u16(&p); *pp = p; return r; } u64 rtv_to_u64(unsigned char ** pp) { unsigned char * p; u64 r; p = *pp; r = rtv_to_u32(&p); r <<= 32; r |= rtv_to_u32(&p); *pp = p; return r; } static void clearstring(unsigned char * p) { while (*p) *p++ = 0; } static char * type_name(unsigned char type) { static char buf[32]; switch(type) { case 'C': return "Cable"; case 'S': return "Satellite"; case 'A': return "Antenna"; default: sprintf(buf, "Unknown '%c'", type); return buf; } } static void dump_zipcode2_record(unsigned char * buf) { static unsigned char zeros[124]; unsigned char * p; unsigned char headend[8]; unsigned char description[56]; unsigned char type; unsigned char extra_data[56]; p = buf; memcpy(headend, p, sizeof headend); p += sizeof headend; memcpy(description, p, sizeof description); p += sizeof description; type = *p; p += 4; memcpy(extra_data, p, sizeof extra_data); p += sizeof extra_data; expect(p - buf == 124); p = buf; clearstring(p); p += sizeof headend; clearstring(p); p += sizeof description; *p = '\0'; p += 4; clearstring(p); p += sizeof extra_data; expect(p - buf == 124); expect(memcmp(buf, zeros, 124) == 0); printf("%-8s %-40s %-10s %s\n", headend, description, type_name(type), extra_data); } int main(int argc, char ** argv) { unsigned char buffer[124]; while (fread(buffer, 124, 1, stdin)) dump_zipcode2_record(buffer); }