Noise-C
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
errors.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "internal.h"
24 #include <stdio.h>
25 #include <string.h>
26 
44 /* Default English strings for all known error codes */
45 static const char * const error_strings[] = {
46  "No error",
47  "Out of memory",
48  "Unknown identifier",
49  "Unknown name",
50  "MAC failure",
51  "Not applicable",
52  "System error",
53  "Remote public key required",
54  "Local keypair required",
55  "Pre shared key required",
56  "Invalid length",
57  "Invalid parameter",
58  "Invalid state",
59  "Invalid nonce",
60  "Invalid private key",
61  "Invalid public key",
62  "Invalid format",
63  "Invalid signature",
64  "END"
65 };
66 #define num_error_strings (sizeof(error_strings) / sizeof(error_strings[0]) - 1)
67 
76 static const char *noise_errstr(int err)
77 {
78  if (err == NOISE_ERROR_NONE)
79  return error_strings[0];
80  if (err < NOISE_ID('E', 1) || err >= NOISE_ID('E', num_error_strings))
81  return 0;
82  return error_strings[err - NOISE_ID('E', 0)];
83 }
84 
92 void noise_perror(const char *s, int err)
93 {
94  const char *errstr = noise_errstr(err);
95  if (!s)
96  s = "(null)";
97  if (errstr)
98  fprintf(stderr, "%s: %s\n", s, errstr);
99  else
100  fprintf(stderr, "%s: Unknown error 0x%x\n", s, err);
101 }
102 
116 int noise_strerror(int err, char *buf, size_t size)
117 {
118  const char *errstr = noise_errstr(err);
119  if (!buf || !size)
120  return -1;
121  if (errstr)
122  strncpy(buf, errstr, size);
123  else
124  snprintf(buf, size, "Unknown error 0x%x", err);
125  buf[size - 1] = '\0';
126  return 0;
127 }
128 
#define NOISE_ERROR_NONE
Success, no error.
size_t size
Total size of the structure including subclass state.
Definition: internal.h:216
Internal definitions for the library.
void noise_perror(const char *s, int err)
Prints a descriptive string for an error to stderr.
Definition: errors.c:92
#define NOISE_ID(ch, num)
Builds an algorithm identifier for the library.
int noise_strerror(int err, char *buf, size_t size)
Gets the descriptive string for an error code.
Definition: errors.c:116