Geant4 Cross Reference |
1 /* gzlib.c -- zlib functions common to reading 1 /* gzlib.c -- zlib functions common to reading and writing gzip files 2 * Copyright (C) 2004-2019 Mark Adler << 2 * Copyright (C) 2004-2017 Mark Adler 3 * For conditions of distribution and use, see 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 4 */ 5 5 6 #include "gzguts.h" 6 #include "gzguts.h" 7 7 8 #if defined(_WIN32) && !defined(__BORLANDC__) << 8 #if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__) 9 # define LSEEK _lseeki64 9 # define LSEEK _lseeki64 10 #else 10 #else 11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LAR 11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 12 # define LSEEK lseek64 12 # define LSEEK lseek64 13 #else 13 #else 14 # define LSEEK lseek 14 # define LSEEK lseek 15 #endif 15 #endif 16 #endif 16 #endif 17 17 18 /* Local functions */ 18 /* Local functions */ 19 local void gz_reset OF((gz_statep)); 19 local void gz_reset OF((gz_statep)); 20 local gzFile gz_open OF((const void *, int, co 20 local gzFile gz_open OF((const void *, int, const char *)); 21 21 22 #if defined UNDER_CE 22 #if defined UNDER_CE 23 23 24 /* Map the Windows error number in ERROR to a 24 /* Map the Windows error number in ERROR to a locale-dependent error message 25 string and return a pointer to it. Typical 25 string and return a pointer to it. Typically, the values for ERROR come 26 from GetLastError. 26 from GetLastError. 27 27 28 The string pointed to shall not be modified 28 The string pointed to shall not be modified by the application, but may be 29 overwritten by a subsequent call to gz_strw 29 overwritten by a subsequent call to gz_strwinerror 30 30 31 The gz_strwinerror function does not change 31 The gz_strwinerror function does not change the current setting of 32 GetLastError. */ 32 GetLastError. */ 33 char ZLIB_INTERNAL *gz_strwinerror(error) << 33 char ZLIB_INTERNAL *gz_strwinerror (error) 34 DWORD error; 34 DWORD error; 35 { 35 { 36 static char buf[1024]; 36 static char buf[1024]; 37 37 38 wchar_t *msgbuf; 38 wchar_t *msgbuf; 39 DWORD lasterr = GetLastError(); 39 DWORD lasterr = GetLastError(); 40 DWORD chars = FormatMessage(FORMAT_MESSAGE 40 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM 41 | FORMAT_MESSAGE_ALLOCATE_BUFFER, 41 | FORMAT_MESSAGE_ALLOCATE_BUFFER, 42 NULL, 42 NULL, 43 error, 43 error, 44 0, /* Default language */ 44 0, /* Default language */ 45 (LPVOID)&msgbuf, 45 (LPVOID)&msgbuf, 46 0, 46 0, 47 NULL); 47 NULL); 48 if (chars != 0) { 48 if (chars != 0) { 49 /* If there is an \r\n appended, zap i 49 /* If there is an \r\n appended, zap it. */ 50 if (chars >= 2 50 if (chars >= 2 51 && msgbuf[chars - 2] == '\r' && ms 51 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { 52 chars -= 2; 52 chars -= 2; 53 msgbuf[chars] = 0; 53 msgbuf[chars] = 0; 54 } 54 } 55 55 56 if (chars > sizeof (buf) - 1) { 56 if (chars > sizeof (buf) - 1) { 57 chars = sizeof (buf) - 1; 57 chars = sizeof (buf) - 1; 58 msgbuf[chars] = 0; 58 msgbuf[chars] = 0; 59 } 59 } 60 60 61 wcstombs(buf, msgbuf, chars + 1); 61 wcstombs(buf, msgbuf, chars + 1); 62 LocalFree(msgbuf); 62 LocalFree(msgbuf); 63 } 63 } 64 else { 64 else { 65 sprintf(buf, "unknown win32 error (%ld 65 sprintf(buf, "unknown win32 error (%ld)", error); 66 } 66 } 67 67 68 SetLastError(lasterr); 68 SetLastError(lasterr); 69 return buf; 69 return buf; 70 } 70 } 71 71 72 #endif /* UNDER_CE */ 72 #endif /* UNDER_CE */ 73 73 74 /* Reset gzip file state */ 74 /* Reset gzip file state */ 75 local void gz_reset(state) 75 local void gz_reset(state) 76 gz_statep state; 76 gz_statep state; 77 { 77 { 78 state->x.have = 0; /* no outp 78 state->x.have = 0; /* no output data available */ 79 if (state->mode == GZ_READ) { /* for rea 79 if (state->mode == GZ_READ) { /* for reading ... */ 80 state->eof = 0; /* not at 80 state->eof = 0; /* not at end of file */ 81 state->past = 0; /* have no 81 state->past = 0; /* have not read past end yet */ 82 state->how = LOOK; /* look fo 82 state->how = LOOK; /* look for gzip header */ 83 } 83 } 84 else /* for wri << 85 state->reset = 0; /* no defl << 86 state->seek = 0; /* no seek 84 state->seek = 0; /* no seek request pending */ 87 gz_error(state, Z_OK, NULL); /* clear e 85 gz_error(state, Z_OK, NULL); /* clear error */ 88 state->x.pos = 0; /* no unco 86 state->x.pos = 0; /* no uncompressed data yet */ 89 state->strm.avail_in = 0; /* no inpu 87 state->strm.avail_in = 0; /* no input data yet */ 90 } 88 } 91 89 92 /* Open a gzip file either by name or file des 90 /* Open a gzip file either by name or file descriptor. */ 93 local gzFile gz_open(path, fd, mode) 91 local gzFile gz_open(path, fd, mode) 94 const void *path; 92 const void *path; 95 int fd; 93 int fd; 96 const char *mode; 94 const char *mode; 97 { 95 { 98 gz_statep state; 96 gz_statep state; 99 z_size_t len; 97 z_size_t len; 100 int oflag; 98 int oflag; 101 #ifdef O_CLOEXEC 99 #ifdef O_CLOEXEC 102 int cloexec = 0; 100 int cloexec = 0; 103 #endif 101 #endif 104 #ifdef O_EXCL 102 #ifdef O_EXCL 105 int exclusive = 0; 103 int exclusive = 0; 106 #endif 104 #endif 107 105 108 /* check input */ 106 /* check input */ 109 if (path == NULL) 107 if (path == NULL) 110 return NULL; 108 return NULL; 111 109 112 /* allocate gzFile structure to return */ 110 /* allocate gzFile structure to return */ 113 state = (gz_statep)malloc(sizeof(gz_state) 111 state = (gz_statep)malloc(sizeof(gz_state)); 114 if (state == NULL) 112 if (state == NULL) 115 return NULL; 113 return NULL; 116 state->size = 0; /* no buffers 114 state->size = 0; /* no buffers allocated yet */ 117 state->want = GZBUFSIZE; /* requested b 115 state->want = GZBUFSIZE; /* requested buffer size */ 118 state->msg = NULL; /* no error me 116 state->msg = NULL; /* no error message yet */ 119 117 120 /* interpret mode */ 118 /* interpret mode */ 121 state->mode = GZ_NONE; 119 state->mode = GZ_NONE; 122 state->level = Z_DEFAULT_COMPRESSION; 120 state->level = Z_DEFAULT_COMPRESSION; 123 state->strategy = Z_DEFAULT_STRATEGY; 121 state->strategy = Z_DEFAULT_STRATEGY; 124 state->direct = 0; 122 state->direct = 0; 125 while (*mode) { 123 while (*mode) { 126 if (*mode >= '0' && *mode <= '9') 124 if (*mode >= '0' && *mode <= '9') 127 state->level = *mode - '0'; 125 state->level = *mode - '0'; 128 else 126 else 129 switch (*mode) { 127 switch (*mode) { 130 case 'r': 128 case 'r': 131 state->mode = GZ_READ; 129 state->mode = GZ_READ; 132 break; 130 break; 133 #ifndef NO_GZCOMPRESS 131 #ifndef NO_GZCOMPRESS 134 case 'w': 132 case 'w': 135 state->mode = GZ_WRITE; 133 state->mode = GZ_WRITE; 136 break; 134 break; 137 case 'a': 135 case 'a': 138 state->mode = GZ_APPEND; 136 state->mode = GZ_APPEND; 139 break; 137 break; 140 #endif 138 #endif 141 case '+': /* can't read and 139 case '+': /* can't read and write at the same time */ 142 free(state); 140 free(state); 143 return NULL; 141 return NULL; 144 case 'b': /* ignore -- will 142 case 'b': /* ignore -- will request binary anyway */ 145 break; 143 break; 146 #ifdef O_CLOEXEC 144 #ifdef O_CLOEXEC 147 case 'e': 145 case 'e': 148 cloexec = 1; 146 cloexec = 1; 149 break; 147 break; 150 #endif 148 #endif 151 #ifdef O_EXCL 149 #ifdef O_EXCL 152 case 'x': 150 case 'x': 153 exclusive = 1; 151 exclusive = 1; 154 break; 152 break; 155 #endif 153 #endif 156 case 'f': 154 case 'f': 157 state->strategy = Z_FILTERED; 155 state->strategy = Z_FILTERED; 158 break; 156 break; 159 case 'h': 157 case 'h': 160 state->strategy = Z_HUFFMAN_ON 158 state->strategy = Z_HUFFMAN_ONLY; 161 break; 159 break; 162 case 'R': 160 case 'R': 163 state->strategy = Z_RLE; 161 state->strategy = Z_RLE; 164 break; 162 break; 165 case 'F': 163 case 'F': 166 state->strategy = Z_FIXED; 164 state->strategy = Z_FIXED; 167 break; 165 break; 168 case 'T': 166 case 'T': 169 state->direct = 1; 167 state->direct = 1; 170 break; 168 break; 171 default: /* could consider 169 default: /* could consider as an error, but just ignore */ 172 ; 170 ; 173 } 171 } 174 mode++; 172 mode++; 175 } 173 } 176 174 177 /* must provide an "r", "w", or "a" */ 175 /* must provide an "r", "w", or "a" */ 178 if (state->mode == GZ_NONE) { 176 if (state->mode == GZ_NONE) { 179 free(state); 177 free(state); 180 return NULL; 178 return NULL; 181 } 179 } 182 180 183 /* can't force transparent read */ 181 /* can't force transparent read */ 184 if (state->mode == GZ_READ) { 182 if (state->mode == GZ_READ) { 185 if (state->direct) { 183 if (state->direct) { 186 free(state); 184 free(state); 187 return NULL; 185 return NULL; 188 } 186 } 189 state->direct = 1; /* for empty f 187 state->direct = 1; /* for empty file */ 190 } 188 } 191 189 192 /* save the path name for error messages * 190 /* save the path name for error messages */ 193 #ifdef WIDECHAR 191 #ifdef WIDECHAR 194 if (fd == -2) { 192 if (fd == -2) { 195 len = wcstombs(NULL, path, 0); 193 len = wcstombs(NULL, path, 0); 196 if (len == (z_size_t)-1) 194 if (len == (z_size_t)-1) 197 len = 0; 195 len = 0; 198 } 196 } 199 else 197 else 200 #endif 198 #endif 201 len = strlen((const char *)path); 199 len = strlen((const char *)path); 202 state->path = (char *)malloc(len + 1); 200 state->path = (char *)malloc(len + 1); 203 if (state->path == NULL) { 201 if (state->path == NULL) { 204 free(state); 202 free(state); 205 return NULL; 203 return NULL; 206 } 204 } 207 #ifdef WIDECHAR 205 #ifdef WIDECHAR 208 if (fd == -2) 206 if (fd == -2) 209 if (len) 207 if (len) 210 wcstombs(state->path, path, len + 208 wcstombs(state->path, path, len + 1); 211 else 209 else 212 *(state->path) = 0; 210 *(state->path) = 0; 213 else 211 else 214 #endif 212 #endif 215 #if !defined(NO_snprintf) && !defined(NO_vsnpr 213 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 216 (void)snprintf(state->path, len + 1, " 214 (void)snprintf(state->path, len + 1, "%s", (const char *)path); 217 #else 215 #else 218 strcpy(state->path, path); 216 strcpy(state->path, path); 219 #endif 217 #endif 220 218 221 /* compute the flags for open() */ 219 /* compute the flags for open() */ 222 oflag = 220 oflag = 223 #ifdef O_LARGEFILE 221 #ifdef O_LARGEFILE 224 O_LARGEFILE | 222 O_LARGEFILE | 225 #endif 223 #endif 226 #ifdef O_BINARY 224 #ifdef O_BINARY 227 O_BINARY | 225 O_BINARY | 228 #endif 226 #endif 229 #ifdef O_CLOEXEC 227 #ifdef O_CLOEXEC 230 (cloexec ? O_CLOEXEC : 0) | 228 (cloexec ? O_CLOEXEC : 0) | 231 #endif 229 #endif 232 (state->mode == GZ_READ ? 230 (state->mode == GZ_READ ? 233 O_RDONLY : 231 O_RDONLY : 234 (O_WRONLY | O_CREAT | 232 (O_WRONLY | O_CREAT | 235 #ifdef O_EXCL 233 #ifdef O_EXCL 236 (exclusive ? O_EXCL : 0) | 234 (exclusive ? O_EXCL : 0) | 237 #endif 235 #endif 238 (state->mode == GZ_WRITE ? 236 (state->mode == GZ_WRITE ? 239 O_TRUNC : 237 O_TRUNC : 240 O_APPEND))); 238 O_APPEND))); 241 239 242 /* open the file with the appropriate flag 240 /* open the file with the appropriate flags (or just use fd) */ 243 state->fd = fd > -1 ? fd : ( 241 state->fd = fd > -1 ? fd : ( 244 #ifdef WIDECHAR 242 #ifdef WIDECHAR 245 fd == -2 ? _wopen(path, oflag, 0666) : 243 fd == -2 ? _wopen(path, oflag, 0666) : 246 #endif 244 #endif 247 open((const char *)path, oflag, 0666)) 245 open((const char *)path, oflag, 0666)); 248 if (state->fd == -1) { 246 if (state->fd == -1) { 249 free(state->path); 247 free(state->path); 250 free(state); 248 free(state); 251 return NULL; 249 return NULL; 252 } 250 } 253 if (state->mode == GZ_APPEND) { 251 if (state->mode == GZ_APPEND) { 254 LSEEK(state->fd, 0, SEEK_END); /* so 252 LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */ 255 state->mode = GZ_WRITE; /* sim 253 state->mode = GZ_WRITE; /* simplify later checks */ 256 } 254 } 257 255 258 /* save the current position for rewinding 256 /* save the current position for rewinding (only if reading) */ 259 if (state->mode == GZ_READ) { 257 if (state->mode == GZ_READ) { 260 state->start = LSEEK(state->fd, 0, SEE 258 state->start = LSEEK(state->fd, 0, SEEK_CUR); 261 if (state->start == -1) state->start = 259 if (state->start == -1) state->start = 0; 262 } 260 } 263 261 264 /* initialize stream */ 262 /* initialize stream */ 265 gz_reset(state); 263 gz_reset(state); 266 264 267 /* return stream */ 265 /* return stream */ 268 return (gzFile)state; 266 return (gzFile)state; 269 } 267 } 270 268 271 /* -- see zlib.h -- */ 269 /* -- see zlib.h -- */ 272 gzFile ZEXPORT gzopen(path, mode) 270 gzFile ZEXPORT gzopen(path, mode) 273 const char *path; 271 const char *path; 274 const char *mode; 272 const char *mode; 275 { 273 { 276 return gz_open(path, -1, mode); 274 return gz_open(path, -1, mode); 277 } 275 } 278 276 279 /* -- see zlib.h -- */ 277 /* -- see zlib.h -- */ 280 gzFile ZEXPORT gzopen64(path, mode) 278 gzFile ZEXPORT gzopen64(path, mode) 281 const char *path; 279 const char *path; 282 const char *mode; 280 const char *mode; 283 { 281 { 284 return gz_open(path, -1, mode); 282 return gz_open(path, -1, mode); 285 } 283 } 286 284 287 /* -- see zlib.h -- */ 285 /* -- see zlib.h -- */ 288 gzFile ZEXPORT gzdopen(fd, mode) 286 gzFile ZEXPORT gzdopen(fd, mode) 289 int fd; 287 int fd; 290 const char *mode; 288 const char *mode; 291 { 289 { 292 char *path; /* identifier for erro 290 char *path; /* identifier for error messages */ 293 gzFile gz; 291 gzFile gz; 294 292 295 if (fd == -1 || (path = (char *)malloc(7 + 293 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) 296 return NULL; 294 return NULL; 297 #if !defined(NO_snprintf) && !defined(NO_vsnpr 295 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 298 (void)snprintf(path, 7 + 3 * sizeof(int), 296 (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); 299 #else 297 #else 300 sprintf(path, "<fd:%d>", fd); /* for deb 298 sprintf(path, "<fd:%d>", fd); /* for debugging */ 301 #endif 299 #endif 302 gz = gz_open(path, fd, mode); 300 gz = gz_open(path, fd, mode); 303 free(path); 301 free(path); 304 return gz; 302 return gz; 305 } 303 } 306 304 307 /* -- see zlib.h -- */ 305 /* -- see zlib.h -- */ 308 #ifdef WIDECHAR 306 #ifdef WIDECHAR 309 gzFile ZEXPORT gzopen_w(path, mode) 307 gzFile ZEXPORT gzopen_w(path, mode) 310 const wchar_t *path; 308 const wchar_t *path; 311 const char *mode; 309 const char *mode; 312 { 310 { 313 return gz_open(path, -2, mode); 311 return gz_open(path, -2, mode); 314 } 312 } 315 #endif 313 #endif 316 314 317 /* -- see zlib.h -- */ 315 /* -- see zlib.h -- */ 318 int ZEXPORT gzbuffer(file, size) 316 int ZEXPORT gzbuffer(file, size) 319 gzFile file; 317 gzFile file; 320 unsigned size; 318 unsigned size; 321 { 319 { 322 gz_statep state; 320 gz_statep state; 323 321 324 /* get internal structure and check integr 322 /* get internal structure and check integrity */ 325 if (file == NULL) 323 if (file == NULL) 326 return -1; 324 return -1; 327 state = (gz_statep)file; 325 state = (gz_statep)file; 328 if (state->mode != GZ_READ && state->mode 326 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 329 return -1; 327 return -1; 330 328 331 /* make sure we haven't already allocated 329 /* make sure we haven't already allocated memory */ 332 if (state->size != 0) 330 if (state->size != 0) 333 return -1; 331 return -1; 334 332 335 /* check and set requested size */ 333 /* check and set requested size */ 336 if ((size << 1) < size) 334 if ((size << 1) < size) 337 return -1; /* need to be 335 return -1; /* need to be able to double it */ 338 if (size < 2) 336 if (size < 2) 339 size = 2; /* need two by 337 size = 2; /* need two bytes to check magic header */ 340 state->want = size; 338 state->want = size; 341 return 0; 339 return 0; 342 } 340 } 343 341 344 /* -- see zlib.h -- */ 342 /* -- see zlib.h -- */ 345 int ZEXPORT gzrewind(file) 343 int ZEXPORT gzrewind(file) 346 gzFile file; 344 gzFile file; 347 { 345 { 348 gz_statep state; 346 gz_statep state; 349 347 350 /* get internal structure */ 348 /* get internal structure */ 351 if (file == NULL) 349 if (file == NULL) 352 return -1; 350 return -1; 353 state = (gz_statep)file; 351 state = (gz_statep)file; 354 352 355 /* check that we're reading and that there 353 /* check that we're reading and that there's no error */ 356 if (state->mode != GZ_READ || 354 if (state->mode != GZ_READ || 357 (state->err != Z_OK && state->err 355 (state->err != Z_OK && state->err != Z_BUF_ERROR)) 358 return -1; 356 return -1; 359 357 360 /* back up and start over */ 358 /* back up and start over */ 361 if (LSEEK(state->fd, state->start, SEEK_SE 359 if (LSEEK(state->fd, state->start, SEEK_SET) == -1) 362 return -1; 360 return -1; 363 gz_reset(state); 361 gz_reset(state); 364 return 0; 362 return 0; 365 } 363 } 366 364 367 /* -- see zlib.h -- */ 365 /* -- see zlib.h -- */ 368 z_off64_t ZEXPORT gzseek64(file, offset, whenc 366 z_off64_t ZEXPORT gzseek64(file, offset, whence) 369 gzFile file; 367 gzFile file; 370 z_off64_t offset; 368 z_off64_t offset; 371 int whence; 369 int whence; 372 { 370 { 373 unsigned n; 371 unsigned n; 374 z_off64_t ret; 372 z_off64_t ret; 375 gz_statep state; 373 gz_statep state; 376 374 377 /* get internal structure and check integr 375 /* get internal structure and check integrity */ 378 if (file == NULL) 376 if (file == NULL) 379 return -1; 377 return -1; 380 state = (gz_statep)file; 378 state = (gz_statep)file; 381 if (state->mode != GZ_READ && state->mode 379 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 382 return -1; 380 return -1; 383 381 384 /* check that there's no error */ 382 /* check that there's no error */ 385 if (state->err != Z_OK && state->err != Z_ 383 if (state->err != Z_OK && state->err != Z_BUF_ERROR) 386 return -1; 384 return -1; 387 385 388 /* can only seek from start or relative to 386 /* can only seek from start or relative to current position */ 389 if (whence != SEEK_SET && whence != SEEK_C 387 if (whence != SEEK_SET && whence != SEEK_CUR) 390 return -1; 388 return -1; 391 389 392 /* normalize offset to a SEEK_CUR specific 390 /* normalize offset to a SEEK_CUR specification */ 393 if (whence == SEEK_SET) 391 if (whence == SEEK_SET) 394 offset -= state->x.pos; 392 offset -= state->x.pos; 395 else if (state->seek) 393 else if (state->seek) 396 offset += state->skip; 394 offset += state->skip; 397 state->seek = 0; 395 state->seek = 0; 398 396 399 /* if within raw area while reading, just 397 /* if within raw area while reading, just go there */ 400 if (state->mode == GZ_READ && state->how = 398 if (state->mode == GZ_READ && state->how == COPY && 401 state->x.pos + offset >= 0) { 399 state->x.pos + offset >= 0) { 402 ret = LSEEK(state->fd, offset - (z_off << 400 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); 403 if (ret == -1) 401 if (ret == -1) 404 return -1; 402 return -1; 405 state->x.have = 0; 403 state->x.have = 0; 406 state->eof = 0; 404 state->eof = 0; 407 state->past = 0; 405 state->past = 0; 408 state->seek = 0; 406 state->seek = 0; 409 gz_error(state, Z_OK, NULL); 407 gz_error(state, Z_OK, NULL); 410 state->strm.avail_in = 0; 408 state->strm.avail_in = 0; 411 state->x.pos += offset; 409 state->x.pos += offset; 412 return state->x.pos; 410 return state->x.pos; 413 } 411 } 414 412 415 /* calculate skip amount, rewinding if nee 413 /* calculate skip amount, rewinding if needed for back seek when reading */ 416 if (offset < 0) { 414 if (offset < 0) { 417 if (state->mode != GZ_READ) /* 415 if (state->mode != GZ_READ) /* writing -- can't go backwards */ 418 return -1; 416 return -1; 419 offset += state->x.pos; 417 offset += state->x.pos; 420 if (offset < 0) /* 418 if (offset < 0) /* before start of file! */ 421 return -1; 419 return -1; 422 if (gzrewind(file) == -1) /* 420 if (gzrewind(file) == -1) /* rewind, then skip to offset */ 423 return -1; 421 return -1; 424 } 422 } 425 423 426 /* if reading, skip what's in output buffe 424 /* if reading, skip what's in output buffer (one less gzgetc() check) */ 427 if (state->mode == GZ_READ) { 425 if (state->mode == GZ_READ) { 428 n = GT_OFF(state->x.have) || (z_off64_ 426 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? 429 (unsigned)offset : state->x.have; 427 (unsigned)offset : state->x.have; 430 state->x.have -= n; 428 state->x.have -= n; 431 state->x.next += n; 429 state->x.next += n; 432 state->x.pos += n; 430 state->x.pos += n; 433 offset -= n; 431 offset -= n; 434 } 432 } 435 433 436 /* request skip (if not zero) */ 434 /* request skip (if not zero) */ 437 if (offset) { 435 if (offset) { 438 state->seek = 1; 436 state->seek = 1; 439 state->skip = offset; 437 state->skip = offset; 440 } 438 } 441 return state->x.pos + offset; 439 return state->x.pos + offset; 442 } 440 } 443 441 444 /* -- see zlib.h -- */ 442 /* -- see zlib.h -- */ 445 z_off_t ZEXPORT gzseek(file, offset, whence) 443 z_off_t ZEXPORT gzseek(file, offset, whence) 446 gzFile file; 444 gzFile file; 447 z_off_t offset; 445 z_off_t offset; 448 int whence; 446 int whence; 449 { 447 { 450 z_off64_t ret; 448 z_off64_t ret; 451 449 452 ret = gzseek64(file, (z_off64_t)offset, wh 450 ret = gzseek64(file, (z_off64_t)offset, whence); 453 return ret == (z_off_t)ret ? (z_off_t)ret 451 return ret == (z_off_t)ret ? (z_off_t)ret : -1; 454 } 452 } 455 453 456 /* -- see zlib.h -- */ 454 /* -- see zlib.h -- */ 457 z_off64_t ZEXPORT gztell64(file) 455 z_off64_t ZEXPORT gztell64(file) 458 gzFile file; 456 gzFile file; 459 { 457 { 460 gz_statep state; 458 gz_statep state; 461 459 462 /* get internal structure and check integr 460 /* get internal structure and check integrity */ 463 if (file == NULL) 461 if (file == NULL) 464 return -1; 462 return -1; 465 state = (gz_statep)file; 463 state = (gz_statep)file; 466 if (state->mode != GZ_READ && state->mode 464 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 467 return -1; 465 return -1; 468 466 469 /* return position */ 467 /* return position */ 470 return state->x.pos + (state->seek ? state 468 return state->x.pos + (state->seek ? state->skip : 0); 471 } 469 } 472 470 473 /* -- see zlib.h -- */ 471 /* -- see zlib.h -- */ 474 z_off_t ZEXPORT gztell(file) 472 z_off_t ZEXPORT gztell(file) 475 gzFile file; 473 gzFile file; 476 { 474 { 477 z_off64_t ret; 475 z_off64_t ret; 478 476 479 ret = gztell64(file); 477 ret = gztell64(file); 480 return ret == (z_off_t)ret ? (z_off_t)ret 478 return ret == (z_off_t)ret ? (z_off_t)ret : -1; 481 } 479 } 482 480 483 /* -- see zlib.h -- */ 481 /* -- see zlib.h -- */ 484 z_off64_t ZEXPORT gzoffset64(file) 482 z_off64_t ZEXPORT gzoffset64(file) 485 gzFile file; 483 gzFile file; 486 { 484 { 487 z_off64_t offset; 485 z_off64_t offset; 488 gz_statep state; 486 gz_statep state; 489 487 490 /* get internal structure and check integr 488 /* get internal structure and check integrity */ 491 if (file == NULL) 489 if (file == NULL) 492 return -1; 490 return -1; 493 state = (gz_statep)file; 491 state = (gz_statep)file; 494 if (state->mode != GZ_READ && state->mode 492 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 495 return -1; 493 return -1; 496 494 497 /* compute and return effective offset in 495 /* compute and return effective offset in file */ 498 offset = LSEEK(state->fd, 0, SEEK_CUR); 496 offset = LSEEK(state->fd, 0, SEEK_CUR); 499 if (offset == -1) 497 if (offset == -1) 500 return -1; 498 return -1; 501 if (state->mode == GZ_READ) /* 499 if (state->mode == GZ_READ) /* reading */ 502 offset -= state->strm.avail_in; /* 500 offset -= state->strm.avail_in; /* don't count buffered input */ 503 return offset; 501 return offset; 504 } 502 } 505 503 506 /* -- see zlib.h -- */ 504 /* -- see zlib.h -- */ 507 z_off_t ZEXPORT gzoffset(file) 505 z_off_t ZEXPORT gzoffset(file) 508 gzFile file; 506 gzFile file; 509 { 507 { 510 z_off64_t ret; 508 z_off64_t ret; 511 509 512 ret = gzoffset64(file); 510 ret = gzoffset64(file); 513 return ret == (z_off_t)ret ? (z_off_t)ret 511 return ret == (z_off_t)ret ? (z_off_t)ret : -1; 514 } 512 } 515 513 516 /* -- see zlib.h -- */ 514 /* -- see zlib.h -- */ 517 int ZEXPORT gzeof(file) 515 int ZEXPORT gzeof(file) 518 gzFile file; 516 gzFile file; 519 { 517 { 520 gz_statep state; 518 gz_statep state; 521 519 522 /* get internal structure and check integr 520 /* get internal structure and check integrity */ 523 if (file == NULL) 521 if (file == NULL) 524 return 0; 522 return 0; 525 state = (gz_statep)file; 523 state = (gz_statep)file; 526 if (state->mode != GZ_READ && state->mode 524 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 527 return 0; 525 return 0; 528 526 529 /* return end-of-file state */ 527 /* return end-of-file state */ 530 return state->mode == GZ_READ ? state->pas 528 return state->mode == GZ_READ ? state->past : 0; 531 } 529 } 532 530 533 /* -- see zlib.h -- */ 531 /* -- see zlib.h -- */ 534 const char * ZEXPORT gzerror(file, errnum) 532 const char * ZEXPORT gzerror(file, errnum) 535 gzFile file; 533 gzFile file; 536 int *errnum; 534 int *errnum; 537 { 535 { 538 gz_statep state; 536 gz_statep state; 539 537 540 /* get internal structure and check integr 538 /* get internal structure and check integrity */ 541 if (file == NULL) 539 if (file == NULL) 542 return NULL; 540 return NULL; 543 state = (gz_statep)file; 541 state = (gz_statep)file; 544 if (state->mode != GZ_READ && state->mode 542 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 545 return NULL; 543 return NULL; 546 544 547 /* return error information */ 545 /* return error information */ 548 if (errnum != NULL) 546 if (errnum != NULL) 549 *errnum = state->err; 547 *errnum = state->err; 550 return state->err == Z_MEM_ERROR ? "out of 548 return state->err == Z_MEM_ERROR ? "out of memory" : 551 (state- 549 (state->msg == NULL ? "" : state->msg); 552 } 550 } 553 551 554 /* -- see zlib.h -- */ 552 /* -- see zlib.h -- */ 555 void ZEXPORT gzclearerr(file) 553 void ZEXPORT gzclearerr(file) 556 gzFile file; 554 gzFile file; 557 { 555 { 558 gz_statep state; 556 gz_statep state; 559 557 560 /* get internal structure and check integr 558 /* get internal structure and check integrity */ 561 if (file == NULL) 559 if (file == NULL) 562 return; 560 return; 563 state = (gz_statep)file; 561 state = (gz_statep)file; 564 if (state->mode != GZ_READ && state->mode 562 if (state->mode != GZ_READ && state->mode != GZ_WRITE) 565 return; 563 return; 566 564 567 /* clear error and end-of-file */ 565 /* clear error and end-of-file */ 568 if (state->mode == GZ_READ) { 566 if (state->mode == GZ_READ) { 569 state->eof = 0; 567 state->eof = 0; 570 state->past = 0; 568 state->past = 0; 571 } 569 } 572 gz_error(state, Z_OK, NULL); 570 gz_error(state, Z_OK, NULL); 573 } 571 } 574 572 575 /* Create an error message in allocated memory 573 /* Create an error message in allocated memory and set state->err and 576 state->msg accordingly. Free any previous 574 state->msg accordingly. Free any previous error message already there. Do 577 not try to free or allocate space if the er 575 not try to free or allocate space if the error is Z_MEM_ERROR (out of 578 memory). Simply save the error message as 576 memory). Simply save the error message as a static string. If there is an 579 allocation failure constructing the error m 577 allocation failure constructing the error message, then convert the error to 580 out of memory. */ 578 out of memory. */ 581 void ZLIB_INTERNAL gz_error(state, err, msg) 579 void ZLIB_INTERNAL gz_error(state, err, msg) 582 gz_statep state; 580 gz_statep state; 583 int err; 581 int err; 584 const char *msg; 582 const char *msg; 585 { 583 { 586 /* free previously allocated message and c 584 /* free previously allocated message and clear */ 587 if (state->msg != NULL) { 585 if (state->msg != NULL) { 588 if (state->err != Z_MEM_ERROR) 586 if (state->err != Z_MEM_ERROR) 589 free(state->msg); 587 free(state->msg); 590 state->msg = NULL; 588 state->msg = NULL; 591 } 589 } 592 590 593 /* if fatal, set state->x.have to 0 so tha 591 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ 594 if (err != Z_OK && err != Z_BUF_ERROR) 592 if (err != Z_OK && err != Z_BUF_ERROR) 595 state->x.have = 0; 593 state->x.have = 0; 596 594 597 /* set error code, and if no message, then 595 /* set error code, and if no message, then done */ 598 state->err = err; 596 state->err = err; 599 if (msg == NULL) 597 if (msg == NULL) 600 return; 598 return; 601 599 602 /* for an out of memory error, return lite 600 /* for an out of memory error, return literal string when requested */ 603 if (err == Z_MEM_ERROR) 601 if (err == Z_MEM_ERROR) 604 return; 602 return; 605 603 606 /* construct error message with path */ 604 /* construct error message with path */ 607 if ((state->msg = (char *)malloc(strlen(st 605 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == 608 NULL) { 606 NULL) { 609 state->err = Z_MEM_ERROR; 607 state->err = Z_MEM_ERROR; 610 return; 608 return; 611 } 609 } 612 #if !defined(NO_snprintf) && !defined(NO_vsnpr 610 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 613 (void)snprintf(state->msg, strlen(state->p 611 (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, 614 "%s%s%s", state->path, ": " 612 "%s%s%s", state->path, ": ", msg); 615 #else 613 #else 616 strcpy(state->msg, state->path); 614 strcpy(state->msg, state->path); 617 strcat(state->msg, ": "); 615 strcat(state->msg, ": "); 618 strcat(state->msg, msg); 616 strcat(state->msg, msg); 619 #endif 617 #endif 620 } 618 } 621 619 622 #ifndef INT_MAX 620 #ifndef INT_MAX 623 /* portably return maximum value for an int (w 621 /* portably return maximum value for an int (when limits.h presumed not 624 available) -- we need to do this to cover c 622 available) -- we need to do this to cover cases where 2's complement not 625 used, since C standard permits 1's compleme 623 used, since C standard permits 1's complement and sign-bit representations, 626 otherwise we could just use ((unsigned)-1) 624 otherwise we could just use ((unsigned)-1) >> 1 */ 627 unsigned ZLIB_INTERNAL gz_intmax() 625 unsigned ZLIB_INTERNAL gz_intmax() 628 { 626 { 629 unsigned p, q; 627 unsigned p, q; 630 628 631 p = 1; 629 p = 1; 632 do { 630 do { 633 q = p; 631 q = p; 634 p <<= 1; 632 p <<= 1; 635 p++; 633 p++; 636 } while (p > q); 634 } while (p > q); 637 return q >> 1; 635 return q >> 1; 638 } 636 } 639 #endif 637 #endif 640 638