~smartboyhw/wubi/bug-1080090-new

« back to all changes in this revision

Viewing changes to src/grubutil/common/.svn/text-base/bin2h.c.svn-base

  • Committer: Howard Chan
  • Date: 2012-11-20 10:16:05 UTC
  • Revision ID: smartboyhw@gmail.com-20121120101605-qfmjfsdynpzg9an9
Added images

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  GRUB Utilities --  Utilities for GRUB Legacy, GRUB2 and GRUB for DOS
3
 
 *  Copyright (C) 2007 Bean (bean123ch@gmail.com)
4
 
 *
5
 
 *  This program is free software: you can redistribute it and/or modify
6
 
 *  it under the terms of the GNU General Public License as published by
7
 
 *  the Free Software Foundation, either version 3 of the License, or
8
 
 *  (at your option) any later version.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
 
22
 
int
23
 
main (int argc, char *argv[])
24
 
{
25
 
  FILE *in, *out;
26
 
  unsigned char *data;
27
 
  int length, i;
28
 
 
29
 
  if (argc != 4)
30
 
    {
31
 
      fprintf (stderr, "Usage: bin2h file.dat outfile.h token_name\n");
32
 
      return 1;
33
 
    }
34
 
 
35
 
  in = fopen (argv[1], "rb");
36
 
 
37
 
  if (!in)
38
 
    {
39
 
      fprintf (stderr, "bin2h: open %s fail\n", argv[1]);
40
 
      return 1;
41
 
    }
42
 
 
43
 
  fseek (in, 0, SEEK_END);
44
 
  length = ftell (in);
45
 
  fseek (in, 0, SEEK_SET);
46
 
 
47
 
  if (length == 0)
48
 
    {
49
 
      fprintf (stderr, "bin2h: %s is empty\n", argv[1]);
50
 
      return 1;
51
 
    }
52
 
 
53
 
  if ((data = malloc (length)) == NULL)
54
 
    {
55
 
      fclose (in);
56
 
      fprintf (stderr, "bin2h: can\'t allocate memory\n");
57
 
      return 1;
58
 
    }
59
 
 
60
 
  if ((fread (data, 1, length, in)) != length)
61
 
    {
62
 
      fclose (in);
63
 
      fprintf (stderr, "bin2h: read %s fail\n", argv[1]);
64
 
      return 1;
65
 
    }
66
 
 
67
 
  fclose (in);
68
 
 
69
 
  out = fopen (argv[2], "wt");
70
 
 
71
 
  if (!out)
72
 
    {
73
 
      fclose (in);
74
 
      fprintf (stderr, "bin2h: open %s fail\n", argv[2]);
75
 
      return 1;
76
 
    }
77
 
 
78
 
  fprintf (out, "unsigned char %s[%d] = {", argv[3], length);
79
 
 
80
 
  for (i = 0; i < length; i++)
81
 
    {
82
 
      if (i % 20 == 0)
83
 
        {
84
 
          fprintf (out, "\n  ");
85
 
        }
86
 
      fprintf (out, "%d", data[i]);
87
 
      if (i != length - 1)
88
 
        fprintf (out, ",");
89
 
    }
90
 
 
91
 
  fprintf (out, "};\n");
92
 
  fclose (out);
93
 
  free (data);
94
 
  return 0;
95
 
}