2 * Copyright 2015 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "flatbuffers/hash.h"
23 enum OutputFormat { kDecimal, kHexadecimal, kHexadecimal0x };
25 int main(int argc, char *argv[]) {
26 const char *name = argv[0];
28 printf("%s HASH [OPTION]... [--] STRING...\n", name);
29 printf("Available hashing algorithms:\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);
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);
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);
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");
56 const char *hash_algorithm = argv[1];
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);
65 if (!hash_function16 && !hash_function32 && !hash_function64) {
66 printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm);
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;
78 output_format = kDecimal;
80 output_format = kHexadecimal;
81 else if (opt == "-0x")
82 output_format = kHexadecimal0x;
88 printf("Unrecognized argument: \"%s\"\n", arg);
91 if (output_format == kDecimal) {
93 } else if (output_format == kHexadecimal) {
95 } else if (output_format == kHexadecimal0x) {
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);
106 if (annotate) ss << " /* \"" << arg << "\" */";
110 std::cout << ss.str();