Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. All rights reserved. |
| 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 <stdio.h> |
| 18 | #include <iostream> |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | #include "flatbuffers/hash.h" |
| 22 | |
| 23 | enum OutputFormat { kDecimal, kHexadecimal, kHexadecimal0x }; |
| 24 | |
| 25 | int main(int argc, char *argv[]) { |
| 26 | const char *name = argv[0]; |
| 27 | if (argc <= 1) { |
| 28 | printf("%s HASH [OPTION]... [--] STRING...\n", name); |
| 29 | printf("Available hashing algorithms:\n"); |
| 30 | printf(" 16 bit:\n"); |
| 31 | size_t size = sizeof(flatbuffers::kHashFunctions16) / |
| 32 | sizeof(flatbuffers::kHashFunctions16[0]); |
| 33 | for (size_t i = 0; i < size; ++i) { |
| 34 | printf(" * %s\n", flatbuffers::kHashFunctions16[i].name); |
| 35 | } |
| 36 | printf(" 32 bit:\n"); |
| 37 | size = sizeof(flatbuffers::kHashFunctions32) / |
| 38 | sizeof(flatbuffers::kHashFunctions32[0]); |
| 39 | for (size_t i = 0; i < size; ++i) { |
| 40 | printf(" * %s\n", flatbuffers::kHashFunctions32[i].name); |
| 41 | } |
| 42 | printf(" 64 bit:\n"); |
| 43 | size = sizeof(flatbuffers::kHashFunctions64) / |
| 44 | sizeof(flatbuffers::kHashFunctions64[0]); |
| 45 | for (size_t i = 0; i < size; ++i) { |
| 46 | printf(" * %s\n", flatbuffers::kHashFunctions64[i].name); |
| 47 | } |
| 48 | printf( |
| 49 | " -d Output hash in decimal.\n" |
| 50 | " -x Output hash in hexadecimal.\n" |
| 51 | " -0x Output hash in hexadecimal and prefix with 0x.\n" |
| 52 | " -c Append the string to the output in a c-style comment.\n"); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | const char *hash_algorithm = argv[1]; |
| 57 | |
| 58 | flatbuffers::NamedHashFunction<uint16_t>::HashFunction hash_function16 = |
| 59 | flatbuffers::FindHashFunction16(hash_algorithm); |
| 60 | flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 = |
| 61 | flatbuffers::FindHashFunction32(hash_algorithm); |
| 62 | flatbuffers::NamedHashFunction<uint64_t>::HashFunction hash_function64 = |
| 63 | flatbuffers::FindHashFunction64(hash_algorithm); |
| 64 | |
| 65 | if (!hash_function16 && !hash_function32 && !hash_function64) { |
| 66 | printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm); |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | OutputFormat output_format = kHexadecimal; |
| 71 | bool annotate = false; |
| 72 | bool escape_dash = false; |
| 73 | for (int i = 2; i < argc; i++) { |
| 74 | const char *arg = argv[i]; |
| 75 | if (!escape_dash && arg[0] == '-') { |
| 76 | std::string opt = arg; |
| 77 | if (opt == "-d") |
| 78 | output_format = kDecimal; |
| 79 | else if (opt == "-x") |
| 80 | output_format = kHexadecimal; |
| 81 | else if (opt == "-0x") |
| 82 | output_format = kHexadecimal0x; |
| 83 | else if (opt == "-c") |
| 84 | annotate = true; |
| 85 | else if (opt == "--") |
| 86 | escape_dash = true; |
| 87 | else |
| 88 | printf("Unrecognized argument: \"%s\"\n", arg); |
| 89 | } else { |
| 90 | std::stringstream ss; |
| 91 | if (output_format == kDecimal) { |
| 92 | ss << std::dec; |
| 93 | } else if (output_format == kHexadecimal) { |
| 94 | ss << std::hex; |
| 95 | } else if (output_format == kHexadecimal0x) { |
| 96 | ss << std::hex; |
| 97 | ss << "0x"; |
| 98 | } |
| 99 | if (hash_function16) |
| 100 | ss << hash_function16(arg); |
| 101 | else if (hash_function32) |
| 102 | ss << hash_function32(arg); |
| 103 | else if (hash_function64) |
| 104 | ss << hash_function64(arg); |
| 105 | |
| 106 | if (annotate) ss << " /* \"" << arg << "\" */"; |
| 107 | |
| 108 | ss << "\n"; |
| 109 | |
| 110 | std::cout << ss.str(); |
| 111 | } |
| 112 | } |
| 113 | return 0; |
| 114 | } |