1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <new>
18#include <memory>
19#include <fstream>
20#include <iostream>
21#include <fec/io.h>
22
23using namespace std;
24const unsigned bufsize = 2 * 1024 * FEC_BLOCKSIZE;
25
26int main(int argc, char **argv)
27{
28    if (argc != 3) {
29        cerr << "usage: " << argv[0] << " input output" << endl;
30        return 1;
31    }
32
33    unique_ptr<uint8_t[]> buffer(new (nothrow) uint8_t[bufsize]);
34
35    if (!buffer) {
36        cerr << "failed to allocate buffer" << endl;
37        return 1;
38    }
39
40    fec::io input(argv[1]);
41
42    if (!input) {
43        return 1;
44    }
45
46    ofstream output(argv[2], ios::binary | ios::trunc);
47
48    if (!output) {
49        cerr << "failed to open " << argv[2] << endl;
50        return 1;
51    }
52
53    ssize_t count;
54
55    do {
56        count = input.read(buffer.get(), bufsize);
57
58        if (count == -1) {
59            return 1;
60        } else if (count > 0) {
61            output.write(reinterpret_cast<const char *>(buffer.get()), count);
62
63            if (!output) {
64                cerr << "write" << endl;
65                return 1;
66            }
67        }
68    } while (count > 0);
69
70    return 0;
71}
72