--- trunk/darkstattype.c 2014/03/20 09:49:12 9 +++ trunk/darkstattype.c 2014/04/09 09:20:17 19 @@ -1,11 +1,11 @@ -/* +/* -*- coding: utf-8 -*- darkstattype.c - Program to decode darkstat's dumpfile. $Ximalas$ Tested on FreeBSD/amd64 stable/9 r263290 with clang 3.3. - Copyright © 2014, Trond Endrestøl + Copyright © 2014, Trond Endrestøl All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -70,7 +71,8 @@ int main(int argc, char **argv) case '?': /*FALLTHROUGH*/ default: - fprintf(stderr, "%s: illegal option -%c\n\n", progname, optopt); + fprintf(stderr, "%s: illegal option -%c\n\n", + progname, optopt); show_usage(EXIT_FAILURE); break; } // switch @@ -78,22 +80,27 @@ int main(int argc, char **argv) argc -= optind; argv += optind; - if (argv[0] == NULL) { - fprintf(stderr, "%s: missing filename\n\n", progname); + if (*argv == NULL) { + fprintf(stderr, "%s: missing filename\n\n", + progname); show_usage(EXIT_FAILURE); } // if - filename = argv[0]; + while (*argv != NULL) { + filename = *argv; - decode_file(); + decode_file(); + argv++; + } // while + return EXIT_SUCCESS; } // main() void show_usage(int exitcode) { fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr, - "Usage: %s [-f] [-h] [-v] filename\n" + "Usage: %s [-f | -h | -v] filename [...]\n" " E.g.: %s darkstat.db\n\n" "Options:\n" @@ -111,7 +118,7 @@ void show_version(void) puts("$Ximalas$"); puts(""); - puts("Copyright © 2014, Trond Endrestøl "); + puts("Copyright © 2014, Trond Endrestøl "); puts("All rights reserved."); puts(""); puts("Redistribution and use in source and binary forms, with or without"); @@ -137,23 +144,15 @@ void show_version(void) exit(EXIT_SUCCESS); } // show_version() -/* -With clang 3.3 on FreeBSD/amd64 stable/9 r263290: +uint8_t read8u(void); + int8_t read8s(void); +uint16_t read16u(void); + int16_t read16s(void); +uint32_t read32u(void); + int32_t read32s(void); +uint64_t read64u(void); + int64_t read64s(void); -char = 1 byte = 8 bits -short = 2 bytes = 16 bits -int = 4 bytes = 32 bits -long = 8 bytes = 64 bits -*/ -unsigned char read8u(void); - signed char read8s(void); -unsigned short read16u(void); - signed short read16s(void); -unsigned int read32u(void); - signed int read32s(void); -unsigned long read64u(void); - signed long read64s(void); - void indent(void); void exdent(void); void print_indentation(void); @@ -165,20 +164,22 @@ void decode_file(void) void decode_host_db_v1(void); void decode_graph_db_v1(void); - unsigned int fileheader; - unsigned int sectionheader; + uint32_t fileheader; + uint32_t sectionheader; - unsigned int i; + uint32_t i; if ( (file = fopen(filename, "rb")) == NULL) { - fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno); + fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n", + progname, filename, strerror(errno), errno); exit(EXIT_FAILURE); } // if #define FILE_HEADER_V1 0xDA314159U if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format - fprintf(stderr, "%s:%s: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, FILE_HEADER_V1); + fprintf(stderr, "%s:%s:%ld: file header = 0x%x, not 0x%x\n", + progname, filename, ftell(file), fileheader, FILE_HEADER_V1); exit(EXIT_FAILURE); } // if @@ -202,12 +203,21 @@ void decode_file(void) decode_graph_db_v1(); } // else if else { - fprintf(stderr, "%s:%s: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, sectionheader, HOST_DB_V1, GRAPH_DB_V1); + fprintf(stderr, + "%s:%s:%ld: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", + progname, filename, ftell(file), sectionheader, HOST_DB_V1, + GRAPH_DB_V1); exit(EXIT_FAILURE); } // else } // for exdent(); + + if (fclose(file) != 0) { + fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", + progname, filename, strerror(errno), errno); + exit(EXIT_FAILURE); + } // if } // decode_file() void decode_host_db_v1(void) @@ -216,21 +226,23 @@ void decode_host_db_v1(void) void decode_host_header_v2(void); void decode_host_header_v3(void); - unsigned int hostcount; - unsigned int i; + uint32_t hostcount; + uint32_t i; indent(); hostcount = read32u(); print_indentation(); - printf("Host count %u\n", hostcount); + printf("Host count %u\n", + hostcount); for (i = 0; i < hostcount; i++) { - unsigned int hostheader; + uint32_t hostheader; print_indentation(); - printf("Host #%u of %u:\n", i + 1, hostcount); + printf("Host #%u of %u:\n", + i + 1, hostcount); indent(); @@ -240,21 +252,27 @@ void decode_host_db_v1(void) if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3 print_indentation(); - printf("Host header v3 0x%x\n", hostheader); + printf("Host header v3 0x%x\n", + hostheader); decode_host_header_v3(); } // if else if (hostheader == HOST_HEADER_V2) { // host header v2 print_indentation(); - printf("Host header v2 0x%x\n", hostheader); + printf("Host header v2 0x%x\n", + hostheader); decode_host_header_v2(); } // else if else if (hostheader == HOST_HEADER_V1) { // host header v1 print_indentation(); - printf("Host header v1 0x%x\n", hostheader); + printf("Host header v1 0x%x\n", + hostheader); decode_host_header_v1(); } // else if else { // unknown host header version - fprintf(stderr, "%s:%s: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n", progname, filename, hostheader, HOST_HEADER_V3, HOST_HEADER_V2, HOST_HEADER_V1); + fprintf(stderr, + "%s:%s:%ld: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n", + progname, filename, ftell(file), hostheader, HOST_HEADER_V3, + HOST_HEADER_V2, HOST_HEADER_V1); exit(EXIT_FAILURE); } // else @@ -270,17 +288,17 @@ void decode_host_header_v1(void) void decode_host_header_v1(void) { - unsigned char ipv4address[4]; - unsigned char macaddress[6]; - unsigned char hostnamelen; - unsigned char hostname[256]; - unsigned long bytesin; - unsigned long bytesout; - unsigned char protosdata; - unsigned char tcpdata; - unsigned char udpdata; + uint8_t ipv4address[4]; + uint8_t macaddress[6]; + uint8_t hostnamelen; + uint8_t hostname[256]; + uint64_t bytesin; + uint64_t bytesout; + uint8_t protosdata; + uint8_t tcpdata; + uint8_t udpdata; - unsigned char i; + uint8_t i; indent(); @@ -290,7 +308,7 @@ void decode_host_header_v1(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %d.%d.%d.%d\n", + printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); macaddress[0] = read8u(); @@ -301,12 +319,15 @@ void decode_host_header_v1(void) macaddress[5] = read8u(); print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); hostnamelen = read8u(); print_indentation(); - printf("Hostname length %d\n", hostnamelen); + printf("Hostname length %u\n", + hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -314,34 +335,43 @@ void decode_host_header_v1(void) hostname[i] = '\0'; print_indentation(); - printf("Hostname %s\n", hostname); + printf("Hostname %s\n", + hostname); bytesin = read64u(); print_indentation(); - printf("Bytes in %lu\n", bytesin); + printf("Bytes in %lu\n", + bytesin); bytesout = read64u(); print_indentation(); - printf("Bytes out %lu\n", bytesout); + printf("Bytes out %lu\n", + bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - fprintf(stderr, "%s: expecting character P, not %c\n", progname, protosdata); + fprintf(stderr, + "%s:%s:%ld: expecting character P, not %c\n", + progname, filename, ftell(file), protosdata); exit(EXIT_FAILURE); } // if decode_protos_data(); if ( (tcpdata = read8u()) != 'T') { // missing tcp data - fprintf(stderr, "%s: expecting character T, not %c\n", progname, tcpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character T, not %c\n", + progname, filename, ftell(file), tcpdata); exit(EXIT_FAILURE); } // if decode_tcp_data(); if ( (udpdata = read8u()) != 'U') { // missing udp data - fprintf(stderr, "%s: expecting character U, not %c\n", progname, udpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character U, not %c\n", + progname, filename, ftell(file), udpdata); exit(EXIT_FAILURE); } // if @@ -352,18 +382,18 @@ void decode_host_header_v2(void) void decode_host_header_v2(void) { - unsigned char ipv4address[4]; - unsigned char macaddress[6]; - signed long lastseen; - unsigned char hostnamelen; - unsigned char hostname[256]; - unsigned long bytesin; - unsigned long bytesout; - unsigned char protosdata; - unsigned char tcpdata; - unsigned char udpdata; + uint8_t ipv4address[4]; + uint8_t macaddress[6]; + int64_t lastseen; + uint8_t hostnamelen; + uint8_t hostname[256]; + uint64_t bytesin; + uint64_t bytesout; + uint8_t protosdata; + uint8_t tcpdata; + uint8_t udpdata; - unsigned char i; + uint8_t i; indent(); @@ -373,7 +403,7 @@ void decode_host_header_v2(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %d.%d.%d.%d\n", + printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); if (follow_specification == true) { @@ -385,12 +415,15 @@ void decode_host_header_v2(void) macaddress[5] = read8u(); print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); } // if @@ -398,7 +431,8 @@ void decode_host_header_v2(void) lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); @@ -410,13 +444,16 @@ void decode_host_header_v2(void) macaddress[5] = read8u(); print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); } // else hostnamelen = read8u(); print_indentation(); - printf("Hostname length %d\n", hostnamelen); + printf("Hostname length %hhu\n", + hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -424,34 +461,43 @@ void decode_host_header_v2(void) hostname[i] = '\0'; print_indentation(); - printf("Hostname %s\n", hostname); + printf("Hostname %s\n", + hostname); bytesin = read64u(); print_indentation(); - printf("Bytes in %lu\n", bytesin); + printf("Bytes in %lu\n", + bytesin); bytesout = read64u(); print_indentation(); - printf("Bytes out %lu\n", bytesout); + printf("Bytes out %lu\n", + bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - fprintf(stderr, "%s: expecting character P, not %c\n", progname, protosdata); + fprintf(stderr, + "%s:%s:%ld: expecting character P, not %c\n", + progname, filename, ftell(file), protosdata); exit(EXIT_FAILURE); } // if decode_protos_data(); if ( (tcpdata = read8u()) != 'T') { // missing tcp data - fprintf(stderr, "%s: expecting character T, not %c\n", progname, tcpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character T, not %c\n", + progname, filename, ftell(file), tcpdata); exit(EXIT_FAILURE); } // if decode_tcp_data(); if ( (udpdata = read8u()) != 'U') { // missing udp data - fprintf(stderr, "%s: expecting character U, not %c\n", progname, udpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character U, not %c\n", + progname, filename, ftell(file), udpdata); exit(EXIT_FAILURE); } // if @@ -462,26 +508,30 @@ void decode_host_header_v3(void) void decode_host_header_v3(void) { - unsigned char addressfamily; - unsigned char ipv4address[4]; - unsigned char ipv6address[16]; - unsigned char macaddress[6]; - signed long lastseen; - unsigned char hostnamelen; - unsigned char hostname[256]; - unsigned long bytesin; - unsigned long bytesout; - unsigned char protosdata; - unsigned char tcpdata; - unsigned char udpdata; + uint8_t addressfamily; + uint8_t ipv4address[4]; + uint16_t ipv6address[8]; + uint8_t macaddress[6]; + int64_t lastseen; + uint8_t hostnamelen; + uint8_t hostname[256]; + uint64_t bytesin; + uint64_t bytesout; + uint8_t protosdata; + uint8_t tcpdata; + uint8_t udpdata; - unsigned char i; + uint8_t i; indent(); - if ( (addressfamily = read8u()) == 0x04) { // IPv4 address +#define A_F_4 0x04 +#define A_F_6 0x06 + + if ( (addressfamily = read8u()) == A_F_4) { // IPv4 address print_indentation(); - printf("IPv4 address family (0x%02x)\n", addressfamily); + printf("IPv4 address family (0x%02hhx)\n", + addressfamily); ipv4address[0] = read8u(); ipv4address[1] = read8u(); @@ -489,51 +539,32 @@ void decode_host_header_v3(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %d.%d.%d.%d\n", + printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); } // if - else if (addressfamily == 0x06) { // IPv6 address + else if (addressfamily == A_F_6) { // IPv6 address print_indentation(); - printf("IPv6 address family (0x%02x)\n", addressfamily); + printf("IPv6 address family (0x%02hhx)\n", + addressfamily); - ipv6address[ 0] = read8u(); - ipv6address[ 1] = read8u(); - ipv6address[ 2] = read8u(); - ipv6address[ 3] = read8u(); - ipv6address[ 4] = read8u(); - ipv6address[ 5] = read8u(); - ipv6address[ 6] = read8u(); - ipv6address[ 7] = read8u(); - ipv6address[ 8] = read8u(); - ipv6address[ 9] = read8u(); - ipv6address[10] = read8u(); - ipv6address[11] = read8u(); - ipv6address[12] = read8u(); - ipv6address[13] = read8u(); - ipv6address[14] = read8u(); - ipv6address[15] = read8u(); + ipv6address[0] = read16u(); + ipv6address[1] = read16u(); + ipv6address[2] = read16u(); + ipv6address[3] = read16u(); + ipv6address[4] = read16u(); + ipv6address[5] = read16u(); + ipv6address[6] = read16u(); + ipv6address[7] = read16u(); print_indentation(); - printf("IPv6 address %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", - ipv6address[ 0], - ipv6address[ 1], - ipv6address[ 2], - ipv6address[ 3], - ipv6address[ 4], - ipv6address[ 5], - ipv6address[ 6], - ipv6address[ 7], - ipv6address[ 8], - ipv6address[ 9], - ipv6address[10], - ipv6address[11], - ipv6address[12], - ipv6address[13], - ipv6address[14], - ipv6address[15]); + printf("IPv6 address %04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx\n", + ipv6address[0], ipv6address[1], ipv6address[2], ipv6address[3], + ipv6address[4], ipv6address[5], ipv6address[6], ipv6address[7]); } // else if else { // unknown address family - fprintf(stderr, "%s:%s: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, addressfamily, 0x04, 0x06); + fprintf(stderr, + "%s:%s:%ld: unknown address family = 0x%hhx, neither 0x%x nor 0x%x\n", + progname, filename, ftell(file), addressfamily, A_F_4, A_F_6); exit(EXIT_FAILURE); } // else @@ -546,12 +577,15 @@ void decode_host_header_v3(void) macaddress[5] = read8u(); print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); } // if @@ -559,7 +593,8 @@ void decode_host_header_v3(void) lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); @@ -571,13 +606,16 @@ void decode_host_header_v3(void) macaddress[5] = read8u(); print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); } // else hostnamelen = read8u(); print_indentation(); - printf("Hostname length %d\n", hostnamelen); + printf("Hostname length %hhu\n", + hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -585,34 +623,43 @@ void decode_host_header_v3(void) hostname[i] = '\0'; print_indentation(); - printf("Hostname %s\n", hostname); + printf("Hostname %s\n", + hostname); bytesin = read64u(); print_indentation(); - printf("Bytes in %lu\n", bytesin); + printf("Bytes in %lu\n", + bytesin); bytesout = read64u(); print_indentation(); - printf("Bytes out %lu\n", bytesout); + printf("Bytes out %lu\n", + bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - fprintf(stderr, "%s: expecting character P, not %c\n", progname, protosdata); + fprintf(stderr, + "%s:%s:%ld: expecting character P, not %c\n", + progname, filename, ftell(file), protosdata); exit(EXIT_FAILURE); } // if decode_protos_data(); if ( (tcpdata = read8u()) != 'T') { // missing tcp data - fprintf(stderr, "%s: expecting character T, not %c\n", progname, tcpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character T, not %c\n", + progname, filename, ftell(file), tcpdata); exit(EXIT_FAILURE); } // if decode_tcp_data(); if ( (udpdata = read8u()) != 'U') { // missing udp data - fprintf(stderr, "%s: expecting character U, not %c\n", progname, udpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character U, not %c\n", + progname, filename, ftell(file), udpdata); exit(EXIT_FAILURE); } // if @@ -623,40 +670,45 @@ void decode_protos_data(void) void decode_protos_data(void) { - unsigned char ipprotocount; + uint8_t ipprotocount; - unsigned char u; + uint8_t i; ipprotocount = read8u(); print_indentation(); - printf("IP Proto count %d\n", ipprotocount); + printf("IP Proto count %hhu\n", + ipprotocount); indent(); - for (u = 0; u < ipprotocount; u++) { - unsigned char proto; - unsigned long in; - unsigned long out; + for (i = 0; i < ipprotocount; i++) { + uint8_t proto; + uint64_t in; + uint64_t out; print_indentation(); - printf("Protocol #%u of %u:\n", u + 1, ipprotocount); + printf("Protocol #%u of %hhu:\n", + i + 1, ipprotocount); proto = read8u(); indent(); print_indentation(); - printf("Protocol 0x%02x\n", proto); + printf("Protocol 0x%02hhx\n", + proto); in = read64u(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -666,43 +718,48 @@ void decode_tcp_data(void) void decode_tcp_data(void) { - unsigned char tcpprotocount; + uint8_t tcpprotocount; - unsigned short i; + uint16_t i; tcpprotocount = read16u(); print_indentation(); - printf("TCP proto count %d\n", tcpprotocount); + printf("TCP proto count %hhu\n", + tcpprotocount); indent(); for (i = 0; i < tcpprotocount; i++) { - unsigned short port; - unsigned long syn; - unsigned long in; - unsigned long out; + uint16_t port; + uint64_t syn; + uint64_t in; + uint64_t out; port = read16u(); print_indentation(); - printf("Port %d:\n", port); + printf("Port %hu:\n", + port); syn = read64u(); indent(); print_indentation(); - printf("SYN %lu\n", syn); + printf("SYN %lu\n", + syn); in = read64u(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -712,37 +769,41 @@ void decode_udp_data(void) void decode_udp_data(void) { - unsigned char udpprotocount; + uint8_t udpprotocount; - unsigned short i; + uint16_t i; udpprotocount = read16u(); print_indentation(); - printf("UDP proto count %d\n", udpprotocount); + printf("UDP proto count %hhu\n", + udpprotocount); indent(); for (i = 0; i < udpprotocount; i++) { - unsigned short port; - unsigned long in; - unsigned long out; + uint16_t port; + uint64_t in; + uint64_t out; port = read16u(); print_indentation(); - printf("Port %d:\n", port); + printf("Port %hu:\n", + port); in = read64u(); indent(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -752,56 +813,63 @@ void decode_graph_db_v1(void) void decode_graph_db_v1(void) { - signed long lasttime; + int64_t lasttime; - unsigned int i; + uint32_t i; lasttime = read64s(); indent(); print_indentation(); - printf("Last time 0x%lx = %ld = ", lasttime, lasttime); + printf("Last time 0x%lx = %ld = ", + lasttime, lasttime); print_time_t(lasttime); puts(""); for (i = 0; i < 4; i++) { - unsigned char nbars; - unsigned char idxlastbar; + uint8_t nbars; + uint8_t idxlastbar; - unsigned int j; + uint32_t j; print_indentation(); - printf("Graph #%d of 4:\n", i + 1); + printf("Graph #%u of 4:\n", + i + 1); nbars = read8u(); indent(); print_indentation(); - printf("Number of bars %u\n", nbars); + printf("Number of bars %hhu\n", + nbars); idxlastbar = read8u(); print_indentation(); - printf("Index of last bar %u\n", idxlastbar); + printf("Index of last bar %hhu\n", + idxlastbar); indent(); for (j = 0; j < idxlastbar; j++) { - unsigned long in; - unsigned long out; + uint64_t in; + uint64_t out; print_indentation(); - printf("Bar #%u of %u:\n", j + 1, idxlastbar); + printf("Bar #%u of %hhu:\n", + j + 1, idxlastbar); in = read64u(); indent(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -815,13 +883,11 @@ void handle_file_error(void); void handle_file_error(void); -unsigned char read8u(void) +uint8_t read8u(void) { size_t r; - unsigned char v; + uint8_t v; - //fprintf(stderr, "%s: sizeof(unsigned char) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -829,13 +895,11 @@ unsigned char read8u(void) return v; } // read8u() -signed char read8s(void) +int8_t read8s(void) { size_t r; - signed char v; + int8_t v; - //fprintf(stderr, "%s: sizeof(signed char) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -847,13 +911,11 @@ signed char read8s(void) #include #endif -unsigned short read16u(void) +uint16_t read16u(void) { size_t r; - unsigned short v; + uint16_t v; - //fprintf(stderr, "%s: sizeof(unsigned short) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -865,13 +927,11 @@ unsigned short read16u(void) return v; } // read16u() -signed short read16s(void) +int16_t read16s(void) { size_t r; - signed short v; + int16_t v; - //fprintf(stderr, "%s: sizeof(signed short) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -883,13 +943,11 @@ signed short read16s(void) return v; } // read16s() -unsigned int read32u(void) +uint32_t read32u(void) { size_t r; - unsigned int v; + uint32_t v; - //fprintf(stderr, "%s: sizeof(unsigned int) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -901,13 +959,11 @@ unsigned int read32u(void) return v; } // read32u() -signed int read32s(void) +int32_t read32s(void) { size_t r; - signed int v; + int32_t v; - //fprintf(stderr, "%s: sizeof(signed int) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -919,19 +975,17 @@ signed int read32s(void) return v; } // read32s() -unsigned long read64u(void) +uint64_t read64u(void) { size_t r; - unsigned long v; + uint64_t v; #ifdef __LITTLE_ENDIAN__ - unsigned long tmp; - unsigned int *p1 = (unsigned int *)&v; - unsigned int *p2 = (unsigned int *)&tmp; + uint64_t tmp; + uint32_t *p1 = (uint32_t *)&v; + uint32_t *p2 = (uint32_t *)&tmp; #endif - //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -945,19 +999,17 @@ unsigned long read64u(void) return v; } // read64u() -signed long read64s(void) +int64_t read64s(void) { size_t r; - signed long v; + int64_t v; #ifdef __LITTLE_ENDIAN__ - signed long tmp; - signed int *p1 = (signed int *)&v; - signed int *p2 = (signed int *)&tmp; + int64_t tmp; + int32_t *p1 = (int32_t *)&v; + int32_t *p2 = (int32_t *)&tmp; #endif - //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v)); - if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { handle_file_error(); } // if @@ -973,22 +1025,31 @@ void handle_file_error(void) void handle_file_error(void) { + int saved_errno = errno; + if (feof(file) != 0) { - fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename); + fprintf(stderr, + "%s:%s:%ld: premature end-of-file\n", + progname, filename, ftell(file)); exit(EXIT_FAILURE); } // if if (ferror(file) != 0) { - fprintf(stderr, "%s:%s: file error, errno = %s (%d)\n", progname, filename, strerror(errno), errno); + fprintf(stderr, + "%s:%s:%ld: file error, errno = %s (%d)\n", + progname, filename, ftell(file), + strerror(saved_errno), saved_errno); exit(EXIT_FAILURE); } // if } // handle_file_error() -signed long long indentation = 0LL; +int64_t indentation = 0LL; void indent(void) { - indentation += 2LL; + if (indentation < (INT64_MAX - 2LL)) { + indentation += 2LL; + } // if } // indent() void exdent(void) @@ -1002,7 +1063,7 @@ void print_indentation(void) void print_indentation(void) { - signed long long i; + int64_t i; for (i = 0; i < indentation; i++) { putchar(' '); @@ -1018,7 +1079,12 @@ void print_time_t(time_t t) // ISO 8601 format if (strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S%z", stm) == 0) { - fprintf(stderr, "%s:%s: strftime() error, errno = %s (%d)\n", progname, filename, strerror(errno), errno); + int saved_errno = errno; + + fprintf(stderr, + "%s:%s:%ld: strftime() error, errno = %s (%d)\n", + progname, filename, ftell(file), + strerror(saved_errno), saved_errno); exit(EXIT_FAILURE); } // if