Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: fdb5c59a98e1765828c5b8f6b4a70aec7d896757 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# Generates typedef from list of strings
#
#   Copyright (C) 2018 R-T Specialty, LLC.
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

# store unique names and values to detect collisions
declare -A _unames=()
declare -A _uvalues=()


# Provide an error and abort if a name or value collision is found
#
# This function stores each name and value that it sees each time it is
# invoked.  If a duplicate is found, an error is output and the script
# terminates.
#
# This is important because both values are transformed in a way that
# the output space is a subset of the input.  In the case of names,
# characters are stripped and replaced; in the case of values, the range
# of the hash function is determined by the number of bytes desired.
#
# Both will cause really bad bugs if left undetected.
assert-collision()
{
  local -r name="${1?Missing name (const)}"
  local -r value="${2?Missing value (hashed)}"

  test -z "${_unames[$name]}" || {
    echo "error: name collision: $name" >&2
    exit 1
  }

  test -z "${_uvalues[$value]}" || {
    echo "error: value collision: $name = $value" >&2
    exit 1
  }

  _unames["$name"]=1
  _uvalues["$value"]=1
}


# Generate typedef item from given string
#
# An item node will be output with the given string converted to a class
# (see `constify'); a BYTES-byte decimal integer as the value (see
# `hash-name'); and the original string as the description.
output-item()
{
  local -r typedef="${1?Missing typedef}"
  local -ri bytes="${2?Missing byte size}"
  local name; read -r name

  local -r const=$( constify "$typedef" <<< "$name" )
  local -r value=$( hash-name "$bytes" <<< "$name" )

  assert-collision "$const" "$value"

  printf '      <item name="%s" value="%s" desc="%s" />\n' \
         "$const" \
         "$value" \
         "$name"
}



# Convert a string into a constant
#
# The returned format will be "TYPE_NAME", where "TYPE" is the typedef
# converted into uppercase and "NAME" is the string converted into
# uppercase with all consecutive non-alphanumeric characters converted
# into an underscore.
constify()
{
  local -r typedef="${1?Missing typedef}"
  local name; read -r name

  echo -n "${typedef^^}_"
  sed 's/[^a-zA-Z0-9 ]\+//g;
       s/ \+/_/g' <<< "${name^^}"
}


# Produce a decimal integer by taking the high BYTES bytes of the SHA256
# hash of stdin
#
# Since the output of sha256sum is hexidecimal, two digits represent a
# single byte.  For example, let BYTES=4: taking the first 8 digits
# gives us the high 4 bytes (32 bits).  Another way to visualize this is
# to convert a truncated hash to decimal.
hash-name()
{
  local -ri bytes="${1?Missing byte size}"
  local -ri digits=$(( bytes * 2 ))

  echo $(( 0x$( sha256sum | head -c"$digits" ) ))
}


# Usage information
#
# This function terminates the script with EX_USAGE.
usage()
{
  cat <<'EOF'
Usage: $0 typename [bytes]
Generate typedef of name TYPENAME using hashed standard input values

Each input line will produce a single typedef item.  Each item has a
decimal value of the high BYTES of its SHA256 hash.  If BYTES is not
provided, it defaults to 4 (32 bits).

Empty lines and lines beginning with a `#' are ignored.

This script checks for collisions of both names and values.  If a
hash collision is found, the number of bytes BYTES must be
increased or the input string changed.  Collisions will immediate
abort the script with a non-zero exit status.
EOF

  return 64  # EX_USAGE
}


# Output typedef with hashed values
main()
{
  local -r typedef="${1?Missing typedef}"
  local -ri bytes="${2:-4}"

  test "$bytes" -gt 0 || {
    echo 'error: byte count must be greater than 0' >&2
    usage
  }

  cat <<EOF
<?xml version="1.0"?>
<package xmlns="http://www.lovullo.com/rater"
         title="$typedef Type">
  <typedef name="$typedef" desc="$typedef">
    <enum type="integer">
EOF

  while read line; do
    # ignore empty and comment lines
    [[ "$line" && ! "$line" =~ ^\#  ]] || continue

    output-item "$typedef" "$bytes" <<< "$line"
  done

  echo '    </enum>'
  echo '  </typedef>'
  echo '</package>'
}


test $# -gt 0 || usage || exit

main "$@"