CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Format/RIFF.cpp
Views: 1401
#include <cstring>12#include "Common/Log.h"3#include "Common/Data/Format/RIFF.h"45inline uint32_t flipID(uint32_t id) {6return ((id >> 24) & 0xFF) | ((id >> 8) & 0xFF00) | ((id << 8) & 0xFF0000) | ((id << 24) & 0xFF000000);7}89RIFFReader::RIFFReader(const uint8_t *data, int dataSize) {10data_ = new uint8_t[dataSize];11memcpy(data_, data, dataSize);12depth_ = 0;13pos_ = 0;14eof_ = dataSize;15fileSize_ = dataSize;16}1718RIFFReader::~RIFFReader() {19delete[] data_;20}2122int RIFFReader::ReadInt() {23int value = 0;24if (data_ && pos_ < eof_ - 3) {25pos_ += 4;26memcpy(&value, data_ + pos_ - 4, 4);27}28return value;29}3031bool RIFFReader::Descend(uint32_t intoId) {32if (depth_ > 30)33return false;3435intoId = flipID(intoId);36bool found = false;3738// save information to restore after the next Ascend39stack[depth_].parentStartLocation = pos_;40stack[depth_].parentEOF = eof_;4142// let's search through children..43while (pos_ < eof_) {44int id = ReadInt();45int length = ReadInt();46int startLocation = pos_;4748if (pos_ + length > fileSize_) {49ERROR_LOG(Log::IO, "Block extends outside of RIFF file - failing descend");50pos_ = stack[depth_].parentStartLocation;51return false;52}5354if (id == intoId) {55stack[depth_].ID = intoId;56stack[depth_].length = length;57stack[depth_].startLocation = startLocation;58found = true;59break;60} else {61if (length > 0) {62pos_ += length; // try next block63} else {64ERROR_LOG(Log::IO, "Bad data in RIFF file : block length %d. Not descending.", length);65pos_ = stack[depth_].parentStartLocation;66return false;67}68}69}7071// if we found nothing, return false so the caller can skip this72if (!found) {73pos_ = stack[depth_].parentStartLocation;74return false;75}7677// descend into it78// pos was set inside the loop above79eof_ = stack[depth_].startLocation + stack[depth_].length;80depth_++;81return true;82}8384void RIFFReader::Ascend() {85// ascend, and restore information86depth_--;87pos_ = stack[depth_].parentStartLocation;88eof_ = stack[depth_].parentEOF;89}9091void RIFFReader::ReadData(void *what, int count) {92memcpy(what, data_ + pos_, count);93pos_ += count;94count &= 3;95if (count) {96count = 4 - count;97pos_ += count;98}99}100101int RIFFReader::GetCurrentChunkSize() {102if (depth_)103return stack[depth_ - 1].length;104else105return 0;106}107108109