00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef GCC_GCOV_IO_H
00031 #define GCC_GCOV_IO_H
00032 #include <stdio.h>
00033 #include <sys/types.h>
00034
00035 static int __fetch_long PARAMS ((long *, char *, size_t))
00036 ATTRIBUTE_UNUSED;
00037 static int __read_long PARAMS ((long *, FILE *, size_t))
00038 ATTRIBUTE_UNUSED;
00039 static int __write_long PARAMS ((long, FILE *, size_t))
00040 ATTRIBUTE_UNUSED;
00041 static int __fetch_gcov_type PARAMS ((gcov_type *, char *, size_t))
00042 ATTRIBUTE_UNUSED;
00043 static int __store_gcov_type PARAMS ((gcov_type, char *, size_t))
00044 ATTRIBUTE_UNUSED;
00045 static int __read_gcov_type PARAMS ((gcov_type *, FILE *, size_t))
00046 ATTRIBUTE_UNUSED;
00047 static int __write_gcov_type PARAMS ((gcov_type, FILE *, size_t))
00048 ATTRIBUTE_UNUSED;
00049 static int __write_gcov_string PARAMS ((const char *, size_t, FILE*, long))
00050 ATTRIBUTE_UNUSED;
00051 static int __read_gcov_string PARAMS ((char *, size_t, FILE*, long))
00052 ATTRIBUTE_UNUSED;
00053
00054
00055
00056
00057
00058
00059
00060 static int
00061 __store_gcov_type (value, dest, bytes)
00062 gcov_type value;
00063 char *dest;
00064 size_t bytes;
00065 {
00066 int upper_bit = (value < 0 ? 128 : 0);
00067 size_t i;
00068
00069 if (value < 0)
00070 {
00071 gcov_type oldvalue = value;
00072 value = -value;
00073 if (oldvalue != -value)
00074 return 1;
00075 }
00076
00077 for(i = 0 ; i < (sizeof (value) < bytes ? sizeof (value) : bytes) ; i++) {
00078 dest[i] = value & (i == (bytes - 1) ? 127 : 255);
00079 value = value / 256;
00080 }
00081
00082 if (value && value != -1)
00083 return 1;
00084
00085 for(; i < bytes ; i++)
00086 dest[i] = 0;
00087 dest[bytes - 1] |= upper_bit;
00088 return 0;
00089 }
00090
00091
00092
00093
00094
00095 static int
00096 __fetch_gcov_type (dest, source, bytes)
00097 gcov_type *dest;
00098 char *source;
00099 size_t bytes;
00100 {
00101 gcov_type value = 0;
00102 int i;
00103
00104 for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
00105 if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
00106 return 1;
00107
00108 for (; i >= 0; i--)
00109 value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));
00110
00111 if ((source[bytes - 1] & 128) && (value > 0))
00112 value = - value;
00113
00114 *dest = value;
00115 return 0;
00116 }
00117
00118 static int
00119 __fetch_long (dest, source, bytes)
00120 long *dest;
00121 char *source;
00122 size_t bytes;
00123 {
00124 long value = 0;
00125 int i;
00126
00127 for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
00128 if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
00129 return 1;
00130
00131 for (; i >= 0; i--)
00132 value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));
00133
00134 if ((source[bytes - 1] & 128) && (value > 0))
00135 value = - value;
00136
00137 *dest = value;
00138 return 0;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 static int
00151 __write_gcov_type (value, file, bytes)
00152 gcov_type value;
00153 FILE *file;
00154 size_t bytes;
00155 {
00156 char c[10];
00157
00158 if (bytes > 10 || __store_gcov_type (value, c, bytes))
00159 return 1;
00160 else
00161 return fwrite(c, 1, bytes, file) != bytes;
00162 }
00163
00164 static int
00165 __write_long (value, file, bytes)
00166 long value;
00167 FILE *file;
00168 size_t bytes;
00169 {
00170 char c[10];
00171
00172 if (bytes > 10 || __store_gcov_type ((gcov_type)value, c, bytes))
00173 return 1;
00174 else
00175 return fwrite(c, 1, bytes, file) != bytes;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 static int
00188 __read_gcov_type (dest, file, bytes)
00189 gcov_type *dest;
00190 FILE *file;
00191 size_t bytes;
00192 {
00193 char c[10];
00194
00195 if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
00196 return 1;
00197 else
00198 return __fetch_gcov_type (dest, c, bytes);
00199 }
00200
00201 static int
00202 __read_long (dest, file, bytes)
00203 long *dest;
00204 FILE *file;
00205 size_t bytes;
00206 {
00207 char c[10];
00208
00209 if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
00210 return 1;
00211 else
00212 return __fetch_long (dest, c, bytes);
00213 }
00214
00215
00216
00217
00218 static int
00219 __write_gcov_string (string, length, file, delim)
00220 const char *string;
00221 size_t length;
00222 FILE *file;
00223 long delim;
00224 {
00225 size_t temp = length + 1;
00226
00227
00228 if (__write_long (delim, file, 4) != 0)
00229 return 1;
00230
00231 if (__write_long (length, file, 4) != 0)
00232 return 1;
00233
00234 if (fwrite (string, temp, 1, file) != 1)
00235 return 1;
00236
00237 temp &= 3;
00238
00239 if (temp)
00240 {
00241 char c[4];
00242
00243 c[0] = c[1] = c[2] = c[3] = 0;
00244
00245 if (fwrite (c, sizeof (char), 4 - temp, file) != 4 - temp)
00246 return 1;
00247 }
00248
00249 if (__write_long (delim, file, 4) != 0)
00250 return 1;
00251
00252 return 0;
00253 }
00254
00255
00256
00257
00258 static int
00259 __read_gcov_string (string, max_length, file, delim)
00260 char *string;
00261 size_t max_length;
00262 FILE *file;
00263 long delim;
00264 {
00265 long delim_from_file;
00266 long length;
00267 long read_length;
00268 long tmp;
00269
00270 if (__read_long (&delim_from_file, file, 4) != 0)
00271 return 1;
00272
00273 if (delim_from_file != delim)
00274 return 1;
00275
00276 if (__read_long (&length, file, 4) != 0)
00277 return 1;
00278
00279 if (length > (long) max_length)
00280 read_length = max_length;
00281 else
00282 read_length = length;
00283
00284 tmp = (((length + 1) - 1) / 4 + 1) * 4;
00285
00286
00287 if (fread (string, read_length, 1, file) != 1)
00288 return 1;
00289
00290 string[read_length] = 0;
00291
00292 if (fseek (file, tmp - read_length, SEEK_CUR) < 0)
00293 return 1;
00294
00295 if (__read_long (&delim_from_file, file, 4) != 0)
00296 return 1;
00297
00298 if (delim_from_file != delim)
00299 return 1;
00300
00301 return 0;
00302 }
00303
00304
00305 #endif