OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [libgloss/] [hp74x/] [checksum.c] - Blame information for rev 179

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
#include <stdio.h>
2
#include <fcntl.h>
3
 
4
#define USAGE   "USAGE: checkum -[vhcs] infile outfile\n\t-v\tverbose\n\
5
\t-h\thelp\n\t-c\tcheck checksum\n\t-s\tprint the ipl size"
6
static int verbose = 0;
7
static int verify  = 0;
8
static int size    = 0;
9
 
10
typedef int word_t;
11
#define WORDSIZE (sizeof(word_t))
12
 
13
main(argc, argv)
14
     int argc;
15
     char **argv;
16
{
17
  char   *infile;
18
  char   *outfile;
19
  int    infd;
20
  int    outfd;
21
  word_t checksum = 0;
22
  int    nbytes;
23
  word_t buf;
24
  int    i        = 1;
25
  int   filesize  = 0;
26
 
27
  while (*argv[i] == '-') {
28
      switch (*(argv[i]+1)) {
29
      case 'v':
30
        verbose++;
31
        break;
32
      case 'c':
33
        verify++;
34
        puts ("Sorry, unimplemented for now");
35
        exit(1);
36
        break;
37
      case 's':
38
        size++;
39
        break;
40
      case 'h':
41
        puts (USAGE);
42
        exit(0);
43
      default:
44
        printf ("\"%s\", Illegal option\n", argv[i]);
45
        puts (USAGE);
46
        exit(1);
47
    }
48
    i++;
49
  }
50
  infile = *(argv + i);
51
  outfile = *(argv + i+1);
52
 
53
  /* see it there were file names on the command line */
54
  if (infile == 0x0) {
55
    puts("Didn't specify an input file name");
56
    exit(1);
57
  }
58
  if (outfile == 0x0) {
59
    puts("Didn't specify an output file name");
60
     exit(1);
61
  }
62
 
63
  /* try to open the files */
64
  infd = open(infile, O_RDONLY);
65
  if (infd == -1) {
66
    printf("Couldn't open %s\n", infile);
67
    exit(1);
68
  }
69
 
70
  outfd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC);
71
  if (outfd == -1) {
72
    printf("Couldn't open %s\n", outfile);
73
    exit(1);
74
  }
75
 
76
  if (verbose > 2)
77
    putchar('\n');
78
 
79
  /* calculate the checksum */
80
  while ((nbytes = read(infd, &buf, WORDSIZE)) == WORDSIZE) {
81
    if (verbose > 2)
82
      putchar('.');
83
    checksum+= buf;
84
    filesize+= WORDSIZE;
85
    if (write(outfd, &buf, WORDSIZE) != WORDSIZE) {
86
      puts("Couldn't write");
87
    }
88
    if (verbose > 3)
89
      putchar('+');
90
  }
91
  if (verbose > 2)
92
    putchar('\n');
93
 
94
  /* write the last byte read */
95
  if (nbytes > 0) {
96
    write(outfd, &buf, nbytes);
97
    checksum+= buf;                             /* calculate the last word */
98
    filesize+= nbytes;
99
  }
100
  /* write the checksum */
101
  buf = -checksum;
102
  write(outfd, &buf, WORDSIZE);
103
  filesize+= WORDSIZE;                          /* checksum increase the size */
104
 
105
  if (verbose > 0)
106
    printf("The calculated checksum is:\n\t0x%x,\n\t%u\n", -checksum, -checksum);
107
 
108
  /* calculate the extra 2K here */
109
  buf = 0;
110
  while ((filesize % 2048) !=0) {
111
    filesize+=WORDSIZE;
112
    write(outfd, &buf, WORDSIZE);
113
  }
114
  if (size > 0) {
115
    printf ("%u is the new file size\n", filesize);
116
  }
117
  close(outfd);
118
  close(infd);
119
  exit(0);
120
}
121
 
122
#if 0
123
/* Calculate a simple checksum and concatenate it to the end of BUF.  */
124
void
125
compute_and_concatenate_checksum (word *buf, size_t bufsize_in_words)
126
{
127
  size_t i;
128
  word sum;
129
  sum = buf[0]
130
  for (i = 1; i < bufsize_in_words; i++)
131
    sum += buf[i];
132
  buf[bufsize_in_words] = -sum;
133
}
134
 
135
/* Calculate a simple checksum and verify it.  NOTE: bufsize_in_words should
136
   include the checksum, i.e., it should be one larger than when the
137
   checksum was calculated using compute_and_concatenate_checksum!  */
138
int
139
compute_and_and_verify_checksum (word *buf, size_t bufsize_in_words)
140
{
141
  size_t i;
142
  word sum;
143
  sum = buf[0];
144
  for (i = 1; i < bufsize_in_words; i++)
145
    sum += buf[i];
146
  if (sum != 0)
147
    return ERROR;
148
  return SUCCESS;
149
}
150
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.