Coverage Report

Created: 2025-09-05 07:09

/src/libxlsxwriter/src/rich_value.c
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * rich_value - A library for creating Excel XLSX rich_value files.
3
 *
4
 * Used in conjunction with the libxlsxwriter library.
5
 *
6
 * Copyright 2014-2025, John McNamara, jmcnamara@cpan.org. See LICENSE.txt.
7
 *
8
 */
9
10
#include "xlsxwriter/xmlwriter.h"
11
#include "xlsxwriter/rich_value.h"
12
#include "xlsxwriter/utility.h"
13
14
/*
15
 * Forward declarations.
16
 */
17
18
/*****************************************************************************
19
 *
20
 * Private functions.
21
 *
22
 ****************************************************************************/
23
24
/*
25
 * Create a new rich_value object.
26
 */
27
lxw_rich_value *
28
lxw_rich_value_new(void)
29
0
{
30
0
    lxw_rich_value *rich_value = calloc(1, sizeof(lxw_rich_value));
31
0
    GOTO_LABEL_ON_MEM_ERROR(rich_value, mem_error);
32
33
0
    return rich_value;
34
35
0
mem_error:
36
0
    lxw_rich_value_free(rich_value);
37
0
    return NULL;
38
0
}
39
40
/*
41
 * Free a rich_value object.
42
 */
43
void
44
lxw_rich_value_free(lxw_rich_value *rich_value)
45
0
{
46
0
    if (!rich_value)
47
0
        return;
48
49
0
    free(rich_value);
50
0
}
51
52
/*****************************************************************************
53
 *
54
 * XML functions.
55
 *
56
 ****************************************************************************/
57
58
/*
59
 * Write the XML declaration.
60
 */
61
STATIC void
62
_rich_value_xml_declaration(lxw_rich_value *self)
63
0
{
64
0
    lxw_xml_declaration(self->file);
65
0
}
66
67
/*****************************************************************************
68
 *
69
 * XML file assembly functions.
70
 *
71
 ****************************************************************************/
72
73
/*
74
 * Write the <v> element.
75
 */
76
STATIC void
77
_rich_value_write_v(lxw_rich_value *self, char *value)
78
0
{
79
0
    lxw_xml_data_element(self->file, "v", value, NULL);
80
81
0
}
82
83
/*
84
 * Write the <rv> element.
85
 */
86
STATIC void
87
_rich_value_write_rv(lxw_rich_value *self)
88
0
{
89
0
    struct xml_attribute_list attributes;
90
0
    struct xml_attribute *attribute;
91
92
0
    LXW_INIT_ATTRIBUTES();
93
0
    LXW_PUSH_ATTRIBUTES_STR("s", "0");
94
95
0
    lxw_xml_start_tag(self->file, "rv", &attributes);
96
97
0
    LXW_FREE_ATTRIBUTES();
98
0
}
99
100
/*
101
 * Write the metadata for each embedded image.
102
 */
103
void
104
lxw_rich_value_write_images(lxw_rich_value *self)
105
0
{
106
107
0
    lxw_workbook *workbook = self->workbook;
108
0
    lxw_sheet *sheet;
109
0
    lxw_worksheet *worksheet;
110
0
    lxw_object_properties *object_props;
111
0
    char value[LXW_UINT32_T_LENGTH];
112
0
    uint32_t index = 0;
113
0
    uint8_t type = 5;
114
115
0
    STAILQ_FOREACH(sheet, workbook->sheets, list_pointers) {
116
0
        if (sheet->is_chartsheet)
117
0
            continue;
118
0
        else
119
0
            worksheet = sheet->u.worksheet;
120
121
0
        STAILQ_FOREACH(object_props, worksheet->embedded_image_props,
122
0
                       list_pointers) {
123
124
0
            if (object_props->is_duplicate)
125
0
                continue;
126
127
0
            if (object_props->decorative)
128
0
                type = 6;
129
130
            /* Write the rv element. */
131
0
            _rich_value_write_rv(self);
132
133
            /* Write the v element. */
134
0
            lxw_snprintf(value, LXW_UINT32_T_LENGTH, "%u", index);
135
0
            _rich_value_write_v(self, value);
136
137
0
            lxw_snprintf(value, LXW_UINT32_T_LENGTH, "%u", type);
138
0
            _rich_value_write_v(self, value);
139
140
0
            if (object_props->description && *object_props->description)
141
0
                _rich_value_write_v(self, object_props->description);
142
143
0
            lxw_xml_end_tag(self->file, "rv");
144
145
0
            index++;
146
0
        }
147
0
    }
148
0
}
149
150
/*
151
 * Write the <rvData> element.
152
 */
153
STATIC void
154
_rich_value_write_rv_data(lxw_rich_value *self)
155
0
{
156
0
    struct xml_attribute_list attributes;
157
0
    struct xml_attribute *attribute;
158
0
    char xmlns[] =
159
0
        "http://schemas.microsoft.com/office/spreadsheetml/2017/richdata";
160
161
0
    LXW_INIT_ATTRIBUTES();
162
0
    LXW_PUSH_ATTRIBUTES_STR("xmlns", xmlns);
163
0
    LXW_PUSH_ATTRIBUTES_INT("count", self->workbook->num_embedded_images);
164
165
0
    lxw_xml_start_tag(self->file, "rvData", &attributes);
166
167
0
    LXW_FREE_ATTRIBUTES();
168
0
}
169
170
/*
171
 * Assemble and write the XML file.
172
 */
173
void
174
lxw_rich_value_assemble_xml_file(lxw_rich_value *self)
175
0
{
176
    /* Write the XML declaration. */
177
0
    _rich_value_xml_declaration(self);
178
179
    /* Write the rvData element. */
180
0
    _rich_value_write_rv_data(self);
181
182
0
    lxw_rich_value_write_images(self);
183
184
0
    lxw_xml_end_tag(self->file, "rvData");
185
0
}
186
187
/*****************************************************************************
188
 *
189
 * Public functions.
190
 *
191
 ****************************************************************************/