fields.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. while(idx < len-1 && idx < f->len) {
  65. const char *charset = "0123456789ABCDEF";
  66. uint32_t nibble = idx & 1 ?
  67. (f->bytes[idx/2] & 0xf) :
  68. (f->bytes[idx/2] >> 4);
  69. buf[idx++] = charset[nibble];
  70. }
  71. buf[idx] = 0;
  72. return idx;
  73. }
  74. }
  75. return 0;
  76. }
  77. /* Set the field value from its string representation in 'buf'.
  78. * The field type must already be set and the field should be valid.
  79. * The string represenation 'buf' must be null termianted. Note that
  80. * even when representing binary values containing zero, this values
  81. * are taken as representations, so that would be the string "00" as
  82. * the Bytes type representation.
  83. *
  84. * The function returns true if the filed was successfully set to the
  85. * new value, otherwise if the specified value is invalid for the
  86. * field type, false is returned. */
  87. bool field_set_from_string(ProtoViewField *f, char *buf, size_t len) {
  88. // Initialize values to zero since the Flipper sscanf() implementation
  89. // is fuzzy... may populate only part of the value.
  90. long long val = 0;
  91. unsigned long long uval = 0;
  92. float fval = 0;
  93. switch(f->type) {
  94. case FieldTypeStr:
  95. free(f->str);
  96. f->len = len;
  97. f->str = malloc(len+1);
  98. memcpy(f->str,buf,len+1);
  99. break;
  100. case FieldTypeSignedInt:
  101. if (!sscanf(buf,"%lld",&val)) return false;
  102. f->value = val;
  103. break;
  104. case FieldTypeUnsignedInt:
  105. if (!sscanf(buf,"%llu",&uval)) return false;
  106. f->uvalue = uval;
  107. break;
  108. case FieldTypeBinary:
  109. {
  110. uint64_t bit_to_set = (1 << (len-1));
  111. uint64_t idx = 0;
  112. uval = 0;
  113. while(buf[idx]) {
  114. if (buf[idx] == '1') uval |= bit_to_set;
  115. else if (buf[idx] != '0') return false;
  116. bit_to_set >>= 1;
  117. idx++;
  118. }
  119. }
  120. break;
  121. case FieldTypeHex:
  122. if (!sscanf(buf,"%llx",&uval) &&
  123. !sscanf(buf,"%llX",&uval)) return false;
  124. f->uvalue = uval;
  125. break;
  126. case FieldTypeFloat:
  127. if (!sscanf(buf,"%f",&fval)) return false;
  128. f->fvalue = fval;
  129. break;
  130. case FieldTypeBytes:
  131. {
  132. if (len > f->len) return false;
  133. uint64_t idx = 0;
  134. while(buf[idx]) {
  135. uint8_t nibble = 0;
  136. char c = toupper(buf[idx]);
  137. if (c >= '0' && c <= '9') nibble = c-'0';
  138. else if (c >= 'A' && c <= 'F') nibble = 10+(c-'A');
  139. else return false;
  140. if (idx & 1) {
  141. f->bytes[idx/2] =
  142. (f->bytes[idx/2] & 0xF0) | nibble;
  143. } else {
  144. f->bytes[idx/2] =
  145. (f->bytes[idx/2] & 0x0F) | (nibble<<4);
  146. }
  147. idx++;
  148. }
  149. buf[idx] = 0;
  150. }
  151. break;
  152. }
  153. return true;
  154. }
  155. /* Free a field set and its contained fields. */
  156. void fieldset_free(ProtoViewFieldSet *fs) {
  157. for (uint32_t j = 0; j < fs->numfields; j++)
  158. field_free(fs->fields[j]);
  159. free(fs->fields);
  160. free(fs);
  161. }
  162. /* Allocate and init an empty field set. */
  163. ProtoViewFieldSet *fieldset_new(void) {
  164. ProtoViewFieldSet *fs = malloc(sizeof(*fs));
  165. fs->numfields = 0;
  166. fs->fields = NULL;
  167. return fs;
  168. }
  169. /* Append an already allocated field at the end of the specified field set. */
  170. static void fieldset_add_field(ProtoViewFieldSet *fs, ProtoViewField *field) {
  171. fs->numfields++;
  172. fs->fields = realloc(fs->fields,sizeof(ProtoViewField*)*fs->numfields);
  173. fs->fields[fs->numfields-1] = field;
  174. }
  175. /* Allocate and append an integer field. */
  176. void fieldset_add_int(ProtoViewFieldSet *fs, const char *name, int64_t val, uint8_t bits) {
  177. ProtoViewField *f = field_new(FieldTypeSignedInt,name);
  178. f->value = val;
  179. f->len = bits;
  180. fieldset_add_field(fs,f);
  181. }
  182. /* Allocate and append an unsigned field. */
  183. void fieldset_add_uint(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  184. ProtoViewField *f = field_new(FieldTypeUnsignedInt,name);
  185. f->uvalue = uval;
  186. f->len = bits;
  187. fieldset_add_field(fs,f);
  188. }
  189. /* Allocate and append a hex field. This is an unsigned number but
  190. * with an hex representation. */
  191. void fieldset_add_hex(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  192. ProtoViewField *f = field_new(FieldTypeHex,name);
  193. f->uvalue = uval;
  194. f->len = bits;
  195. fieldset_add_field(fs,f);
  196. }
  197. /* Allocate and append a bin field. This is an unsigned number but
  198. * with a binary representation. */
  199. void fieldset_add_bin(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  200. ProtoViewField *f = field_new(FieldTypeBinary,name);
  201. f->uvalue = uval;
  202. f->len = bits;
  203. fieldset_add_field(fs,f);
  204. }
  205. /* Allocate and append a string field. */
  206. void fieldset_add_str(ProtoViewFieldSet *fs, const char *name, const char *s) {
  207. ProtoViewField *f = field_new(FieldTypeStr,name);
  208. f->str = strdup(s);
  209. f->len = strlen(s);
  210. fieldset_add_field(fs,f);
  211. }
  212. /* Allocate and append a bytes field. Note that 'count' is specified in
  213. * nibbles (bytes*2). */
  214. void fieldset_add_bytes(ProtoViewFieldSet *fs, const char *name, const uint8_t *bytes, uint32_t count_nibbles) {
  215. uint32_t numbytes = (count_nibbles+count_nibbles%2)/2;
  216. ProtoViewField *f = field_new(FieldTypeBytes,name);
  217. f->bytes = malloc(numbytes);
  218. memcpy(f->bytes,bytes,numbytes);
  219. f->len = count_nibbles;
  220. fieldset_add_field(fs,f);
  221. }
  222. /* Allocate and append a float field. */
  223. void fieldset_add_float(ProtoViewFieldSet *fs, const char *name, float val, uint32_t digits_after_dot) {
  224. ProtoViewField *f = field_new(FieldTypeFloat,name);
  225. f->fvalue = val;
  226. f->len = digits_after_dot;
  227. fieldset_add_field(fs,f);
  228. }