Typo in previous changelog commit.
[official-gcc.git] / gcc / gcov-io.h
blob9e1c081b16de1c898e1394f357b4c430ada764aa
1 /* Machine-independent I/O routines for gcov.
2 Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #ifndef GCC_GCOV_IO_H
23 #define GCC_GCOV_IO_H
24 #include <stdio.h>
25 #include <sys/types.h>
27 static int __fetch_long PARAMS ((long *, char *, size_t))
28 ATTRIBUTE_UNUSED;
29 static int __read_long PARAMS ((long *, FILE *, size_t))
30 ATTRIBUTE_UNUSED;
31 static int __write_long PARAMS ((long, FILE *, size_t))
32 ATTRIBUTE_UNUSED;
33 static int __fetch_gcov_type PARAMS ((gcov_type *, char *, size_t))
34 ATTRIBUTE_UNUSED;
35 static int __store_gcov_type PARAMS ((gcov_type, char *, size_t))
36 ATTRIBUTE_UNUSED;
37 static int __read_gcov_type PARAMS ((gcov_type *, FILE *, size_t))
38 ATTRIBUTE_UNUSED;
39 static int __write_gcov_type PARAMS ((gcov_type, FILE *, size_t))
40 ATTRIBUTE_UNUSED;
41 static int __write_gcov_string PARAMS ((const char *, size_t, FILE*, long))
42 ATTRIBUTE_UNUSED;
43 static int __read_gcov_string PARAMS ((char *, size_t, FILE*, long))
44 ATTRIBUTE_UNUSED;
46 /* These routines only work for signed values. */
48 /* Store a portable representation of VALUE in DEST using BYTES*8-1 bits.
49 Return a non-zero value if VALUE requires more than BYTES*8-1 bits
50 to store. */
52 static int
53 __store_gcov_type (value, dest, bytes)
54 gcov_type value;
55 char *dest;
56 size_t bytes;
58 int upper_bit = (value < 0 ? 128 : 0);
59 size_t i;
61 if (value < 0)
63 gcov_type oldvalue = value;
64 value = -value;
65 if (oldvalue != -value)
66 return 1;
69 for(i = 0 ; i < (sizeof (value) < bytes ? sizeof (value) : bytes) ; i++) {
70 dest[i] = value & (i == (bytes - 1) ? 127 : 255);
71 value = value / 256;
74 if (value && value != -1)
75 return 1;
77 for(; i < bytes ; i++)
78 dest[i] = 0;
79 dest[bytes - 1] |= upper_bit;
80 return 0;
83 /* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store
84 the result in DEST. Returns a non-zero value if the value in SOURCE
85 will not fit in DEST. */
87 static int
88 __fetch_gcov_type (dest, source, bytes)
89 gcov_type *dest;
90 char *source;
91 size_t bytes;
93 gcov_type value = 0;
94 int i;
96 for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
97 if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
98 return 1;
100 for (; i >= 0; i--)
101 value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));
103 if ((source[bytes - 1] & 128) && (value > 0))
104 value = - value;
106 *dest = value;
107 return 0;
110 static int
111 __fetch_long (dest, source, bytes)
112 long *dest;
113 char *source;
114 size_t bytes;
116 long value = 0;
117 int i;
119 for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
120 if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
121 return 1;
123 for (; i >= 0; i--)
124 value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));
126 if ((source[bytes - 1] & 128) && (value > 0))
127 value = - value;
129 *dest = value;
130 return 0;
133 /* Write a BYTES*8-bit quantity to FILE, portably. Returns a non-zero
134 value if the write fails, or if VALUE can't be stored in BYTES*8
135 bits.
137 Note that VALUE may not actually be large enough to hold BYTES*8
138 bits, but BYTES characters will be written anyway.
140 BYTES may be a maximum of 10. */
142 static int
143 __write_gcov_type (value, file, bytes)
144 gcov_type value;
145 FILE *file;
146 size_t bytes;
148 char c[10];
150 if (bytes > 10 || __store_gcov_type (value, c, bytes))
151 return 1;
152 else
153 return fwrite(c, 1, bytes, file) != bytes;
156 static int
157 __write_long (value, file, bytes)
158 long value;
159 FILE *file;
160 size_t bytes;
162 char c[10];
164 if (bytes > 10 || __store_gcov_type ((gcov_type)value, c, bytes))
165 return 1;
166 else
167 return fwrite(c, 1, bytes, file) != bytes;
170 /* Read a quantity containing BYTES bytes from FILE, portably. Return
171 a non-zero value if the read fails or if the value will not fit
172 in DEST.
174 Note that DEST may not be large enough to hold all of the requested
175 data, but the function will read BYTES characters anyway.
177 BYTES may be a maximum of 10. */
179 static int
180 __read_gcov_type (dest, file, bytes)
181 gcov_type *dest;
182 FILE *file;
183 size_t bytes;
185 char c[10];
187 if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
188 return 1;
189 else
190 return __fetch_gcov_type (dest, c, bytes);
193 static int
194 __read_long (dest, file, bytes)
195 long *dest;
196 FILE *file;
197 size_t bytes;
199 char c[10];
201 if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
202 return 1;
203 else
204 return __fetch_long (dest, c, bytes);
208 /* Writes string in gcov format. */
210 static int
211 __write_gcov_string (string, length, file, delim)
212 const char *string;
213 size_t length;
214 FILE *file;
215 long delim;
217 size_t temp = length + 1;
219 /* delimiter */
220 if (__write_long (delim, file, 4) != 0)
221 return 1;
223 if (__write_long (length, file, 4) != 0)
224 return 1;
226 if (fwrite (string, temp, 1, file) != 1)
227 return 1;
229 temp &= 3;
231 if (temp)
233 char c[4];
235 c[0] = c[1] = c[2] = c[3] = 0;
237 if (fwrite (c, sizeof (char), 4 - temp, file) != 4 - temp)
238 return 1;
241 if (__write_long (delim, file, 4) != 0)
242 return 1;
244 return 0;
247 /* Reads string in gcov format. */
250 static int
251 __read_gcov_string (string, max_length, file, delim)
252 char *string;
253 size_t max_length;
254 FILE *file;
255 long delim;
257 long delim_from_file;
258 long length;
259 long read_length;
260 long tmp;
262 if (__read_long (&delim_from_file, file, 4) != 0)
263 return 1;
265 if (delim_from_file != delim)
266 return 1;
268 if (__read_long (&length, file, 4) != 0)
269 return 1;
271 if (length > (long) max_length)
272 read_length = max_length;
273 else
274 read_length = length;
276 tmp = (((length + 1) - 1) / 4 + 1) * 4;
277 /* This is the size occupied by the string in the file */
279 if (fread (string, read_length, 1, file) != 1)
280 return 1;
282 string[read_length] = 0;
284 if (fseek (file, tmp - read_length, SEEK_CUR) < 0)
285 return 1;
287 if (__read_long (&delim_from_file, file, 4) != 0)
288 return 1;
290 if (delim_from_file != delim)
291 return 1;
293 return 0;
297 #endif /* ! GCC_GCOV_IO_H */