skiff.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include "skiff.h"
  2. #include "skiff_validator.h"
  3. #include <util/stream/buffered.h>
  4. #include <util/system/byteorder.h>
  5. #include <util/system/unaligned_mem.h>
  6. namespace NSkiff {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. bool operator==(TInt128 lhs, TInt128 rhs)
  9. {
  10. return lhs.Low == rhs.Low && lhs.High == rhs.High;
  11. }
  12. bool operator!=(TInt128 lhs, TInt128 rhs)
  13. {
  14. return !(lhs == rhs);
  15. }
  16. bool operator==(TUint128 lhs, TUint128 rhs)
  17. {
  18. return lhs.Low == rhs.Low && lhs.High == rhs.High;
  19. }
  20. bool operator!=(TUint128 lhs, TUint128 rhs)
  21. {
  22. return !(lhs == rhs);
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////
  25. TUncheckedSkiffParser::TUncheckedSkiffParser(IZeroCopyInput* underlying)
  26. : Underlying_(underlying)
  27. , Buffer_(512 * 1024)
  28. { }
  29. TUncheckedSkiffParser::TUncheckedSkiffParser(const std::shared_ptr<TSkiffSchema>& /*schema*/, IZeroCopyInput* underlying)
  30. : TUncheckedSkiffParser(underlying)
  31. { }
  32. i8 TUncheckedSkiffParser::ParseInt8()
  33. {
  34. return ParseSimple<i8>();
  35. }
  36. i16 TUncheckedSkiffParser::ParseInt16()
  37. {
  38. return ParseSimple<i16>();
  39. }
  40. i32 TUncheckedSkiffParser::ParseInt32()
  41. {
  42. return ParseSimple<i32>();
  43. }
  44. i64 TUncheckedSkiffParser::ParseInt64()
  45. {
  46. return ParseSimple<i64>();
  47. }
  48. ui8 TUncheckedSkiffParser::ParseUint8()
  49. {
  50. return ParseSimple<ui8>();
  51. }
  52. ui16 TUncheckedSkiffParser::ParseUint16()
  53. {
  54. return ParseSimple<ui16>();
  55. }
  56. ui32 TUncheckedSkiffParser::ParseUint32()
  57. {
  58. return ParseSimple<ui32>();
  59. }
  60. ui64 TUncheckedSkiffParser::ParseUint64()
  61. {
  62. return ParseSimple<ui64>();
  63. }
  64. TInt128 TUncheckedSkiffParser::ParseInt128()
  65. {
  66. auto low = ParseSimple<ui64>();
  67. auto high = ParseSimple<i64>();
  68. return {low, high};
  69. }
  70. TUint128 TUncheckedSkiffParser::ParseUint128()
  71. {
  72. auto low = ParseSimple<ui64>();
  73. auto high = ParseSimple<ui64>();
  74. return {low, high};
  75. }
  76. double TUncheckedSkiffParser::ParseDouble()
  77. {
  78. return ParseSimple<double>();
  79. }
  80. bool TUncheckedSkiffParser::ParseBoolean()
  81. {
  82. ui8 result = ParseSimple<ui8>();
  83. if (result > 1) {
  84. ythrow TSkiffException() << "Invalid boolean value \"" << result << "\"";
  85. }
  86. return result;
  87. }
  88. TStringBuf TUncheckedSkiffParser::ParseString32()
  89. {
  90. ui32 len = ParseSimple<ui32>();
  91. const void* data = GetData(len);
  92. return TStringBuf(static_cast<const char*>(data), len);
  93. }
  94. TStringBuf TUncheckedSkiffParser::ParseYson32()
  95. {
  96. return ParseString32();
  97. }
  98. ui8 TUncheckedSkiffParser::ParseVariant8Tag()
  99. {
  100. return ParseSimple<ui8>();
  101. }
  102. ui16 TUncheckedSkiffParser::ParseVariant16Tag()
  103. {
  104. return ParseSimple<ui16>();
  105. }
  106. template <typename T>
  107. T TUncheckedSkiffParser::ParseSimple()
  108. {
  109. return ReadUnaligned<T>(GetData(sizeof(T)));
  110. }
  111. const void* TUncheckedSkiffParser::GetData(size_t size)
  112. {
  113. if (RemainingBytes() >= size) {
  114. const void* result = Position_;
  115. Advance(size);
  116. return result;
  117. }
  118. return GetDataViaBuffer(size);
  119. }
  120. const void* TUncheckedSkiffParser::GetDataViaBuffer(size_t size)
  121. {
  122. Buffer_.Clear();
  123. Buffer_.Reserve(size);
  124. while (Buffer_.Size() < size) {
  125. size_t toCopy = Min(size - Buffer_.Size(), RemainingBytes());
  126. Buffer_.Append(Position_, toCopy);
  127. Advance(toCopy);
  128. if (RemainingBytes() == 0) {
  129. RefillBuffer();
  130. if (Exhausted_ && Buffer_.Size() < size) {
  131. ythrow TSkiffException() << "Premature end of stream while parsing Skiff";
  132. }
  133. }
  134. }
  135. return Buffer_.Data();
  136. }
  137. size_t TUncheckedSkiffParser::RemainingBytes() const
  138. {
  139. Y_ASSERT(End_ >= Position_);
  140. return End_ - Position_;
  141. }
  142. void TUncheckedSkiffParser::Advance(size_t size)
  143. {
  144. Y_ASSERT(size <= RemainingBytes());
  145. Position_ += size;
  146. ReadBytesCount_ += size;
  147. }
  148. void TUncheckedSkiffParser::RefillBuffer()
  149. {
  150. size_t bufferSize = Underlying_->Next(&Position_);
  151. End_ = Position_ + bufferSize;
  152. if (bufferSize == 0) {
  153. Exhausted_ = true;
  154. }
  155. }
  156. bool TUncheckedSkiffParser::HasMoreData()
  157. {
  158. if (RemainingBytes() == 0 && !Exhausted_) {
  159. RefillBuffer();
  160. }
  161. return !(RemainingBytes() == 0 && Exhausted_);
  162. }
  163. void TUncheckedSkiffParser::ValidateFinished()
  164. { }
  165. ui64 TUncheckedSkiffParser::GetReadBytesCount() const
  166. {
  167. return ReadBytesCount_;
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////
  170. TCheckedSkiffParser::TCheckedSkiffParser(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyInput* stream)
  171. : Parser_(stream)
  172. , Validator_(std::make_unique<TSkiffValidator>(schema))
  173. { }
  174. TCheckedSkiffParser::~TCheckedSkiffParser() = default;
  175. i8 TCheckedSkiffParser::ParseInt8()
  176. {
  177. Validator_->OnSimpleType(EWireType::Int8);
  178. return Parser_.ParseInt8();
  179. }
  180. i16 TCheckedSkiffParser::ParseInt16()
  181. {
  182. Validator_->OnSimpleType(EWireType::Int16);
  183. return Parser_.ParseInt16();
  184. }
  185. i32 TCheckedSkiffParser::ParseInt32()
  186. {
  187. Validator_->OnSimpleType(EWireType::Int32);
  188. return Parser_.ParseInt32();
  189. }
  190. i64 TCheckedSkiffParser::ParseInt64()
  191. {
  192. Validator_->OnSimpleType(EWireType::Int64);
  193. return Parser_.ParseInt64();
  194. }
  195. ui8 TCheckedSkiffParser::ParseUint8()
  196. {
  197. Validator_->OnSimpleType(EWireType::Uint8);
  198. return Parser_.ParseUint8();
  199. }
  200. ui16 TCheckedSkiffParser::ParseUint16()
  201. {
  202. Validator_->OnSimpleType(EWireType::Uint16);
  203. return Parser_.ParseUint16();
  204. }
  205. ui32 TCheckedSkiffParser::ParseUint32()
  206. {
  207. Validator_->OnSimpleType(EWireType::Uint32);
  208. return Parser_.ParseUint32();
  209. }
  210. ui64 TCheckedSkiffParser::ParseUint64()
  211. {
  212. Validator_->OnSimpleType(EWireType::Uint64);
  213. return Parser_.ParseUint64();
  214. }
  215. TInt128 TCheckedSkiffParser::ParseInt128()
  216. {
  217. Validator_->OnSimpleType(EWireType::Int128);
  218. return Parser_.ParseInt128();
  219. }
  220. TUint128 TCheckedSkiffParser::ParseUint128()
  221. {
  222. Validator_->OnSimpleType(EWireType::Uint128);
  223. return Parser_.ParseUint128();
  224. }
  225. double TCheckedSkiffParser::ParseDouble()
  226. {
  227. Validator_->OnSimpleType(EWireType::Double);
  228. return Parser_.ParseDouble();
  229. }
  230. bool TCheckedSkiffParser::ParseBoolean()
  231. {
  232. Validator_->OnSimpleType(EWireType::Boolean);
  233. return Parser_.ParseBoolean();
  234. }
  235. TStringBuf TCheckedSkiffParser::ParseString32()
  236. {
  237. Validator_->OnSimpleType(EWireType::String32);
  238. return Parser_.ParseString32();
  239. }
  240. TStringBuf TCheckedSkiffParser::ParseYson32()
  241. {
  242. Validator_->OnSimpleType(EWireType::Yson32);
  243. return Parser_.ParseYson32();
  244. }
  245. ui8 TCheckedSkiffParser::ParseVariant8Tag()
  246. {
  247. Validator_->BeforeVariant8Tag();
  248. auto result = Parser_.ParseVariant8Tag();
  249. Validator_->OnVariant8Tag(result);
  250. return result;
  251. }
  252. ui16 TCheckedSkiffParser::ParseVariant16Tag()
  253. {
  254. Validator_->BeforeVariant16Tag();
  255. auto result = Parser_.ParseVariant16Tag();
  256. Validator_->OnVariant16Tag(result);
  257. return result;
  258. }
  259. bool TCheckedSkiffParser::HasMoreData()
  260. {
  261. return Parser_.HasMoreData();
  262. }
  263. void TCheckedSkiffParser::ValidateFinished()
  264. {
  265. Validator_->ValidateFinished();
  266. Parser_.ValidateFinished();
  267. }
  268. ui64 TCheckedSkiffParser::GetReadBytesCount() const
  269. {
  270. return Parser_.GetReadBytesCount();
  271. }
  272. ////////////////////////////////////////////////////////////////////////////////
  273. TUncheckedSkiffWriter::TUncheckedSkiffWriter(IZeroCopyOutput* underlying)
  274. : Underlying_(underlying)
  275. { }
  276. TUncheckedSkiffWriter::TUncheckedSkiffWriter(IOutputStream* underlying)
  277. : BufferedOutput_(MakeHolder<TBufferedOutput>(underlying))
  278. , Underlying_(BufferedOutput_.Get())
  279. { }
  280. TUncheckedSkiffWriter::TUncheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& /*schema*/, IZeroCopyOutput* underlying)
  281. : TUncheckedSkiffWriter(underlying)
  282. { }
  283. TUncheckedSkiffWriter::TUncheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& /*schema*/, IOutputStream* underlying)
  284. : TUncheckedSkiffWriter(underlying)
  285. { }
  286. TUncheckedSkiffWriter::~TUncheckedSkiffWriter()
  287. {
  288. try {
  289. Flush();
  290. } catch (...) {
  291. }
  292. }
  293. void TUncheckedSkiffWriter::WriteInt8(i8 value)
  294. {
  295. WriteSimple<i8>(value);
  296. }
  297. void TUncheckedSkiffWriter::WriteInt16(i16 value)
  298. {
  299. WriteSimple<i16>(value);
  300. }
  301. void TUncheckedSkiffWriter::WriteInt32(i32 value)
  302. {
  303. WriteSimple<i32>(value);
  304. }
  305. void TUncheckedSkiffWriter::WriteInt64(i64 value)
  306. {
  307. WriteSimple<i64>(value);
  308. }
  309. void TUncheckedSkiffWriter::WriteInt128(TInt128 value)
  310. {
  311. WriteSimple<ui64>(value.Low);
  312. WriteSimple<i64>(value.High);
  313. }
  314. void TUncheckedSkiffWriter::WriteUint128(TUint128 value)
  315. {
  316. WriteSimple<ui64>(value.Low);
  317. WriteSimple<ui64>(value.High);
  318. }
  319. void TUncheckedSkiffWriter::WriteUint8(ui8 value)
  320. {
  321. WriteSimple<ui8>(value);
  322. }
  323. void TUncheckedSkiffWriter::WriteUint16(ui16 value)
  324. {
  325. WriteSimple<ui16>(value);
  326. }
  327. void TUncheckedSkiffWriter::WriteUint32(ui32 value)
  328. {
  329. WriteSimple<ui32>(value);
  330. }
  331. void TUncheckedSkiffWriter::WriteUint64(ui64 value)
  332. {
  333. WriteSimple<ui64>(value);
  334. }
  335. void TUncheckedSkiffWriter::WriteDouble(double value)
  336. {
  337. return WriteSimple<double>(value);
  338. }
  339. void TUncheckedSkiffWriter::WriteBoolean(bool value)
  340. {
  341. return WriteSimple<ui8>(value ? 1 : 0);
  342. }
  343. void TUncheckedSkiffWriter::WriteString32(TStringBuf value)
  344. {
  345. WriteSimple<ui32>(value.size());
  346. Underlying_.Write(value.data(), value.size());
  347. }
  348. void TUncheckedSkiffWriter::WriteYson32(TStringBuf value)
  349. {
  350. WriteSimple<ui32>(value.size());
  351. Underlying_.Write(value.data(), value.size());
  352. }
  353. void TUncheckedSkiffWriter::WriteVariant8Tag(ui8 tag)
  354. {
  355. WriteSimple<ui8>(tag);
  356. }
  357. void TUncheckedSkiffWriter::WriteVariant16Tag(ui16 tag)
  358. {
  359. WriteSimple<ui16>(tag);
  360. }
  361. void TUncheckedSkiffWriter::Flush()
  362. {
  363. Underlying_.UndoRemaining();
  364. if (BufferedOutput_) {
  365. BufferedOutput_->Flush();
  366. }
  367. }
  368. template <typename T>
  369. Y_FORCE_INLINE void TUncheckedSkiffWriter::WriteSimple(T value)
  370. {
  371. if constexpr (std::is_integral_v<T>) {
  372. value = HostToLittle(value);
  373. Underlying_.Write(&value, sizeof(T));
  374. } else {
  375. Underlying_.Write(&value, sizeof(T));
  376. }
  377. }
  378. void TUncheckedSkiffWriter::Finish()
  379. {
  380. Flush();
  381. }
  382. ////////////////////////////////////////////////////////////////////////////////
  383. TCheckedSkiffWriter::TCheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyOutput* underlying)
  384. : Writer_(underlying)
  385. , Validator_(std::make_unique<TSkiffValidator>(schema))
  386. { }
  387. TCheckedSkiffWriter::TCheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IOutputStream* underlying)
  388. : Writer_(underlying)
  389. , Validator_(std::make_unique<TSkiffValidator>(schema))
  390. { }
  391. TCheckedSkiffWriter::~TCheckedSkiffWriter() = default;
  392. void TCheckedSkiffWriter::WriteDouble(double value)
  393. {
  394. Validator_->OnSimpleType(EWireType::Double);
  395. Writer_.WriteDouble(value);
  396. }
  397. void TCheckedSkiffWriter::WriteBoolean(bool value)
  398. {
  399. Validator_->OnSimpleType(EWireType::Boolean);
  400. Writer_.WriteBoolean(value);
  401. }
  402. void TCheckedSkiffWriter::WriteInt8(i8 value)
  403. {
  404. Validator_->OnSimpleType(EWireType::Int8);
  405. Writer_.WriteInt8(value);
  406. }
  407. void TCheckedSkiffWriter::WriteInt16(i16 value)
  408. {
  409. Validator_->OnSimpleType(EWireType::Int16);
  410. Writer_.WriteInt16(value);
  411. }
  412. void TCheckedSkiffWriter::WriteInt32(i32 value)
  413. {
  414. Validator_->OnSimpleType(EWireType::Int32);
  415. Writer_.WriteInt32(value);
  416. }
  417. void TCheckedSkiffWriter::WriteInt64(i64 value)
  418. {
  419. Validator_->OnSimpleType(EWireType::Int64);
  420. Writer_.WriteInt64(value);
  421. }
  422. void TCheckedSkiffWriter::WriteUint8(ui8 value)
  423. {
  424. Validator_->OnSimpleType(EWireType::Uint8);
  425. Writer_.WriteUint8(value);
  426. }
  427. void TCheckedSkiffWriter::WriteUint16(ui16 value)
  428. {
  429. Validator_->OnSimpleType(EWireType::Uint16);
  430. Writer_.WriteUint16(value);
  431. }
  432. void TCheckedSkiffWriter::WriteUint32(ui32 value)
  433. {
  434. Validator_->OnSimpleType(EWireType::Uint32);
  435. Writer_.WriteUint32(value);
  436. }
  437. void TCheckedSkiffWriter::WriteUint64(ui64 value)
  438. {
  439. Validator_->OnSimpleType(EWireType::Uint64);
  440. Writer_.WriteUint64(value);
  441. }
  442. void TCheckedSkiffWriter::WriteInt128(TInt128 value)
  443. {
  444. Validator_->OnSimpleType(EWireType::Int128);
  445. Writer_.WriteInt128(value);
  446. }
  447. void TCheckedSkiffWriter::WriteUint128(TUint128 value)
  448. {
  449. Validator_->OnSimpleType(EWireType::Uint128);
  450. Writer_.WriteUint128(value);
  451. }
  452. void TCheckedSkiffWriter::WriteString32(TStringBuf value)
  453. {
  454. Validator_->OnSimpleType(EWireType::String32);
  455. Writer_.WriteString32(value);
  456. }
  457. void TCheckedSkiffWriter::WriteYson32(TStringBuf value)
  458. {
  459. Validator_->OnSimpleType(EWireType::Yson32);
  460. Writer_.WriteYson32(value);
  461. }
  462. void TCheckedSkiffWriter::WriteVariant8Tag(ui8 tag)
  463. {
  464. Validator_->OnVariant8Tag(tag);
  465. Writer_.WriteVariant8Tag(tag);
  466. }
  467. void TCheckedSkiffWriter::WriteVariant16Tag(ui16 tag)
  468. {
  469. Validator_->OnVariant16Tag(tag);
  470. Writer_.WriteVariant16Tag(tag);
  471. }
  472. void TCheckedSkiffWriter::Flush()
  473. {
  474. Writer_.Flush();
  475. }
  476. void TCheckedSkiffWriter::Finish()
  477. {
  478. Validator_->ValidateFinished();
  479. Writer_.Finish();
  480. }
  481. ////////////////////////////////////////////////////////////////////
  482. } // namespace NSkiff