aboutsummaryrefslogtreecommitdiff
path: root/src/flathash.cpp
blob: bc3d2df29eabe33fb844fa8d72c722bc89f40727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2015 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include "flatbuffers/hash.h"

enum OutputFormat { kDecimal, kHexadecimal, kHexadecimal0x };

int main(int argc, char *argv[]) {
  const char *name = argv[0];
  if (argc <= 1) {
    printf("%s HASH [OPTION]... [--] STRING...\n", name);
    printf("Available hashing algorithms:\n");
    printf("  16 bit:\n");
    size_t size = sizeof(flatbuffers::kHashFunctions16) /
                  sizeof(flatbuffers::kHashFunctions16[0]);
    for (size_t i = 0; i < size; ++i) {
      printf("    * %s\n", flatbuffers::kHashFunctions16[i].name);
    }
    printf("  32 bit:\n");
    size = sizeof(flatbuffers::kHashFunctions32) /
                  sizeof(flatbuffers::kHashFunctions32[0]);
    for (size_t i = 0; i < size; ++i) {
      printf("    * %s\n", flatbuffers::kHashFunctions32[i].name);
    }
    printf("  64 bit:\n");
    size = sizeof(flatbuffers::kHashFunctions64) /
           sizeof(flatbuffers::kHashFunctions64[0]);
    for (size_t i = 0; i < size; ++i) {
      printf("    * %s\n", flatbuffers::kHashFunctions64[i].name);
    }
    printf(
        "  -d         Output hash in decimal.\n"
        "  -x         Output hash in hexadecimal.\n"
        "  -0x        Output hash in hexadecimal and prefix with 0x.\n"
        "  -c         Append the string to the output in a c-style comment.\n");
    return 1;
  }

  const char *hash_algorithm = argv[1];

  flatbuffers::NamedHashFunction<uint16_t>::HashFunction hash_function16 =
      flatbuffers::FindHashFunction16(hash_algorithm);
  flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 =
      flatbuffers::FindHashFunction32(hash_algorithm);
  flatbuffers::NamedHashFunction<uint64_t>::HashFunction hash_function64 =
      flatbuffers::FindHashFunction64(hash_algorithm);

  if (!hash_function16 && !hash_function32 && !hash_function64) {
    printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm);
    return 1;
  }

  OutputFormat output_format = kHexadecimal;
  bool annotate = false;
  bool escape_dash = false;
  for (int i = 2; i < argc; i++) {
    const char *arg = argv[i];
    if (!escape_dash && arg[0] == '-') {
      std::string opt = arg;
      if (opt == "-d")
        output_format = kDecimal;
      else if (opt == "-x")
        output_format = kHexadecimal;
      else if (opt == "-0x")
        output_format = kHexadecimal0x;
      else if (opt == "-c")
        annotate = true;
      else if (opt == "--")
        escape_dash = true;
      else
        printf("Unrecognized argument: \"%s\"\n", arg);
    } else {
      std::stringstream ss;
      if (output_format == kDecimal) {
        ss << std::dec;
      } else if (output_format == kHexadecimal) {
        ss << std::hex;
      } else if (output_format == kHexadecimal0x) {
        ss << std::hex;
        ss << "0x";
      }
      if (hash_function16)
        ss << hash_function16(arg);
      else if (hash_function32)
        ss << hash_function32(arg);
      else if (hash_function64)
        ss << hash_function64(arg);

      if (annotate) ss << " /* \"" << arg << "\" */";

      ss << "\n";

      std::cout << ss.str();
    }
  }
  return 0;
}