1
0

read.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "read.h"
  2. /* helper macro to do a read, check error code and number of bytes read, and return from the function if necessary
  3. has to be a macro because of the return */
  4. #define NSAVI_READ(reader, buffer, size, bytes_read) { int ret = reader->Read(buffer, size, &bytes_read); if (ret) return ret; if (bytes_read != size) return READ_EOF; }
  5. int nsavi::read_riff_chunk(nsavi::avi_reader *reader, nsavi::riff_chunk *chunk, uint32_t *out_bytes_read)
  6. {
  7. uint32_t total_bytes_read=0;
  8. uint32_t bytes_read;
  9. NSAVI_READ(reader, chunk, 8, bytes_read); // read id and size
  10. total_bytes_read += bytes_read;
  11. if (chunk->id == 'FFIR' || chunk->id == 'TSIL')
  12. {
  13. if (chunk->size < 4)
  14. return READ_INVALID_DATA;
  15. NSAVI_READ(reader, &chunk->type, 4, bytes_read);
  16. total_bytes_read += bytes_read;
  17. chunk->size -= 4;
  18. }
  19. else
  20. chunk->type = 0;
  21. if (out_bytes_read)
  22. *out_bytes_read = total_bytes_read;
  23. return READ_OK;
  24. }
  25. // we pass riff_chunk instead of size
  26. // to avoid any confusion about who is responsible for adding the padding byte
  27. // (this function is responsible)
  28. int nsavi::skip_chunk(nsavi::avi_reader *reader, const nsavi::riff_chunk *chunk, uint32_t *out_bytes_read)
  29. {
  30. uint32_t chunk_size = chunk->size;
  31. if (chunk_size & 1)
  32. {
  33. chunk_size ++; // odd chunk sizes must be padded by one
  34. if (chunk_size == 0) // check for overflow
  35. return READ_INVALID_DATA;
  36. }
  37. int ret = reader->Skip(chunk_size);
  38. if (ret)
  39. return ret;
  40. if (out_bytes_read)
  41. *out_bytes_read = chunk_size;
  42. return READ_OK;
  43. }