+static const struct declaration_integer *
+get_declaration_integer(const struct declaration *decl)
+{
+ if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_INTEGER)
+ return NULL;
+ return container_of(decl, const struct declaration_integer, p);
+}
+
+static const struct declaration_string *
+get_declaration_string(const struct declaration *decl)
+{
+ if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_STRING)
+ return NULL;
+ return container_of(decl, const struct declaration_string, p);
+}
+
+static const struct declaration_array *
+get_declaration_array(const struct declaration *decl)
+{
+ if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_ARRAY)
+ return NULL;
+ return container_of(decl, const struct declaration_array, p);
+}
+
+static const struct declaration_sequence *
+get_declaration_sequence(const struct declaration *decl)
+{
+ if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_SEQUENCE)
+ return NULL;
+ return container_of(decl, const struct declaration_sequence, p);
+}
+
+int bt_ctf_get_int_signedness(const struct declaration *decl)
+{
+ const struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (!integer) {
+ bt_ctf_field_set_error(-EINVAL);
+ return -EINVAL;
+ }
+ return integer->signedness;
+}
+
+int bt_ctf_get_int_base(const struct declaration *decl)
+{
+ const struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (!integer) {
+ bt_ctf_field_set_error(-EINVAL);
+ return -EINVAL;
+ }
+ return integer->base;
+}
+
+int bt_ctf_get_int_byte_order(const struct declaration *decl)
+{
+ const struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (!integer) {
+ bt_ctf_field_set_error(-EINVAL);
+ return -EINVAL;
+ }
+ return integer->byte_order;
+}
+
+ssize_t bt_ctf_get_int_len(const struct declaration *decl)
+{
+ const struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (!integer) {
+ bt_ctf_field_set_error(-EINVAL);
+ return -EINVAL;
+ }
+ return (ssize_t) integer->len;
+}
+
+const struct definition *bt_ctf_get_enum_int(const struct definition *field)
+{
+ const struct definition_enum *def_enum;
+
+ if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
+ bt_ctf_field_set_error(-EINVAL);
+ return NULL;
+ }
+ def_enum = container_of(field, const struct definition_enum, p);
+ return &def_enum->integer->p;
+}
+
+const char *bt_ctf_get_enum_str(const struct definition *field)
+{
+ const struct definition_enum *def_enum;
+ const struct declaration_enum *decl_enum;
+ GArray *array;
+ const char *ret;
+
+ if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
+ bt_ctf_field_set_error(-EINVAL);
+ return NULL;
+ }
+ def_enum = container_of(field, const struct definition_enum, p);
+ decl_enum = def_enum->declaration;
+ if (bt_get_int_signedness(&def_enum->integer->p)) {
+ array = bt_enum_int_to_quark_set(decl_enum,
+ bt_get_signed_int(&def_enum->integer->p));
+ } else {
+ array = bt_enum_uint_to_quark_set(decl_enum,
+ bt_get_unsigned_int(&def_enum->integer->p));
+ }
+ if (!array) {
+ bt_ctf_field_set_error(-ENOENT);
+ return NULL;
+ }
+
+ if (array->len == 0) {
+ g_array_unref(array);
+ bt_ctf_field_set_error(-ENOENT);
+ return NULL;
+ }
+ /* Return first string. Arbitrary choice. */
+ ret = g_quark_to_string(g_array_index(array, GQuark, 0));
+ g_array_unref(array);
+ return ret;
+}
+
+enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl)
+{
+ enum ctf_string_encoding ret = 0;
+ enum ctf_type_id type;
+ const struct declaration_integer *integer;
+ const struct declaration_string *string;
+ const struct declaration_array *array;
+ const struct declaration_sequence *sequence;
+
+ if (!decl)
+ goto error;
+
+ type = bt_ctf_field_type(decl);
+
+ switch (type) {
+ case CTF_TYPE_ARRAY:
+ array = get_declaration_array(decl);
+ if (!array)
+ goto error;
+ integer = get_declaration_integer(array->elem);
+ if (!integer)
+ goto error;
+ ret = integer->encoding;
+ break;
+ case CTF_TYPE_SEQUENCE:
+ sequence = get_declaration_sequence(decl);
+ if (!sequence)
+ goto error;
+ integer = get_declaration_integer(sequence->elem);
+ if (!integer)
+ goto error;
+ ret = integer->encoding;
+ break;
+ case CTF_TYPE_STRING:
+ string = get_declaration_string(decl);
+ if (!string)
+ goto error;
+ ret = string->encoding;
+ break;
+ case CTF_TYPE_INTEGER:
+ integer = get_declaration_integer(decl);
+ if (!integer)
+ goto error;
+ ret = integer->encoding;
+ break;
+ default:
+ goto error;
+ }
+ return ret;
+
+error:
+ bt_ctf_field_set_error(-EINVAL);
+ return -1;
+}
+
+int bt_ctf_get_array_len(const struct declaration *decl)
+{
+ const struct declaration_array *array;
+
+ array = get_declaration_array(decl);
+ if (!array)
+ goto error;
+ return array->len;
+
+error:
+ bt_ctf_field_set_error(-EINVAL);
+ return -1;
+}
+