1 | /* Machine-independent I/O routines for gcov. |
---|
2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. |
---|
3 | Contributed by Bob Manson <manson@cygnus.com>. |
---|
4 | |
---|
5 | This file is part of GNU CC. |
---|
6 | |
---|
7 | GNU CC is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | GNU CC is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with GNU CC; see the file COPYING. If not, write to |
---|
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #ifndef GCOV_IO_H |
---|
23 | #define GCOV_IO_H |
---|
24 | #include <stdio.h> |
---|
25 | |
---|
26 | /* These routines only work for signed values. */ |
---|
27 | |
---|
28 | /* Store a portable representation of VALUE in DEST using BYTES*8-1 bits. |
---|
29 | Return a non-zero value if VALUE requires more than BYTES*8-1 bits |
---|
30 | to store. */ |
---|
31 | |
---|
32 | static int |
---|
33 | __store_long (value, dest, bytes) |
---|
34 | long value; |
---|
35 | char *dest; |
---|
36 | int bytes; |
---|
37 | { |
---|
38 | int upper_bit = (value < 0 ? 128 : 0); |
---|
39 | int i; |
---|
40 | |
---|
41 | if (value < 0) |
---|
42 | { |
---|
43 | long oldvalue = value; |
---|
44 | value = -value; |
---|
45 | if (oldvalue != -value) |
---|
46 | return 1; |
---|
47 | } |
---|
48 | |
---|
49 | for(i = 0 ; i < (sizeof (value) < bytes ? sizeof (value) : bytes) ; i++) { |
---|
50 | dest[i] = value & (i == (bytes - 1) ? 127 : 255); |
---|
51 | value = value / 256; |
---|
52 | } |
---|
53 | |
---|
54 | if (value && value != -1) |
---|
55 | return 1; |
---|
56 | |
---|
57 | for(; i < bytes ; i++) |
---|
58 | dest[i] = 0; |
---|
59 | dest[bytes - 1] |= upper_bit; |
---|
60 | return 0; |
---|
61 | } |
---|
62 | |
---|
63 | /* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store |
---|
64 | the result in DEST. Returns a non-zero value if the value in SOURCE |
---|
65 | will not fit in DEST. */ |
---|
66 | |
---|
67 | static int |
---|
68 | __fetch_long (dest, source, bytes) |
---|
69 | long *dest; |
---|
70 | char *source; |
---|
71 | int bytes; |
---|
72 | { |
---|
73 | long value = 0; |
---|
74 | int i; |
---|
75 | |
---|
76 | for (i = bytes - 1; i > (sizeof (*dest) - 1); i--) |
---|
77 | if (source[i] & (i == (bytes - 1) ? 127 : 255 )) |
---|
78 | return 1; |
---|
79 | |
---|
80 | for (; i >= 0; i--) |
---|
81 | value = value * 256 + (source[i] & (i == (bytes - 1) ? 127 : 255)); |
---|
82 | |
---|
83 | if ((source[bytes - 1] & 128) && (value > 0)) |
---|
84 | value = - value; |
---|
85 | |
---|
86 | *dest = value; |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | /* Write a BYTES*8-bit quantity to FILE, portably. Returns a non-zero |
---|
91 | value if the write fails, or if VALUE can't be stored in BYTES*8 |
---|
92 | bits. |
---|
93 | |
---|
94 | Note that VALUE may not actually be large enough to hold BYTES*8 |
---|
95 | bits, but BYTES characters will be written anyway. |
---|
96 | |
---|
97 | BYTES may be a maximum of 10. */ |
---|
98 | |
---|
99 | static int |
---|
100 | __write_long (value, file, bytes) |
---|
101 | long value; |
---|
102 | FILE *file; |
---|
103 | int bytes; |
---|
104 | { |
---|
105 | char c[10]; |
---|
106 | |
---|
107 | if (bytes > 10 || __store_long (value, c, bytes)) |
---|
108 | return 1; |
---|
109 | else |
---|
110 | return fwrite(c, 1, bytes, file) != bytes; |
---|
111 | } |
---|
112 | |
---|
113 | /* Read a quantity containing BYTES bytes from FILE, portably. Return |
---|
114 | a non-zero value if the read fails or if the value will not fit |
---|
115 | in DEST. |
---|
116 | |
---|
117 | Note that DEST may not be large enough to hold all of the requested |
---|
118 | data, but the function will read BYTES characters anyway. |
---|
119 | |
---|
120 | BYTES may be a maximum of 10. */ |
---|
121 | |
---|
122 | static int |
---|
123 | __read_long (dest, file, bytes) |
---|
124 | long *dest; |
---|
125 | FILE *file; |
---|
126 | int bytes; |
---|
127 | { |
---|
128 | char c[10]; |
---|
129 | |
---|
130 | if (bytes > 10 || fread(c, 1, bytes, file) != bytes) |
---|
131 | return 1; |
---|
132 | else |
---|
133 | return __fetch_long (dest, c, bytes); |
---|
134 | } |
---|
135 | |
---|
136 | #endif |
---|