encode.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <endian.h>
  18. #include <byteswap.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include "packet/packet.h"
  23. #include "table/table_def.h"
  24. #include "decode/decode.h"
  25. #include "task/task_base.h"
  26. /* encoding DataValue by type */
  27. char *encode_data_value(char *p, const DTCValue *v, int type)
  28. {
  29. char *t;
  30. int n;
  31. switch (type) {
  32. case DField::None:
  33. *p++ = 0;
  34. break;
  35. case DField::Signed:
  36. case DField::Unsigned:
  37. if (v->s64 >= 0) {
  38. if (v->s64 < 0x80LL)
  39. n = 1;
  40. else if (v->s64 < 0x8000LL)
  41. n = 2;
  42. else if (v->s64 < 0x800000LL)
  43. n = 3;
  44. else if (v->s64 < 0x80000000LL)
  45. n = 4;
  46. else if (v->s64 < 0x8000000000LL)
  47. n = 5;
  48. else if (v->s64 < 0x800000000000LL)
  49. n = 6;
  50. else if (v->s64 < 0x80000000000000LL)
  51. n = 7;
  52. else
  53. n = 8;
  54. } else {
  55. if (v->s64 >= -0x80LL)
  56. n = 1;
  57. else if (v->s64 >= -0x8000LL)
  58. n = 2;
  59. else if (v->s64 >= -0x800000LL)
  60. n = 3;
  61. else if (v->s64 >= -0x80000000LL)
  62. n = 4;
  63. else if (v->s64 >= -0x8000000000LL)
  64. n = 5;
  65. else if (v->s64 >= -0x800000000000LL)
  66. n = 6;
  67. else if (v->s64 >= -0x80000000000000LL)
  68. n = 7;
  69. else
  70. n = 8;
  71. }
  72. t = (char *)&v->s64;
  73. *p++ = n;
  74. #if __BYTE_ORDER == __BIG_ENDIAN
  75. for (int i = 0; i < n; i++)
  76. p[i] = t[7 - n + i];
  77. #else
  78. for (int i = 0; i < n; i++)
  79. p[i] = t[n - 1 - i];
  80. #endif
  81. p += n;
  82. break;
  83. case DField::Float:
  84. char buf[sizeof(long double) * 2 + 8];
  85. if (snprintf(buf, sizeof(buf), __FLTFMT__,
  86. (long double)v->flt) >= (int)sizeof(buf))
  87. memcpy(buf, "NAN", 4);
  88. n = strlen(buf) + 1;
  89. *p++ = n;
  90. memcpy(p, buf, n);
  91. p += n;
  92. break;
  93. case DField::String:
  94. case DField::Binary:
  95. if (v->str.ptr == NULL) {
  96. *p++ = 0;
  97. } else {
  98. p = encode_length(p, v->str.len + 1);
  99. if (v->bin.len)
  100. memcpy(p, v->bin.ptr, v->str.len);
  101. p += v->str.len;
  102. *p++ = '\0';
  103. }
  104. break;
  105. }
  106. return p;
  107. }
  108. int encoded_bytes_data_value(const DTCValue *v, int type)
  109. {
  110. switch (type) {
  111. case DField::None:
  112. return 1;
  113. case DField::Signed:
  114. case DField::Unsigned:
  115. if (v->s64 >= 0) {
  116. if (v->s64 < 0x80LL)
  117. return 2;
  118. if (v->s64 < 0x8000LL)
  119. return 3;
  120. if (v->s64 < 0x800000LL)
  121. return 4;
  122. if (v->s64 < 0x80000000LL)
  123. return 5;
  124. if (v->s64 < 0x8000000000LL)
  125. return 6;
  126. if (v->s64 < 0x800000000000LL)
  127. return 7;
  128. if (v->s64 < 0x80000000000000LL)
  129. return 8;
  130. } else {
  131. if (v->s64 >= -0x80LL)
  132. return 2;
  133. if (v->s64 >= -0x8000LL)
  134. return 3;
  135. if (v->s64 >= -0x800000LL)
  136. return 4;
  137. if (v->s64 >= -0x80000000LL)
  138. return 5;
  139. if (v->s64 >= -0x8000000000LL)
  140. return 6;
  141. if (v->s64 >= -0x800000000000LL)
  142. return 7;
  143. if (v->s64 >= -0x80000000000000LL)
  144. return 8;
  145. }
  146. return 9;
  147. case DField::Float:
  148. char b[sizeof(long double) * 2 + 8];
  149. if (snprintf(b, sizeof(b), __FLTFMT__, (long double)v->flt) >=
  150. (int)sizeof(b))
  151. return 5;
  152. return 2 + strlen(b);
  153. case DField::String:
  154. case DField::Binary:
  155. if (v->str.ptr == NULL)
  156. return 1;
  157. return v->str.len + 1 + encoded_bytes_length(v->str.len + 1);
  158. }
  159. return 0;
  160. }
  161. /*
  162. * Encoding simple section
  163. * <ID> <LEN> <VAL> <ID> <LEN> <VAL>...
  164. */
  165. int encoded_bytes_simple_section(const SimpleSection &sct, uint8_t kt)
  166. {
  167. int len = 0;
  168. for (int i = 0; i <= sct.max_tags(); i++) {
  169. if (sct.tag_present(i) == 0)
  170. continue;
  171. const int t = i == 0 ? kt : sct.tag_type(i);
  172. len += 1 + encoded_bytes_data_value(sct.get_tag(i), t);
  173. }
  174. return len;
  175. }
  176. char *encode_simple_section(char *p, const SimpleSection &sct, uint8_t kt)
  177. {
  178. for (int i = 0; i <= sct.max_tags(); i++) {
  179. if (sct.tag_present(i) == 0)
  180. continue;
  181. const int t = i == 0 ? kt : sct.tag_type(i);
  182. *p++ = i;
  183. p = encode_data_value(p, sct.get_tag(i), t);
  184. }
  185. return p;
  186. }
  187. /*
  188. * FieldSet format:
  189. * <NUM> <ID>...
  190. * NUM: 1 byte, total fields
  191. * ID: 1 byte per fieldid
  192. */
  193. int encoded_bytes_field_set(const DTCFieldSet &fs)
  194. {
  195. if (fs.num_fields() == 0)
  196. return 0;
  197. if (fs.field_present(0)) {
  198. return fs.num_fields() + 2;
  199. }
  200. return fs.num_fields() + 1;
  201. }
  202. char *encode_field_set(char *p, const DTCFieldSet &fs)
  203. {
  204. if (fs.num_fields() == 0)
  205. return p;
  206. *p++ = fs.num_fields();
  207. for (int i = 0; i < fs.num_fields(); i++) {
  208. *p++ = fs.field_id(i);
  209. if (fs.field_id(i) == 0)
  210. *p++ = 0;
  211. }
  212. return p;
  213. }
  214. /*
  215. * field_value format:
  216. * <NUM> <OPER:TYPE> <ID> <LEN> <VALUE>...
  217. * NUM: 1 byte, total fields
  218. * ID: 1 byte
  219. */
  220. int encoded_bytes_field_value(const DTCFieldValue &fv)
  221. {
  222. if (fv.num_fields() == 0)
  223. return 0;
  224. int len = 1;
  225. for (int i = 0; i < fv.num_fields(); i++) {
  226. //for migrate
  227. if (fv.field_id(i) == 0)
  228. len++;
  229. len += 2 + encoded_bytes_data_value(fv.field_value(i),
  230. fv.field_type(i));
  231. }
  232. return len;
  233. }
  234. char *encode_field_value(char *p, const DTCFieldValue &fv)
  235. {
  236. if (fv.num_fields() == 0)
  237. return p;
  238. *p++ = fv.num_fields();
  239. for (int i = 0; i < fv.num_fields(); i++) {
  240. const int n = fv.field_id(i);
  241. const int t = fv.field_type(i);
  242. *p++ = (fv.field_operation(i) << 4) + t;
  243. *p++ = n;
  244. //for migrate
  245. if (n == 0)
  246. *p++ = 0;
  247. p = encode_data_value(p, fv.field_value(i), t);
  248. }
  249. return p;
  250. }
  251. int encoded_bytes_multi_key(const DTCValue *v, const DTCTableDefinition *tdef)
  252. {
  253. if (tdef->key_fields() <= 1)
  254. return 0;
  255. int len = 1;
  256. for (int i = 1; i < tdef->key_fields(); i++)
  257. len += 2 + encoded_bytes_data_value(v + i, tdef->field_type(i));
  258. return len;
  259. }
  260. char *encode_multi_key(char *p, const DTCValue *v,
  261. const DTCTableDefinition *tdef)
  262. {
  263. if (tdef->key_fields() <= 1)
  264. return p;
  265. *p++ = tdef->key_fields() - 1;
  266. for (int i = 1; i < tdef->key_fields(); i++) {
  267. const int t = tdef->field_type(i);
  268. *p++ = ((DField::Set) << 4) + t;
  269. *p++ = i;
  270. p = encode_data_value(p, v + i, t);
  271. }
  272. return p;
  273. }
  274. /*
  275. * FieldSet format:
  276. * <NUM> <FIELD> <FIELD>...
  277. * NUM: 1 byte, total fields
  278. * FIELD: encoded field ID/name
  279. */
  280. int encoded_bytes_field_set(const FieldSetByName &fs)
  281. {
  282. if (fs.num_fields() == 0)
  283. return 0;
  284. int len = 1;
  285. for (int i = 0; i < fs.num_fields(); i++) {
  286. switch (fs.field_id(i)) {
  287. case 255:
  288. len += 2 + fs.field_name_length(i);
  289. break;
  290. case 0:
  291. len += 2;
  292. break;
  293. default:
  294. len += 1;
  295. }
  296. }
  297. return len;
  298. }
  299. char *encode_field_set(char *p, const FieldSetByName &fs)
  300. {
  301. if (fs.num_fields() == 0)
  302. return p;
  303. *p++ = fs.num_fields();
  304. for (int i = 0; i < fs.num_fields(); i++) {
  305. switch (fs.field_id(i)) {
  306. case 0:
  307. *p++ = 0;
  308. *p++ = 0;
  309. break;
  310. default:
  311. *p++ = fs.field_id(i);
  312. break;
  313. case 255:
  314. *p++ = 0;
  315. const int n = fs.field_name_length(i);
  316. *p++ = n;
  317. memcpy(p, fs.field_name(i), n);
  318. p += n;
  319. break;
  320. }
  321. }
  322. return p;
  323. }
  324. /*
  325. * field_value format:
  326. * <NUM> <OPER:TYPE> <FIELD> <LEN> <VALUE>
  327. * NUM: 1 byte, total fields
  328. * FIELD: encoded field ID/name
  329. */
  330. int encoded_bytes_field_value(const FieldValueByName &fv)
  331. {
  332. if (fv.num_fields() == 0)
  333. return 0;
  334. int len = 1;
  335. for (int i = 0; i < fv.num_fields(); i++) {
  336. switch (fv.field_id(i)) {
  337. case 255:
  338. len += 3 + fv.field_name_length(i);
  339. break;
  340. case 0:
  341. len += 3;
  342. break;
  343. default:
  344. len += 2;
  345. }
  346. len += encoded_bytes_data_value(fv.field_value(i),
  347. fv.field_type(i));
  348. }
  349. return len;
  350. }
  351. char *encode_field_value(char *p, const FieldValueByName &fv)
  352. {
  353. if (fv.num_fields() == 0)
  354. return p;
  355. *p++ = fv.num_fields();
  356. for (int i = 0; i < fv.num_fields(); i++) {
  357. int n = fv.field_name_length(i);
  358. int t = fv.field_type(i);
  359. if (t == DField::Unsigned)
  360. t = DField::Signed;
  361. *p++ = (fv.field_operation(i) << 4) + t;
  362. switch (fv.field_id(i)) {
  363. case 0:
  364. *p++ = 0;
  365. *p++ = 0;
  366. break;
  367. case 255:
  368. *p++ = 0;
  369. *p++ = n;
  370. memcpy(p, fv.field_name(i), n);
  371. p += n;
  372. break;
  373. default:
  374. *p++ = fv.field_id(i);
  375. }
  376. p = encode_data_value(p, fv.field_value(i), t);
  377. }
  378. return p;
  379. }