| 2184 | } |
| 2185 | |
| 2186 | void write_varlong(int f, int64 x, uchar min_bytes) |
| 2187 | { |
| 2188 | char b[9]; |
| 2189 | uchar bit; |
| 2190 | int cnt = 8; |
| 2191 | |
| 2192 | #if SIZEOF_INT64 >= 8 |
| 2193 | SIVAL64(b, 1, x); |
| 2194 | #else |
| 2195 | SIVAL(b, 1, x); |
| 2196 | if (x <= 0x7FFFFFFF && x >= 0) |
| 2197 | memset(b + 5, 0, 4); |
| 2198 | else { |
| 2199 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); |
| 2200 | exit_cleanup(RERR_UNSUPPORTED); |
| 2201 | } |
| 2202 | #endif |
| 2203 | |
| 2204 | while (cnt > min_bytes && b[cnt] == 0) |
| 2205 | cnt--; |
| 2206 | bit = ((uchar)1<<(7-cnt+min_bytes)); |
| 2207 | if (CVAL(b, cnt) >= bit) { |
| 2208 | cnt++; |
| 2209 | *b = ~(bit-1); |
| 2210 | } else if (cnt > min_bytes) |
| 2211 | *b = b[cnt] | ~(bit*2-1); |
| 2212 | else |
| 2213 | *b = b[cnt]; |
| 2214 | |
| 2215 | write_buf(f, b, cnt); |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any |
no test coverage detected