fields.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /* Free a field set and its contained fields. */
  25. void fieldset_free(ProtoViewFieldSet *fs) {
  26. for (uint32_t j = 0; j < fs->numfields; j++)
  27. field_free(fs->fields[j]);
  28. free(fs->fields);
  29. free(fs);
  30. }
  31. /* Allocate and init an empty field set. */
  32. ProtoViewFieldSet *fieldset_new(void) {
  33. ProtoViewFieldSet *fs = malloc(sizeof(*fs));
  34. fs->numfields = 0;
  35. fs->fields = NULL;
  36. return fs;
  37. }
  38. /* Append an already allocated field at the end of the specified field set. */
  39. static void fieldset_add_field(ProtoViewFieldSet *fs, ProtoViewField *field) {
  40. fs->numfields++;
  41. fs->fields = realloc(fs->fields,sizeof(ProtoViewField*)*fs->numfields);
  42. fs->fields[fs->numfields-1] = field;
  43. }
  44. /* Allocate and append an integer field. */
  45. void fieldset_add_int(ProtoViewFieldSet *fs, const char *name, int64_t val, uint8_t bits) {
  46. ProtoViewField *f = field_new(FieldTypeSignedInt,name);
  47. f->value = val;
  48. f->len = bits;
  49. fieldset_add_field(fs,f);
  50. }
  51. /* Allocate and append an unsigned field. */
  52. void fieldset_add_uint(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  53. ProtoViewField *f = field_new(FieldTypeUnsignedInt,name);
  54. f->uvalue = uval;
  55. f->len = bits;
  56. fieldset_add_field(fs,f);
  57. }
  58. /* Allocate and append a hex field. This is an unsigned number but
  59. * with an hex representation. */
  60. void fieldset_add_hex(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  61. ProtoViewField *f = field_new(FieldTypeHex,name);
  62. f->uvalue = uval;
  63. f->len = bits;
  64. fieldset_add_field(fs,f);
  65. }
  66. /* Allocate and append a bin field. This is an unsigned number but
  67. * with a binary representation. */
  68. void fieldset_add_bin(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits) {
  69. ProtoViewField *f = field_new(FieldTypeBinary,name);
  70. f->uvalue = uval;
  71. f->len = bits;
  72. fieldset_add_field(fs,f);
  73. }
  74. /* Allocate and append a string field. */
  75. void fieldset_add_str(ProtoViewFieldSet *fs, const char *name, const char *s) {
  76. ProtoViewField *f = field_new(FieldTypeStr,name);
  77. f->str = strdup(s);
  78. f->len = strlen(s);
  79. fieldset_add_field(fs,f);
  80. }
  81. /* Allocate and append a bytes field. Note that 'count' is specified in
  82. * nibbles (bytes*2). */
  83. void fieldset_add_bytes(ProtoViewFieldSet *fs, const char *name, const uint8_t *bytes, uint32_t count_nibbles) {
  84. ProtoViewField *f = field_new(FieldTypeBytes,name);
  85. f->bytes = malloc(count_nibbles/2);
  86. memcpy(f->bytes,bytes,count_nibbles/2);
  87. f->len = count_nibbles;
  88. fieldset_add_field(fs,f);
  89. }
  90. /* Allocate and append a float field. */
  91. void fieldset_add_float(ProtoViewFieldSet *fs, const char *name, float val, uint32_t digits_after_dot) {
  92. ProtoViewField *f = field_new(FieldTypeFloat,name);
  93. f->fvalue = val;
  94. f->len = digits_after_dot;
  95. fieldset_add_field(fs,f);
  96. }