]> sigrok.org Git - libsigrok.git/blame - filter.c
libsigrok: Quickfix for a segfault in ChronoVu LA8.
[libsigrok.git] / filter.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <stdint.h>
22#include <string.h>
62c82025 23#include <sigrok.h>
a1bb33af 24
25b4fb85
UH
25/**
26 * Remove unused probes from samples.
27 *
62c82025 28 * Convert sample from maximum probes -- the way the hardware driver sent
a1bb33af
UH
29 * it -- to a sample taking up only as much space as required, with
30 * unused probes removed.
25b4fb85
UH
31 *
32 * @param in_unitsize The unit size of the input (data_in).
33 * @param out_unitsize The unit size of the output (data_out).
34 * @param probelist Pointer to a list of integers (probe numbers).
35 * @param data_in The input data.
36 * @param length_in The input data length.
37 * @param data_out The output data.
38 * @param length_out The output data length.
39 * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors.
a1bb33af 40 */
c25d2039 41int sr_filter_probes(int in_unitsize, int out_unitsize, int *probelist,
60eb1eb5
BV
42 const unsigned char *data_in, uint64_t length_in,
43 char **data_out, uint64_t *length_out)
a1bb33af 44{
afc8e4de
UH
45 unsigned int in_offset, out_offset;
46 int num_enabled_probes, out_bit, i;
a1bb33af
UH
47 uint64_t sample_in, sample_out;
48
fdebec21 49 if (!(*data_out = malloc(length_in)))
e46b8fb1 50 return SR_ERR_MALLOC;
fdebec21 51
a1bb33af 52 num_enabled_probes = 0;
62c82025 53 for (i = 0; probelist[i]; i++)
a1bb33af
UH
54 num_enabled_probes++;
55
fdebec21 56 if (num_enabled_probes == in_unitsize * 8) {
62c82025 57 /* All probes are used -- no need to compress anything. */
a1bb33af
UH
58 memcpy(*data_out, data_in, length_in);
59 *length_out = length_in;
e46b8fb1 60 return SR_OK;
fdebec21
UH
61 }
62
63 /* If we reached this point, not all probes are used, so "compress". */
64 in_offset = out_offset = 0;
65 while (in_offset <= length_in - in_unitsize) {
66 memcpy(&sample_in, data_in + in_offset, in_unitsize);
67 sample_out = out_bit = 0;
68 for (i = 0; probelist[i]; i++) {
69 if (sample_in & (1 << (probelist[i] - 1)))
70 sample_out |= (1 << out_bit);
71 out_bit++;
72 }
73 memcpy((*data_out) + out_offset, &sample_out, out_unitsize);
74 in_offset += in_unitsize;
75 out_offset += out_unitsize;
a1bb33af 76 }
fdebec21 77 *length_out = out_offset;
a1bb33af 78
e46b8fb1 79 return SR_OK;
a1bb33af 80}