Coverage Report

Created: 2025-08-29 07:09

/src/hdf5/src/H5Oname.c
Line
Count
Source (jump to first uncovered line)
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*-------------------------------------------------------------------------
14
 *
15
 * Created:             H5Oname.c
16
 *
17
 * Purpose:             Object name (comment) message
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
#include "H5Omodule.h" /* This source code file is part of the H5O module */
23
24
#include "H5private.h"   /* Generic Functions     */
25
#include "H5Eprivate.h"  /* Error handling        */
26
#include "H5MMprivate.h" /* Memory management     */
27
#include "H5Opkg.h"      /* Object headers      */
28
29
/* PRIVATE PROTOTYPES */
30
static void *H5O__name_decode(H5F_t *f, H5O_t *open_oh, unsigned mesg_flags, unsigned *ioflags, size_t p_size,
31
                              const uint8_t *p);
32
static herr_t H5O__name_encode(H5F_t *f, bool disable_shared, size_t H5_ATTR_UNUSED p_size, uint8_t *p,
33
                               const void *_mesg);
34
static void  *H5O__name_copy(const void *_mesg, void *_dest);
35
static size_t H5O__name_size(const H5F_t *f, bool disable_shared, const void *_mesg);
36
static herr_t H5O__name_reset(void *_mesg);
37
static herr_t H5O__name_debug(H5F_t *f, const void *_mesg, FILE *stream, int indent, int fwidth);
38
39
/* This message derives from H5O message class */
40
const H5O_msg_class_t H5O_MSG_NAME[1] = {{
41
    H5O_NAME_ID,        /*message id number             */
42
    "name",             /*message name for debugging    */
43
    sizeof(H5O_name_t), /*native message size           */
44
    0,                  /* messages are shareable?       */
45
    H5O__name_decode,   /*decode message                */
46
    H5O__name_encode,   /*encode message                */
47
    H5O__name_copy,     /*copy the native value         */
48
    H5O__name_size,     /*raw message size              */
49
    H5O__name_reset,    /*free internal memory          */
50
    NULL,               /* free method      */
51
    NULL,               /* file delete method   */
52
    NULL,               /* link method      */
53
    NULL,               /*set share method    */
54
    NULL,               /*can share method    */
55
    NULL,               /* pre copy native value to file */
56
    NULL,               /* copy native value to file    */
57
    NULL,               /* post copy native value to file    */
58
    NULL,               /* get creation index   */
59
    NULL,               /* set creation index   */
60
    H5O__name_debug     /*debug the message             */
61
}};
62
63
/*-------------------------------------------------------------------------
64
 * Function:    H5O__name_decode
65
 *
66
 * Purpose:     Decode a name message and return a pointer to a new
67
 *              native message struct.
68
 *
69
 * Return:      Success:    Ptr to new message in native struct.
70
 *              Failure:    NULL
71
 *-------------------------------------------------------------------------
72
 */
73
static void *
74
H5O__name_decode(H5F_t H5_ATTR_NDEBUG_UNUSED *f, H5O_t H5_ATTR_UNUSED *open_oh,
75
                 unsigned H5_ATTR_UNUSED mesg_flags, unsigned H5_ATTR_UNUSED *ioflags, size_t p_size,
76
                 const uint8_t *p)
77
0
{
78
0
    H5O_name_t *mesg      = NULL;
79
0
    void       *ret_value = NULL;
80
81
0
    FUNC_ENTER_PACKAGE
82
83
0
    assert(f);
84
0
    assert(p);
85
86
0
    if (NULL == (mesg = (H5O_name_t *)H5MM_calloc(sizeof(H5O_name_t))))
87
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
88
89
0
    if (NULL == (mesg->s = (char *)H5MM_strndup((const char *)p, p_size - 1)))
90
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
91
92
0
    ret_value = mesg;
93
94
0
done:
95
0
    if (NULL == ret_value)
96
0
        if (mesg) {
97
0
            H5MM_xfree(mesg->s);
98
0
            H5MM_xfree(mesg);
99
0
        }
100
101
0
    FUNC_LEAVE_NOAPI(ret_value)
102
0
} /* end H5O__name_decode() */
103
104
/*-------------------------------------------------------------------------
105
 * Function:    H5O__name_encode
106
 *
107
 * Purpose:     Encodes a name message.
108
 *
109
 * Return:      Non-negative on success/Negative on failure
110
 *
111
 *-------------------------------------------------------------------------
112
 */
113
static herr_t
114
H5O__name_encode(H5F_t H5_ATTR_UNUSED *f, bool H5_ATTR_UNUSED disable_shared, size_t H5_ATTR_UNUSED p_size,
115
                 uint8_t *p, const void *_mesg)
116
0
{
117
0
    const H5O_name_t *mesg = (const H5O_name_t *)_mesg;
118
119
0
    FUNC_ENTER_PACKAGE_NOERR
120
121
    /* check args */
122
0
    assert(f);
123
0
    assert(p);
124
0
    assert(mesg && mesg->s);
125
126
    /* encode */
127
0
    strcpy((char *)p, mesg->s);
128
129
0
    FUNC_LEAVE_NOAPI(SUCCEED)
130
0
} /* end H5O__name_encode() */
131
132
/*-------------------------------------------------------------------------
133
 * Function:    H5O__name_copy
134
 *
135
 * Purpose:     Copies a message from _MESG to _DEST, allocating _DEST if
136
 *              necessary.
137
 *
138
 * Return:      Success:        Ptr to _DEST
139
 *
140
 *              Failure:        NULL
141
 *
142
 *-------------------------------------------------------------------------
143
 */
144
static void *
145
H5O__name_copy(const void *_mesg, void *_dest)
146
0
{
147
0
    const H5O_name_t *mesg      = (const H5O_name_t *)_mesg;
148
0
    H5O_name_t       *dest      = (H5O_name_t *)_dest;
149
0
    void             *ret_value = NULL; /* Return value */
150
151
0
    FUNC_ENTER_PACKAGE
152
153
    /* check args */
154
0
    assert(mesg);
155
156
0
    if (!dest && NULL == (dest = (H5O_name_t *)H5MM_calloc(sizeof(H5O_name_t))))
157
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
158
159
    /* copy */
160
0
    *dest = *mesg;
161
0
    if (NULL == (dest->s = H5MM_xstrdup(mesg->s)))
162
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
163
164
    /* Set return value */
165
0
    ret_value = dest;
166
167
0
done:
168
0
    if (NULL == ret_value)
169
0
        if (dest && NULL == _dest)
170
0
            dest = (H5O_name_t *)H5MM_xfree(dest);
171
172
0
    FUNC_LEAVE_NOAPI(ret_value)
173
0
} /* end H5O__name_copy() */
174
175
/*-------------------------------------------------------------------------
176
 * Function:    H5O__name_size
177
 *
178
 * Purpose:     Returns the size of the raw message in bytes not
179
 *              counting the message typ or size fields, but only the data
180
 *              fields.  This function doesn't take into account
181
 *              alignment.
182
 *
183
 * Return:      Success:        Message data size in bytes w/o alignment.
184
 *
185
 *              Failure:        Negative
186
 *
187
 *-------------------------------------------------------------------------
188
 */
189
static size_t
190
H5O__name_size(const H5F_t H5_ATTR_UNUSED *f, bool H5_ATTR_UNUSED disable_shared, const void *_mesg)
191
0
{
192
0
    const H5O_name_t *mesg      = (const H5O_name_t *)_mesg;
193
0
    size_t            ret_value = 0; /* Return value */
194
195
0
    FUNC_ENTER_PACKAGE_NOERR
196
197
    /* check args */
198
0
    assert(f);
199
0
    assert(mesg);
200
201
0
    ret_value = mesg->s ? strlen(mesg->s) + 1 : 0;
202
203
0
    FUNC_LEAVE_NOAPI(ret_value)
204
0
} /* end H5O__name_size() */
205
206
/*-------------------------------------------------------------------------
207
 * Function:    H5O__name_reset
208
 *
209
 * Purpose:     Frees internal pointers and resets the message to an
210
 *              initial state.
211
 *
212
 * Return:      Non-negative on success/Negative on failure
213
 *
214
 *-------------------------------------------------------------------------
215
 */
216
static herr_t
217
H5O__name_reset(void *_mesg)
218
0
{
219
0
    H5O_name_t *mesg = (H5O_name_t *)_mesg;
220
221
0
    FUNC_ENTER_PACKAGE_NOERR
222
223
    /* check args */
224
0
    assert(mesg);
225
226
    /* reset */
227
0
    mesg->s = (char *)H5MM_xfree(mesg->s);
228
229
0
    FUNC_LEAVE_NOAPI(SUCCEED)
230
0
} /* end H5O__name_reset() */
231
232
/*-------------------------------------------------------------------------
233
 * Function:    H5O__name_debug
234
 *
235
 * Purpose:     Prints debugging info for the message.
236
 *
237
 * Return:      Non-negative on success/Negative on failure
238
 *
239
 *-------------------------------------------------------------------------
240
 */
241
static herr_t
242
H5O__name_debug(H5F_t H5_ATTR_UNUSED *f, const void *_mesg, FILE *stream, int indent, int fwidth)
243
0
{
244
0
    const H5O_name_t *mesg = (const H5O_name_t *)_mesg;
245
246
0
    FUNC_ENTER_PACKAGE_NOERR
247
248
    /* check args */
249
0
    assert(f);
250
0
    assert(mesg);
251
0
    assert(stream);
252
0
    assert(indent >= 0);
253
0
    assert(fwidth >= 0);
254
255
0
    fprintf(stream, "%*s%-*s `%s'\n", indent, "", fwidth, "Name:", mesg->s);
256
257
0
    FUNC_LEAVE_NOAPI(SUCCEED)
258
0
} /* end H5O__name_debug() */