Andre Eisenbach | 877ff89 | 2014-12-30 16:37:43 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2015 Google, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <fstream> |
| 22 | #include <sstream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "btsnooz_utils.h" |
| 27 | |
| 28 | int main(int argc, char *argv[]) { |
| 29 | if (argc > 3) { |
| 30 | std::cerr << "Usage: " << argv[0] << " [input_file] [output_file]\n"; |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | std::vector<char> buffer; |
| 35 | |
| 36 | int read = 0; |
| 37 | if (argc < 3) { |
| 38 | std::cerr << "<Reading from stdin>\n"; |
| 39 | read = readLog(std::cin, buffer); |
| 40 | |
| 41 | } else { |
| 42 | std::cerr << "<Reading " << argv[1] << ">\n"; |
| 43 | std::ifstream ff(argv[1]); |
| 44 | read = readLog(ff, buffer); |
| 45 | ff.close(); |
| 46 | } |
| 47 | |
| 48 | if (read == 0) { |
| 49 | std::cerr << "File not found or not BTSNOOP data block....\n"; |
| 50 | return 2; |
| 51 | } |
| 52 | |
| 53 | std::cerr << std::setw(8) << read << " bytes of base64 data read\n"; |
| 54 | |
| 55 | read = base64Decode(buffer); |
| 56 | if (read <= 0) { |
| 57 | std::cerr << "Decoding base64 data failed...\n"; |
| 58 | return 3; |
| 59 | } |
| 60 | |
| 61 | std::cerr << std::setw(8) << read << " bytes of compressed data decoded\n"; |
| 62 | |
| 63 | std::vector<uint8_t> uncompressed; |
| 64 | read = inflate(buffer, uncompressed); |
| 65 | if (read <= 0) { |
| 66 | std::cerr << "Error inflating data...\n"; |
| 67 | return 4; |
| 68 | } |
| 69 | |
| 70 | std::cerr << std::setw(8) << read << " bytes of data inflated\n"; |
| 71 | |
| 72 | if (argc < 2) { |
| 73 | std::cerr << "<Writing to stdout>\n"; |
| 74 | read = writeBtSnoop(std::cout, uncompressed); |
| 75 | |
| 76 | } else { |
| 77 | const int arg = argc > 2 ? 2 : 1; |
| 78 | std::cerr << "<Writing " << argv[arg] << ">\n"; |
| 79 | std::ofstream ff(argv[arg]); |
| 80 | read = writeBtSnoop(ff, uncompressed); |
| 81 | ff.close(); |
| 82 | } |
| 83 | |
| 84 | std::cerr << std::setw(8) << read << " btsnoop packets written\n"; |
| 85 | |
| 86 | return 0; |
| 87 | } |