1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <stdlib.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include "apps.h"
14e1051a39Sopenharmony_ci#include "progs.h"
15e1051a39Sopenharmony_ci#include <openssl/bio.h>
16e1051a39Sopenharmony_ci#include <openssl/err.h>
17e1051a39Sopenharmony_ci#include <openssl/ssl.h>
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_citypedef enum OPTION_choice {
20e1051a39Sopenharmony_ci    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP
21e1051a39Sopenharmony_ci} OPTION_CHOICE;
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ciconst OPTIONS errstr_options[] = {
24e1051a39Sopenharmony_ci    {OPT_HELP_STR, 1, '-', "Usage: %s [options] errnum...\n"},
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci    OPT_SECTION("General"),
27e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci    OPT_PARAMETERS(),
30e1051a39Sopenharmony_ci    {"errnum", 0, 0, "Error number(s) to decode"},
31e1051a39Sopenharmony_ci    {NULL}
32e1051a39Sopenharmony_ci};
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ciint errstr_main(int argc, char **argv)
35e1051a39Sopenharmony_ci{
36e1051a39Sopenharmony_ci    OPTION_CHOICE o;
37e1051a39Sopenharmony_ci    char buf[256], *prog;
38e1051a39Sopenharmony_ci    int ret = 1;
39e1051a39Sopenharmony_ci    unsigned long l;
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, errstr_options);
42e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
43e1051a39Sopenharmony_ci        switch (o) {
44e1051a39Sopenharmony_ci        case OPT_EOF:
45e1051a39Sopenharmony_ci        case OPT_ERR:
46e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
47e1051a39Sopenharmony_ci            goto end;
48e1051a39Sopenharmony_ci        case OPT_HELP:
49e1051a39Sopenharmony_ci            opt_help(errstr_options);
50e1051a39Sopenharmony_ci            ret = 0;
51e1051a39Sopenharmony_ci            goto end;
52e1051a39Sopenharmony_ci        }
53e1051a39Sopenharmony_ci    }
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci    /*
56e1051a39Sopenharmony_ci     * We're not really an SSL application so this won't auto-init, but
57e1051a39Sopenharmony_ci     * we're still interested in SSL error strings
58e1051a39Sopenharmony_ci     */
59e1051a39Sopenharmony_ci    OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
60e1051a39Sopenharmony_ci                    | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci    /* All remaining arg are error code. */
63e1051a39Sopenharmony_ci    ret = 0;
64e1051a39Sopenharmony_ci    for (argv = opt_rest(); *argv != NULL; argv++) {
65e1051a39Sopenharmony_ci        if (sscanf(*argv, "%lx", &l) == 0) {
66e1051a39Sopenharmony_ci            ret++;
67e1051a39Sopenharmony_ci        } else {
68e1051a39Sopenharmony_ci            ERR_error_string_n(l, buf, sizeof(buf));
69e1051a39Sopenharmony_ci            BIO_printf(bio_out, "%s\n", buf);
70e1051a39Sopenharmony_ci        }
71e1051a39Sopenharmony_ci    }
72e1051a39Sopenharmony_ci end:
73e1051a39Sopenharmony_ci    return ret;
74e1051a39Sopenharmony_ci}
75