--- trunk/darkstattype.c 2014/03/21 18:35:50 14 +++ trunk/darkstattype.c 2014/03/21 18:57:27 15 @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -141,23 +142,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); @@ -169,10 +162,10 @@ 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); @@ -225,8 +218,8 @@ 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(); @@ -236,7 +229,7 @@ void decode_host_db_v1(void) 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); @@ -279,17 +272,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(); @@ -361,18 +354,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(); @@ -471,20 +464,20 @@ void decode_host_header_v3(void) void decode_host_header_v3(void) { - unsigned char addressfamily; - unsigned char ipv4address[4]; - unsigned short ipv6address[8]; - 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(); @@ -616,9 +609,9 @@ void decode_protos_data(void) void decode_protos_data(void) { - unsigned char ipprotocount; + uint8_t ipprotocount; - unsigned char i; + uint8_t i; ipprotocount = read8u(); @@ -628,9 +621,9 @@ void decode_protos_data(void) indent(); for (i = 0; i < ipprotocount; i++) { - unsigned char proto; - unsigned long in; - unsigned long out; + uint8_t proto; + uint64_t in; + uint64_t out; print_indentation(); printf("Protocol #%u of %u:\n", i + 1, ipprotocount); @@ -659,9 +652,9 @@ void decode_tcp_data(void) void decode_tcp_data(void) { - unsigned char tcpprotocount; + uint8_t tcpprotocount; - unsigned short i; + uint16_t i; tcpprotocount = read16u(); @@ -671,10 +664,10 @@ void decode_tcp_data(void) 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(); @@ -705,9 +698,9 @@ void decode_udp_data(void) void decode_udp_data(void) { - unsigned char udpprotocount; + uint8_t udpprotocount; - unsigned short i; + uint16_t i; udpprotocount = read16u(); @@ -717,9 +710,9 @@ void decode_udp_data(void) 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(); @@ -745,9 +738,9 @@ 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(); @@ -758,10 +751,10 @@ void decode_graph_db_v1(void) 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 #%u of 4:\n", i + 1); @@ -779,8 +772,8 @@ void decode_graph_db_v1(void) 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); @@ -808,13 +801,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 @@ -822,13 +813,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 @@ -840,13 +829,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 @@ -858,13 +845,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 @@ -876,13 +861,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 @@ -894,13 +877,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 @@ -912,19 +893,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 @@ -938,19 +917,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 @@ -979,11 +956,13 @@ void handle_file_error(void) } // 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) @@ -997,7 +976,7 @@ void print_indentation(void) void print_indentation(void) { - signed long long i; + int64_t i; for (i = 0; i < indentation; i++) { putchar(' ');