char * fvalue_to_string_repr(fvalue_t *fv, ftrepr_t rtype, char *buf) { g_assert(fv->ftype->val_to_string_repr); if (!buf) { int len; if ((len = fvalue_string_repr_len(fv, rtype)) >= 0) { buf = g_malloc0(len + 1); } else { /* the value cannot be represented in the given representation type (rtype) */ return NULL; } } fv->ftype->val_to_string_repr(fv, rtype, buf); return buf; }
char * fvalue_to_string_repr(wmem_allocator_t *scope, fvalue_t *fv, ftrepr_t rtype, int field_display) { char *buf; int len; if (fv->ftype->val_to_string_repr == NULL) { /* no value-to-string-representation function, so the value cannot be represented */ return NULL; } if ((len = fvalue_string_repr_len(fv, rtype, field_display)) >= 0) { buf = (char *)wmem_alloc0(scope, len + 1); } else { /* the value cannot be represented in the given representation type (rtype) */ return NULL; } fv->ftype->val_to_string_repr(fv, rtype, field_display, buf, (unsigned int)len+1); return buf; }
static gboolean print_field_value(field_info *finfo, int cmd_line_index) { header_field_info *hfinfo; static char *fs_buf = NULL; char *fs_ptr = fs_buf; static GString *label_s = NULL; int fs_buf_len = FIELD_STR_INIT_LEN, fs_len; guint i; string_fmt_t *sf; guint32 uvalue; gint32 svalue; const true_false_string *tfstring = &tfs_true_false; hfinfo = finfo->hfinfo; if (!fs_buf) { fs_buf = g_malloc(fs_buf_len + 1); fs_ptr = fs_buf; } if (!label_s) { label_s = g_string_new(""); } if(finfo->value.ftype->val_to_string_repr) { /* * this field has an associated value, * e.g: ip.hdr_len */ fs_len = fvalue_string_repr_len(&finfo->value, FTREPR_DFILTER); while (fs_buf_len < fs_len) { fs_buf_len *= 2; fs_buf = g_realloc(fs_buf, fs_buf_len + 1); fs_ptr = fs_buf; } fvalue_to_string_repr(&finfo->value, FTREPR_DFILTER, fs_buf); /* String types are quoted. Remove them. */ if ((finfo->value.ftype->ftype == FT_STRING || finfo->value.ftype->ftype == FT_STRINGZ) && fs_len > 2) { fs_buf[fs_len - 1] = '\0'; fs_ptr++; } } if (string_fmts->len > 0 && finfo->hfinfo->strings) { g_string_truncate(label_s, 0); for (i = 0; i < string_fmts->len; i++) { sf = g_ptr_array_index(string_fmts, i); if (sf->plain) { g_string_append(label_s, sf->plain); } else { switch (sf->format) { case SF_NAME: g_string_append(label_s, hfinfo->name); break; case SF_NUMVAL: g_string_append(label_s, fs_ptr); break; case SF_STRVAL: switch(hfinfo->type) { case FT_BOOLEAN: uvalue = fvalue_get_uinteger(&finfo->value); tfstring = (const struct true_false_string*) hfinfo->strings; g_string_append(label_s, uvalue ? tfstring->true_string : tfstring->false_string); break; case FT_INT8: case FT_INT16: case FT_INT24: case FT_INT32: DISSECTOR_ASSERT(!hfinfo->bitmask); svalue = fvalue_get_sinteger(&finfo->value); if (hfinfo->display & BASE_RANGE_STRING) { g_string_append(label_s, rval_to_str(svalue, hfinfo->strings, "Unknown")); } else { g_string_append(label_s, val_to_str(svalue, cVALS(hfinfo->strings), "Unknown")); } case FT_UINT8: case FT_UINT16: case FT_UINT24: case FT_UINT32: uvalue = fvalue_get_uinteger(&finfo->value); if (!hfinfo->bitmask && hfinfo->display & BASE_RANGE_STRING) { g_string_append(label_s, rval_to_str(uvalue, hfinfo->strings, "Unknown")); } else { g_string_append(label_s, val_to_str(uvalue, cVALS(hfinfo->strings), "Unknown")); } break; default: break; } break; default: break; } } } printf(" %u=\"%s\"", cmd_line_index, label_s->str); return TRUE; } if(finfo->value.ftype->val_to_string_repr) { printf(" %u=\"%s\"", cmd_line_index, fs_ptr); return TRUE; } /* * This field doesn't have an associated value, * e.g. http * We return n.a. */ printf(" %u=\"n.a.\"", cmd_line_index); return TRUE; }