File: tokenise_name3_test.c

package info (click to toggle)
htscodecs 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,640 kB
  • sloc: ansic: 15,651; javascript: 2,907; makefile: 330; sh: 184
file content (238 lines) | stat: -rw-r--r-- 6,527 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2016-2020 Genome Research Ltd.
 * Author(s): James Bonfield
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 * 
 *    3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
 *    Institute nor the names of its contributors may be used to endorse
 *    or promote products derived from this software without specific
 *    prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
 * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <ctype.h>
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>

#include "htscodecs/tokenise_name3.h"

//-----------------------------------------------------------------------------
// main() implementation for testing

// Large enough for whole file for now.
#ifndef BLK_SIZE
#define BLK_SIZE 1*1024*1024
#endif
static char *blk;

// Max 4GB
static unsigned char *load(FILE *infp, uint32_t *lenp) {
    unsigned char *data = NULL;
    uint32_t dsize = 0;
    uint32_t dcurr = 0;
    signed int len;

    do {
        if (dsize - dcurr < BLK_SIZE) {
            dsize = dsize ? dsize * 2 : BLK_SIZE;
            data = realloc(data, dsize);
        }

        len = fread(data + dcurr, 1, BLK_SIZE, infp);
        if (len > 0)
            dcurr += len;
    } while (len > 0);

    if (len == -1) {
        perror("fread");
    }

    *lenp = dcurr;
    return data;
}

static int encode(int argc, char **argv) {
    FILE *fp;
    int len, level = 9;
    int use_arith = 0;
    int raw = 0;

    while (argc > 1 && argv[1][0] == '-') {
        if (strcmp(argv[1], "-r") == 0) {
            raw = 1;
            argc--;
            argv++;
        }

        else if (argv[1][1] >= '0' && argv[1][1] <= '9') {
            level = atoi(argv[1]+1);
            if (level > 10) {
                level -= 10;
                use_arith = 1;
            }
            argc--;
            argv++;
        }

        else
            exit(1);
    }

    if (argc > 1) {
        fp = fopen(argv[1], "r");
        if (!fp) {
            perror(argv[1]);
            return 1;
        }
    } else {
        fp = stdin;
    }

    if (raw) {
        // One naked / raw block, to match the specification
        uint32_t in_len;
        int out_len;
        unsigned char *in = load(fp, &in_len), *out;
        if (!in) exit(1);
        out = tok3_encode_names((char *)in, in_len, level, use_arith,
                                &out_len, NULL);
        if (!out || write(1, out, out_len) < out_len) exit(1);   // encoded data
        free(in);
        free(out);
    } else {
        // Block based, to permit arbitrarily large files for benchmarking
        int blk_offset = 0;
        for (;;) {
            int last_start = 0;

            len = fread(blk+blk_offset, 1, BLK_SIZE-blk_offset, fp);
            if (len <= 0)
                break;
            len += blk_offset;

            int out_len;
            uint8_t *out = tok3_encode_names(blk, len, level, use_arith,
                                             &out_len, &last_start);
            if (!out) {
                fprintf(stderr, "Couldn't encode names\n");
                exit(1);
            }
            if (write(1, &out_len, 4) < 4) exit(1);
            if (write(1, out, out_len) < out_len) exit(1);   // encoded data
            free(out);

            if (len > last_start)
                memmove(blk, &blk[last_start], len - last_start);
            blk_offset = len - last_start;
        }
    }

    if (fclose(fp) < 0) {
        perror("closing file");
        return 1;
    }

    return 0;
}

static int decode(int argc, char **argv) {
    uint32_t in_sz, out_sz;
    int raw = 0;

    if (argc > 1 && strcmp(argv[1], "-r") == 0) {
        raw = 1;
        argc--;
        argv++;
    }

    if (raw) {
        // One naked / raw block, to match the specification
        uint32_t in_len;
        unsigned char *in = load(stdin, &in_len), *out;
        if (!in) exit(1);

        if ((out = tok3_decode_names(in, in_len, &out_sz)) == NULL)
            exit(1);
        if (write(1, out, out_sz) != out_sz)
            exit(1);

        free(in);
        free(out);
    } else {
        while (fread(&in_sz, 1, 4, stdin) == 4) {
            uint8_t *in = malloc(in_sz), *out;
            if (!in)
                return -1;

            if (fread(in, 1, in_sz, stdin) != in_sz) {
                free(in);
                return -1;
            }

            if ((out = tok3_decode_names(in, in_sz, &out_sz)) == NULL) {
                free(in);
                return -1;
            }

            if (write(1, out, out_sz) < out_sz) exit(1);

            free(in);
            free(out);
        }
    }

    return 0;
}

int main(int argc, char **argv) {
#ifdef _WIN32
        _setmode(_fileno(stdin),  _O_BINARY);
        _setmode(_fileno(stdout), _O_BINARY);
#endif

    // temporary fix for decoder, which needs more space
    blk = malloc(BLK_SIZE*2);

    int ret;

    if (argc > 1 && strcmp(argv[1], "-d") == 0)
        ret = decode(argc-1, argv+1);
    else
        ret = encode(argc, argv);

    free(blk);
    return ret;
}