| 1 | /* Machine-independent I/O routines for gcov. | 
|---|
| 2 |     | 
|---|
| 3 |    Copyright (c) 2000  Red Hat, Inc. All rights reserved. | 
|---|
| 4 |  | 
|---|
| 5 |    This copyrighted material is made available to anyone wishing to use, modify, | 
|---|
| 6 |    copy, or redistribute it subject to the terms and conditions of the BSD  | 
|---|
| 7 |    License.   This program is distributed in the hope that it will be useful,  | 
|---|
| 8 |    but WITHOUT ANY WARRANTY expressed or implied, including the implied  | 
|---|
| 9 |    warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  A copy of | 
|---|
| 10 |    this license is available at http://www.opensource.org/licenses. Any Red Hat | 
|---|
| 11 |    trademarks that are incorporated in the source code or documentation are not | 
|---|
| 12 |    subject to the BSD License and may only be used or replicated with the  | 
|---|
| 13 |    express permission of Red Hat, Inc. | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef GCC_GCOV_IO_H | 
|---|
| 18 | #define GCC_GCOV_IO_H | 
|---|
| 19 | #include <stdio.h> | 
|---|
| 20 | #include <sys/types.h> | 
|---|
| 21 |  | 
|---|
| 22 | static int __fetch_long (long *, char *, size_t); | 
|---|
| 23 | static int __store_long (long, char *, size_t); | 
|---|
| 24 | static int __read_long  (long *, FILE *, size_t); | 
|---|
| 25 | static int __write_long (long, FILE *, size_t); | 
|---|
| 26 |  | 
|---|
| 27 | /* These routines only work for signed values. */ | 
|---|
| 28 |  | 
|---|
| 29 | /* Store a portable representation of VALUE in DEST using BYTES*8-1 bits. | 
|---|
| 30 |    Return a non-zero value if VALUE requires more than BYTES*8-1 bits | 
|---|
| 31 |    to store. */ | 
|---|
| 32 |  | 
|---|
| 33 | static int | 
|---|
| 34 | __store_long (value, dest, bytes) | 
|---|
| 35 |      long value; | 
|---|
| 36 |      char *dest; | 
|---|
| 37 |      size_t bytes; | 
|---|
| 38 | { | 
|---|
| 39 |   int upper_bit = (value < 0 ? 128 : 0); | 
|---|
| 40 |   size_t i; | 
|---|
| 41 |  | 
|---|
| 42 |   if (value < 0) | 
|---|
| 43 |     { | 
|---|
| 44 |       long oldvalue = value; | 
|---|
| 45 |       value = -value; | 
|---|
| 46 |       if (oldvalue != -value) | 
|---|
| 47 |         return 1; | 
|---|
| 48 |     } | 
|---|
| 49 |  | 
|---|
| 50 |   for(i = 0 ; i < (sizeof (value) < bytes ? sizeof (value) : bytes) ; i++) { | 
|---|
| 51 |     dest[i] = value & (i == (bytes - 1) ? 127 : 255); | 
|---|
| 52 |     value = value / 256; | 
|---|
| 53 |   } | 
|---|
| 54 |  | 
|---|
| 55 |   if (value && value != -1) | 
|---|
| 56 |     return 1; | 
|---|
| 57 |  | 
|---|
| 58 |   for(; i < bytes ; i++)  | 
|---|
| 59 |     dest[i] = 0; | 
|---|
| 60 |   dest[bytes - 1] |= upper_bit; | 
|---|
| 61 |   return 0; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | /* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store | 
|---|
| 65 |    the result in DEST. Returns a non-zero value if the value in SOURCE | 
|---|
| 66 |    will not fit in DEST. */ | 
|---|
| 67 |  | 
|---|
| 68 | static int | 
|---|
| 69 | __fetch_long (dest, source, bytes) | 
|---|
| 70 |      long *dest; | 
|---|
| 71 |      char *source; | 
|---|
| 72 |      size_t bytes; | 
|---|
| 73 | { | 
|---|
| 74 |   long value = 0; | 
|---|
| 75 |   int i; | 
|---|
| 76 |  | 
|---|
| 77 |   for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--) | 
|---|
| 78 |     if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 )) | 
|---|
| 79 |       return 1; | 
|---|
| 80 |  | 
|---|
| 81 |   for (; i >= 0; i--) | 
|---|
| 82 |     value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255)); | 
|---|
| 83 |  | 
|---|
| 84 |   if ((source[bytes - 1] & 128) && (value > 0)) | 
|---|
| 85 |     value = - value; | 
|---|
| 86 |  | 
|---|
| 87 |   *dest = value; | 
|---|
| 88 |   return 0; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /* Write a BYTES*8-bit quantity to FILE, portably. Returns a non-zero | 
|---|
| 92 |    value if the write fails, or if VALUE can't be stored in BYTES*8 | 
|---|
| 93 |    bits. | 
|---|
| 94 |  | 
|---|
| 95 |    Note that VALUE may not actually be large enough to hold BYTES*8 | 
|---|
| 96 |    bits, but BYTES characters will be written anyway. | 
|---|
| 97 |  | 
|---|
| 98 |    BYTES may be a maximum of 10. */ | 
|---|
| 99 |  | 
|---|
| 100 | static int | 
|---|
| 101 | __write_long (value, file, bytes) | 
|---|
| 102 |      long value; | 
|---|
| 103 |      FILE *file; | 
|---|
| 104 |      size_t bytes; | 
|---|
| 105 | { | 
|---|
| 106 |   char c[10]; | 
|---|
| 107 |  | 
|---|
| 108 |   if (bytes > 10 || __store_long (value, c, bytes)) | 
|---|
| 109 |     return 1; | 
|---|
| 110 |   else | 
|---|
| 111 |     return fwrite(c, 1, bytes, file) != bytes; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | /* Read a quantity containing BYTES bytes from FILE, portably. Return | 
|---|
| 115 |    a non-zero value if the read fails or if the value will not fit | 
|---|
| 116 |    in DEST. | 
|---|
| 117 |  | 
|---|
| 118 |    Note that DEST may not be large enough to hold all of the requested | 
|---|
| 119 |    data, but the function will read BYTES characters anyway. | 
|---|
| 120 |  | 
|---|
| 121 |    BYTES may be a maximum of 10. */ | 
|---|
| 122 |  | 
|---|
| 123 | static int | 
|---|
| 124 | __read_long (dest, file, bytes) | 
|---|
| 125 |      long *dest; | 
|---|
| 126 |      FILE *file; | 
|---|
| 127 |      size_t bytes; | 
|---|
| 128 | { | 
|---|
| 129 |   char c[10]; | 
|---|
| 130 |  | 
|---|
| 131 |   if (bytes > 10 || fread(c, 1, bytes, file) != bytes) | 
|---|
| 132 |     return 1; | 
|---|
| 133 |   else | 
|---|
| 134 |     return __fetch_long (dest, c, bytes); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | #endif /* ! GCC_GCOV_IO_H */ | 
|---|