fields.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Protocol fields implementation. */
  5. #include "app.h"
  6. /* Create a new field of the specified type. Without populating its
  7. * type-specific value. */
  8. static ProtoViewField *field_new(ProtoViewFieldType type, const char *name) {
  9. ProtoViewField *f = malloc(sizeof(*f));
  10. f->type = type;
  11. f->name = strdup(name);
  12. return f;
  13. }
  14. /* Free a field an associated data. */
  15. static void field_free(ProtoViewField *f) {
  16. free(f->name);
  17. switch(f->type) {
  18. case FieldTypeStr: free(f->str); break;
  19. case FieldTypeBytes: free(f->bytes); break;
  20. default: break; // Nothing to free for other types.
  21. }
  22. free(f);
  23. }
  24. /* Return the type of the field as string. */
  25. const char *field_get_type_name(ProtoViewField *f) {
  26. switch(f->type) {
  27. case FieldTypeStr: return "str";
  28. case FieldTypeSignedInt: return "int";
  29. case FieldTypeUnsignedInt: return "uint";
  30. case FieldTypeBinary: return "bin";
  31. case FieldTypeHex: return "hex";
  32. case FieldTypeBytes: return "bytes";
  33. case FieldTypeFloat: return "float";
  34. }
  35. return "unknown";
  36. }
  37. /* Set a string representation of the specified field in buf. */
  38. int field_to_string(char *buf, size_t len, ProtoViewField *f) {
  39. switch(f->type) {
  40. case FieldTypeStr:
  41. return snprintf(buf,len,"%s", f->str);
  42. case FieldTypeSignedInt:
  43. return snprintf(buf,len,"%lld", (long long) f->value);
  44. case FieldTypeUnsignedInt:
  45. return snprintf(buf,len,"%llu", (unsigned long long) f->uvalue);
  46. case FieldTypeBinary:
  47. {
  48. uint64_t test_bit = (1 << (f->len-1));
  49. uint64_t idx = 0;
  50. while(idx < len-1 && test_bit) {
  51. buf[idx++] = (f->uvalue & test_bit) ? '1' : '0';
  52. test_bit >>= 1;
  53. }
  54. buf[idx] = 0;
  55. return idx;
  56. }
  57. case FieldTypeHex:
  58. return snprintf(buf, len, "%*llX", (int)(f->len+7)/8, f->uvalue);
  59. case FieldTypeFloat:
  60. return snprintf(buf, len, "%.*f", (int)f->len, (double)f->fvalue);
  61. case FieldTypeBytes:
  62. {
  63. uint64_t idx = 0;
  64. uint32_t nibble_num = f->len;
  65. while(idx < len-1 && nibble_num) {
  66. const char *charset = "0123456789ABCDEF";
  67. uint32_t nibble = nibble_num & 1 ?
  68. (f->bytes[idx/2] >> 4) :
  69. (f->bytes[idx/2] & 0xf);
  70. buf[idx++] = charset[nibble];
  71. nibble_num--;
  72. }
  73. buf[idx] = 0;
  74. return idx;
  75. }
  76. }
  77. return 0;
  78. }
  79. /* Set the field value from its string representation in 'buf'.
  80. * The field type must already be set and the field should be valid.
  81. * The string represenation 'buf' must be null termianted. Note that
  82. * even when representing binary values containing zero, this values
  83. * are taken as representations, so that would be the string "00" as
  84. * the Bytes type representation.
  85. *
  86. * The function returns true if the filed was successfully set to the
  87. * new value, otherwise if the specified value is invalid for the
  88. * field type, false is returned. */
  89. bool field_set_from_string(ProtoViewField *f, char *buf, size_t len) {
  90. // Initialize values to zero since the Flipper sscanf() implementation
  91. // is fuzzy... may populate only part of the value.
  92. long long val = 0;
  93. unsigned long long uval = 0;
  94. float fval = 0;
  95. switch(f->type) {
  96. case FieldTypeStr:
  97. free(f->str);
  98. f->len = len;
  99. f->str = malloc(len+1);
  100. memcpy(f->str,buf,len+1);
  101. break;
  102. case FieldTypeSignedInt:
  103. if (!sscanf(buf,"%lld",&val)) return false;
  104. f->value = val;
  105. break;
  106. case FieldTypeUnsignedInt:
  107. if (!sscanf(buf,"%llu",&uval)) return false;
  108. f->uvalue = uval;
  109. break;
  110. case FieldTypeBinary:
  111. {
  112. uint64_t bit_to_set = (1 << (len-1));
  113. uint64_t idx = 0;
  114. uval = 0;
  115. while(buf[idx]) {
  116. if (buf[idx] == '1') uval |= bit_to_set;
  117. else if (buf[idx] != '0') return false;
  118. bit_to_set >>= 1;
  119. idx++;
  120. }
  121. }
  122. break;
  123. case FieldTypeHex:
  124. if (!sscanf(buf,"%llx",&uval) &&
  125. !sscanf(buf,"%llX",&uval)) return false;
  126. f->uvalue = uval;
  127. break;
  128. case FieldTypeFloat:
  129. if (!sscanf(buf,"%f",&fval)) return false;
  130. f->fvalue = fval;
  131. break;
  132. case FieldTypeBytes:
  133. {
  134. if (len > f->len) return false;
  135. uint64_t idx = 0;
  136. uint64_t nibble_idx = len-1;
  137. while(buf[idx]) {
  138. uint8_t nibble = 0;
  139. char c = toupper(buf[idx]);
  140. if (c >= '0' && c <= '9') nibble = c-'0';
  141. else if (c >= 'A' && c <= 'F') nibble = 10+(c-'A');
  142. else return false;
  143. if (nibble_idx & 1) {
  144. f->bytes[idx/2] =
  145. (f->bytes[idx/2] & 0xF0) | nibble;
  146. } else {
  147. f->bytes[idx/2] =
  148. (f->bytes[idx/2] & 0x0F) | (nibble<<4);
  149. }
  150. nibble_idx--;
  151. idx++;
  152. }
  153. buf[idx] = 0;
  154. }
  155. break;
  156. }
  157. return true;
  158. }
  159. /* Free a field set and its contained fields. */
  160. void fieldset_free(ProtoViewFieldSet *fs) {
  161. for (uint32_t j = 0; j < fs->numfields; j++)
  162. field_free(fs->fields[j]);
  163. free(fs->fields);
  164. free(fs);
  165. }
  166. /* Allocate and init an empty field set. */
  167. ProtoViewFieldSet *fieldset_new(void) {
  168. ProtoViewFieldSet *fs = malloc(sizeof(*fs));
  169. fs->numfields = 0;
  170. fs->fields = NULL;
  171. return fs;
  172. }
  173. /* Append an already allocated field at the end of the specified field set. */
  174. static void fieldset_add_field(ProtoViewFieldSet *fs, ProtoViewField *field) {
  175. fs->numfields++;
  176. fs->fields = realloc(fs->fields,sizeof(ProtoViewField*)*fs->numfields);
  177. fs->fields[fs->numfields-1] = field;
  178. }
  179. /* Allocate and append an integer field. */
  180. void fieldset_add_int(ProtoViewFieldSet *fs, const char *name, int64_t val, uint8_t bits) {
  181. ProtoViewField *f = field_new(FieldTypeSignedInt,name);
  182. f->value = val;
  183. f->len = bits;
  184. fieldset_add_field(fs,f);
  185. }
  186. /* Allocate and append an unsigned field. */
  187. void fieldset_add_uint(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  188. ProtoViewField *f = field_new(FieldTypeUnsignedInt,name);
  189. f->uvalue = uval;
  190. f->len = bits;
  191. fieldset_add_field(fs,f);
  192. }
  193. /* Allocate and append a hex field. This is an unsigned number but
  194. * with an hex representation. */
  195. void fieldset_add_hex(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  196. ProtoViewField *f = field_new(FieldTypeHex,name);
  197. f->uvalue = uval;
  198. f->len = bits;
  199. fieldset_add_field(fs,f);
  200. }
  201. /* Allocate and append a bin field. This is an unsigned number but
  202. * with a binary representation. */
  203. void fieldset_add_bin(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  204. ProtoViewField *f = field_new(FieldTypeBinary,name);
  205. f->uvalue = uval;
  206. f->len = bits;
  207. fieldset_add_field(fs,f);
  208. }
  209. /* Allocate and append a string field. */
  210. void fieldset_add_str(ProtoViewFieldSet *fs, const char *name, const char *s) {
  211. ProtoViewField *f = field_new(FieldTypeStr,name);
  212. f->str = strdup(s);
  213. f->len = strlen(s);
  214. fieldset_add_field(fs,f);
  215. }
  216. /* Allocate and append a bytes field. Note that 'count' is specified in
  217. * nibbles (bytes*2). */
  218. void fieldset_add_bytes(ProtoViewFieldSet *fs, const char *name, const uint8_t *bytes, uint32_t count_nibbles) {
  219. uint32_t numbytes = (count_nibbles+count_nibbles%2)/2;
  220. ProtoViewField *f = field_new(FieldTypeBytes,name);
  221. f->bytes = malloc(numbytes);
  222. memcpy(f->bytes,bytes,numbytes);
  223. f->len = count_nibbles;
  224. fieldset_add_field(fs,f);
  225. }
  226. /* Allocate and append a float field. */
  227. void fieldset_add_float(ProtoViewFieldSet *fs, const char *name, float val, uint32_t digits_after_dot) {
  228. ProtoViewField *f = field_new(FieldTypeFloat,name);
  229. f->fvalue = val;
  230. f->len = digits_after_dot;
  231. fieldset_add_field(fs,f);
  232. }