1867d70fcSchristos /* Labelled ranges of type IDs.
2*c42dbd0eSchristos Copyright (C) 2019-2022 Free Software Foundation, Inc.
3867d70fcSchristos
4867d70fcSchristos This file is part of libctf.
5867d70fcSchristos
6867d70fcSchristos libctf is free software; you can redistribute it and/or modify it under
7867d70fcSchristos the terms of the GNU General Public License as published by the Free
8867d70fcSchristos Software Foundation; either version 3, or (at your option) any later
9867d70fcSchristos version.
10867d70fcSchristos
11867d70fcSchristos This program is distributed in the hope that it will be useful, but
12867d70fcSchristos WITHOUT ANY WARRANTY; without even the implied warranty of
13867d70fcSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14867d70fcSchristos See the GNU General Public License for more details.
15867d70fcSchristos
16867d70fcSchristos You should have received a copy of the GNU General Public License
17867d70fcSchristos along with this program; see the file COPYING. If not see
18867d70fcSchristos <http://www.gnu.org/licenses/>. */
19867d70fcSchristos
20867d70fcSchristos #include <ctf-impl.h>
21867d70fcSchristos #include <string.h>
22867d70fcSchristos
23867d70fcSchristos static int
extract_label_info(ctf_dict_t * fp,const ctf_lblent_t ** ctl,uint32_t * num_labels)24*c42dbd0eSchristos extract_label_info (ctf_dict_t *fp, const ctf_lblent_t **ctl,
25867d70fcSchristos uint32_t *num_labels)
26867d70fcSchristos {
27867d70fcSchristos const ctf_header_t *h;
28867d70fcSchristos
29867d70fcSchristos h = (const ctf_header_t *) fp->ctf_data.cts_data;
30867d70fcSchristos
31867d70fcSchristos *ctl = (const ctf_lblent_t *) (fp->ctf_buf + h->cth_lbloff);
32867d70fcSchristos *num_labels = (h->cth_objtoff - h->cth_lbloff) / sizeof (ctf_lblent_t);
33867d70fcSchristos
34867d70fcSchristos return 0;
35867d70fcSchristos }
36867d70fcSchristos
37867d70fcSchristos /* Returns the topmost label, or NULL if any errors are encountered. */
38867d70fcSchristos
39867d70fcSchristos const char *
ctf_label_topmost(ctf_dict_t * fp)40*c42dbd0eSchristos ctf_label_topmost (ctf_dict_t *fp)
41867d70fcSchristos {
42867d70fcSchristos const ctf_lblent_t *ctlp = NULL;
43867d70fcSchristos const char *s;
44867d70fcSchristos uint32_t num_labels = 0;
45867d70fcSchristos
46867d70fcSchristos if (extract_label_info (fp, &ctlp, &num_labels) < 0)
47867d70fcSchristos return NULL; /* errno is set for us. */
48867d70fcSchristos
49867d70fcSchristos if (num_labels == 0)
50867d70fcSchristos {
51867d70fcSchristos (void) ctf_set_errno (fp, ECTF_NOLABELDATA);
52867d70fcSchristos return NULL;
53867d70fcSchristos }
54867d70fcSchristos
55867d70fcSchristos if ((s = ctf_strraw (fp, (ctlp + num_labels - 1)->ctl_label)) == NULL)
56867d70fcSchristos (void) ctf_set_errno (fp, ECTF_CORRUPT);
57867d70fcSchristos
58867d70fcSchristos return s;
59867d70fcSchristos }
60867d70fcSchristos
61867d70fcSchristos /* Iterate over all labels. We pass the label string and the lblinfo_t struct
62867d70fcSchristos to the specified callback function. */
63867d70fcSchristos int
ctf_label_iter(ctf_dict_t * fp,ctf_label_f * func,void * arg)64*c42dbd0eSchristos ctf_label_iter (ctf_dict_t *fp, ctf_label_f *func, void *arg)
65867d70fcSchristos {
66867d70fcSchristos const ctf_lblent_t *ctlp = NULL;
67867d70fcSchristos uint32_t i;
68867d70fcSchristos uint32_t num_labels = 0;
69867d70fcSchristos ctf_lblinfo_t linfo;
70867d70fcSchristos const char *lname;
71867d70fcSchristos int rc;
72867d70fcSchristos
73867d70fcSchristos if (extract_label_info (fp, &ctlp, &num_labels) < 0)
74867d70fcSchristos return -1; /* errno is set for us. */
75867d70fcSchristos
76867d70fcSchristos if (num_labels == 0)
77867d70fcSchristos return (ctf_set_errno (fp, ECTF_NOLABELDATA));
78867d70fcSchristos
79867d70fcSchristos for (i = 0; i < num_labels; i++, ctlp++)
80867d70fcSchristos {
81867d70fcSchristos if ((lname = ctf_strraw (fp, ctlp->ctl_label)) == NULL)
82867d70fcSchristos {
83*c42dbd0eSchristos /* Not marked for translation: label code not used yet. */
84*c42dbd0eSchristos ctf_err_warn (fp, 0, ECTF_CORRUPT,
85*c42dbd0eSchristos "failed to decode label %u with type %u",
86*c42dbd0eSchristos ctlp->ctl_label, ctlp->ctl_type);
87867d70fcSchristos return (ctf_set_errno (fp, ECTF_CORRUPT));
88867d70fcSchristos }
89867d70fcSchristos
90867d70fcSchristos linfo.ctb_type = ctlp->ctl_type;
91867d70fcSchristos if ((rc = func (lname, &linfo, arg)) != 0)
92867d70fcSchristos return rc;
93867d70fcSchristos }
94867d70fcSchristos
95867d70fcSchristos return 0;
96867d70fcSchristos }
97867d70fcSchristos
98867d70fcSchristos typedef struct linfo_cb_arg
99867d70fcSchristos {
100867d70fcSchristos const char *lca_name; /* Label we want to retrieve info for. */
101867d70fcSchristos ctf_lblinfo_t *lca_info; /* Where to store the info about the label. */
102867d70fcSchristos } linfo_cb_arg_t;
103867d70fcSchristos
104867d70fcSchristos static int
label_info_cb(const char * lname,const ctf_lblinfo_t * linfo,void * arg)105867d70fcSchristos label_info_cb (const char *lname, const ctf_lblinfo_t *linfo, void *arg)
106867d70fcSchristos {
107867d70fcSchristos /* If lname matches the label we are looking for, copy the
108867d70fcSchristos lblinfo_t struct for the caller. */
109867d70fcSchristos
110867d70fcSchristos if (strcmp (lname, ((linfo_cb_arg_t *) arg)->lca_name) == 0)
111867d70fcSchristos {
112867d70fcSchristos /* * Allow caller not to allocate storage to test if label exists. */
113867d70fcSchristos
114867d70fcSchristos if (((linfo_cb_arg_t *) arg)->lca_info != NULL)
115867d70fcSchristos memcpy (((linfo_cb_arg_t *) arg)->lca_info, linfo,
116867d70fcSchristos sizeof (ctf_lblinfo_t));
117867d70fcSchristos return 1; /* Indicate we found a match. */
118867d70fcSchristos }
119867d70fcSchristos
120867d70fcSchristos return 0;
121867d70fcSchristos }
122867d70fcSchristos
123867d70fcSchristos /* Retrieve information about the label with name "lname". */
124867d70fcSchristos int
ctf_label_info(ctf_dict_t * fp,const char * lname,ctf_lblinfo_t * linfo)125*c42dbd0eSchristos ctf_label_info (ctf_dict_t *fp, const char *lname, ctf_lblinfo_t *linfo)
126867d70fcSchristos {
127867d70fcSchristos linfo_cb_arg_t cb_arg;
128867d70fcSchristos int rc;
129867d70fcSchristos
130867d70fcSchristos cb_arg.lca_name = lname;
131867d70fcSchristos cb_arg.lca_info = linfo;
132867d70fcSchristos
133867d70fcSchristos if ((rc = ctf_label_iter (fp, label_info_cb, &cb_arg)) < 0)
134867d70fcSchristos return rc;
135867d70fcSchristos
136867d70fcSchristos if (rc != 1)
137867d70fcSchristos return (ctf_set_errno (fp, ECTF_NOLABEL));
138867d70fcSchristos
139867d70fcSchristos return 0;
140867d70fcSchristos }
141