summaryrefslogtreecommitdiff
path: root/lib/file-input.c
blob: c80e7149f441b1cf7fdcb9bec48e9f8e9ecc02a7 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* file-input.c: file reading routines for binary files in BigEndian
   order, 2's complement representation.

Copyright (C) 1992, 2011 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "config.h"

#include "file-input.h"


one_byte
get_byte (FILE *f, string filename)
{
  one_byte b;

  if (fread (&b, 1, 1, f) != 1)
    FATAL_PERROR (concat3 ("get_byte (", filename, ")"));

  return b;
}


two_bytes
get_two (FILE *f, string filename)
{
  two_bytes b;

  b = get_byte (f, filename) << 8;
  b |= get_byte (f, filename);

  return b;
}


four_bytes
get_four (FILE *f, string filename)
{
  four_bytes b;

  b = get_byte (f, filename) << 24;
  b |= get_byte (f, filename) << 16;
  b |= get_byte (f, filename) << 8;
  b |= get_byte (f, filename);

  return b;
}


/* In order to get a signed value, we merely read an unsigned value and
   cast it; the value in the file is assumed to be 2's complement.  */

signed_4_bytes
get_signed_four (FILE *f, string filename)
{
  return (signed_4_bytes) get_four (f, filename);
}


/* Return the next N bytes in the file F as an array.  */

address
get_n_bytes (unsigned n, FILE *f, string filename)
{
  one_byte *b;

  if (n == 0)
    FATAL1 ("get_n_bytes (%s): can't get zero bytes", filename);

  b = xmalloc (n);

  if (fread (b, n, 1, f) != 1)
    FATAL2 ("get_n_bytes (%s): fread of %u bytes failed", filename, n);

  return b;
}



/* Reading backwards. This macro is shared among all the routines by assuming
   the name `f' for the file pointer.  */

#define MOVE_BACK(size) xfseek (f, (long) -size, SEEK_CUR, filename)

one_byte
get_previous_byte (FILE *f, string filename)
{
  one_byte b;

  MOVE_BACK (1);
  b = get_byte (f, filename);
  MOVE_BACK (1);
  return b;
}


two_bytes
get_previous_two (FILE *f, string filename)
{
  two_bytes b;

  MOVE_BACK (2);
  b = get_two (f, filename);
  MOVE_BACK (2);

  return b;
}



four_bytes
get_previous_four (FILE *f, string filename)
{
  four_bytes b;

  MOVE_BACK (4);
  b = get_four (f, filename);
  MOVE_BACK (4);

  return b;
}



/* Looking for specific values in the input.  */

void 
match_byte (one_byte expected, FILE *f, string filename)
{
  one_byte b = get_byte (f, filename);

  if (b != expected)
    FATAL3 ("%s: Expected byte value %u, found %u", filename, expected, b); 
}


void
match_previous_byte (one_byte expected, FILE *f, string filename)
{
  one_byte b = get_previous_byte (f, filename);

  if (b != expected)
    FATAL3 ("%s: Expected previous byte value %u, found %u", filename, 
            expected, b);
}