root/drivers/sound/hex2hex.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. hex2hex

   1 /*
   2  * This file is a part of configure.c
   3  *
   4  * hex2hex reads an input file in Intel HEX format and produces
   5  * an (unsigned char) array which contains the bytes and writes it to the
   6  * output file using C syntax
   7  */
   8 
   9 #define MAX_SIZE (256*1024)
  10 #define ABANDON(why) { \
  11                 fprintf(stderr, "%s: " why "\n", source); \
  12                 fclose(inf);fclose(outf);return 0; \
  13                 }
  14 
  15 int hex2hex(char *source, char *target, char *varline)
     /* [previous][next][first][last][top][bottom][index][help] */
  16 {
  17         FILE *inf, *outf;
  18 
  19         int i,l, c;
  20         unsigned char buf[MAX_SIZE];
  21 
  22         if ((inf=fopen(source, "r"))==NULL)
  23         {
  24                 perror(source);
  25                 return 0;
  26         }
  27 
  28         if ((outf=fopen(target, "w"))==NULL)
  29         {
  30                 perror(target);
  31                 fclose(inf);
  32                 return 0;
  33         }
  34 
  35         l=0;
  36 
  37         while ((c=getc(inf))!=EOF)
  38         {
  39                 if (c == ':')   /* Sync with beginning of line */
  40                 {
  41                         int n, check;
  42                         unsigned char sum;
  43                         int addr;
  44                         int linetype;
  45 
  46                         if (fscanf(inf, "%02x", &n) != 1)
  47                            ABANDON("File format error");
  48                         sum = n;
  49 
  50                         if (fscanf(inf, "%04x", &addr) != 1)
  51                            ABANDON("File format error");
  52                         sum += addr/256;
  53                         sum += addr%256;
  54 
  55                         if (fscanf(inf, "%02x", &linetype) != 1)
  56                            ABANDON("File format error");
  57                         sum += linetype;
  58 
  59                         if (linetype != 0)
  60                            continue;
  61 
  62                         for (i=0;i<n;i++)
  63                         {
  64                                 if (fscanf(inf, "%02x", &c) != 1)
  65                                    ABANDON("File format error");
  66                                 if (addr >= MAX_SIZE)
  67                                    ABANDON("File too large");
  68                                 buf[addr++] = c;
  69                                 if (addr > l)
  70                                    l = addr;
  71                                 sum += c;
  72                         }
  73 
  74                         if (fscanf(inf, "%02x", &check) != 1)
  75                            ABANDON("File format error");
  76 
  77                         sum = ~sum + 1;
  78                         if (check != sum)
  79                            ABANDON("Line checksum error");
  80                 }
  81         }
  82 
  83         fprintf(outf, "/*\n *\t Computer generated file. Do not edit.\n */\n");
  84         fprintf(outf, "static unsigned char %s[] = {\n", varline);
  85 
  86         for (i=0;i<l;i++)
  87         {
  88                 if (i) fprintf(outf, ",");
  89                 if (i && !(i % 16)) fprintf(outf, "\n");
  90                 fprintf(outf, "0x%02x", buf[i]);
  91         }
  92 
  93         fprintf(outf, "\n};\n\n");
  94         fclose(inf);
  95         fclose(outf);
  96         return 1;
  97 }

/* [previous][next][first][last][top][bottom][index][help] */