BSER.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <stdint.h>
  2. #include "./BSER.hh"
  3. BSERType decodeType(std::istream &iss) {
  4. int8_t type;
  5. iss.read(reinterpret_cast<char*>(&type), sizeof(type));
  6. return (BSERType) type;
  7. }
  8. void expectType(std::istream &iss, BSERType expected) {
  9. BSERType got = decodeType(iss);
  10. if (got != expected) {
  11. throw std::runtime_error("Unexpected BSER type");
  12. }
  13. }
  14. void encodeType(std::ostream &oss, BSERType type) {
  15. int8_t t = (int8_t)type;
  16. oss.write(reinterpret_cast<char*>(&t), sizeof(t));
  17. }
  18. template<typename T>
  19. class Value : public BSERValue {
  20. public:
  21. T value;
  22. Value(T val) {
  23. value = val;
  24. }
  25. Value() {}
  26. };
  27. class BSERInteger : public Value<int64_t> {
  28. public:
  29. BSERInteger(int64_t value) : Value(value) {}
  30. BSERInteger(std::istream &iss) {
  31. int8_t int8;
  32. int16_t int16;
  33. int32_t int32;
  34. int64_t int64;
  35. BSERType type = decodeType(iss);
  36. switch (type) {
  37. case BSER_INT8:
  38. iss.read(reinterpret_cast<char*>(&int8), sizeof(int8));
  39. value = int8;
  40. break;
  41. case BSER_INT16:
  42. iss.read(reinterpret_cast<char*>(&int16), sizeof(int16));
  43. value = int16;
  44. break;
  45. case BSER_INT32:
  46. iss.read(reinterpret_cast<char*>(&int32), sizeof(int32));
  47. value = int32;
  48. break;
  49. case BSER_INT64:
  50. iss.read(reinterpret_cast<char*>(&int64), sizeof(int64));
  51. value = int64;
  52. break;
  53. default:
  54. throw std::runtime_error("Invalid BSER int type");
  55. }
  56. }
  57. int64_t intValue() override {
  58. return value;
  59. }
  60. void encode(std::ostream &oss) override {
  61. if (value <= INT8_MAX) {
  62. encodeType(oss, BSER_INT8);
  63. int8_t v = (int8_t)value;
  64. oss.write(reinterpret_cast<char*>(&v), sizeof(v));
  65. } else if (value <= INT16_MAX) {
  66. encodeType(oss, BSER_INT16);
  67. int16_t v = (int16_t)value;
  68. oss.write(reinterpret_cast<char*>(&v), sizeof(v));
  69. } else if (value <= INT32_MAX) {
  70. encodeType(oss, BSER_INT32);
  71. int32_t v = (int32_t)value;
  72. oss.write(reinterpret_cast<char*>(&v), sizeof(v));
  73. } else {
  74. encodeType(oss, BSER_INT64);
  75. oss.write(reinterpret_cast<char*>(&value), sizeof(value));
  76. }
  77. }
  78. };
  79. class BSERArray : public Value<BSER::Array> {
  80. public:
  81. BSERArray() : Value() {}
  82. BSERArray(BSER::Array value) : Value(value) {}
  83. BSERArray(std::istream &iss) {
  84. expectType(iss, BSER_ARRAY);
  85. int64_t len = BSERInteger(iss).intValue();
  86. for (int64_t i = 0; i < len; i++) {
  87. value.push_back(BSER(iss));
  88. }
  89. }
  90. BSER::Array arrayValue() override {
  91. return value;
  92. }
  93. void encode(std::ostream &oss) override {
  94. encodeType(oss, BSER_ARRAY);
  95. BSERInteger(value.size()).encode(oss);
  96. for (auto it = value.begin(); it != value.end(); it++) {
  97. it->encode(oss);
  98. }
  99. }
  100. };
  101. class BSERString : public Value<std::string> {
  102. public:
  103. BSERString(std::string value) : Value(value) {}
  104. BSERString(std::istream &iss) {
  105. expectType(iss, BSER_STRING);
  106. int64_t len = BSERInteger(iss).intValue();
  107. value.resize(len);
  108. iss.read(&value[0], len);
  109. }
  110. std::string stringValue() override {
  111. return value;
  112. }
  113. void encode(std::ostream &oss) override {
  114. encodeType(oss, BSER_STRING);
  115. BSERInteger(value.size()).encode(oss);
  116. oss << value;
  117. }
  118. };
  119. class BSERObject : public Value<BSER::Object> {
  120. public:
  121. BSERObject() : Value() {}
  122. BSERObject(BSER::Object value) : Value(value) {}
  123. BSERObject(std::istream &iss) {
  124. expectType(iss, BSER_OBJECT);
  125. int64_t len = BSERInteger(iss).intValue();
  126. for (int64_t i = 0; i < len; i++) {
  127. auto key = BSERString(iss).stringValue();
  128. auto val = BSER(iss);
  129. value.emplace(key, val);
  130. }
  131. }
  132. BSER::Object objectValue() override {
  133. return value;
  134. }
  135. void encode(std::ostream &oss) override {
  136. encodeType(oss, BSER_OBJECT);
  137. BSERInteger(value.size()).encode(oss);
  138. for (auto it = value.begin(); it != value.end(); it++) {
  139. BSERString(it->first).encode(oss);
  140. it->second.encode(oss);
  141. }
  142. }
  143. };
  144. class BSERDouble : public Value<double> {
  145. public:
  146. BSERDouble(double value) : Value(value) {}
  147. BSERDouble(std::istream &iss) {
  148. expectType(iss, BSER_REAL);
  149. iss.read(reinterpret_cast<char*>(&value), sizeof(value));
  150. }
  151. double doubleValue() override {
  152. return value;
  153. }
  154. void encode(std::ostream &oss) override {
  155. encodeType(oss, BSER_REAL);
  156. oss.write(reinterpret_cast<char*>(&value), sizeof(value));
  157. }
  158. };
  159. class BSERBoolean : public Value<bool> {
  160. public:
  161. BSERBoolean(bool value) : Value(value) {}
  162. bool boolValue() override { return value; }
  163. void encode(std::ostream &oss) override {
  164. int8_t t = value == true ? BSER_BOOL_TRUE : BSER_BOOL_FALSE;
  165. oss.write(reinterpret_cast<char*>(&t), sizeof(t));
  166. }
  167. };
  168. class BSERNull : public Value<bool> {
  169. public:
  170. BSERNull() : Value(false) {}
  171. void encode(std::ostream &oss) override {
  172. encodeType(oss, BSER_NULL);
  173. }
  174. };
  175. std::shared_ptr<BSERArray> decodeTemplate(std::istream &iss) {
  176. expectType(iss, BSER_TEMPLATE);
  177. auto keys = BSERArray(iss).arrayValue();
  178. auto len = BSERInteger(iss).intValue();
  179. std::shared_ptr<BSERArray> arr = std::make_shared<BSERArray>();
  180. for (int64_t i = 0; i < len; i++) {
  181. BSER::Object obj;
  182. for (auto it = keys.begin(); it != keys.end(); it++) {
  183. if (iss.peek() == 0x0c) {
  184. iss.ignore(1);
  185. continue;
  186. }
  187. auto val = BSER(iss);
  188. obj.emplace(it->stringValue(), val);
  189. }
  190. arr->value.push_back(obj);
  191. }
  192. return arr;
  193. }
  194. BSER::BSER(std::istream &iss) {
  195. BSERType type = decodeType(iss);
  196. iss.unget();
  197. switch (type) {
  198. case BSER_ARRAY:
  199. m_ptr = std::make_shared<BSERArray>(iss);
  200. break;
  201. case BSER_OBJECT:
  202. m_ptr = std::make_shared<BSERObject>(iss);
  203. break;
  204. case BSER_STRING:
  205. m_ptr = std::make_shared<BSERString>(iss);
  206. break;
  207. case BSER_INT8:
  208. case BSER_INT16:
  209. case BSER_INT32:
  210. case BSER_INT64:
  211. m_ptr = std::make_shared<BSERInteger>(iss);
  212. break;
  213. case BSER_REAL:
  214. m_ptr = std::make_shared<BSERDouble>(iss);
  215. break;
  216. case BSER_BOOL_TRUE:
  217. iss.ignore(1);
  218. m_ptr = std::make_shared<BSERBoolean>(true);
  219. break;
  220. case BSER_BOOL_FALSE:
  221. iss.ignore(1);
  222. m_ptr = std::make_shared<BSERBoolean>(false);
  223. break;
  224. case BSER_NULL:
  225. iss.ignore(1);
  226. m_ptr = std::make_shared<BSERNull>();
  227. break;
  228. case BSER_TEMPLATE:
  229. m_ptr = decodeTemplate(iss);
  230. break;
  231. default:
  232. throw std::runtime_error("unknown BSER type");
  233. }
  234. }
  235. BSER::BSER() : m_ptr(std::make_shared<BSERNull>()) {}
  236. BSER::BSER(BSER::Array value) : m_ptr(std::make_shared<BSERArray>(value)) {}
  237. BSER::BSER(BSER::Object value) : m_ptr(std::make_shared<BSERObject>(value)) {}
  238. BSER::BSER(const char *value) : m_ptr(std::make_shared<BSERString>(value)) {}
  239. BSER::BSER(std::string value) : m_ptr(std::make_shared<BSERString>(value)) {}
  240. BSER::BSER(int64_t value) : m_ptr(std::make_shared<BSERInteger>(value)) {}
  241. BSER::BSER(double value) : m_ptr(std::make_shared<BSERDouble>(value)) {}
  242. BSER::BSER(bool value) : m_ptr(std::make_shared<BSERBoolean>(value)) {}
  243. BSER::Array BSER::arrayValue() { return m_ptr->arrayValue(); }
  244. BSER::Object BSER::objectValue() { return m_ptr->objectValue(); }
  245. std::string BSER::stringValue() { return m_ptr->stringValue(); }
  246. int64_t BSER::intValue() { return m_ptr->intValue(); }
  247. double BSER::doubleValue() { return m_ptr->doubleValue(); }
  248. bool BSER::boolValue() { return m_ptr->boolValue(); }
  249. void BSER::encode(std::ostream &oss) {
  250. m_ptr->encode(oss);
  251. }
  252. int64_t BSER::decodeLength(std::istream &iss) {
  253. char pdu[2];
  254. if (!iss.read(pdu, 2) || pdu[0] != 0 || pdu[1] != 1) {
  255. throw std::runtime_error("Invalid BSER");
  256. }
  257. return BSERInteger(iss).intValue();
  258. }
  259. std::string BSER::encode() {
  260. std::ostringstream oss(std::ios_base::binary);
  261. encode(oss);
  262. std::ostringstream res(std::ios_base::binary);
  263. res.write("\x00\x01", 2);
  264. BSERInteger(oss.str().size()).encode(res);
  265. res << oss.str();
  266. return res.str();
  267. }