fields.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 = 0;
  65. while(idx < len-1 && nibble_num < f->len) {
  66. const char *charset = "0123456789ABCDEF";
  67. uint32_t nibble = nibble_num & 1 ?
  68. (f->bytes[nibble_num/2] >> 4) :
  69. (f->bytes[nibble_num/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. long long val;
  91. unsigned long long uval;
  92. float fval;
  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. uint64_t nibble_idx = len-1;
  135. while(buf[idx]) {
  136. uint8_t nibble = 0;
  137. char c = toupper(buf[idx]);
  138. if (c >= '0' && c <= '9') nibble = c-'0';
  139. else if (c >= 'A' && c <= 'F') nibble = c-'A';
  140. else return false;
  141. if (nibble_idx & 1) {
  142. f->bytes[idx/2] =
  143. (f->bytes[idx/2] & 0x0F) | (nibble<<4);
  144. } else {
  145. f->bytes[idx/2] =
  146. (f->bytes[idx/2] & 0xF0) | nibble;
  147. }
  148. nibble_idx--;
  149. idx++;
  150. }
  151. buf[idx] = 0;
  152. }
  153. break;
  154. }
  155. return true;
  156. }
  157. /* Free a field set and its contained fields. */
  158. void fieldset_free(ProtoViewFieldSet *fs) {
  159. for (uint32_t j = 0; j < fs->numfields; j++)
  160. field_free(fs->fields[j]);
  161. free(fs->fields);
  162. free(fs);
  163. }
  164. /* Allocate and init an empty field set. */
  165. ProtoViewFieldSet *fieldset_new(void) {
  166. ProtoViewFieldSet *fs = malloc(sizeof(*fs));
  167. fs->numfields = 0;
  168. fs->fields = NULL;
  169. return fs;
  170. }
  171. /* Append an already allocated field at the end of the specified field set. */
  172. static void fieldset_add_field(ProtoViewFieldSet *fs, ProtoViewField *field) {
  173. fs->numfields++;
  174. fs->fields = realloc(fs->fields,sizeof(ProtoViewField*)*fs->numfields);
  175. fs->fields[fs->numfields-1] = field;
  176. }
  177. /* Allocate and append an integer field. */
  178. void fieldset_add_int(ProtoViewFieldSet *fs, const char *name, int64_t val, uint8_t bits) {
  179. ProtoViewField *f = field_new(FieldTypeSignedInt,name);
  180. f->value = val;
  181. f->len = bits;
  182. fieldset_add_field(fs,f);
  183. }
  184. /* Allocate and append an unsigned field. */
  185. void fieldset_add_uint(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  186. ProtoViewField *f = field_new(FieldTypeUnsignedInt,name);
  187. f->uvalue = uval;
  188. f->len = bits;
  189. fieldset_add_field(fs,f);
  190. }
  191. /* Allocate and append a hex field. This is an unsigned number but
  192. * with an hex representation. */
  193. void fieldset_add_hex(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  194. ProtoViewField *f = field_new(FieldTypeHex,name);
  195. f->uvalue = uval;
  196. f->len = bits;
  197. fieldset_add_field(fs,f);
  198. }
  199. /* Allocate and append a bin field. This is an unsigned number but
  200. * with a binary representation. */
  201. void fieldset_add_bin(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  202. ProtoViewField *f = field_new(FieldTypeBinary,name);
  203. f->uvalue = uval;
  204. f->len = bits;
  205. fieldset_add_field(fs,f);
  206. }
  207. /* Allocate and append a string field. */
  208. void fieldset_add_str(ProtoViewFieldSet *fs, const char *name, const char *s) {
  209. ProtoViewField *f = field_new(FieldTypeStr,name);
  210. f->str = strdup(s);
  211. f->len = strlen(s);
  212. fieldset_add_field(fs,f);
  213. }
  214. /* Allocate and append a bytes field. Note that 'count' is specified in
  215. * nibbles (bytes*2). */
  216. void fieldset_add_bytes(ProtoViewFieldSet *fs, const char *name, const uint8_t *bytes, uint32_t count_nibbles) {
  217. uint32_t numbytes = (count_nibbles+count_nibbles%2)/2;
  218. ProtoViewField *f = field_new(FieldTypeBytes,name);
  219. f->bytes = malloc(numbytes);
  220. memcpy(f->bytes,bytes,numbytes);
  221. f->len = count_nibbles;
  222. fieldset_add_field(fs,f);
  223. }
  224. /* Allocate and append a float field. */
  225. void fieldset_add_float(ProtoViewFieldSet *fs, const char *name, float val, uint32_t digits_after_dot) {
  226. ProtoViewField *f = field_new(FieldTypeFloat,name);
  227. f->fvalue = val;
  228. f->len = digits_after_dot;
  229. fieldset_add_field(fs,f);
  230. }