-static
-int riff_read(wav_reader_t *reader, void *buffer, uint32_t size)
-{
- int rc;
- uint32_t count = 0;
-
- if (reader->last_error)
- return -1;
- do {
- rc = reader->io.read(reader->io_cookie, buffer, size - count);
- if (rc > 0)
- count += rc;
- else if (rc < 0)
- reader->last_error = WAV_IO_ERROR;
- } while (rc > 0 && count < size);
- return count > 0 ? count : rc;
-}
-
-static
-int riff_skip(wav_reader_t *reader, int64_t count)
-{
- char buff[8192];
- int rc;
-
- if (reader->last_error)
- return -1;
- if (count == 0)
- return 0;
- if (reader->io.seek &&
- reader->io.seek(reader->io_cookie, count, SEEK_CUR) >= 0)
- return 0;
-
- do {
- if ((rc = riff_read(reader, buff, count > 8192 ? 8192 : count)) > 0)
- count -= rc;
- } while (rc > 0 && count > 0);
-
- if (count > 0)
- reader->last_error = WAV_IO_ERROR;
- return reader->last_error ? -1 : 0;
-}
-
-static
-int riff_seek(wav_reader_t *reader, int64_t off, int whence)
-{
- int rc;
- if (reader->last_error)
- return -1;
- if (!reader->io.seek)
- goto FAIL;
- if ((rc = reader->io.seek(reader->io_cookie, off, whence)) < 0)
- goto FAIL;
- return 0;
-FAIL:
- reader->last_error = WAV_IO_ERROR;
- return -1;
-}
-
-static
-int64_t riff_tell(wav_reader_t *reader)
-{
- int64_t off;
-
- if (reader->last_error || !reader->io.tell)
- return -1;
- off = reader->io.tell(reader->io_cookie);
- if (off < 0) {
- reader->last_error = WAV_IO_ERROR;
- return -1;
- }
- return off;
-}
-
-static
-int riff_read16(wav_reader_t *reader, uint16_t *value)
-{
- TRY_IO(riff_read(reader, value, 2) != 2);
- *value = m4af_ltoh16(*value);
- return 0;
-FAIL:
- return -1;
-}
-
-static
-int riff_read32(wav_reader_t *reader, uint32_t *value)
-{
- TRY_IO(riff_read(reader, value, 4) != 4);
- *value = m4af_ltoh32(*value);
- return 0;
-FAIL:
- return -1;
-}
-
-static
-int riff_read64(wav_reader_t *reader, uint64_t *value)
-{
- TRY_IO(riff_read(reader, value, 8) != 8);
- *value = m4af_ltoh64(*value);
- return 0;
-FAIL:
- return -1;
-}
-
-static
-int riff_scan(wav_reader_t *reader, const char *fmt, ...)
-{
- int c, count = 0;
- va_list ap;
-
- va_start(ap, fmt);
- while ((c = *fmt++)) {
- switch (c) {
- case 'S':
- TRY_IO(riff_read16(reader, va_arg(ap, uint16_t*)));
- ++count;
- break;
- case 'L':
- TRY_IO(riff_read32(reader, va_arg(ap, uint32_t*)));
- ++count;
- break;
- case 'Q':
- TRY_IO(riff_read64(reader, va_arg(ap, uint64_t*)));
- ++count;
- break;
- }
- }
-FAIL:
- va_end(ap);
- return count;
-}
-