/[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 7 by trond, 2014-03-19T15:40:13Z
# 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 85  void show_version(void) Line 91  void show_version(void)
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",
98      progname, progname);      progname, progname);
99    
100    exit(exitcode);    exit(exitcode);
101  } // show_usage()  } // show_usage()
102    
103  void show_version(void)  void show_version(void)
104  {  {
105    puts("darkstattype 1.0");    puts("darkstattype 1.0");
106      puts("$Ximalas$");
107    puts("");    puts("");
108    
109    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");
110    puts("All rights reserved.");    puts("All rights reserved.");
111    puts("");    puts("");
112    puts("Redistribution and use in source and binary forms, with or without");    puts("Redistribution and use in source and binary forms, with or without");
113    puts("modification, are permitted provided that the following conditions are met:");    puts("modification, are permitted provided that the following conditions are met:");
114    puts("");    puts("");
115    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");
116    puts("   list of conditions and the following disclaimer.");    puts("   list of conditions and the following disclaimer.");
117    puts("2. Redistributions in binary form must reproduce the above copyright notice,");    puts("2. Redistributions in binary form must reproduce the above copyright notice,");
118    puts("   this list of conditions and the following disclaimer in the documentation");    puts("   this list of conditions and the following disclaimer in the documentation");
119    puts("   and/or other materials provided with the distribution.");    puts("   and/or other materials provided with the distribution.");
120    puts("");    puts("");
121    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 158  void decode_file(void)
158  void decode_file(void)  void decode_file(void)
159  {  {
160    void decode_host_db_v1(void);    void decode_host_db_v1(void);
161    void decode_graph_db_v1(void);    void decode_graph_db_v1(void);
162    
163    unsigned int fileheader;    unsigned int fileheader;
164    unsigned int sectionheader;    unsigned int sectionheader;
165    
166    unsigned int i;    unsigned int i;
167    
168    if ( (file = fopen(filename, "rb")) == NULL) {    if ( (file = fopen(filename, "rb")) == NULL) {
169      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);
170      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
171    } // if    } // if
172    
173    if ( (fileheader = read32u()) != 0xDA314159U) { // not darkstat export format  #define FILE_HEADER_V1 0xDA314159U
174      fprintf(stderr, "%s:%s: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, 0xDA314159U);  
175      if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format
176        fprintf(stderr, "%s:%s: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, FILE_HEADER_V1);
177      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
178    } // if    } // if
179    
180    printf("File header 0x%x\n", fileheader);    printf("File header 0x%x\n", fileheader);
181    
182    // Possible section header for host_db v1 and later graph_db v1.    // Possible section header for host_db v1 and later graph_db v1.
183    indent();    indent();
184    
185    #define HOST_DB_V1 0xDA485301U
186    #define GRAPH_DB_V1 0xDA475201U
187    
188    for (i = 0; i < 2; i++) {    for (i = 0; i < 2; i++) {
189      if ( (sectionheader = read32u()) == 0xDA485301U) {      if ( (sectionheader = read32u()) == HOST_DB_V1) {
190        print_indentation();        print_indentation();
191        printf("Section header 0x%x\n", sectionheader);        printf("Section header host_db v1 0x%x\n", sectionheader);
192        decode_host_db_v1();        decode_host_db_v1();
193      } // if      } // if
194      else if (sectionheader == 0xDA475201U) {      else if (sectionheader == GRAPH_DB_V1) {
195        print_indentation();        print_indentation();
196        printf("Section header 0x%x\n", sectionheader);        printf("Section header graph_db v1 0x%x\n", sectionheader);
197        decode_graph_db_v1();        decode_graph_db_v1();
198      } // else if      } // else if
199      else {      else {
200        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);
201        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
202      } // else      } // else
203    } // for    } // for
204    
205    exdent();    exdent();
206  } // decode_file()  } // decode_file()
207    
208  void decode_host_db_v1(void)  void decode_host_db_v1(void)
209  {  {
210    void decode_host_header_v1(void);    void decode_host_header_v1(void);
211    void decode_host_header_v2(void);    void decode_host_header_v2(void);
212    void decode_host_header_v3(void);    void decode_host_header_v3(void);
213    
214    unsigned int hostcount;    unsigned int hostcount;
215    unsigned int i;    unsigned int i;
# Line 205  void decode_host_db_v1(void) Line 217  void decode_host_db_v1(void)
217    indent();    indent();
218    
219    hostcount = read32u();    hostcount = read32u();
220    
221    print_indentation();    print_indentation();
222    printf("Host count %u\n", hostcount);    printf("Host count %u\n", hostcount);
223    
224    for (i = 0; i < hostcount; i++) {    for (i = 0; i < hostcount; i++) {
225      unsigned int hostheader;      unsigned int hostheader;
226    
227      print_indentation();      print_indentation();
228      printf("Host #%u of %u:\n", i + 1, hostcount);      printf("Host #%u of %u:\n", i + 1, hostcount);
229    
230      indent();      indent();
231    
232      if ( (hostheader = read32u()) == 0x48535403U) { // host header v3  #define HOST_HEADER_V3 0x48535403U
233    #define HOST_HEADER_V2 0x48535402U
234    #define HOST_HEADER_V1 0x48535401U
235    
236        if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3
237        print_indentation();        print_indentation();
238        printf("Host header v3 0x%x\n", hostheader);        printf("Host header v3 0x%x\n", hostheader);
239        decode_host_header_v3();        decode_host_header_v3();
240      } // if      } // if
241      else if (hostheader == 0x48535402U) { // host header v2      else if (hostheader == HOST_HEADER_V2) { // host header v2
242        print_indentation();        print_indentation();
243        printf("Host header v2 0x%x\n", hostheader);        printf("Host header v2 0x%x\n", hostheader);
244        decode_host_header_v2();        decode_host_header_v2();
245      } // else if      } // else if
246      else if (hostheader == 0x48535401U) { // host header v1      else if (hostheader == HOST_HEADER_V1) { // host header v1
247        print_indentation();        print_indentation();
248        printf("Host header v1 0x%x\n", hostheader);        printf("Host header v1 0x%x\n", hostheader);
249        decode_host_header_v1();        decode_host_header_v1();
250      } // else if      } // else if
251      else { // unknown host header version      else { // unknown host header version
252        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);
253        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
254      } // else      } // else
255    
256      exdent();      exdent();
257    } // for    } // for
258    
259    exdent();    exdent();
260  } // decode_host_db_v1()  } // decode_host_db_v1()
261    
262  void decode_protos_data(void);  void decode_protos_data(void);
263  void decode_tcp_data(void);  void decode_tcp_data(void);
264  void decode_udp_data(void);  void decode_udp_data(void);
265    
266  void decode_host_header_v1(void)  void decode_host_header_v1(void)
267  {  {
# Line 343  void decode_host_header_v2(void) Line 359  void decode_host_header_v2(void)
359    unsigned char udpdata;    unsigned char udpdata;
360    
361    unsigned char i;    unsigned char i;
362    
363    indent();    indent();
364    
365    ipv4address[0] = read8u();    ipv4address[0] = read8u();
366    ipv4address[1] = read8u();    ipv4address[1] = read8u();
367    ipv4address[2] = read8u();    ipv4address[2] = read8u();
368    ipv4address[3] = read8u();    ipv4address[3] = read8u();
369    
370    print_indentation();    print_indentation();
371    printf("IPv4 address %d.%d.%d.%d\n",    printf("IPv4 address %d.%d.%d.%d\n",
372           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
373    
374    #if FOLLOW_SPECIFICATION == 1
375    macaddress[0] = read8u();    macaddress[0] = read8u();
376    macaddress[1] = read8u();    macaddress[1] = read8u();
377    macaddress[2] = read8u();    macaddress[2] = read8u();
378    macaddress[3] = read8u();    macaddress[3] = read8u();
379    macaddress[4] = read8u();    macaddress[4] = read8u();
380    macaddress[5] = read8u();    macaddress[5] = read8u();
381    
382    print_indentation();    print_indentation();
383    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]);
384    
385    lastseen = read64s();    lastseen = read64s();
386    
387    print_indentation();    print_indentation();
388    printf("Last seen %ld = ", lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
389    print_time_t(lastseen);    print_time_t(lastseen);
390    puts("");    puts("");
391    #else
392      lastseen = read64s();
393    
394      print_indentation();
395      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
396      print_time_t(lastseen);
397      puts("");
398    
399      macaddress[0] = read8u();
400      macaddress[1] = read8u();
401      macaddress[2] = read8u();
402      macaddress[3] = read8u();
403      macaddress[4] = read8u();
404      macaddress[5] = read8u();
405    
406      print_indentation();
407      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
408    #endif
409    
410    hostnamelen = read8u();    hostnamelen = read8u();
411    
412    print_indentation();    print_indentation();
413    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
414    
415    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
416      hostname[i] = read8u();      hostname[i] = read8u();
417    } // for    } // for
418    hostname[i] = '\0';    hostname[i] = '\0';
419    
420    print_indentation();    print_indentation();
421    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
422    
423    bytesin = read64u();    bytesin = read64u();
424    
# Line 484  void decode_host_header_v3(void) Line 519  void decode_host_header_v3(void)
519             ipv6address[ 7],             ipv6address[ 7],
520             ipv6address[ 8],             ipv6address[ 8],
521             ipv6address[ 9],             ipv6address[ 9],
522             ipv6address[10],             ipv6address[10],
523             ipv6address[11],             ipv6address[11],
524             ipv6address[12],             ipv6address[12],
525             ipv6address[13],             ipv6address[13],
526             ipv6address[14],             ipv6address[14],
527             ipv6address[15]);             ipv6address[15]);
528    } // else if    } // else if
529    else { // unknown address family    else { // unknown address family
530      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);
531      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
532    } // else    } // else
533    
534    #if FOLLOW_SPECIFICATION == 1
535    macaddress[0] = read8u();    macaddress[0] = read8u();
536    macaddress[1] = read8u();    macaddress[1] = read8u();
537    macaddress[2] = read8u();    macaddress[2] = read8u();
538    macaddress[3] = read8u();    macaddress[3] = read8u();
539    macaddress[4] = read8u();    macaddress[4] = read8u();
540    macaddress[5] = read8u();    macaddress[5] = read8u();
541    
542    print_indentation();    print_indentation();
543    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]);
544    
545    lastseen = read64s();    lastseen = read64s();
546    
547    print_indentation();    print_indentation();
548    printf("Last seen %ld = ", lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
549    print_time_t(lastseen);    print_time_t(lastseen);
550    puts("");    puts("");
551    #else
552      lastseen = read64s();
553    
554      print_indentation();
555      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
556      print_time_t(lastseen);
557      puts("");
558    
559      macaddress[0] = read8u();
560      macaddress[1] = read8u();
561      macaddress[2] = read8u();
562      macaddress[3] = read8u();
563      macaddress[4] = read8u();
564      macaddress[5] = read8u();
565    
566      print_indentation();
567      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
568    #endif
569    
570    hostnamelen = read8u();    hostnamelen = read8u();
571    
572    print_indentation();    print_indentation();
573    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
574    
575    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
576      hostname[i] = read8u();      hostname[i] = read8u();
577    } // for    } // for
578    hostname[i] = '\0';    hostname[i] = '\0';
579    
580    print_indentation();    print_indentation();
581    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
582    
583    bytesin = read64u();    bytesin = read64u();
584    
# Line 573  void decode_protos_data(void) Line 627  void decode_protos_data(void)
627    
628    indent();    indent();
629    
630    for (u = 0; u < ipprotocount; u++) {    for (u = 0; u < ipprotocount; u++) {
631      unsigned char proto;      unsigned char proto;
632      unsigned long in;      unsigned long in;
633      unsigned long out;      unsigned long out;
634    
635      print_indentation();      print_indentation();
636      printf("Protocol #%u of %u:\n", u + 1, ipprotocount);      printf("Protocol #%u of %u:\n", u + 1, ipprotocount);
637    
638      proto = read8u();      proto = read8u();
639    
640      indent();      indent();
641      print_indentation();      print_indentation();
642      printf("Protocol 0x%x\n", proto);      printf("Protocol 0x%02x\n", proto);
643    
644      in = read64u();      in = read64u();
645    
646      print_indentation();      print_indentation();
647      printf("In %lu\n", in);      printf("In %lu\n", in);
648    
649      out = read64u();      out = read64u();
650    
651      print_indentation();      print_indentation();
652      printf("Out %lu\n", out);      printf("Out %lu\n", out);
653    
654      exdent();      exdent();
655    } // for    } // for
656    
657    exdent();    exdent();
# Line 687  void decode_graph_db_v1(void) Line 741  void decode_graph_db_v1(void)
741    } // for    } // for
742    
743    exdent();    exdent();
744  } // decode_udp_data()  } // decode_udp_data()
745    
746  void decode_graph_db_v1(void)  void decode_graph_db_v1(void)
747  {  {
748    signed long lasttime;    signed long lasttime;
749    
750    unsigned int i;    unsigned int i;
751    
752    lasttime = read64s();    lasttime = read64s();
753    
754    indent();    indent();
755    print_indentation();    print_indentation();
756    printf("Last time %ld = ", lasttime);    printf("Last time 0x%lx = %ld = ", lasttime, lasttime);
757    print_time_t(lasttime);    print_time_t(lasttime);
758    puts("");    puts("");
759    
760    for (i = 0; i < 4; i++) {    for (i = 0; i < 4; i++) {
761      unsigned char nbars;      unsigned char nbars;
762      unsigned char idxlastbar;      unsigned char idxlastbar;
763    
764      unsigned int j;      unsigned int j;
765    
766      print_indentation();      print_indentation();
767      printf("Graph #%d of 4:\n", i + 1);      printf("Graph #%d of 4:\n", i + 1);
768    
769      nbars = read8u();      nbars = read8u();
770    
771      indent();      indent();
# Line 850  unsigned long read64u(void) Line 904  unsigned long read64u(void)
904    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
905      handle_file_error();      handle_file_error();
906    } // if    } // if
907    
908  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
909    v = ntohl(v);    v = ntohl(v);
910  #endif  #endif
911    
912    return v;    return v;
913  } // read32s()  } // read32s()
914    
915  unsigned long read64u(void)  unsigned long read64u(void)
916  {  {
917    size_t r;    size_t r;
918    unsigned long v;    unsigned long v;
   unsigned int *p = (unsigned int *)&v;  
919    
920    #ifdef __LITTLE_ENDIAN__
921      unsigned long tmp;
922      unsigned int *p1 = (unsigned int *)&v;
923      unsigned int *p2 = (unsigned int *)&tmp;
924    #endif
925    
926    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));
927    
928    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
929      handle_file_error();      handle_file_error();
930    } // if    } // if
931    
932  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
933    p[0] = ntohl((unsigned int)p[0]);    p2[1] = ntohl(p1[0]);
934    p[1] = ntohl((unsigned int)p[1]);    p2[0] = ntohl(p1[1]);
935      v = tmp;
936  #endif  #endif
937    
938    return v;    return v;
939  } // read64u()  } // read64u()
940    
941  signed long read64s(void)  signed long read64s(void)
942  {  {
943    size_t r;    size_t r;
944    signed long v;    signed long v;
   signed int *p = (signed int *)&v;  
945    
946    #ifdef __LITTLE_ENDIAN__
947      signed long tmp;
948      signed int *p1 = (signed int *)&v;
949      signed int *p2 = (signed int *)&tmp;
950    #endif
951    
952    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));
953    
954    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
955      handle_file_error();      handle_file_error();
956    } // if    } // if
957    
958  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
959    p[0] = ntohl((signed int)p[0]);    p2[1] = ntohl(p1[0]);
960    p[1] = ntohl((signed int)p[1]);    p2[0] = ntohl(p1[1]);
961      v = tmp;
962  #endif  #endif
963    
964    return v;    return v;
965  } // read64s()  } // read64s()
966    
967  void handle_file_error(void)  void handle_file_error(void)
968  {  {
969    if (feof(file) != 0) {    if (feof(file) != 0) {
970      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);
971      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
972    } // if    } // if
973    
974    if (ferror(file) != 0) {    if (ferror(file) != 0) {
975      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);
976      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