Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014, Google Inc. |
| 2 | // |
| 3 | // Permission to use, copy, modify, and/or distribute this software for any |
| 4 | // purpose with or without fee is hereby granted, provided that the above |
| 5 | // copyright notice and this permission notice appear in all copies. |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 19 | "errors" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 20 | "flag" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "sort" |
| 26 | "strconv" |
| 27 | "strings" |
| 28 | "unicode" |
| 29 | ) |
| 30 | |
| 31 | var resetFlag *bool = flag.Bool("reset", false, "If true, ignore current assignments and reassign from scratch") |
| 32 | |
| 33 | func makeErrors(reset bool) error { |
| 34 | dirName, err := os.Getwd() |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | lib := filepath.Base(dirName) |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 40 | headerPath, err := findHeader(lib + ".h") |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 44 | sourcePath := lib + "_error.c" |
| 45 | |
| 46 | headerFile, err := os.Open(headerPath) |
| 47 | if err != nil { |
| 48 | if os.IsNotExist(err) { |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 49 | return fmt.Errorf("No header %s. Run in the right directory or touch the file.", headerPath) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | prefix := strings.ToUpper(lib) |
| 56 | functions, reasons, err := parseHeader(prefix, headerFile) |
| 57 | headerFile.Close() |
| 58 | |
| 59 | if reset { |
| 60 | err = nil |
| 61 | functions = make(map[string]int) |
| 62 | reasons = make(map[string]int) |
| 63 | } |
| 64 | |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | dir, err := os.Open(".") |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | defer dir.Close() |
| 74 | |
| 75 | filenames, err := dir.Readdirnames(-1) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | for _, name := range filenames { |
| 81 | if !strings.HasSuffix(name, ".c") { |
| 82 | continue |
| 83 | } |
| 84 | |
| 85 | if err := addFunctionsAndReasons(functions, reasons, name, prefix); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | assignNewValues(functions) |
| 91 | assignNewValues(reasons) |
| 92 | |
| 93 | headerFile, err = os.Open(headerPath) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | defer headerFile.Close() |
| 98 | |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 99 | newHeaderFile, err := os.OpenFile(headerPath+".tmp", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | defer newHeaderFile.Close() |
| 104 | |
| 105 | if err := writeHeaderFile(newHeaderFile, headerFile, prefix, functions, reasons); err != nil { |
| 106 | return err |
| 107 | } |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 108 | os.Rename(headerPath+".tmp", headerPath) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 109 | |
| 110 | sourceFile, err := os.OpenFile(sourcePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | defer sourceFile.Close() |
| 115 | |
| 116 | fmt.Fprintf(sourceFile, `/* Copyright (c) 2014, Google Inc. |
| 117 | * |
| 118 | * Permission to use, copy, modify, and/or distribute this software for any |
| 119 | * purpose with or without fee is hereby granted, provided that the above |
| 120 | * copyright notice and this permission notice appear in all copies. |
| 121 | * |
| 122 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 123 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 124 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 125 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 126 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 127 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 128 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 129 | |
| 130 | #include <openssl/err.h> |
| 131 | |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 132 | #include <openssl/%s.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
| 134 | const ERR_STRING_DATA %s_error_string_data[] = { |
| 135 | `, lib, prefix) |
| 136 | outputStrings(sourceFile, lib, typeFunctions, functions) |
| 137 | outputStrings(sourceFile, lib, typeReasons, reasons) |
| 138 | |
| 139 | sourceFile.WriteString(" {0, NULL},\n};\n") |
| 140 | |
| 141 | return nil |
| 142 | } |
| 143 | |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 144 | func findHeader(basename string) (path string, err error) { |
| 145 | includeDir := filepath.Join("..", "include") |
| 146 | |
| 147 | fi, err := os.Stat(includeDir) |
| 148 | if err != nil && os.IsNotExist(err) { |
| 149 | includeDir = filepath.Join("..", includeDir) |
| 150 | fi, err = os.Stat(includeDir) |
| 151 | } |
| 152 | if err != nil { |
| 153 | return "", errors.New("cannot find path to include directory") |
| 154 | } |
| 155 | if !fi.IsDir() { |
| 156 | return "", errors.New("include node is not a directory") |
| 157 | } |
| 158 | return filepath.Join(includeDir, "openssl", basename), nil |
| 159 | } |
| 160 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | type assignment struct { |
| 162 | key string |
| 163 | value int |
| 164 | } |
| 165 | |
| 166 | type assignmentsSlice []assignment |
| 167 | |
| 168 | func (a assignmentsSlice) Len() int { |
| 169 | return len(a) |
| 170 | } |
| 171 | |
| 172 | func (a assignmentsSlice) Less(i, j int) bool { |
| 173 | return a[i].value < a[j].value |
| 174 | } |
| 175 | |
| 176 | func (a assignmentsSlice) Swap(i, j int) { |
| 177 | a[i], a[j] = a[j], a[i] |
| 178 | } |
| 179 | |
| 180 | func outputAssignments(w io.Writer, assignments map[string]int) { |
| 181 | var sorted assignmentsSlice |
| 182 | |
| 183 | for key, value := range assignments { |
| 184 | sorted = append(sorted, assignment{key, value}) |
| 185 | } |
| 186 | |
| 187 | sort.Sort(sorted) |
| 188 | |
| 189 | for _, assignment := range sorted { |
| 190 | fmt.Fprintf(w, "#define %s %d\n", assignment.key, assignment.value) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func parseDefineLine(line, lib string) (typ int, key string, value int, ok bool) { |
| 195 | if !strings.HasPrefix(line, "#define ") { |
| 196 | return |
| 197 | } |
| 198 | |
| 199 | fields := strings.Fields(line) |
| 200 | if len(fields) != 3 { |
| 201 | return |
| 202 | } |
| 203 | |
| 204 | funcPrefix := lib + "_F_" |
| 205 | reasonPrefix := lib + "_R_" |
| 206 | |
| 207 | key = fields[1] |
| 208 | switch { |
| 209 | case strings.HasPrefix(key, funcPrefix): |
| 210 | typ = typeFunctions |
| 211 | case strings.HasPrefix(key, reasonPrefix): |
| 212 | typ = typeReasons |
| 213 | default: |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | var err error |
| 218 | if value, err = strconv.Atoi(fields[2]); err != nil { |
| 219 | return |
| 220 | } |
| 221 | |
| 222 | ok = true |
| 223 | return |
| 224 | } |
| 225 | |
| 226 | func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, functions, reasons map[string]int) error { |
| 227 | var last []byte |
| 228 | var haveLast, sawDefine bool |
| 229 | newLine := []byte("\n") |
| 230 | |
| 231 | scanner := bufio.NewScanner(headerFile) |
| 232 | for scanner.Scan() { |
| 233 | line := scanner.Text() |
| 234 | _, _, _, ok := parseDefineLine(line, lib) |
| 235 | if ok { |
| 236 | sawDefine = true |
| 237 | continue |
| 238 | } |
| 239 | |
| 240 | if haveLast { |
| 241 | w.Write(last) |
| 242 | w.Write(newLine) |
| 243 | } |
| 244 | |
| 245 | if len(line) > 0 || !sawDefine { |
| 246 | last = []byte(line) |
| 247 | haveLast = true |
| 248 | } else { |
| 249 | haveLast = false |
| 250 | } |
| 251 | sawDefine = false |
| 252 | } |
| 253 | |
| 254 | if err := scanner.Err(); err != nil { |
| 255 | return err |
| 256 | } |
| 257 | |
| 258 | outputAssignments(w, functions) |
| 259 | outputAssignments(w, reasons) |
| 260 | w.Write(newLine) |
| 261 | |
| 262 | if haveLast { |
| 263 | w.Write(last) |
| 264 | w.Write(newLine) |
| 265 | } |
| 266 | |
| 267 | return nil |
| 268 | } |
| 269 | |
| 270 | const ( |
| 271 | typeFunctions = iota |
| 272 | typeReasons |
| 273 | ) |
| 274 | |
| 275 | func outputStrings(w io.Writer, lib string, ty int, assignments map[string]int) { |
| 276 | lib = strings.ToUpper(lib) |
| 277 | prefixLen := len(lib + "_F_") |
| 278 | |
| 279 | keys := make([]string, 0, len(assignments)) |
| 280 | for key := range assignments { |
| 281 | keys = append(keys, key) |
| 282 | } |
| 283 | sort.Strings(keys) |
| 284 | |
| 285 | for _, key := range keys { |
| 286 | var pack string |
| 287 | if ty == typeFunctions { |
| 288 | pack = key + ", 0" |
| 289 | } else { |
| 290 | pack = "0, " + key |
| 291 | } |
| 292 | |
| 293 | fmt.Fprintf(w, " {ERR_PACK(ERR_LIB_%s, %s), \"%s\"},\n", lib, pack, key[prefixLen:]) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | func assignNewValues(assignments map[string]int) { |
| 298 | max := 99 |
| 299 | |
| 300 | for _, value := range assignments { |
| 301 | if value > max { |
| 302 | max = value |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | max++ |
| 307 | |
| 308 | for key, value := range assignments { |
| 309 | if value == -1 { |
| 310 | assignments[key] = max |
| 311 | max++ |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | func handleDeclareMacro(line, join, macroName string, m map[string]int) { |
| 317 | if i := strings.Index(line, macroName); i >= 0 { |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 318 | contents := line[i+len(macroName):] |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | if i := strings.Index(contents, ")"); i >= 0 { |
| 320 | contents = contents[:i] |
| 321 | args := strings.Split(contents, ",") |
| 322 | for i := range args { |
| 323 | args[i] = strings.TrimSpace(args[i]) |
| 324 | } |
| 325 | if len(args) != 2 { |
| 326 | panic("Bad macro line: " + line) |
| 327 | } |
| 328 | token := args[0] + join + args[1] |
| 329 | if _, ok := m[token]; !ok { |
| 330 | m[token] = -1 |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | func addFunctionsAndReasons(functions, reasons map[string]int, filename, prefix string) error { |
| 337 | file, err := os.Open(filename) |
| 338 | if err != nil { |
| 339 | return err |
| 340 | } |
| 341 | defer file.Close() |
| 342 | |
| 343 | prefix += "_" |
| 344 | reasonPrefix := prefix + "R_" |
| 345 | var currentFunction string |
| 346 | |
| 347 | scanner := bufio.NewScanner(file) |
| 348 | for scanner.Scan() { |
| 349 | line := scanner.Text() |
| 350 | |
| 351 | if len(line) > 0 && unicode.IsLetter(rune(line[0])) { |
| 352 | /* Function start */ |
| 353 | fields := strings.Fields(line) |
| 354 | for _, field := range fields { |
| 355 | if i := strings.Index(field, "("); i != -1 { |
| 356 | f := field[:i] |
| 357 | // The return type of some functions is |
| 358 | // a macro that contains a "(". |
| 359 | if f == "STACK_OF" { |
| 360 | continue |
| 361 | } |
| 362 | currentFunction = f |
| 363 | for len(currentFunction) > 0 && currentFunction[0] == '*' { |
| 364 | currentFunction = currentFunction[1:] |
| 365 | } |
| 366 | break |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if strings.Contains(line, "OPENSSL_PUT_ERROR(") { |
| 372 | functionToken := prefix + "F_" + currentFunction |
| 373 | if _, ok := functions[functionToken]; !ok { |
| 374 | functions[functionToken] = -1 |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | handleDeclareMacro(line, "_R_", "OPENSSL_DECLARE_ERROR_REASON(", reasons) |
| 379 | handleDeclareMacro(line, "_F_", "OPENSSL_DECLARE_ERROR_FUNCTION(", functions) |
| 380 | |
| 381 | for len(line) > 0 { |
| 382 | i := strings.Index(line, prefix) |
| 383 | if i == -1 { |
| 384 | break |
| 385 | } |
| 386 | |
| 387 | line = line[i:] |
| 388 | end := strings.IndexFunc(line, func(r rune) bool { |
| 389 | return !(r == '_' || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9')) |
| 390 | }) |
| 391 | if end == -1 { |
| 392 | end = len(line) |
| 393 | } |
| 394 | |
| 395 | var token string |
| 396 | token, line = line[:end], line[end:] |
| 397 | |
| 398 | switch { |
| 399 | case strings.HasPrefix(token, reasonPrefix): |
| 400 | if _, ok := reasons[token]; !ok { |
| 401 | reasons[token] = -1 |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | return scanner.Err() |
| 408 | } |
| 409 | |
| 410 | func parseHeader(lib string, file io.Reader) (functions, reasons map[string]int, err error) { |
| 411 | functions = make(map[string]int) |
| 412 | reasons = make(map[string]int) |
| 413 | |
| 414 | scanner := bufio.NewScanner(file) |
| 415 | for scanner.Scan() { |
| 416 | typ, key, value, ok := parseDefineLine(scanner.Text(), lib) |
| 417 | if !ok { |
| 418 | continue |
| 419 | } |
| 420 | |
| 421 | switch typ { |
| 422 | case typeFunctions: |
| 423 | functions[key] = value |
| 424 | case typeReasons: |
| 425 | reasons[key] = value |
| 426 | default: |
| 427 | panic("internal error") |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | err = scanner.Err() |
| 432 | return |
| 433 | } |
| 434 | |
| 435 | func main() { |
| 436 | flag.Parse() |
| 437 | |
| 438 | if err := makeErrors(*resetFlag); err != nil { |
| 439 | fmt.Fprintf(os.Stderr, "%s\n", err) |
| 440 | os.Exit(1) |
| 441 | } |
| 442 | } |