/[darkstattype]/trunk/darkstattype.c
ViewVC logotype

Diff of /trunk/darkstattype.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 3 by trond, 2014-03-19T08:49:18Z Revision 8 by trond, 2014-03-20T09:00:33Z
# Line 17  Line 17 
17      this list of conditions and the following disclaimer in the documentation      this list of conditions and the following disclaimer in the documentation
18      and/or other materials provided with the distribution.      and/or other materials provided with the distribution.
19    
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */  */
31    
32    // Define FOLLOW_SPECIFICATION to 0 (zero) to go with darkstat's own format as of 3.0.718.
33    #define FOLLOW_SPECIFICATION 0
34    
35    // Define FOLLOW_SPECIFICATION to 1 (one) to go with the format specified in the export-format.txt file.
36    //#define FOLLOW_SPECIFICATION 1
37    
38  #include <errno.h>  #include <errno.h>
39  #include <stdio.h>  #include <stdio.h>
40  #include <stdlib.h>  #include <stdlib.h>
41  #include <string.h>  #include <string.h>
42  #include <time.h>  #include <time.h>
43  #include <unistd.h>  #include <unistd.h>
44    
45  const char * progname = NULL;  const char *progname = NULL;
46  const char * filename = NULL;  const char *filename = NULL;
47  FILE *file = NULL;  FILE *file = NULL;
48    
49  int main(int argc, char **argv)  int main(int argc, char **argv)
50  {  {
51    void show_usage(int exitcode);    void show_usage(int exitcode);
52    void show_version(void);    void show_version(void);
53    void decode_file(void);    void decode_file(void);
54    
55    int i;    int i;
56    
57    progname = argv[0];    progname = argv[0];
58    
59    opterr = 0;    opterr = 0;
60    while ( (i = getopt(argc, argv, "hv")) != -1) {    while ( (i = getopt(argc, argv, "hv")) != -1) {
61      switch (i) {      switch (i) {
# Line 76  void show_usage(int exitcode) Line 82  void show_usage(int exitcode)
82      fprintf(stderr, "%s: missing filename\n\n", progname);      fprintf(stderr, "%s: missing filename\n\n", progname);
83      show_usage(EXIT_FAILURE);      show_usage(EXIT_FAILURE);
84    } // if    } // if
85    
86    filename = argv[0];    filename = argv[0];
87    
88    decode_file();    decode_file();
89    
90    return EXIT_SUCCESS;    return EXIT_SUCCESS;
91  } // main()  } // main()
92    
93  void show_usage(int exitcode)  void show_usage(int exitcode)
94  {  {
95    fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr,    fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr,
96      "Usage: %s [-h] [-v] filename\n"      "Usage: %s [-h] [-v] filename\n"
97      " E.g.: %s darkstat.db\n",      " E.g.: %s darkstat.db\n\n"
98    
99        "Options:\n"
100        "-h\tShow this help message and exit.\n"
101        "-v\tShow version and copyright and exit.\n",
102      progname, progname);      progname, progname);
103    
104    exit(exitcode);    exit(exitcode);
105  } // show_usage()  } // show_usage()
106    
107  void show_version(void)  void show_version(void)
108  {  {
109    puts("darkstattype 1.0");    puts("darkstattype 1.0");
110      puts("$Ximalas$");
111    puts("");    puts("");
112    
113    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");
114    puts("All rights reserved.");    puts("All rights reserved.");
115    puts("");    puts("");
116    puts("Redistribution and use in source and binary forms, with or without");    puts("Redistribution and use in source and binary forms, with or without");
117    puts("modification, are permitted provided that the following conditions are met:");    puts("modification, are permitted provided that the following conditions are met:");
118    puts("");    puts("");
119    puts("1. Redistributions of source code must retain the above copyright notice, this");    puts("1. Redistributions of source code must retain the above copyright notice, this");
120    puts("   list of conditions and the following disclaimer.");    puts("   list of conditions and the following disclaimer.");
121    puts("2. Redistributions in binary form must reproduce the above copyright notice,");    puts("2. Redistributions in binary form must reproduce the above copyright notice,");
122    puts("   this list of conditions and the following disclaimer in the documentation");    puts("   this list of conditions and the following disclaimer in the documentation");
123    puts("   and/or other materials provided with the distribution.");    puts("   and/or other materials provided with the distribution.");
124    puts("");    puts("");
125    puts("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND");    puts("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND");
# Line 151  void decode_file(void) Line 162  void decode_file(void)
162  void decode_file(void)  void decode_file(void)
163  {  {
164    void decode_host_db_v1(void);    void decode_host_db_v1(void);
165    void decode_graph_db_v1(void);    void decode_graph_db_v1(void);
166    
167    unsigned int fileheader;    unsigned int fileheader;
168    unsigned int sectionheader;    unsigned int sectionheader;
169    
170    unsigned int i;    unsigned int i;
171    
172    if ( (file = fopen(filename, "rb")) == NULL) {    if ( (file = fopen(filename, "rb")) == NULL) {
173      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);
174      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
175    } // if    } // if
176    
177    if ( (fileheader = read32u()) != 0xDA314159U) { // not darkstat export format  #define FILE_HEADER_V1 0xDA314159U
178      fprintf(stderr, "%s:%s: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, 0xDA314159U);  
179      if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format
180        fprintf(stderr, "%s:%s: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, FILE_HEADER_V1);
181      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
182    } // if    } // if
183    
184    printf("File header 0x%x\n", fileheader);    printf("File header 0x%x\n", fileheader);
185    
186    // Possible section header for host_db v1 and later graph_db v1.    // Possible section header for host_db v1 and later graph_db v1.
187    indent();    indent();
188    
189    #define HOST_DB_V1 0xDA485301U
190    #define GRAPH_DB_V1 0xDA475201U
191    
192    for (i = 0; i < 2; i++) {    for (i = 0; i < 2; i++) {
193      if ( (sectionheader = read32u()) == 0xDA485301U) {      if ( (sectionheader = read32u()) == HOST_DB_V1) {
194        print_indentation();        print_indentation();
195        printf("Section header 0x%x\n", sectionheader);        printf("Section header host_db v1 0x%x\n", sectionheader);
196        decode_host_db_v1();        decode_host_db_v1();
197      } // if      } // if
198      else if (sectionheader == 0xDA475201U) {      else if (sectionheader == GRAPH_DB_V1) {
199        print_indentation();        print_indentation();
200        printf("Section header 0x%x\n", sectionheader);        printf("Section header graph_db v1 0x%x\n", sectionheader);
201        decode_graph_db_v1();        decode_graph_db_v1();
202      } // else if      } // else if
203      else {      else {
204        fprintf(stderr, "%s:%s: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, sectionheader, 0xDA485301U, 0xDA475201U);        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);
205        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
206      } // else      } // else
207    } // for    } // for
208    
209    exdent();    exdent();
210  } // decode_file()  } // decode_file()
211    
212  void decode_host_db_v1(void)  void decode_host_db_v1(void)
213  {  {
214    void decode_host_header_v1(void);    void decode_host_header_v1(void);
215    void decode_host_header_v2(void);    void decode_host_header_v2(void);
216    void decode_host_header_v3(void);    void decode_host_header_v3(void);
217    
218    unsigned int hostcount;    unsigned int hostcount;
219    unsigned int i;    unsigned int i;
# Line 205  void decode_host_db_v1(void) Line 221  void decode_host_db_v1(void)
221    indent();    indent();
222    
223    hostcount = read32u();    hostcount = read32u();
224    
225    print_indentation();    print_indentation();
226    printf("Host count %u\n", hostcount);    printf("Host count %u\n", hostcount);
227    
228    for (i = 0; i < hostcount; i++) {    for (i = 0; i < hostcount; i++) {
229      unsigned int hostheader;      unsigned int hostheader;
230    
231      print_indentation();      print_indentation();
232      printf("Host #%u of %u:\n", i + 1, hostcount);      printf("Host #%u of %u:\n", i + 1, hostcount);
233    
234      indent();      indent();
235    
236      if ( (hostheader = read32u()) == 0x48535403U) { // host header v3  #define HOST_HEADER_V3 0x48535403U
237    #define HOST_HEADER_V2 0x48535402U
238    #define HOST_HEADER_V1 0x48535401U
239    
240        if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3
241        print_indentation();        print_indentation();
242        printf("Host header v3 0x%x\n", hostheader);        printf("Host header v3 0x%x\n", hostheader);
243        decode_host_header_v3();        decode_host_header_v3();
244      } // if      } // if
245      else if (hostheader == 0x48535402U) { // host header v2      else if (hostheader == HOST_HEADER_V2) { // host header v2
246        print_indentation();        print_indentation();
247        printf("Host header v2 0x%x\n", hostheader);        printf("Host header v2 0x%x\n", hostheader);
248        decode_host_header_v2();        decode_host_header_v2();
249      } // else if      } // else if
250      else if (hostheader == 0x48535401U) { // host header v1      else if (hostheader == HOST_HEADER_V1) { // host header v1
251        print_indentation();        print_indentation();
252        printf("Host header v1 0x%x\n", hostheader);        printf("Host header v1 0x%x\n", hostheader);
253        decode_host_header_v1();        decode_host_header_v1();
254      } // else if      } // else if
255      else { // unknown host header version      else { // unknown host header version
256        fprintf(stderr, "%s:%s: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n", progname, filename, hostheader, 0x48535403U, 0x48535402U, 0x48535401U);        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);
257        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
258      } // else      } // else
259    
260      exdent();      exdent();
261    } // for    } // for
262    
263    exdent();    exdent();
264  } // decode_host_db_v1()  } // decode_host_db_v1()
265    
266  void decode_protos_data(void);  void decode_protos_data(void);
267  void decode_tcp_data(void);  void decode_tcp_data(void);
268  void decode_udp_data(void);  void decode_udp_data(void);
269    
270  void decode_host_header_v1(void)  void decode_host_header_v1(void)
271  {  {
# Line 343  void decode_host_header_v2(void) Line 363  void decode_host_header_v2(void)
363    unsigned char udpdata;    unsigned char udpdata;
364    
365    unsigned char i;    unsigned char i;
366    
367    indent();    indent();
368    
369    ipv4address[0] = read8u();    ipv4address[0] = read8u();
370    ipv4address[1] = read8u();    ipv4address[1] = read8u();
371    ipv4address[2] = read8u();    ipv4address[2] = read8u();
372    ipv4address[3] = read8u();    ipv4address[3] = read8u();
373    
374    print_indentation();    print_indentation();
375    printf("IPv4 address %d.%d.%d.%d\n",    printf("IPv4 address %d.%d.%d.%d\n",
376           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
377    
378    #if FOLLOW_SPECIFICATION == 1
379    macaddress[0] = read8u();    macaddress[0] = read8u();
380    macaddress[1] = read8u();    macaddress[1] = read8u();
381    macaddress[2] = read8u();    macaddress[2] = read8u();
382    macaddress[3] = read8u();    macaddress[3] = read8u();
383    macaddress[4] = read8u();    macaddress[4] = read8u();
384    macaddress[5] = read8u();    macaddress[5] = read8u();
385    
386    print_indentation();    print_indentation();
387    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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
388    
389    lastseen = read64s();    lastseen = read64s();
390    
391    print_indentation();    print_indentation();
392    printf("Last seen %ld = ", lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
393    print_time_t(lastseen);    print_time_t(lastseen);
394    puts("");    puts("");
395    #else
396      lastseen = read64s();
397    
398      print_indentation();
399      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
400      print_time_t(lastseen);
401      puts("");
402    
403      macaddress[0] = read8u();
404      macaddress[1] = read8u();
405      macaddress[2] = read8u();
406      macaddress[3] = read8u();
407      macaddress[4] = read8u();
408      macaddress[5] = read8u();
409    
410      print_indentation();
411      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
412    #endif
413    
414    hostnamelen = read8u();    hostnamelen = read8u();
415    
416    print_indentation();    print_indentation();
417    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
418    
419    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
420      hostname[i] = read8u();      hostname[i] = read8u();
421    } // for    } // for
422    hostname[i] = '\0';    hostname[i] = '\0';
423    
424    print_indentation();    print_indentation();
425    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
426    
427    bytesin = read64u();    bytesin = read64u();
428    
# Line 484  void decode_host_header_v3(void) Line 523  void decode_host_header_v3(void)
523             ipv6address[ 7],             ipv6address[ 7],
524             ipv6address[ 8],             ipv6address[ 8],
525             ipv6address[ 9],             ipv6address[ 9],
526             ipv6address[10],             ipv6address[10],
527             ipv6address[11],             ipv6address[11],
528             ipv6address[12],             ipv6address[12],
529             ipv6address[13],             ipv6address[13],
530             ipv6address[14],             ipv6address[14],
531             ipv6address[15]);             ipv6address[15]);
532    } // else if    } // else if
533    else { // unknown address family    else { // unknown address family
534      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: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, addressfamily, 0x04, 0x06);
535      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
536    } // else    } // else
537    
538    #if FOLLOW_SPECIFICATION == 1
539    macaddress[0] = read8u();    macaddress[0] = read8u();
540    macaddress[1] = read8u();    macaddress[1] = read8u();
541    macaddress[2] = read8u();    macaddress[2] = read8u();
542    macaddress[3] = read8u();    macaddress[3] = read8u();
543    macaddress[4] = read8u();    macaddress[4] = read8u();
544    macaddress[5] = read8u();    macaddress[5] = read8u();
545    
546    print_indentation();    print_indentation();
547    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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
548    
549    lastseen = read64s();    lastseen = read64s();
550    
551    print_indentation();    print_indentation();
552    printf("Last seen %ld = ", lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
553    print_time_t(lastseen);    print_time_t(lastseen);
554    puts("");    puts("");
555    #else
556      lastseen = read64s();
557    
558      print_indentation();
559      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
560      print_time_t(lastseen);
561      puts("");
562    
563      macaddress[0] = read8u();
564      macaddress[1] = read8u();
565      macaddress[2] = read8u();
566      macaddress[3] = read8u();
567      macaddress[4] = read8u();
568      macaddress[5] = read8u();
569    
570      print_indentation();
571      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
572    #endif
573    
574    hostnamelen = read8u();    hostnamelen = read8u();
575    
576    print_indentation();    print_indentation();
577    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
578    
579    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
580      hostname[i] = read8u();      hostname[i] = read8u();
581    } // for    } // for
582    hostname[i] = '\0';    hostname[i] = '\0';
583    
584    print_indentation();    print_indentation();
585    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
586    
587    bytesin = read64u();    bytesin = read64u();
588    
# Line 573  void decode_protos_data(void) Line 631  void decode_protos_data(void)
631    
632    indent();    indent();
633    
634    for (u = 0; u < ipprotocount; u++) {    for (u = 0; u < ipprotocount; u++) {
635      unsigned char proto;      unsigned char proto;
636      unsigned long in;      unsigned long in;
637      unsigned long out;      unsigned long out;
638    
639      print_indentation();      print_indentation();
640      printf("Protocol #%u of %u:\n", u + 1, ipprotocount);      printf("Protocol #%u of %u:\n", u + 1, ipprotocount);
641    
642      proto = read8u();      proto = read8u();
643    
644      indent();      indent();
645      print_indentation();      print_indentation();
646      printf("Protocol 0x%x\n", proto);      printf("Protocol 0x%02x\n", proto);
647    
648      in = read64u();      in = read64u();
649    
650      print_indentation();      print_indentation();
651      printf("In %lu\n", in);      printf("In %lu\n", in);
652    
653      out = read64u();      out = read64u();
654    
655      print_indentation();      print_indentation();
656      printf("Out %lu\n", out);      printf("Out %lu\n", out);
657    
658      exdent();      exdent();
659    } // for    } // for
660    
661    exdent();    exdent();
# Line 687  void decode_graph_db_v1(void) Line 745  void decode_graph_db_v1(void)
745    } // for    } // for
746    
747    exdent();    exdent();
748  } // decode_udp_data()  } // decode_udp_data()
749    
750  void decode_graph_db_v1(void)  void decode_graph_db_v1(void)
751  {  {
752    signed long lasttime;    signed long lasttime;
753    
754    unsigned int i;    unsigned int i;
755    
756    lasttime = read64s();    lasttime = read64s();
757    
758    indent();    indent();
759    print_indentation();    print_indentation();
760    printf("Last time %ld = ", lasttime);    printf("Last time 0x%lx = %ld = ", lasttime, lasttime);
761    print_time_t(lasttime);    print_time_t(lasttime);
762    puts("");    puts("");
763    
764    for (i = 0; i < 4; i++) {    for (i = 0; i < 4; i++) {
765      unsigned char nbars;      unsigned char nbars;
766      unsigned char idxlastbar;      unsigned char idxlastbar;
767    
768      unsigned int j;      unsigned int j;
769    
770      print_indentation();      print_indentation();
771      printf("Graph #%d of 4:\n", i + 1);      printf("Graph #%d of 4:\n", i + 1);
772    
773      nbars = read8u();      nbars = read8u();
774    
775      indent();      indent();
# Line 850  unsigned long read64u(void) Line 908  unsigned long read64u(void)
908    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
909      handle_file_error();      handle_file_error();
910    } // if    } // if
911    
912  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
913    v = ntohl(v);    v = ntohl(v);
914  #endif  #endif
915    
916    return v;    return v;
917  } // read32s()  } // read32s()
918    
919  unsigned long read64u(void)  unsigned long read64u(void)
920  {  {
921    size_t r;    size_t r;
922    unsigned long v;    unsigned long v;
   unsigned int *p = (unsigned int *)&v;  
923    
924    #ifdef __LITTLE_ENDIAN__
925      unsigned long tmp;
926      unsigned int *p1 = (unsigned int *)&v;
927      unsigned int *p2 = (unsigned int *)&tmp;
928    #endif
929    
930    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));
931    
932    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
933      handle_file_error();      handle_file_error();
934    } // if    } // if
935    
936  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
937    p[0] = ntohl((unsigned int)p[0]);    p2[1] = ntohl(p1[0]);
938    p[1] = ntohl((unsigned int)p[1]);    p2[0] = ntohl(p1[1]);
939      v = tmp;
940  #endif  #endif
941    
942    return v;    return v;
943  } // read64u()  } // read64u()
944    
945  signed long read64s(void)  signed long read64s(void)
946  {  {
947    size_t r;    size_t r;
948    signed long v;    signed long v;
   signed int *p = (signed int *)&v;  
949    
950    #ifdef __LITTLE_ENDIAN__
951      signed long tmp;
952      signed int *p1 = (signed int *)&v;
953      signed int *p2 = (signed int *)&tmp;
954    #endif
955    
956    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));
957    
958    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
959      handle_file_error();      handle_file_error();
960    } // if    } // if
961    
962  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
963    p[0] = ntohl((signed int)p[0]);    p2[1] = ntohl(p1[0]);
964    p[1] = ntohl((signed int)p[1]);    p2[0] = ntohl(p1[1]);
965      v = tmp;
966  #endif  #endif
967    
968    return v;    return v;
969  } // read64s()  } // read64s()
970    
971  void handle_file_error(void)  void handle_file_error(void)
972  {  {
973    if (feof(file) != 0) {    if (feof(file) != 0) {
974      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);
975      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
976    } // if    } // if
977    
978    if (ferror(file) != 0) {    if (ferror(file) != 0) {
979      fprintf(stderr, "%s:%s: file error, errno = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s:%s: file error, errno = %s (%d)\n", progname, filename, strerror(errno), errno);
980      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

svn@ximalas.info
ViewVC Help
Powered by ViewVC 1.3.0-dev