| 1734 | Return the buffer's adjusted start, either BUF or BUF + 1. */ |
| 1735 | |
| 1736 | static char * |
| 1737 | swab_buffer (char *buf, idx_t *nread, int *saved_byte) |
| 1738 | { |
| 1739 | if (*nread == 0) |
| 1740 | return buf; |
| 1741 | |
| 1742 | /* Update *SAVED_BYTE, and set PREV_SAVED to its old value. */ |
| 1743 | int prev_saved = *saved_byte; |
| 1744 | if ((prev_saved < 0) == (*nread & 1)) |
| 1745 | { |
| 1746 | unsigned char c = buf[--*nread]; |
| 1747 | *saved_byte = c; |
| 1748 | } |
| 1749 | else |
| 1750 | *saved_byte = -1; |
| 1751 | |
| 1752 | /* Do the byte-swapping by moving every other byte two |
| 1753 | positions toward the end, working from the end of the buffer |
| 1754 | toward the beginning. This way we move only half the data. */ |
| 1755 | for (idx_t i = *nread; 1 < i; i -= 2) |
| 1756 | buf[i] = buf[i - 2]; |
| 1757 | |
| 1758 | if (prev_saved < 0) |
| 1759 | return buf + 1; |
| 1760 | |
| 1761 | buf[1] = prev_saved; |
| 1762 | ++*nread; |
| 1763 | return buf; |
| 1764 | } |
| 1765 | |
| 1766 | /* Add OFFSET to the input offset, setting the overflow flag if |
| 1767 | necessary. */ |