2022-01-05 17:12:38 -08:00
|
|
|
/*
|
|
|
|
* FragmentEntry.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jan 5, 2022
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2022
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "FragmentEntry.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* copy dataLength of fragmented outputs starting at dataOffset into destination starting at zero
|
|
|
|
*/
|
2022-04-13 20:31:32 -07:00
|
|
|
void copyRange(uint8_t *destination,
|
|
|
|
FragmentEntry *fragments, int fragmentsCount,
|
|
|
|
size_t dataOffset, size_t dataLength) {
|
2022-01-05 17:12:38 -08:00
|
|
|
int fragmentIndex = 0;
|
|
|
|
|
|
|
|
// scroll to starting fragment
|
2022-04-13 20:31:32 -07:00
|
|
|
while (dataOffset > fragments[fragmentIndex].size && fragmentIndex <= fragmentsCount) {
|
2022-01-05 17:12:38 -08:00
|
|
|
dataOffset -= fragments[fragmentIndex].size;
|
|
|
|
fragmentIndex ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int destinationIndex = 0;
|
|
|
|
|
|
|
|
while (dataLength > 0) {
|
2022-04-13 20:31:32 -07:00
|
|
|
if (fragmentIndex > fragmentsCount) {
|
|
|
|
// somehow we are past the end of fragments - fill with zeros
|
|
|
|
memset(destination + destinationIndex, 0, dataLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-05 17:12:38 -08:00
|
|
|
int copyNowSize = minI(dataLength, fragments[fragmentIndex].size - dataOffset);
|
2022-04-13 15:22:40 -07:00
|
|
|
const uint8_t *fromBase = fragments[fragmentIndex].data;
|
|
|
|
if (fromBase == nullptr) {
|
2022-04-13 20:31:32 -07:00
|
|
|
// we have no buffer for this fragment - fill with zeroes
|
2022-04-13 15:22:40 -07:00
|
|
|
memset(destination + destinationIndex, 0, copyNowSize);
|
|
|
|
} else {
|
|
|
|
memcpy(destination + destinationIndex, fromBase + dataOffset, copyNowSize);
|
|
|
|
}
|
2022-01-05 17:12:38 -08:00
|
|
|
destinationIndex += copyNowSize;
|
|
|
|
dataOffset = 0;
|
|
|
|
dataLength -= copyNowSize;
|
|
|
|
fragmentIndex++;
|
|
|
|
}
|
|
|
|
}
|