summaryrefslogtreecommitdiff
path: root/src/pulse/message-params.c
blob: 8885f95e89f36ce41be286fe33c2b0dba2d1560a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/***
  This file is part of PulseAudio.

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <sys/types.h>

#include <pulse/xmalloc.h>

#include <pulsecore/macro.h>
#include <pulsecore/strbuf.h>
#include <pulsecore/core-util.h>

#include "message-params.h"

/* Message parameter structure, a wrapper for pa_strbuf */
struct pa_message_params {
    pa_strbuf *buffer;
};

/* Split the specified string into elements. An element is defined as
 * a sub-string between curly braces. The function is needed to parse
 * the parameters of messages. Each time it is called it returns the
 * position of the current element in result and the state pointer is
 * advanced to the next list element. On return, the parameter
 * *is_unpacked indicates if the string is plain text or contains a
 * sub-list. is_unpacked may be NULL.
 *
 * The variable state points to, should be initialized to NULL before
 * the first call. The function returns 1 on success, 0 if end of string
 * is encountered and -1 on parse error.
 *
 * result is set to NULL on end of string or parse error. */
static int split_list(char *c, char **result, bool *is_unpacked, void **state) {
    char *current = *state ? *state : c;
    uint32_t open_braces;
    bool found_backslash = false;

    pa_assert(result);

    *result = NULL;

    /* Empty or no string */
    if (!current || *current == 0)
        return PA_MESSAGE_PARAMS_LIST_END;

    /* Find opening brace */
    while (*current != 0) {

        /* Skip escaped curly braces. */
        if (*current == '\\' && !found_backslash) {
            found_backslash = true;
            current++;
            continue;
        }

        if (*current == '{' && !found_backslash)
            break;

        /* unexpected closing brace, parse error */
        if (*current == '}' && !found_backslash)
            return PA_MESSAGE_PARAMS_PARSE_ERROR;

        found_backslash = false;
        current++;
    }

    /* No opening brace found, end of string */
    if (*current == 0)
        return PA_MESSAGE_PARAMS_LIST_END;

    if (is_unpacked)
        *is_unpacked = true;
    *result = current + 1;
    found_backslash = false;
    open_braces = 1;

    while (open_braces != 0 && *current != 0) {
        current++;

        /* Skip escaped curly braces. */
        if (*current == '\\' && !found_backslash) {
            found_backslash = true;
            continue;
        }

        if (*current == '{' && !found_backslash) {
            open_braces++;
            if (is_unpacked)
                *is_unpacked = false;
        }
        if (*current == '}' && !found_backslash)
            open_braces--;

        found_backslash = false;
    }

    /* Parse error, closing brace missing */
    if (open_braces != 0) {
        *result = NULL;
        return PA_MESSAGE_PARAMS_PARSE_ERROR;
    }

    /* Replace } with 0 */
    *current = 0;

    *state = current + 1;

    return PA_MESSAGE_PARAMS_OK;
}

/* Read functions */

/* Read a string from the parameter list. The state pointer is
 * advanced to the next element of the list. Returns a pointer
 * to a sub-string within c. Escape characters will be removed
 * from the string. The result must not be freed. */
int pa_message_params_read_string(char *c, const char **result, void **state) {
    char *start_pos;
    char *value = NULL;
    int r;
    bool is_unpacked = true;

    pa_assert(result);

    if ((r = split_list(c, &start_pos, &is_unpacked, state)) == PA_MESSAGE_PARAMS_OK)
        value = start_pos;

    /* Check if we got a plain string not containing further lists */
    if (!is_unpacked) {
        /* Parse error */
        r = PA_MESSAGE_PARAMS_PARSE_ERROR;
        value = NULL;
    }

    if (value)
        *result = pa_unescape(value);

    return r;
}

/* A wrapper for split_list() to distinguish between reading pure
 * string data and raw data which may contain further lists. */
int pa_message_params_read_raw(char *c, char **result, void **state) {
    return split_list(c, result, NULL, state);
}

/* Read a double from the parameter list. The state pointer is
 * advanced to the next element of the list. */
int pa_message_params_read_double(char *c, double *result, void **state) {
    char *start_pos, *end_pos, *s;
    int err;
    struct lconv *locale;
    double value;
    bool is_unpacked = true;

    pa_assert(result);

    if ((err = split_list(c, &start_pos, &is_unpacked, state)) != PA_MESSAGE_PARAMS_OK)
        return err;

    /* Empty element */
    if (!*start_pos)
        return PA_MESSAGE_PARAMS_IS_NULL;

    /* Check if we got a plain string not containing further lists */
    if (!is_unpacked)
        return PA_MESSAGE_PARAMS_PARSE_ERROR;

    /* Convert to double */
    locale = localeconv();

    /* Replace decimal point with the correct character for the
     * current locale. This assumes that no thousand separator
     * is used. */
    for (s = start_pos; *s; s++) {
        if (*s == '.' || *s == ',')
            *s = *locale->decimal_point;
     }

    /* Convert to double */
    errno = 0;
    value = strtod(start_pos, &end_pos);

    /* Conversion error or string contains invalid characters. If the
     * whole string was used for conversion, end_pos should point to
     * the end of the string. */
    if (errno != 0 || *end_pos != 0 || end_pos == start_pos)
        return PA_MESSAGE_PARAMS_PARSE_ERROR;

    *result = value;
    return PA_MESSAGE_PARAMS_OK;
}

/* Read an integer from the parameter list. The state pointer is
 * advanced to the next element of the list. */
int pa_message_params_read_int64(char *c, int64_t *result, void **state) {
    char *start_pos;
    int err;
    int64_t value;
    bool is_unpacked = true;

    pa_assert(result);

    if ((err = split_list(c, &start_pos, &is_unpacked, state)) != PA_MESSAGE_PARAMS_OK)
        return err;

    /* Empty element */
    if (!*start_pos)
        return PA_MESSAGE_PARAMS_IS_NULL;

    /* Check if we got a plain string not containing further lists */
    if (!is_unpacked)
        return PA_MESSAGE_PARAMS_PARSE_ERROR;

    /* Convert to int64 */
    if (pa_atoi64(start_pos, &value) < 0)
        return PA_MESSAGE_PARAMS_PARSE_ERROR;

    *result = value;
    return PA_MESSAGE_PARAMS_OK;
}

/* Read an unsigned integer from the parameter list. The state pointer is
 * advanced to the next element of the list. */
int pa_message_params_read_uint64(char *c, uint64_t *result, void **state) {
    char *start_pos;
    int err;
    uint64_t value;
    bool is_unpacked = true;

    pa_assert(result);

    if ((err = split_list(c, &start_pos, &is_unpacked, state)) != PA_MESSAGE_PARAMS_OK)
        return err;

    /* Empty element */
    if (!*start_pos)
        return PA_MESSAGE_PARAMS_IS_NULL;

    /* Check if we got a plain string not containing further lists */
    if (!is_unpacked)
        return PA_MESSAGE_PARAMS_PARSE_ERROR;

    /* Convert to int64 */
    if (pa_atou64(start_pos, &value) < 0)
        return PA_MESSAGE_PARAMS_PARSE_ERROR;

    *result = value;
    return PA_MESSAGE_PARAMS_OK;
}

/* Read a boolean from the parameter list. The state pointer is
 * advanced to the next element of the list. */
int pa_message_params_read_bool(char *c, bool *result, void **state) {
    int err;
    uint64_t value;

    pa_assert(result);

    if ((err = pa_message_params_read_uint64(c, &value, state)) != PA_MESSAGE_PARAMS_OK)
        return err;

    *result = false;
    if (value)
        *result = true;

    return PA_MESSAGE_PARAMS_OK;
}

/* Write functions. The functions are wrapper functions around pa_strbuf,
 * so that the client does not need to use pa_strbuf directly. */

/* Creates a new pa_message_param structure */
pa_message_params *pa_message_params_new(void) {
    pa_message_params *params;

    params = pa_xnew(pa_message_params, 1);
    params->buffer = pa_strbuf_new();

    return params;
}

/* Frees a pa_message_params structure */
void pa_message_params_free(pa_message_params *params) {
    pa_assert(params);

    pa_strbuf_free(params->buffer);
    pa_xfree(params);
}

/* Converts a pa_message_param structure to string and frees the structure.
 * The returned string needs to be freed with pa_xree(). */
char *pa_message_params_to_string_free(pa_message_params *params) {
    char *result;

    pa_assert(params);

    result = pa_strbuf_to_string_free(params->buffer);

    pa_xfree(params);
    return result;
}

/* Writes an opening curly brace */
void pa_message_params_begin_list(pa_message_params *params) {

    pa_assert(params);

    pa_strbuf_putc(params->buffer, '{');
}

/* Writes a closing curly brace */
void pa_message_params_end_list(pa_message_params *params) {

    pa_assert(params);

    pa_strbuf_putc(params->buffer, '}');
}

/* Writes a string to a message_params structure, adding curly braces
 * around the string and escaping curly braces within the string. */
void pa_message_params_write_string(pa_message_params *params, const char *value) {
    char *output;

    pa_assert(params);

    /* Null value is written as empty element */
    if (!value)
        value = "";

    output = pa_escape(value, "{}");
    pa_strbuf_printf(params->buffer, "{%s}", output);

    pa_xfree(output);
}

/* Writes a raw string to a message_params structure, adding curly braces
 * around the string if add_braces is true. This function can be used to
 * write parts of a string or whole parameter lists that have been prepared
 * elsewhere (for example an array). */
void pa_message_params_write_raw(pa_message_params *params, const char *value, bool add_braces) {
    pa_assert(params);

    /* Null value is written as empty element if add_braces is true.
     * Otherwise nothing is written. */
    if (!value)
        value = "";

    if (add_braces)
        pa_strbuf_printf(params->buffer, "{%s}", value);
    else
        pa_strbuf_puts(params->buffer, value);
}

/* Writes a double to a message_params structure, adding curly braces.
 * precision gives the number of significant digits, not digits after
 * the decimal point. */
void pa_message_params_write_double(pa_message_params *params, double value, int precision) {
    char *buf, *s;

    pa_assert(params);

    /* We do not care about locale because we do not know which locale is
     * used on the server side. If the decimal separator is a comma, we
     * replace it with a dot to achieve consistent output on all locales. */
    buf = pa_sprintf_malloc("{%.*g}",  precision, value);
    for (s = buf; *s; s++) {
        if (*s == ',') {
            *s = '.';
            break;
        }
     }

    pa_strbuf_puts(params->buffer, buf);

    pa_xfree(buf);
}

/* Writes an integer to a message_param structure, adding curly braces. */
void pa_message_params_write_int64(pa_message_params *params, int64_t value) {

    pa_assert(params);

    pa_strbuf_printf(params->buffer, "{%lli}", (long long)value);
}

/* Writes an unsigned integer to a message_params structure, adding curly braces. */
void pa_message_params_write_uint64(pa_message_params *params, uint64_t value) {

    pa_assert(params);

    pa_strbuf_printf(params->buffer, "{%llu}", (unsigned long long)value);
}

/* Writes a boolean to a message_params structure, adding curly braces. */
void pa_message_params_write_bool(pa_message_params *params, bool value) {

    pa_assert(params);

    if (value)
        pa_strbuf_puts(params->buffer, "{1}");
    else
        pa_strbuf_puts(params->buffer, "{0}");
}