mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-10-29 05:40:23 -04:00
Add pa_sample_format_valid()
I think this makes the code a bit nicer to read and write. This also reduces the chances of off-by-one errors when checking the bounds of the sample format value.
This commit is contained in:
parent
643eb5bae2
commit
1cda717252
8 changed files with 30 additions and 45 deletions
|
|
@ -251,6 +251,7 @@ pa_rtclock_now;
|
|||
pa_sample_format_is_be;
|
||||
pa_sample_format_is_le;
|
||||
pa_sample_format_to_string;
|
||||
pa_sample_format_valid;
|
||||
pa_sample_size;
|
||||
pa_sample_size_of_format;
|
||||
pa_sample_spec_equal;
|
||||
|
|
|
|||
|
|
@ -510,7 +510,7 @@ static void handle_set_default_sample_format(DBusConnection *conn, DBusMessage *
|
|||
|
||||
dbus_message_iter_get_basic(iter, &default_sample_format);
|
||||
|
||||
if (default_sample_format >= PA_SAMPLE_MAX) {
|
||||
if (!pa_sample_format_valid(default_sample_format)) {
|
||||
pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
|
||||
return;
|
||||
}
|
||||
|
|
@ -1317,7 +1317,7 @@ static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *u
|
|||
dbus_message_iter_recurse(&msg_iter, &array_iter);
|
||||
dbus_message_iter_get_fixed_array(&array_iter, &data, &data_length);
|
||||
|
||||
if (sample_format >= PA_SAMPLE_MAX) {
|
||||
if (!pa_sample_format_valid(sample_format)) {
|
||||
pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
|
||||
goto finish;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,8 +52,7 @@ static const size_t size_table[] = {
|
|||
};
|
||||
|
||||
size_t pa_sample_size_of_format(pa_sample_format_t f) {
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return size_table[f];
|
||||
}
|
||||
|
|
@ -104,6 +103,10 @@ pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec) {
|
|||
return spec;
|
||||
}
|
||||
|
||||
int pa_sample_format_valid(unsigned format) {
|
||||
return format < PA_SAMPLE_MAX;
|
||||
}
|
||||
|
||||
int pa_sample_spec_valid(const pa_sample_spec *spec) {
|
||||
pa_assert(spec);
|
||||
|
||||
|
|
@ -111,8 +114,7 @@ int pa_sample_spec_valid(const pa_sample_spec *spec) {
|
|||
spec->rate > PA_RATE_MAX ||
|
||||
spec->channels <= 0 ||
|
||||
spec->channels > PA_CHANNELS_MAX ||
|
||||
spec->format >= PA_SAMPLE_MAX ||
|
||||
spec->format < 0))
|
||||
!pa_sample_format_valid(spec->format)))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
|
|
@ -152,7 +154,7 @@ const char *pa_sample_format_to_string(pa_sample_format_t f) {
|
|||
[PA_SAMPLE_S24_32BE] = "s24-32be",
|
||||
};
|
||||
|
||||
if (f < 0 || f >= PA_SAMPLE_MAX)
|
||||
if (!pa_sample_format_valid(f))
|
||||
return NULL;
|
||||
|
||||
return table[f];
|
||||
|
|
@ -245,8 +247,7 @@ pa_sample_format_t pa_parse_sample_format(const char *format) {
|
|||
}
|
||||
|
||||
int pa_sample_format_is_le(pa_sample_format_t f) {
|
||||
pa_assert(f >= PA_SAMPLE_U8);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
switch (f) {
|
||||
case PA_SAMPLE_S16LE:
|
||||
|
|
|
|||
|
|
@ -289,6 +289,9 @@ size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;
|
|||
* pa_sample_spec_valid() will fail for it. \since 0.9.13 */
|
||||
pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec);
|
||||
|
||||
/** Return non-zero if the given integer is a valid sample format. \since 5.0 */
|
||||
int pa_sample_format_valid(unsigned format) PA_GCC_PURE;
|
||||
|
||||
/** Return non-zero when the sample type specification is valid */
|
||||
int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
|
||||
|
||||
|
|
|
|||
|
|
@ -650,15 +650,13 @@ size_t pa_mix(
|
|||
}
|
||||
|
||||
pa_do_mix_func_t pa_get_mix_func(pa_sample_format_t f) {
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return do_mix_table[f];
|
||||
}
|
||||
|
||||
void pa_set_mix_func(pa_sample_format_t f, pa_do_mix_func_t func) {
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
do_mix_table[f] = func;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -253,8 +253,8 @@ static pa_resample_method_t pa_resampler_fix_method(
|
|||
|
||||
/* Return true if a is a more precise sample format than b, else return false */
|
||||
static bool sample_format_more_precise(pa_sample_format_t a, pa_sample_format_t b) {
|
||||
pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
|
||||
pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(a));
|
||||
pa_assert(pa_sample_format_valid(b));
|
||||
|
||||
switch (a) {
|
||||
case PA_SAMPLE_U8:
|
||||
|
|
@ -305,8 +305,8 @@ static pa_sample_format_t pa_resampler_choose_work_format(
|
|||
bool map_required) {
|
||||
pa_sample_format_t work_format;
|
||||
|
||||
pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
|
||||
pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(a));
|
||||
pa_assert(pa_sample_format_valid(b));
|
||||
pa_assert(method >= 0);
|
||||
pa_assert(method < PA_RESAMPLER_MAX);
|
||||
|
||||
|
|
|
|||
|
|
@ -202,17 +202,13 @@ static pa_convert_func_t to_float32ne_table[] = {
|
|||
};
|
||||
|
||||
pa_convert_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return to_float32ne_table[f];
|
||||
}
|
||||
|
||||
void pa_set_convert_to_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
to_float32ne_table[f] = func;
|
||||
}
|
||||
|
|
@ -234,17 +230,13 @@ static pa_convert_func_t from_float32ne_table[] = {
|
|||
};
|
||||
|
||||
pa_convert_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return from_float32ne_table[f];
|
||||
}
|
||||
|
||||
void pa_set_convert_from_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
from_float32ne_table[f] = func;
|
||||
}
|
||||
|
|
@ -266,17 +258,13 @@ static pa_convert_func_t to_s16ne_table[] = {
|
|||
};
|
||||
|
||||
pa_convert_func_t pa_get_convert_to_s16ne_function(pa_sample_format_t f) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return to_s16ne_table[f];
|
||||
}
|
||||
|
||||
void pa_set_convert_to_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
to_s16ne_table[f] = func;
|
||||
}
|
||||
|
|
@ -298,17 +286,13 @@ static pa_convert_func_t from_s16ne_table[] = {
|
|||
};
|
||||
|
||||
pa_convert_func_t pa_get_convert_from_s16ne_function(pa_sample_format_t f) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return from_s16ne_table[f];
|
||||
}
|
||||
|
||||
void pa_set_convert_from_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
|
||||
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
from_s16ne_table[f] = func;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -261,15 +261,13 @@ static pa_do_volume_func_t do_volume_table[] = {
|
|||
};
|
||||
|
||||
pa_do_volume_func_t pa_get_volume_func(pa_sample_format_t f) {
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return do_volume_table[f];
|
||||
}
|
||||
|
||||
void pa_set_volume_func(pa_sample_format_t f, pa_do_volume_func_t func) {
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
do_volume_table[f] = func;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue