summaryrefslogtreecommitdiffstats
path: root/gcc/gcov-io.h
blob: 29a44017fde87ece5604b3943ead785dcd5eb8c6 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Machine-independent I/O routines for gcov.
   Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
   Contributed by Bob Manson <manson@cygnus.com>.

This file is part of GCC.

GCC 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 2, or (at your option) any later
version.

GCC 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 GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

#ifndef GCC_GCOV_IO_H
#define GCC_GCOV_IO_H
#include <stdio.h>
#include <sys/types.h>

static int __fetch_long	PARAMS ((long *, char *, size_t))
	ATTRIBUTE_UNUSED;
static int __read_long  PARAMS ((long *, FILE *, size_t))
	ATTRIBUTE_UNUSED;
static int __write_long PARAMS ((long, FILE *, size_t))
	ATTRIBUTE_UNUSED;
static int __fetch_gcov_type PARAMS ((gcov_type *, char *, size_t))
	ATTRIBUTE_UNUSED;
static int __store_gcov_type PARAMS ((gcov_type, char *, size_t))
	ATTRIBUTE_UNUSED;
static int __read_gcov_type  PARAMS ((gcov_type *, FILE *, size_t))
	ATTRIBUTE_UNUSED;
static int __write_gcov_type PARAMS ((gcov_type, FILE *, size_t))
	ATTRIBUTE_UNUSED;
static int __write_gcov_string PARAMS ((const char *, size_t, FILE*, long))
	ATTRIBUTE_UNUSED;
static int __read_gcov_string PARAMS ((char *, size_t, FILE*, long))
	ATTRIBUTE_UNUSED;

/* These routines only work for signed values.  */

/* Store a portable representation of VALUE in DEST using BYTES*8-1 bits.
   Return a nonzero value if VALUE requires more than BYTES*8-1 bits
   to store.  */

static int
__store_gcov_type (value, dest, bytes)
     gcov_type value;
     char *dest;
     size_t bytes;
{
  int upper_bit = (value < 0 ? 128 : 0);
  size_t i;

  if (value < 0)
    {
      gcov_type oldvalue = value;
      value = -value;
      if (oldvalue != -value)
	return 1;
    }

  for(i = 0 ; i < (sizeof (value) < bytes ? sizeof (value) : bytes) ; i++) {
    dest[i] = value & (i == (bytes - 1) ? 127 : 255);
    value = value / 256;
  }

  if (value && value != -1)
    return 1;

  for(; i < bytes ; i++)
    dest[i] = 0;
  dest[bytes - 1] |= upper_bit;
  return 0;
}

/* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store
   the result in DEST. Returns a nonzero value if the value in SOURCE
   will not fit in DEST.  */

static int
__fetch_gcov_type (dest, source, bytes)
     gcov_type *dest;
     char *source;
     size_t bytes;
{
  gcov_type value = 0;
  int i;

  for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
    if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
      return 1;

  for (; i >= 0; i--)
    value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));

  if ((source[bytes - 1] & 128) && (value > 0))
    value = - value;

  *dest = value;
  return 0;
}

static int
__fetch_long (dest, source, bytes)
     long *dest;
     char *source;
     size_t bytes;
{
  long value = 0;
  int i;

  for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
    if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
      return 1;

  for (; i >= 0; i--)
    value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));

  if ((source[bytes - 1] & 128) && (value > 0))
    value = - value;

  *dest = value;
  return 0;
}

/* Write a BYTES*8-bit quantity to FILE, portably. Returns a nonzero
   value if the write fails, or if VALUE can't be stored in BYTES*8
   bits.

   Note that VALUE may not actually be large enough to hold BYTES*8
   bits, but BYTES characters will be written anyway.

   BYTES may be a maximum of 10.  */

static int
__write_gcov_type (value, file, bytes)
     gcov_type value;
     FILE *file;
     size_t bytes;
{
  char c[10];

  if (bytes > 10 || __store_gcov_type (value, c, bytes))
    return 1;
  else
    return fwrite(c, 1, bytes, file) != bytes;
}

static int
__write_long (value, file, bytes)
     long value;
     FILE *file;
     size_t bytes;
{
  char c[10];

  if (bytes > 10 || __store_gcov_type ((gcov_type)value, c, bytes))
    return 1;
  else
    return fwrite(c, 1, bytes, file) != bytes;
}

/* Read a quantity containing BYTES bytes from FILE, portably. Return
   a nonzero value if the read fails or if the value will not fit
   in DEST.

   Note that DEST may not be large enough to hold all of the requested
   data, but the function will read BYTES characters anyway.

   BYTES may be a maximum of 10.  */

static int
__read_gcov_type (dest, file, bytes)
     gcov_type *dest;
     FILE *file;
     size_t bytes;
{
  char c[10];

  if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
    return 1;
  else
    return __fetch_gcov_type (dest, c, bytes);
}

static int
__read_long (dest, file, bytes)
     long *dest;
     FILE *file;
     size_t bytes;
{
  char c[10];

  if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
    return 1;
  else
    return __fetch_long (dest, c, bytes);
}


/* Writes string in gcov format.  */

static int
__write_gcov_string (string, length, file, delim)
     const char *string;
     size_t length;
     FILE *file;
     long delim;
{
  size_t temp = length + 1;

  /* delimiter */
  if (__write_long (delim, file, 4) != 0)
    return 1;

  if (__write_long (length, file, 4) != 0)
    return 1;

  if (fwrite (string, temp, 1, file) != 1)
    return 1;

  temp &= 3;

  if (temp)
    {
      char c[4];

      c[0] = c[1] = c[2] = c[3] = 0;

      if (fwrite (c, sizeof (char), 4 - temp, file) != 4 - temp)
	return 1;
    }

  if (__write_long (delim, file, 4) != 0)
    return 1;

  return 0;
}

/* Reads string in gcov format.  */


static int
__read_gcov_string (string, max_length, file, delim)
     char *string;
     size_t max_length;
     FILE *file;
     long delim;
{
  long delim_from_file;
  long length;
  long read_length;
  long tmp;

  if (__read_long (&delim_from_file, file, 4) != 0)
    return 1;

  if (delim_from_file != delim)
    return 1;

  if (__read_long (&length, file, 4) != 0)
    return 1;

  if (length > (long) max_length)
    read_length = max_length;
  else
    read_length = length;

  tmp = (((length + 1) - 1) / 4 + 1) * 4;
  /* This is the size occupied by the string in the file */

  if (fread (string, read_length, 1, file) != 1)
    return 1;

  string[read_length] = 0;

  if (fseek (file, tmp - read_length, SEEK_CUR) < 0)
    return 1;

  if (__read_long (&delim_from_file, file, 4) != 0)
    return 1;

  if (delim_from_file != delim)
    return 1;

  return 0;
}


#endif /* ! GCC_GCOV_IO_H */
OpenPOWER on IntegriCloud