reader.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <util/generic/cast.h>
  2. #include <util/memory/blob.h>
  3. #include <util/system/unaligned_mem.h>
  4. #include "reader.h"
  5. template <typename T>
  6. static inline void ReadAux(const char* data, T* aux, T count, TVector<const char*>* result) {
  7. result->resize(count);
  8. for (size_t i = 0; i < count; ++i) {
  9. (*result)[i] = data + ReadUnaligned<T>(aux + i);
  10. }
  11. }
  12. TChunkedDataReader::TChunkedDataReader(const TBlob& blob) {
  13. const char* cdata = blob.AsCharPtr();
  14. const size_t size = blob.Size();
  15. Y_ENSURE(size >= sizeof(ui32), "Empty file with chunks. ");
  16. ui32 last = ReadUnaligned<ui32>((ui32*)(cdata + size) - 1);
  17. if (last != 0) { // old version file
  18. ui32* aux = (ui32*)(cdata + size);
  19. ui32 count = last;
  20. Size = size - (count + 1) * sizeof(ui32);
  21. aux -= (count + 1);
  22. ReadAux<ui32>(cdata, aux, count, &Offsets);
  23. return;
  24. }
  25. Y_ENSURE(size >= 3 * sizeof(ui64), "Blob size must be >= 3 * sizeof(ui64). ");
  26. ui64* aux = (ui64*)(cdata + size);
  27. Version = ReadUnaligned<ui64>(aux - 2);
  28. Y_ENSURE(Version > 0, "Invalid chunked array version. ");
  29. ui64 count = ReadUnaligned<ui64>(aux - 3);
  30. aux -= (count + 3);
  31. ReadAux<ui64>(cdata, aux, count, &Offsets);
  32. aux -= count;
  33. Lengths.resize(count);
  34. for (size_t i = 0; i < count; ++i) {
  35. Lengths[i] = IntegerCast<size_t>(ReadUnaligned<ui64>(aux + i));
  36. }
  37. }
  38. TBlob TChunkedDataReader::GetBlob(size_t index) const {
  39. return TBlob::NoCopy(GetBlock(index), GetBlockLen(index));
  40. }