| 1615 | } |
| 1616 | |
| 1617 | static int |
| 1618 | WaveReadFormat(WaveFile *file) |
| 1619 | { |
| 1620 | WaveChunk *chunk = &file->chunk; |
| 1621 | WaveFormat *format = &file->format; |
| 1622 | SDL_RWops *fmtsrc; |
| 1623 | size_t fmtlen = chunk->size; |
| 1624 | |
| 1625 | if (fmtlen > SDL_MAX_SINT32) { |
| 1626 | /* Limit given by SDL_RWFromConstMem. */ |
| 1627 | return SDL_SetError("Data of WAVE fmt chunk too big"); |
| 1628 | } |
| 1629 | fmtsrc = SDL_RWFromConstMem(chunk->data, (int)chunk->size); |
| 1630 | if (fmtsrc == NULL) { |
| 1631 | return SDL_OutOfMemory(); |
| 1632 | } |
| 1633 | |
| 1634 | format->formattag = SDL_ReadLE16(fmtsrc); |
| 1635 | format->encoding = format->formattag; |
| 1636 | format->channels = SDL_ReadLE16(fmtsrc); |
| 1637 | format->frequency = SDL_ReadLE32(fmtsrc); |
| 1638 | format->byterate = SDL_ReadLE32(fmtsrc); |
| 1639 | format->blockalign = SDL_ReadLE16(fmtsrc); |
| 1640 | |
| 1641 | /* This is PCM specific in the first version of the specification. */ |
| 1642 | if (fmtlen >= 16) { |
| 1643 | format->bitspersample = SDL_ReadLE16(fmtsrc); |
| 1644 | } else if (format->encoding == PCM_CODE) { |
| 1645 | SDL_RWclose(fmtsrc); |
| 1646 | return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk"); |
| 1647 | } |
| 1648 | |
| 1649 | /* The earlier versions also don't have this field. */ |
| 1650 | if (fmtlen >= 18) { |
| 1651 | format->extsize = SDL_ReadLE16(fmtsrc); |
| 1652 | } |
| 1653 | |
| 1654 | if (format->formattag == EXTENSIBLE_CODE) { |
| 1655 | /* note that this ignores channel masks, smaller valid bit counts |
| 1656 | * inside a larger container, and most subtypes. This is just enough |
| 1657 | * to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE |
| 1658 | * to be useful working when they use this format flag. |
| 1659 | */ |
| 1660 | |
| 1661 | /* Extensible header must be at least 22 bytes. */ |
| 1662 | if (fmtlen < 40 || format->extsize < 22) { |
| 1663 | SDL_RWclose(fmtsrc); |
| 1664 | return SDL_SetError("Extensible WAVE header too small"); |
| 1665 | } |
| 1666 | |
| 1667 | format->validsamplebits = SDL_ReadLE16(fmtsrc); |
| 1668 | format->samplesperblock = format->validsamplebits; |
| 1669 | format->channelmask = SDL_ReadLE32(fmtsrc); |
| 1670 | SDL_RWread(fmtsrc, format->subformat, 1, 16); |
| 1671 | format->encoding = WaveGetFormatGUIDEncoding(format); |
| 1672 | } |
| 1673 | |
| 1674 | SDL_RWclose(fmtsrc); |
no test coverage detected