summaryrefslogtreecommitdiff
path: root/lib/file-output.c
blob: e1874a8ee9baebb3180a86f566c572d87929ea18 (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
# file-output.c: file writing 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 of the License, 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, see <http://www.gnu.org/licenses/>.
#


#include "config.h"

#include "file-output.h"


/* Output in BigEndian order.  We could do these more efficiently by
   checking if the hardware already uses BigEndian order, but it's not
   worth it.  These routines are never the bottleneck (in the ways
   they've been used so far, anyway).  We assume that we write negative
   numbers in 2's complement, also.  */

void
put_byte (one_byte b, FILE *f, string filename)
{
  if (fwrite (&b, 1, 1, f) != 1)
    FATAL2 ("%s: Could not write byte %u", filename, b);
}


void
put_two (two_bytes b, FILE *f, string filename)
{
  put_byte ((b & 0xff00) >> 8, f, filename); /* High-order byte.  */
  put_byte (b & 0x00ff, f, filename);        /* And low-order.   */
}


void
put_three (four_bytes b, FILE *f, string filename)
{
  put_byte ((b & 0x00ff0000) >> 16, f, filename);
  put_byte ((b & 0x0000ff00) >> 8, f, filename);
  put_byte (b & 0x000000ff, f, filename);
}


void
put_four (four_bytes b, FILE *f, string filename)
{
  put_byte ((b & 0xff000000) >> 24, f, filename);
  put_byte ((b & 0x00ff0000) >> 16, f, filename);
  put_byte ((b & 0x0000ff00) >> 8, f, filename);
  put_byte (b & 0x000000ff, f, filename);
}


void
put_n_bytes (unsigned n, address b, FILE *f, string filename)
{
  one_byte *data_b = b;  /* We can't dereference a pure `address'.  */

  if (fwrite (data_b, n, 1, f) != 1)
    FATAL2 ("%s: Could not write %u-byte block", filename, n);
}