xref: /third_party/pulseaudio/src/tests/stripnul.c (revision 53a5a1b3)
1/***
2  This file is part of PulseAudio.
3
4  PulseAudio is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published
6  by the Free Software Foundation; either version 2.1 of the License,
7  or (at your option) any later version.
8
9  PulseAudio is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public License
15  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16***/
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include <stdio.h>
23#include <string.h>
24#include <inttypes.h>
25
26#include <pulse/xmalloc.h>
27#include <pulsecore/macro.h>
28
29#define MAX_BUFFER (16*1024)
30
31int main(int argc, char *argv[]) {
32    FILE *i, *o;
33    size_t granularity;
34    bool found = false;
35    uint8_t *zero;
36
37    pa_assert_se(argc >= 2);
38    pa_assert_se((granularity = (size_t) atoi(argv[1])) >= 1);
39    pa_assert(granularity <= MAX_BUFFER);
40    pa_assert_se((i = (argc >= 3) ? fopen(argv[2], "r") : stdin));
41    pa_assert_se((o = (argc >= 4) ? fopen(argv[3], "w") : stdout));
42
43    zero = pa_xmalloc0(granularity);
44
45    for (;;) {
46        uint8_t buffer[MAX_BUFFER], *p;
47        size_t k;
48
49        k = fread(buffer, granularity, sizeof(buffer)/granularity, i);
50
51        if (k <= 0)
52            break;
53
54        if (found)
55            pa_assert_se(fwrite(buffer, granularity, k, o) == k);
56        else {
57            for (p = buffer; ((size_t) (p-buffer)/granularity) < k; p += granularity)
58                if (memcmp(p, zero, granularity)) {
59                    size_t left;
60                    found = true;
61                    left = (size_t) (k - (size_t) (p-buffer)/granularity);
62                    pa_assert_se(fwrite(p, granularity, left, o) == left);
63                    break;
64                }
65        }
66    }
67
68    fflush(o);
69
70    return 0;
71}
72