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