| 241 | } |
| 242 | |
| 243 | int64_t getSignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits) { |
| 244 | int64_t value; |
| 245 | union {uint64_t u; int64_t i;} conv; |
| 246 | |
| 247 | /* Converting from unsigned to signed is undefined when the value does |
| 248 | * not fit, however here we assume two's complement and the original value |
| 249 | * was obtained from signed -> unsigned conversion, so we'll find the |
| 250 | * most significant bit set if the original value was negative. |
| 251 | * |
| 252 | * Note that two's complement is mandatory for exact-width types |
| 253 | * according to the C99 standard. */ |
| 254 | conv.u = getUnsignedBitfield(p,offset,bits); |
| 255 | value = conv.i; |
| 256 | |
| 257 | /* If the top significant bit is 1, propagate it to all the |
| 258 | * higher bits for two's complement representation of signed |
| 259 | * integers. */ |
| 260 | if (bits < 64 && (value & ((uint64_t)1 << (bits-1)))) |
| 261 | value |= ((uint64_t)-1) << bits; |
| 262 | return value; |
| 263 | } |
| 264 | |
| 265 | /* The following two functions detect overflow of a value in the context |
| 266 | * of storing it as an unsigned or signed integer with the specified |
no test coverage detected