| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
module Prism |
| 7 |
|
| 8 |
module Pack |
| 9 |
%i[ |
| 10 |
SPACE |
| 11 |
COMMENT |
| 12 |
INTEGER |
| 13 |
UTF8 |
| 14 |
BER |
| 15 |
FLOAT |
| 16 |
STRING_SPACE_PADDED |
| 17 |
STRING_NULL_PADDED |
| 18 |
STRING_NULL_TERMINATED |
| 19 |
STRING_MSB |
| 20 |
STRING_LSB |
| 21 |
STRING_HEX_HIGH |
| 22 |
STRING_HEX_LOW |
| 23 |
STRING_UU |
| 24 |
STRING_MIME |
| 25 |
STRING_BASE64 |
| 26 |
STRING_FIXED |
| 27 |
STRING_POINTER |
| 28 |
MOVE |
| 29 |
BACK |
| 30 |
NULL |
| 31 |
|
| 32 |
UNSIGNED |
| 33 |
SIGNED |
| 34 |
SIGNED_NA |
| 35 |
|
| 36 |
AGNOSTIC_ENDIAN |
| 37 |
LITTLE_ENDIAN |
| 38 |
BIG_ENDIAN |
| 39 |
NATIVE_ENDIAN |
| 40 |
ENDIAN_NA |
| 41 |
|
| 42 |
SIZE_SHORT |
| 43 |
SIZE_INT |
| 44 |
SIZE_LONG |
| 45 |
SIZE_LONG_LONG |
| 46 |
SIZE_8 |
| 47 |
SIZE_16 |
| 48 |
SIZE_32 |
| 49 |
SIZE_64 |
| 50 |
SIZE_P |
| 51 |
SIZE_NA |
| 52 |
|
| 53 |
LENGTH_FIXED |
| 54 |
LENGTH_MAX |
| 55 |
LENGTH_RELATIVE |
| 56 |
LENGTH_NA |
| 57 |
].each do |const| |
| 58 |
const_set(const, const) |
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
class Directive |
| 63 |
|
| 64 |
attr_reader :version |
| 65 |
|
| 66 |
|
| 67 |
attr_reader :variant |
| 68 |
|
| 69 |
|
| 70 |
attr_reader :source |
| 71 |
|
| 72 |
|
| 73 |
attr_reader :type |
| 74 |
|
| 75 |
|
| 76 |
attr_reader :signed |
| 77 |
|
| 78 |
|
| 79 |
attr_reader :endian |
| 80 |
|
| 81 |
|
| 82 |
attr_reader :size |
| 83 |
|
| 84 |
|
| 85 |
attr_reader :length_type |
| 86 |
|
| 87 |
|
| 88 |
attr_reader :length |
| 89 |
|
| 90 |
|
| 91 |
def initialize(version, variant, source, type, signed, endian, size, length_type, length) |
| 92 |
@version = version |
| 93 |
@variant = variant |
| 94 |
@source = source |
| 95 |
@type = type |
| 96 |
@signed = signed |
| 97 |
@endian = endian |
| 98 |
@size = size |
| 99 |
@length_type = length_type |
| 100 |
@length = length |
| 101 |
end |
| 102 |
|
| 103 |
|
| 104 |
ENDIAN_DESCRIPTIONS = { |
| 105 |
AGNOSTIC_ENDIAN: "agnostic", |
| 106 |
LITTLE_ENDIAN: "little-endian (VAX)", |
| 107 |
BIG_ENDIAN: "big-endian (network)", |
| 108 |
NATIVE_ENDIAN: "native-endian", |
| 109 |
ENDIAN_NA: "n/a" |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
SIGNED_DESCRIPTIONS = { |
| 114 |
UNSIGNED: "unsigned", |
| 115 |
SIGNED: "signed", |
| 116 |
SIGNED_NA: "n/a" |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
SIZE_DESCRIPTIONS = { |
| 121 |
SIZE_SHORT: "short", |
| 122 |
SIZE_INT: "int-width", |
| 123 |
SIZE_LONG: "long", |
| 124 |
SIZE_LONG_LONG: "long long", |
| 125 |
SIZE_8: "8-bit", |
| 126 |
SIZE_16: "16-bit", |
| 127 |
SIZE_32: "32-bit", |
| 128 |
SIZE_64: "64-bit", |
| 129 |
SIZE_P: "pointer-width" |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
def describe |
| 134 |
case type |
| 135 |
when SPACE |
| 136 |
"whitespace" |
| 137 |
when COMMENT |
| 138 |
"comment" |
| 139 |
when INTEGER |
| 140 |
if size == SIZE_8 |
| 141 |
base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} integer" |
| 142 |
else |
| 143 |
base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} integer" |
| 144 |
end |
| 145 |
case length_type |
| 146 |
when LENGTH_FIXED |
| 147 |
if length > 1 |
| 148 |
base + ", x#{length}" |
| 149 |
else |
| 150 |
base |
| 151 |
end |
| 152 |
when LENGTH_MAX |
| 153 |
base + ", as many as possible" |
| 154 |
else |
| 155 |
raise |
| 156 |
end |
| 157 |
when UTF8 |
| 158 |
"UTF-8 character" |
| 159 |
when BER |
| 160 |
"BER-compressed integer" |
| 161 |
when FLOAT |
| 162 |
"#{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} float" |
| 163 |
when STRING_SPACE_PADDED |
| 164 |
"arbitrary binary string (space padded)" |
| 165 |
when STRING_NULL_PADDED |
| 166 |
"arbitrary binary string (null padded, count is width)" |
| 167 |
when STRING_NULL_TERMINATED |
| 168 |
"arbitrary binary string (null padded, count is width), except that null is added with *" |
| 169 |
when STRING_MSB |
| 170 |
"bit string (MSB first)" |
| 171 |
when STRING_LSB |
| 172 |
"bit string (LSB first)" |
| 173 |
when STRING_HEX_HIGH |
| 174 |
"hex string (high nibble first)" |
| 175 |
when STRING_HEX_LOW |
| 176 |
"hex string (low nibble first)" |
| 177 |
when STRING_UU |
| 178 |
"UU-encoded string" |
| 179 |
when STRING_MIME |
| 180 |
"quoted printable, MIME encoding" |
| 181 |
when STRING_BASE64 |
| 182 |
"base64 encoded string" |
| 183 |
when STRING_FIXED |
| 184 |
"pointer to a structure (fixed-length string)" |
| 185 |
when STRING_POINTER |
| 186 |
"pointer to a null-terminated string" |
| 187 |
when MOVE |
| 188 |
"move to absolute position" |
| 189 |
when BACK |
| 190 |
"back up a byte" |
| 191 |
when NULL |
| 192 |
"null byte" |
| 193 |
else |
| 194 |
raise |
| 195 |
end |
| 196 |
end |
| 197 |
end |
| 198 |
|
| 199 |
|
| 200 |
class Format |
| 201 |
|
| 202 |
attr_reader :directives |
| 203 |
|
| 204 |
|
| 205 |
attr_reader :encoding |
| 206 |
|
| 207 |
|
| 208 |
def initialize(directives, encoding) |
| 209 |
@directives = directives |
| 210 |
@encoding = encoding |
| 211 |
end |
| 212 |
|
| 213 |
|
| 214 |
def describe |
| 215 |
source_width = directives.map { |d| d.source.inspect.length }.max |
| 216 |
directive_lines = directives.map do |directive| |
| 217 |
if directive.type == SPACE |
| 218 |
source = directive.source.inspect |
| 219 |
else |
| 220 |
source = directive.source |
| 221 |
end |
| 222 |
|
| 223 |
" #{source.ljust(source_width)} #{directive.describe}" |
| 224 |
end |
| 225 |
|
| 226 |
(["Directives:"] + directive_lines + ["Encoding:", " #{encoding}"]).join("\n") |
| 227 |
end |
| 228 |
end |
| 229 |
end |
| 230 |
end |