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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [libgloss/] [hp74x/] [checksum.c] - Diff between revs 158 and 816

Only display areas with differences | Details | Blame | View Log

Rev 158 Rev 816
#include <stdio.h>
#include <stdio.h>
#include <fcntl.h>
#include <fcntl.h>
 
 
#define USAGE   "USAGE: checkum -[vhcs] infile outfile\n\t-v\tverbose\n\
#define USAGE   "USAGE: checkum -[vhcs] infile outfile\n\t-v\tverbose\n\
\t-h\thelp\n\t-c\tcheck checksum\n\t-s\tprint the ipl size"
\t-h\thelp\n\t-c\tcheck checksum\n\t-s\tprint the ipl size"
static int verbose = 0;
static int verbose = 0;
static int verify  = 0;
static int verify  = 0;
static int size    = 0;
static int size    = 0;
 
 
typedef int word_t;
typedef int word_t;
#define WORDSIZE (sizeof(word_t))
#define WORDSIZE (sizeof(word_t))
 
 
main(argc, argv)
main(argc, argv)
     int argc;
     int argc;
     char **argv;
     char **argv;
{
{
  char   *infile;
  char   *infile;
  char   *outfile;
  char   *outfile;
  int    infd;
  int    infd;
  int    outfd;
  int    outfd;
  word_t checksum = 0;
  word_t checksum = 0;
  int    nbytes;
  int    nbytes;
  word_t buf;
  word_t buf;
  int    i        = 1;
  int    i        = 1;
  int   filesize  = 0;
  int   filesize  = 0;
 
 
  while (*argv[i] == '-') {
  while (*argv[i] == '-') {
      switch (*(argv[i]+1)) {
      switch (*(argv[i]+1)) {
      case 'v':
      case 'v':
        verbose++;
        verbose++;
        break;
        break;
      case 'c':
      case 'c':
        verify++;
        verify++;
        puts ("Sorry, unimplemented for now");
        puts ("Sorry, unimplemented for now");
        exit(1);
        exit(1);
        break;
        break;
      case 's':
      case 's':
        size++;
        size++;
        break;
        break;
      case 'h':
      case 'h':
        puts (USAGE);
        puts (USAGE);
        exit(0);
        exit(0);
      default:
      default:
        printf ("\"%s\", Illegal option\n", argv[i]);
        printf ("\"%s\", Illegal option\n", argv[i]);
        puts (USAGE);
        puts (USAGE);
        exit(1);
        exit(1);
    }
    }
    i++;
    i++;
  }
  }
  infile = *(argv + i);
  infile = *(argv + i);
  outfile = *(argv + i+1);
  outfile = *(argv + i+1);
 
 
  /* see it there were file names on the command line */
  /* see it there were file names on the command line */
  if (infile == 0x0) {
  if (infile == 0x0) {
    puts("Didn't specify an input file name");
    puts("Didn't specify an input file name");
    exit(1);
    exit(1);
  }
  }
  if (outfile == 0x0) {
  if (outfile == 0x0) {
    puts("Didn't specify an output file name");
    puts("Didn't specify an output file name");
     exit(1);
     exit(1);
  }
  }
 
 
  /* try to open the files */
  /* try to open the files */
  infd = open(infile, O_RDONLY);
  infd = open(infile, O_RDONLY);
  if (infd == -1) {
  if (infd == -1) {
    printf("Couldn't open %s\n", infile);
    printf("Couldn't open %s\n", infile);
    exit(1);
    exit(1);
  }
  }
 
 
  outfd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC);
  outfd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC);
  if (outfd == -1) {
  if (outfd == -1) {
    printf("Couldn't open %s\n", outfile);
    printf("Couldn't open %s\n", outfile);
    exit(1);
    exit(1);
  }
  }
 
 
  if (verbose > 2)
  if (verbose > 2)
    putchar('\n');
    putchar('\n');
 
 
  /* calculate the checksum */
  /* calculate the checksum */
  while ((nbytes = read(infd, &buf, WORDSIZE)) == WORDSIZE) {
  while ((nbytes = read(infd, &buf, WORDSIZE)) == WORDSIZE) {
    if (verbose > 2)
    if (verbose > 2)
      putchar('.');
      putchar('.');
    checksum+= buf;
    checksum+= buf;
    filesize+= WORDSIZE;
    filesize+= WORDSIZE;
    if (write(outfd, &buf, WORDSIZE) != WORDSIZE) {
    if (write(outfd, &buf, WORDSIZE) != WORDSIZE) {
      puts("Couldn't write");
      puts("Couldn't write");
    }
    }
    if (verbose > 3)
    if (verbose > 3)
      putchar('+');
      putchar('+');
  }
  }
  if (verbose > 2)
  if (verbose > 2)
    putchar('\n');
    putchar('\n');
 
 
  /* write the last byte read */
  /* write the last byte read */
  if (nbytes > 0) {
  if (nbytes > 0) {
    write(outfd, &buf, nbytes);
    write(outfd, &buf, nbytes);
    checksum+= buf;                             /* calculate the last word */
    checksum+= buf;                             /* calculate the last word */
    filesize+= nbytes;
    filesize+= nbytes;
  }
  }
  /* write the checksum */
  /* write the checksum */
  buf = -checksum;
  buf = -checksum;
  write(outfd, &buf, WORDSIZE);
  write(outfd, &buf, WORDSIZE);
  filesize+= WORDSIZE;                          /* checksum increase the size */
  filesize+= WORDSIZE;                          /* checksum increase the size */
 
 
  if (verbose > 0)
  if (verbose > 0)
    printf("The calculated checksum is:\n\t0x%x,\n\t%u\n", -checksum, -checksum);
    printf("The calculated checksum is:\n\t0x%x,\n\t%u\n", -checksum, -checksum);
 
 
  /* calculate the extra 2K here */
  /* calculate the extra 2K here */
  buf = 0;
  buf = 0;
  while ((filesize % 2048) !=0) {
  while ((filesize % 2048) !=0) {
    filesize+=WORDSIZE;
    filesize+=WORDSIZE;
    write(outfd, &buf, WORDSIZE);
    write(outfd, &buf, WORDSIZE);
  }
  }
  if (size > 0) {
  if (size > 0) {
    printf ("%u is the new file size\n", filesize);
    printf ("%u is the new file size\n", filesize);
  }
  }
  close(outfd);
  close(outfd);
  close(infd);
  close(infd);
  exit(0);
  exit(0);
}
}
 
 
#if 0
#if 0
/* Calculate a simple checksum and concatenate it to the end of BUF.  */
/* Calculate a simple checksum and concatenate it to the end of BUF.  */
void
void
compute_and_concatenate_checksum (word *buf, size_t bufsize_in_words)
compute_and_concatenate_checksum (word *buf, size_t bufsize_in_words)
{
{
  size_t i;
  size_t i;
  word sum;
  word sum;
  sum = buf[0]
  sum = buf[0]
  for (i = 1; i < bufsize_in_words; i++)
  for (i = 1; i < bufsize_in_words; i++)
    sum += buf[i];
    sum += buf[i];
  buf[bufsize_in_words] = -sum;
  buf[bufsize_in_words] = -sum;
}
}
 
 
/* Calculate a simple checksum and verify it.  NOTE: bufsize_in_words should
/* Calculate a simple checksum and verify it.  NOTE: bufsize_in_words should
   include the checksum, i.e., it should be one larger than when the
   include the checksum, i.e., it should be one larger than when the
   checksum was calculated using compute_and_concatenate_checksum!  */
   checksum was calculated using compute_and_concatenate_checksum!  */
int
int
compute_and_and_verify_checksum (word *buf, size_t bufsize_in_words)
compute_and_and_verify_checksum (word *buf, size_t bufsize_in_words)
{
{
  size_t i;
  size_t i;
  word sum;
  word sum;
  sum = buf[0];
  sum = buf[0];
  for (i = 1; i < bufsize_in_words; i++)
  for (i = 1; i < bufsize_in_words; i++)
    sum += buf[i];
    sum += buf[i];
  if (sum != 0)
  if (sum != 0)
    return ERROR;
    return ERROR;
  return SUCCESS;
  return SUCCESS;
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

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