diff options
| author | Mark Adler <git@madler.net> | 2025-05-25 19:01:36 -0700 |
|---|---|---|
| committer | Mark Adler <git@madler.net> | 2025-12-08 03:52:25 -0800 |
| commit | 81cc0bebedd935daeb81b0b6e475d8786b51af3d (patch) | |
| tree | dbd05e0ccc349d0ab90d20374716fb400d383e68 /zlib.h | |
| parent | 598130fd078f7b712498d348cf94b4b9806c4435 (diff) | |
| download | zlib-81cc0bebedd935daeb81b0b6e475d8786b51af3d.tar.gz zlib-81cc0bebedd935daeb81b0b6e475d8786b51af3d.tar.bz2 zlib-81cc0bebedd935daeb81b0b6e475d8786b51af3d.zip | |
Support non-blocking devices in the gz* routines.
Diffstat (limited to 'zlib.h')
| -rw-r--r-- | zlib.h | 73 |
1 files changed, 58 insertions, 15 deletions
| @@ -1349,13 +1349,13 @@ ZEXTERN gzFile ZEXPORT gzopen(const char *path, const char *mode); | |||
| 1349 | used to force transparent reading. Transparent reading is automatically | 1349 | used to force transparent reading. Transparent reading is automatically |
| 1350 | performed if there is no gzip header at the start. Transparent reading can | 1350 | performed if there is no gzip header at the start. Transparent reading can |
| 1351 | be disabled with the 'G' option, which will instead return an error if there | 1351 | be disabled with the 'G' option, which will instead return an error if there |
| 1352 | is no gzip header. | 1352 | is no gzip header. 'N' will open the file in non-blocking mode. |
| 1353 | 1353 | ||
| 1354 | "a" can be used instead of "w" to request that the gzip stream that will | 1354 | 'a' can be used instead of 'w' to request that the gzip stream that will |
| 1355 | be written be appended to the file. "+" will result in an error, since | 1355 | be written be appended to the file. '+' will result in an error, since |
| 1356 | reading and writing to the same gzip file is not supported. The addition of | 1356 | reading and writing to the same gzip file is not supported. The addition of |
| 1357 | "x" when writing will create the file exclusively, which fails if the file | 1357 | 'x' when writing will create the file exclusively, which fails if the file |
| 1358 | already exists. On systems that support it, the addition of "e" when | 1358 | already exists. On systems that support it, the addition of 'e' when |
| 1359 | reading or writing will set the flag to close the file on an execve() call. | 1359 | reading or writing will set the flag to close the file on an execve() call. |
| 1360 | 1360 | ||
| 1361 | These functions, as well as gzip, will read and decode a sequence of gzip | 1361 | These functions, as well as gzip, will read and decode a sequence of gzip |
| @@ -1374,14 +1374,22 @@ ZEXTERN gzFile ZEXPORT gzopen(const char *path, const char *mode); | |||
| 1374 | insufficient memory to allocate the gzFile state, or if an invalid mode was | 1374 | insufficient memory to allocate the gzFile state, or if an invalid mode was |
| 1375 | specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). | 1375 | specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). |
| 1376 | errno can be checked to determine if the reason gzopen failed was that the | 1376 | errno can be checked to determine if the reason gzopen failed was that the |
| 1377 | file could not be opened. | 1377 | file could not be opened. Note that if 'N' is in mode for non-blocking, the |
| 1378 | open() itself can fail in order to not block. In that case gzopen() will | ||
| 1379 | return NULL and errno will be EAGAIN or ENONBLOCK. The call to gzopen() can | ||
| 1380 | then be re-tried. If the application would like to block on opening the | ||
| 1381 | file, then it can use open() without O_NONBLOCK, and then gzdopen() with the | ||
| 1382 | resulting file descriptor and 'N' in the mode, which will set it to non- | ||
| 1383 | blocking. | ||
| 1378 | */ | 1384 | */ |
| 1379 | 1385 | ||
| 1380 | ZEXTERN gzFile ZEXPORT gzdopen(int fd, const char *mode); | 1386 | ZEXTERN gzFile ZEXPORT gzdopen(int fd, const char *mode); |
| 1381 | /* | 1387 | /* |
| 1382 | Associate a gzFile with the file descriptor fd. File descriptors are | 1388 | Associate a gzFile with the file descriptor fd. File descriptors are |
| 1383 | obtained from calls like open, dup, creat, pipe or fileno (if the file has | 1389 | obtained from calls like open, dup, creat, pipe or fileno (if the file has |
| 1384 | been previously opened with fopen). The mode parameter is as in gzopen. | 1390 | been previously opened with fopen). The mode parameter is as in gzopen. An |
| 1391 | 'e' in mode will set fd's flag to close the file on an execve() call. An 'N' | ||
| 1392 | in mode will set fd's non-blocking flag. | ||
| 1385 | 1393 | ||
| 1386 | The next call of gzclose on the returned gzFile will also close the file | 1394 | The next call of gzclose on the returned gzFile will also close the file |
| 1387 | descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor | 1395 | descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor |
| @@ -1451,6 +1459,11 @@ ZEXTERN int ZEXPORT gzread(gzFile file, voidp buf, unsigned len); | |||
| 1451 | stream. Alternatively, gzerror can be used before gzclose to detect this | 1459 | stream. Alternatively, gzerror can be used before gzclose to detect this |
| 1452 | case. | 1460 | case. |
| 1453 | 1461 | ||
| 1462 | gzread can be used to read a gzip file on a non-blocking device. If the | ||
| 1463 | input stalls and there is no uncompressed data to return, then gzread() will | ||
| 1464 | return -1, and errno will be EAGAIN or EWOULDBLOCK. gzread() can then be | ||
| 1465 | called again. | ||
| 1466 | |||
| 1454 | gzread returns the number of uncompressed bytes actually read, less than | 1467 | gzread returns the number of uncompressed bytes actually read, less than |
| 1455 | len for end of file, or -1 for error. If len is too large to fit in an int, | 1468 | len for end of file, or -1 for error. If len is too large to fit in an int, |
| 1456 | then nothing is read, -1 is returned, and the error state is set to | 1469 | then nothing is read, -1 is returned, and the error state is set to |
| @@ -1479,15 +1492,20 @@ ZEXTERN z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems, | |||
| 1479 | multiple of size, then the final partial item is nevertheless read into buf | 1492 | multiple of size, then the final partial item is nevertheless read into buf |
| 1480 | and the end-of-file flag is set. The length of the partial item read is not | 1493 | and the end-of-file flag is set. The length of the partial item read is not |
| 1481 | provided, but could be inferred from the result of gztell(). This behavior | 1494 | provided, but could be inferred from the result of gztell(). This behavior |
| 1482 | is the same as the behavior of fread() implementations in common libraries, | 1495 | is the same as that of fread() implementations in common libraries. This |
| 1483 | but it prevents the direct use of gzfread() to read a concurrently written | 1496 | could result in data loss if used with size != 1 when reading a concurrently |
| 1484 | file, resetting and retrying on end-of-file, when size is not 1. | 1497 | written file or a non-blocking file. In that case, use size == 1 or gzread() |
| 1498 | instead. | ||
| 1485 | */ | 1499 | */ |
| 1486 | 1500 | ||
| 1487 | ZEXTERN int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len); | 1501 | ZEXTERN int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len); |
| 1488 | /* | 1502 | /* |
| 1489 | Compress and write the len uncompressed bytes at buf to file. gzwrite | 1503 | Compress and write the len uncompressed bytes at buf to file. gzwrite |
| 1490 | returns the number of uncompressed bytes written or 0 in case of error. | 1504 | returns the number of uncompressed bytes written, or 0 in case of error or |
| 1505 | if len is 0. If the write destination is non-blocking, then gzwrite() may | ||
| 1506 | return a number of bytes written that is not 0 and less than len. | ||
| 1507 | |||
| 1508 | If len does not fit in an int, then 0 is returned and nothing is written. | ||
| 1491 | */ | 1509 | */ |
| 1492 | 1510 | ||
| 1493 | ZEXTERN z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, | 1511 | ZEXTERN z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, |
| @@ -1502,6 +1520,11 @@ ZEXTERN z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, | |||
| 1502 | if there was an error. If the multiplication of size and nitems overflows, | 1520 | if there was an error. If the multiplication of size and nitems overflows, |
| 1503 | i.e. the product does not fit in a z_size_t, then nothing is written, zero | 1521 | i.e. the product does not fit in a z_size_t, then nothing is written, zero |
| 1504 | is returned, and the error state is set to Z_STREAM_ERROR. | 1522 | is returned, and the error state is set to Z_STREAM_ERROR. |
| 1523 | |||
| 1524 | If writing a concurrently read file or a non-blocking file with size != 1, | ||
| 1525 | a partial item could be written, with no way of knowing how much of it was | ||
| 1526 | not written, resulting in data loss. In that case, use size == 1 or | ||
| 1527 | gzwrite() instead. | ||
| 1505 | */ | 1528 | */ |
| 1506 | 1529 | ||
| 1507 | ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...); | 1530 | ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...); |
| @@ -1517,6 +1540,9 @@ ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...); | |||
| 1517 | zlib was compiled with the insecure functions sprintf() or vsprintf(), | 1540 | zlib was compiled with the insecure functions sprintf() or vsprintf(), |
| 1518 | because the secure snprintf() or vsnprintf() functions were not available. | 1541 | because the secure snprintf() or vsnprintf() functions were not available. |
| 1519 | This can be determined using zlibCompileFlags(). | 1542 | This can be determined using zlibCompileFlags(). |
| 1543 | |||
| 1544 | If a Z_BUF_ERROR is returned, then nothing was written due to a stall on | ||
| 1545 | the non-blocking write destination. | ||
| 1520 | */ | 1546 | */ |
| 1521 | 1547 | ||
| 1522 | ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); | 1548 | ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); |
| @@ -1525,6 +1551,11 @@ ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); | |||
| 1525 | the terminating null character. | 1551 | the terminating null character. |
| 1526 | 1552 | ||
| 1527 | gzputs returns the number of characters written, or -1 in case of error. | 1553 | gzputs returns the number of characters written, or -1 in case of error. |
| 1554 | The number of characters written may be less than the length of the string | ||
| 1555 | if the write destination is non-blocking. | ||
| 1556 | |||
| 1557 | If the length of the string does not fit in an int, then -1 is returned | ||
| 1558 | and nothing is written. | ||
| 1528 | */ | 1559 | */ |
| 1529 | 1560 | ||
| 1530 | ZEXTERN char * ZEXPORT gzgets(gzFile file, char *buf, int len); | 1561 | ZEXTERN char * ZEXPORT gzgets(gzFile file, char *buf, int len); |
| @@ -1540,6 +1571,10 @@ ZEXTERN char * ZEXPORT gzgets(gzFile file, char *buf, int len); | |||
| 1540 | for end-of-file or in case of error. If some data was read before an error, | 1571 | for end-of-file or in case of error. If some data was read before an error, |
| 1541 | then that data is returned until exhausted, after which the next call will | 1572 | then that data is returned until exhausted, after which the next call will |
| 1542 | return NULL to signal the error. | 1573 | return NULL to signal the error. |
| 1574 | |||
| 1575 | gzgets can be used on a file being concurrently written, and on a non- | ||
| 1576 | blocking device, both as for gzread(). However lines may be broken in the | ||
| 1577 | middle, leaving it up to the application to reassemble them as needed. | ||
| 1543 | */ | 1578 | */ |
| 1544 | 1579 | ||
| 1545 | ZEXTERN int ZEXPORT gzputc(gzFile file, int c); | 1580 | ZEXTERN int ZEXPORT gzputc(gzFile file, int c); |
| @@ -1550,14 +1585,19 @@ ZEXTERN int ZEXPORT gzputc(gzFile file, int c); | |||
| 1550 | 1585 | ||
| 1551 | ZEXTERN int ZEXPORT gzgetc(gzFile file); | 1586 | ZEXTERN int ZEXPORT gzgetc(gzFile file); |
| 1552 | /* | 1587 | /* |
| 1553 | Read and decompress one byte from file. gzgetc returns this byte or -1 in | 1588 | Read and decompress one byte from file. gzgetc returns this byte or -1 in |
| 1554 | case of end of file or error. If some data was read before an error, then | 1589 | case of end of file or error. If some data was read before an error, then |
| 1555 | that data is returned until exhausted, after which the next call will return | 1590 | that data is returned until exhausted, after which the next call will return |
| 1556 | -1 to signal the error. | 1591 | -1 to signal the error. |
| 1557 | 1592 | ||
| 1558 | This is implemented as a macro for speed. As such, it does not do all of | 1593 | This is implemented as a macro for speed. As such, it does not do all of |
| 1559 | the checking the other functions do. I.e. it does not check to see if file | 1594 | the checking the other functions do. I.e. it does not check to see if file |
| 1560 | is NULL, nor whether the structure file points to has been clobbered or not. | 1595 | is NULL, nor whether the structure file points to has been clobbered or not. |
| 1596 | |||
| 1597 | gzgetc can be used to read a gzip file on a non-blocking device. If the | ||
| 1598 | input stalls and there is no uncompressed data to return, then gzgetc() will | ||
| 1599 | return -1, and errno will be EAGAIN or EWOULDBLOCK. gzread() can then be | ||
| 1600 | called again. | ||
| 1561 | */ | 1601 | */ |
| 1562 | 1602 | ||
| 1563 | ZEXTERN int ZEXPORT gzungetc(int c, gzFile file); | 1603 | ZEXTERN int ZEXPORT gzungetc(int c, gzFile file); |
| @@ -1666,8 +1706,11 @@ ZEXTERN int ZEXPORT gzdirect(gzFile file); | |||
| 1666 | 1706 | ||
| 1667 | If gzdirect() is used immediately after gzopen() or gzdopen() it will | 1707 | If gzdirect() is used immediately after gzopen() or gzdopen() it will |
| 1668 | cause buffers to be allocated to allow reading the file to determine if it | 1708 | cause buffers to be allocated to allow reading the file to determine if it |
| 1669 | is a gzip file. Therefore if gzbuffer() is used, it should be called before | 1709 | is a gzip file. Therefore if gzbuffer() is used, it should be called before |
| 1670 | gzdirect(). | 1710 | gzdirect(). If the input is being written concurrently or the device is non- |
| 1711 | blocking, then gzdirect() may give a different answer once four bytes of | ||
| 1712 | input have been accumulated, which is what is needed to confirm or deny a | ||
| 1713 | gzip header. Before this, gzdirect() will return true (1). | ||
| 1671 | 1714 | ||
| 1672 | When writing, gzdirect() returns true (1) if transparent writing was | 1715 | When writing, gzdirect() returns true (1) if transparent writing was |
| 1673 | requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: | 1716 | requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: |
