| 1546 | } |
| 1547 | |
| 1548 | static int check_stream_oid(git_zstream *stream, |
| 1549 | const char *hdr, |
| 1550 | unsigned long size, |
| 1551 | const char *path, |
| 1552 | const struct object_id *expected_oid, |
| 1553 | const struct git_hash_algo *algop) |
| 1554 | { |
| 1555 | struct git_hash_ctx c; |
| 1556 | struct object_id real_oid; |
| 1557 | unsigned char buf[4096]; |
| 1558 | unsigned long total_read; |
| 1559 | int status = Z_OK; |
| 1560 | |
| 1561 | algop->init_fn(&c); |
| 1562 | git_hash_update(&c, hdr, stream->total_out); |
| 1563 | |
| 1564 | /* |
| 1565 | * We already read some bytes into hdr, but the ones up to the NUL |
| 1566 | * do not count against the object's content size. |
| 1567 | */ |
| 1568 | total_read = stream->total_out - strlen(hdr) - 1; |
| 1569 | |
| 1570 | /* |
| 1571 | * This size comparison must be "<=" to read the final zlib packets; |
| 1572 | * see the comment in unpack_loose_rest for details. |
| 1573 | */ |
| 1574 | while (total_read <= size && |
| 1575 | (status == Z_OK || |
| 1576 | (status == Z_BUF_ERROR && !stream->avail_out))) { |
| 1577 | stream->next_out = buf; |
| 1578 | stream->avail_out = sizeof(buf); |
| 1579 | if (size - total_read < stream->avail_out) |
| 1580 | stream->avail_out = size - total_read; |
| 1581 | status = git_inflate(stream, Z_FINISH); |
| 1582 | git_hash_update(&c, buf, stream->next_out - buf); |
| 1583 | total_read += stream->next_out - buf; |
| 1584 | } |
| 1585 | |
| 1586 | if (status != Z_STREAM_END) { |
| 1587 | error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid)); |
| 1588 | return -1; |
| 1589 | } |
| 1590 | if (stream->avail_in) { |
| 1591 | error(_("garbage at end of loose object '%s'"), |
| 1592 | oid_to_hex(expected_oid)); |
| 1593 | return -1; |
| 1594 | } |
| 1595 | |
| 1596 | git_hash_final_oid(&real_oid, &c); |
| 1597 | if (!oideq(expected_oid, &real_oid)) { |
| 1598 | error(_("hash mismatch for %s (expected %s)"), path, |
| 1599 | oid_to_hex(expected_oid)); |
| 1600 | return -1; |
| 1601 | } |
| 1602 | |
| 1603 | return 0; |
| 1604 | } |
| 1605 |
no test coverage detected