skiff.cpp 14 KB

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