~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/oss/hex2hex.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * hex2hex reads stdin in Intel HEX format and produces an
  3  * (unsigned char) array which contains the bytes and writes it
  4  * to stdout using C syntax
  5  */
  6 
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include <stdlib.h>
 10 
 11 #define ABANDON(why) { fprintf(stderr, "%s\n", why); exit(1); }
 12 #define MAX_SIZE (256*1024)
 13 unsigned char buf[MAX_SIZE];
 14 
 15 static int loadhex(FILE *inf, unsigned char *buf)
 16 {
 17         int l=0, c, i;
 18 
 19         while ((c=getc(inf))!=EOF)
 20         {
 21                 if (c == ':')   /* Sync with beginning of line */
 22                 {
 23                         int n, check;
 24                         unsigned char sum;
 25                         int addr;
 26                         int linetype;
 27 
 28                         if (fscanf(inf, "%02x", &n) != 1)
 29                            ABANDON("File format error");
 30                         sum = n;
 31 
 32                         if (fscanf(inf, "%04x", &addr) != 1)
 33                            ABANDON("File format error");
 34                         sum += addr/256;
 35                         sum += addr%256;
 36 
 37                         if (fscanf(inf, "%02x", &linetype) != 1)
 38                            ABANDON("File format error");
 39                         sum += linetype;
 40 
 41                         if (linetype != 0)
 42                            continue;
 43 
 44                         for (i=0;i<n;i++)
 45                         {
 46                                 if (fscanf(inf, "%02x", &c) != 1)
 47                                    ABANDON("File format error");
 48                                 if (addr >= MAX_SIZE)
 49                                    ABANDON("File too large");
 50                                 buf[addr++] = c;
 51                                 if (addr > l)
 52                                    l = addr;
 53                                 sum += c;
 54                         }
 55 
 56                         if (fscanf(inf, "%02x", &check) != 1)
 57                            ABANDON("File format error");
 58 
 59                         sum = ~sum + 1;
 60                         if (check != sum)
 61                            ABANDON("Line checksum error");
 62                 }
 63         }
 64 
 65         return l;
 66 }
 67 
 68 int main( int argc, const char * argv [] )
 69 {
 70         const char * varline;
 71         int i,l;
 72         int id=0;
 73 
 74         if(argv[1] && strcmp(argv[1], "-i")==0)
 75         {
 76                 argv++;
 77                 argc--;
 78                 id=1;
 79         }
 80         if(argv[1]==NULL)
 81         {
 82                 fprintf(stderr,"hex2hex: [-i] filename\n");
 83                 exit(1);
 84         }
 85         varline = argv[1];
 86         l = loadhex(stdin, buf);
 87 
 88         printf("/*\n *\t Computer generated file. Do not edit.\n */\n");
 89         printf("static int %s_len = %d;\n", varline, l);
 90         printf("static unsigned char %s[] %s = {\n", varline, id?"__initdata":"");
 91 
 92         for (i=0;i<l;i++)
 93         {
 94                 if (i) printf(",");
 95                 if (i && !(i % 16)) printf("\n");
 96                 printf("0x%02x", buf[i]);
 97         }
 98 
 99         printf("\n};\n\n");
100         return 0;
101 }
102 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp