File: convhexbin.c

package info (click to toggle)
isdnutils 1%3A3.1pre4-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,096 kB
  • ctags: 11,039
  • sloc: ansic: 95,142; sh: 15,612; perl: 4,429; makefile: 3,064; cpp: 2,708; sql: 54
file content (136 lines) | stat: -rw-r--r-- 3,110 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

/*
 * Copyright (c) 1993, 1994, 1995, INESC. All Rights Reserved.
 * Licensed Material-Property of INESC
 * 
 */

int hexbin(FILE *, unsigned char *, int, int);

int
convhexbin (char *filename, unsigned char *buf, int size)
{
   FILE *fpr;
   int retval;
   register int lastmem;
   char str[100];

   /* Abre ficheiro de entrada */
   if ((fpr=fopen (filename,"rb")) == NULL)
    return (-1);

   /* le primeira linha do ficheiro stpd */
   fgets(str, 80, fpr);

  /* Poe buf a 0x00 */
  for (lastmem=0 ; lastmem < size ; lastmem++)
    buf[lastmem]=0x00;

  /* Converte para binario */
  retval = hexbin(fpr, buf, 0x100000-1024, 0xfffff);

  fclose (fpr);
  return retval;
}

int
hexbin(FILE *fpr, unsigned char *ar256k, int firstend, int lastend)
{

   unsigned char str [80], i, cc;
   u_int linha, nn, xx, nnn, pp;
   int lastmem, ind, end, ss;

   ind = 0;

   linha=0; lastmem=0; ss=0;

   while (fgets((char *)str, 80, fpr) != NULL) {
      linha++;
      if (str[0] == 26)
         break;

      if (str[0] != ':' || str[7] != '0') {
         printf("Error in file input format\n");
         printf("%c - unknown format in line %u\n", str[0], linha);
         fclose(fpr);
         exit(2);
      }

      sscanf((char *)&str[1], "%2x", &nn);
      cc=nn;

      switch (str[8]) {
         case '0' :
            nnn=(nn<<1)+9;
            sscanf((char *)&str[3], "%4x", &pp);
            cc+=pp+(pp>>8);
            end=ss+pp;
            for (i=9 ; i<nnn; i+= 2) {
               sscanf((char *)&str[i], "%2x", &xx);
               if ((end >= firstend) && (end <= lastend)) {
                  ind=end-firstend;
                  if (ind > lastmem)
                     lastmem=ind;

                  ar256k[ind]=xx;
               }
               ind++;
               end++;
               cc+=xx;
            }
            sscanf((char *)&str[i], "%2x", &xx);
            cc+=xx;
            if (cc) {
               printf("CHECK SUM Error in line %u\n", linha);
               fclose(fpr);
               exit(2);
            }
            break;

         case '1' :
            for (i=3 ; i<11 ; i+=2) {
               sscanf((char *)&str[i],"%2x",&xx);
               cc+=xx;
            }
            if (cc) {
               printf("CHECK SUM Error in line %u\n", linha);
               fclose(fpr);
               exit(2);
            }
            break;

         case '2' :
            ss=0;
            sscanf((char *)&str[9], "%4X", &ss);
            ss<<=4;
            for (i=3; i<15; i+= 2) {
               sscanf((char *)&str[i], "%2x", &xx);
               cc+=xx;
            }
            if (cc) {
               printf("CHECK SUM Error in line %u\n",linha);
               fclose(fpr);
               exit(2);
            }
            break;

         case '3' :
            break;

         default :
            printf("Error in file input format\n");
            printf("Type %c - unknown type in line %u\n", str[8], linha);
            fclose(fpr);
            exit(2);
      }
   }
   return 0;
}