X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=filter.c;h=7c4fd0b5b0444478a9ee54a3066267c47b9b218b;hb=ecaf59db8d79286b69a489273f4262901c0588f8;hp=f9c9a09c78e9d8c056741e4a5fa49bbbdad27077;hpb=a1bb33afbde769156ad4bef7a60579da64aebbb7;p=libsigrok.git diff --git a/filter.c b/filter.c index f9c9a09c..7c4fd0b5 100644 --- a/filter.c +++ b/filter.c @@ -20,47 +20,61 @@ #include #include #include -#include "sigrok.h" +#include -/* convert sample from maximum probes -- the way the hardware driver sent +/** + * Remove unused probes from samples. + * + * Convert sample from maximum probes -- the way the hardware driver sent * it -- to a sample taking up only as much space as required, with * unused probes removed. + * + * @param in_unitsize The unit size of the input (data_in). + * @param out_unitsize The unit size of the output (data_out). + * @param probelist Pointer to a list of integers (probe numbers). + * @param data_in The input data. + * @param length_in The input data length. + * @param data_out The output data. + * @param length_out The output data length. + * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors. */ -int filter_probes(int in_unitsize, int out_unitsize, int *probelist, - char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out) +int sr_filter_probes(int in_unitsize, int out_unitsize, int *probelist, + const char *data_in, uint64_t length_in, char **data_out, + uint64_t *length_out) { - int num_enabled_probes, in_offset, out_offset, out_bit, i; + unsigned int in_offset, out_offset; + int num_enabled_probes, out_bit, i; uint64_t sample_in, sample_out; - *data_out = malloc(length_in); + if (!(*data_out = malloc(length_in))) + return SR_ERR_MALLOC; + num_enabled_probes = 0; - for(i = 0; probelist[i]; i++) + for (i = 0; probelist[i]; i++) num_enabled_probes++; - if(num_enabled_probes != in_unitsize * 8) { - in_offset = out_offset = 0; - while(in_offset <= length_in - in_unitsize) { - memcpy(&sample_in, data_in + in_offset, in_unitsize); - sample_out = 0; - out_bit = 0; - for(i = 0; probelist[i]; i++) { - if(sample_in & (1 << (probelist[i]-1))) - sample_out |= 1 << out_bit; - out_bit++; - } - memcpy((*data_out) + out_offset, &sample_out, out_unitsize); - in_offset += in_unitsize; - out_offset += out_unitsize; - } - *length_out = out_offset; - } - else { - /* all probes are used -- no need to compress anything */ + if (num_enabled_probes == in_unitsize * 8) { + /* All probes are used -- no need to compress anything. */ memcpy(*data_out, data_in, length_in); *length_out = length_in; + return SR_OK; } - return SIGROK_OK; -} - + /* If we reached this point, not all probes are used, so "compress". */ + in_offset = out_offset = 0; + while (in_offset <= length_in - in_unitsize) { + memcpy(&sample_in, data_in + in_offset, in_unitsize); + sample_out = out_bit = 0; + for (i = 0; probelist[i]; i++) { + if (sample_in & (1 << (probelist[i] - 1))) + sample_out |= (1 << out_bit); + out_bit++; + } + memcpy((*data_out) + out_offset, &sample_out, out_unitsize); + in_offset += in_unitsize; + out_offset += out_unitsize; + } + *length_out = out_offset; + return SR_OK; +}