MsgPackWriter.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //===- MsgPackWriter.cpp - Simple MsgPack writer ----------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements a MessagePack writer.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/BinaryFormat/MsgPackWriter.h"
  14. #include "llvm/BinaryFormat/MsgPack.h"
  15. #include <cmath>
  16. using namespace llvm;
  17. using namespace msgpack;
  18. Writer::Writer(raw_ostream &OS, bool Compatible)
  19. : EW(OS, Endianness), Compatible(Compatible) {}
  20. void Writer::writeNil() { EW.write(FirstByte::Nil); }
  21. void Writer::write(bool b) { EW.write(b ? FirstByte::True : FirstByte::False); }
  22. void Writer::write(int64_t i) {
  23. if (i >= 0) {
  24. write(static_cast<uint64_t>(i));
  25. return;
  26. }
  27. if (i >= FixMin::NegativeInt) {
  28. EW.write(static_cast<int8_t>(i));
  29. return;
  30. }
  31. if (i >= INT8_MIN) {
  32. EW.write(FirstByte::Int8);
  33. EW.write(static_cast<int8_t>(i));
  34. return;
  35. }
  36. if (i >= INT16_MIN) {
  37. EW.write(FirstByte::Int16);
  38. EW.write(static_cast<int16_t>(i));
  39. return;
  40. }
  41. if (i >= INT32_MIN) {
  42. EW.write(FirstByte::Int32);
  43. EW.write(static_cast<int32_t>(i));
  44. return;
  45. }
  46. EW.write(FirstByte::Int64);
  47. EW.write(i);
  48. }
  49. void Writer::write(uint64_t u) {
  50. if (u <= FixMax::PositiveInt) {
  51. EW.write(static_cast<uint8_t>(u));
  52. return;
  53. }
  54. if (u <= UINT8_MAX) {
  55. EW.write(FirstByte::UInt8);
  56. EW.write(static_cast<uint8_t>(u));
  57. return;
  58. }
  59. if (u <= UINT16_MAX) {
  60. EW.write(FirstByte::UInt16);
  61. EW.write(static_cast<uint16_t>(u));
  62. return;
  63. }
  64. if (u <= UINT32_MAX) {
  65. EW.write(FirstByte::UInt32);
  66. EW.write(static_cast<uint32_t>(u));
  67. return;
  68. }
  69. EW.write(FirstByte::UInt64);
  70. EW.write(u);
  71. }
  72. void Writer::write(double d) {
  73. // If no loss of precision, encode as a Float32.
  74. double a = std::fabs(d);
  75. if (a >= std::numeric_limits<float>::min() &&
  76. a <= std::numeric_limits<float>::max()) {
  77. EW.write(FirstByte::Float32);
  78. EW.write(static_cast<float>(d));
  79. } else {
  80. EW.write(FirstByte::Float64);
  81. EW.write(d);
  82. }
  83. }
  84. void Writer::write(StringRef s) {
  85. size_t Size = s.size();
  86. if (Size <= FixMax::String)
  87. EW.write(static_cast<uint8_t>(FixBits::String | Size));
  88. else if (!Compatible && Size <= UINT8_MAX) {
  89. EW.write(FirstByte::Str8);
  90. EW.write(static_cast<uint8_t>(Size));
  91. } else if (Size <= UINT16_MAX) {
  92. EW.write(FirstByte::Str16);
  93. EW.write(static_cast<uint16_t>(Size));
  94. } else {
  95. assert(Size <= UINT32_MAX && "String object too long to be encoded");
  96. EW.write(FirstByte::Str32);
  97. EW.write(static_cast<uint32_t>(Size));
  98. }
  99. EW.OS << s;
  100. }
  101. void Writer::write(MemoryBufferRef Buffer) {
  102. assert(!Compatible && "Attempt to write Bin format in compatible mode");
  103. size_t Size = Buffer.getBufferSize();
  104. if (Size <= UINT8_MAX) {
  105. EW.write(FirstByte::Bin8);
  106. EW.write(static_cast<uint8_t>(Size));
  107. } else if (Size <= UINT16_MAX) {
  108. EW.write(FirstByte::Bin16);
  109. EW.write(static_cast<uint16_t>(Size));
  110. } else {
  111. assert(Size <= UINT32_MAX && "Binary object too long to be encoded");
  112. EW.write(FirstByte::Bin32);
  113. EW.write(static_cast<uint32_t>(Size));
  114. }
  115. EW.OS.write(Buffer.getBufferStart(), Size);
  116. }
  117. void Writer::writeArraySize(uint32_t Size) {
  118. if (Size <= FixMax::Array) {
  119. EW.write(static_cast<uint8_t>(FixBits::Array | Size));
  120. return;
  121. }
  122. if (Size <= UINT16_MAX) {
  123. EW.write(FirstByte::Array16);
  124. EW.write(static_cast<uint16_t>(Size));
  125. return;
  126. }
  127. EW.write(FirstByte::Array32);
  128. EW.write(Size);
  129. }
  130. void Writer::writeMapSize(uint32_t Size) {
  131. if (Size <= FixMax::Map) {
  132. EW.write(static_cast<uint8_t>(FixBits::Map | Size));
  133. return;
  134. }
  135. if (Size <= UINT16_MAX) {
  136. EW.write(FirstByte::Map16);
  137. EW.write(static_cast<uint16_t>(Size));
  138. return;
  139. }
  140. EW.write(FirstByte::Map32);
  141. EW.write(Size);
  142. }
  143. void Writer::writeExt(int8_t Type, MemoryBufferRef Buffer) {
  144. size_t Size = Buffer.getBufferSize();
  145. switch (Size) {
  146. case FixLen::Ext1:
  147. EW.write(FirstByte::FixExt1);
  148. break;
  149. case FixLen::Ext2:
  150. EW.write(FirstByte::FixExt2);
  151. break;
  152. case FixLen::Ext4:
  153. EW.write(FirstByte::FixExt4);
  154. break;
  155. case FixLen::Ext8:
  156. EW.write(FirstByte::FixExt8);
  157. break;
  158. case FixLen::Ext16:
  159. EW.write(FirstByte::FixExt16);
  160. break;
  161. default:
  162. if (Size <= UINT8_MAX) {
  163. EW.write(FirstByte::Ext8);
  164. EW.write(static_cast<uint8_t>(Size));
  165. } else if (Size <= UINT16_MAX) {
  166. EW.write(FirstByte::Ext16);
  167. EW.write(static_cast<uint16_t>(Size));
  168. } else {
  169. assert(Size <= UINT32_MAX && "Ext size too large to be encoded");
  170. EW.write(FirstByte::Ext32);
  171. EW.write(static_cast<uint32_t>(Size));
  172. }
  173. }
  174. EW.write(Type);
  175. EW.OS.write(Buffer.getBufferStart(), Size);
  176. }