-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathzen_error.h
More file actions
152 lines (136 loc) · 5.35 KB
/
Copy pathzen_error.h
File metadata and controls
152 lines (136 loc) · 5.35 KB
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
/*
* This file is part of zenroom
*
* Copyright (C) 2017-2026 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3.0
*
* 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 Affero General Public License for more details.
*
* Along with this program you should have received a copy of the
* GNU Affero General Public License v3.0
* If not, see http://www.gnu.org/licenses/agpl.txt
*
* Last modified by Denis Roio
* on Tuesday, 27th July 2021
*/
#ifndef __ZEN_ERROR_H__
#define __ZEN_ERROR_H__
#include <string.h>
#include <stdarg.h>
#include <hedley.h>
/* Resolve the current runtime context from a Lua state, or fall back to
* the process-global singleton when no state is available yet. */
void *zen_get_context(void *L);
void *zen_get_global_context(void);
// tracing wrappers for all C->Lua functions
#define BEGIN() trace(L, "vv begin %s",__func__)
#define END(n) trace(L, "^^ end %s",__func__); return(n)
#define ENDV() trace(L, "^^ end %s",__func__); return
#define THROW(ERR) \
lerror(L, "fatal %s: %s", __func__, (ERR)); \
lua_pushnil(L)
#define SAFE_GOTO(x, msg) \
if(HEDLEY_UNLIKELY(!(x))) { failed_msg=msg; goto end; }
#define SAFE(x, msg) \
if(HEDLEY_UNLIKELY(!(x))) { THROW(msg); END(1); }
#define SAFEV(x, msg) \
if(HEDLEY_UNLIKELY(!(x))) { THROW(msg); ENDV(); }
// common error messages
#define MALLOC_ERROR "Could not allocate memory"
#define CREATE_BIG_ERR "Could not create BIG"
#define ALLOCATE_BIG_ERR "Could not allocate BIG"
#define DUPLICATE_BIG_ERR "Could not duplicate BIG"
#define CREATE_OCT_ERR "Could not create OCTET"
#define ALLOCATE_OCT_ERR "Could not allocate OCTET"
#define DUPLICATE_OCT_ERR "Could not duplicate OCTET"
#define CREATE_ECP_ERR "Could not create ECP"
#define ALLOCATE_ECP_ERR "Could not allocate ECP"
#define DUPLICATE_ECP_ERR "Could not duplicate ECP"
#define CREATE_ECP2_ERR "Could not create ECP2"
#define ALLOCATE_ECP2_ERR "Could not allocate ECP2"
#define DUPLICATE_ECP2_ERR "Could not duplicate ECP2"
#define CREATE_FP12_ERR "Could not create FP12"
#define ALLOCATE_FP12_ERR "Could not allocate FP12"
#define DUPLICATE_FP12_ERR "Could not duplicate FP12"
#define CREATE_FLT_ERR "Could not create FLOAT"
#define ALLOCATE_FLT_ERR "Could not allocate FLOAT"
#define CREATE_HASH_ERR "Could not create HASH"
#define ALLOCATE_HASH_ERR "Could not allocate HASH"
#define CREATE_TIME_ERR "Could not create TIME"
#define ALLOCATE_TIME_ERR "Could not allocate TIME"
// same as Android
typedef enum log_priority {
LOG_UNKNOWN = 0,
LOG_DEFAULT,
LOG_VERBOSE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
LOG_SILENT,
} log_priority;
#define LOG_DEFAULT " . "
void get_log_prefix(void *Z, log_priority prio, char dest[5]);
void zen_raw_write(int fd, const void *buf, size_t count);
void zen_out_string(const char *msg);
// context free print and error messages
HEDLEY_PRINTF_FORMAT(1,2)
void _out(const char *fmt, ...);
HEDLEY_PRINTF_FORMAT(1,2)
void _err(const char *fmt, ...);
// context free results
int OK(void);
int FAIL(void);
// lua context error message
HEDLEY_PRINTF_FORMAT(2,3)
void lerror(void *L, const char *fmt, ...);
HEDLEY_PRINTF_FORMAT(2,3)
int notice(void *L, const char *format, ...); // INFO
HEDLEY_PRINTF_FORMAT(2,3)
int func(void *L, const char *format, ...); // VERBOSE
HEDLEY_PRINTF_FORMAT(2,3)
int trace(void *L, const char *format, ...); // TRACE (VERY VERBOSE)
HEDLEY_PRINTF_FORMAT(2,3)
int zerror(void *L, const char *format, ...); // ERROR
HEDLEY_PRINTF_FORMAT(2,3)
int act(void *L, const char *format, ...); // DEBUG
HEDLEY_PRINTF_FORMAT(2,3)
int warning(void *L, const char *format, ...); // WARN
int hexdump(void *L, const char *src, size_t len); // DEBUG hex sequence
void json_start(void *L);
void json_end(void *L);
// #define SAFE(x) HEDLEY_REQUIRE(x != NULL)
// if(!x) lerror(L, "NULL variable in %s",__func__)
// useful for debugging
#if DEBUG == 1
#define HERE() _err( "-> %s()\n",__func__)
#define HEREs(s) _err( "-> %s(%s)\n",__func__,s)
#define HEREp(p) _err( "-> %s(%p)\n",__func__,p)
#define HEREn(n) _err( "-> %s(%i)\n",__func__,n)
#define HEREc(c) _err( "-> %s(%c)\n",__func__,c)
#define HEREoct(o) \
_err( "-> %s - octet %p (%i/%i) ref:%i\n",__func__,o->val,o->len,o->max,o->ref)
#define HEREecp(e) \
_err( "-> %s - ecp %p ref:%i\n",__func__,o->val,o->ref)
#define HEREecdh(e) \
_err( "--> %s - ecdh %p\n\tcurve[%s] type[%s]\n\t fieldsize[%i] hash[%i]\n\tpubkey[%p(%i/%i)] publen[%i]\n\tseckey[%p(%i/%i)] seclen[%i]\m",__func__, e, e->curve, e->type, e->fieldsize, e->hash, e->pubkey, e->pubkey?e->pubkey->len:0x0, e->pubkey?e->pubkey->max:0x0, e->publen, e->seckey, e->seckey?e->seckey->len:0x0, e->seckey?e->seckey->max:0x0, e->seclen)
#define HEREhex(b, len) \
char *dst = malloc((len<<1)+2); buf2hex(dst, b, len); \
dst[(len<<1)] = 0x0; _err("%s\n",dst); free(dst);
#else
#define HERE() (void)__func__
#define HEREs(s) (void)__func__
#define HEREp(s) (void)__func__
#define HEREn(s) (void)__func__
#define HEREoct(o) (void)__func__
#define HEREecdh(o) (void)__func__
#define HEREhex(b,len) (void)__func__
#endif
#endif