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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* wslua_nstime.c
*
* Wireshark's interface to the Lua Programming Language
*
* (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
* (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
* (c) 2011, Stig Bjorlykke <stig@bjorlykke.org>
* (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "wslua.h"
#include <wsutil/nstime.h>
/* 1s = 10^9 ns. */
#define NS_PER_S 1000000000
/* WSLUA_CONTINUE_MODULE Pinfo */
WSLUA_CLASS_DEFINE(NSTime,FAIL_ON_NULL("NSTime"));
/* NSTime represents a nstime_t. This is an object with seconds and nanoseconds. */
WSLUA_CONSTRUCTOR NSTime_new(lua_State *L) {
/* Creates a new NSTime object. */
#define WSLUA_OPTARG_NSTime_new_SECONDS 1 /* Seconds. */
#define WSLUA_OPTARG_NSTime_new_NSECONDS 2 /* Nano seconds. */
lua_Integer secs = luaL_optinteger(L,WSLUA_OPTARG_NSTime_new_SECONDS,0);
lua_Integer nsecs = luaL_optinteger(L,WSLUA_OPTARG_NSTime_new_NSECONDS,0);
NSTime nstime = g_new(nstime_t, 1);
if (!nstime) return 0;
nstime->secs = (time_t) secs;
nstime->nsecs = (int) nsecs;
pushNSTime(L,nstime);
WSLUA_RETURN(1); /* The new NSTime object. */
}
WSLUA_METAMETHOD NSTime__call(lua_State* L) { /* Creates a NSTime object. */
#define WSLUA_OPTARG_NSTime__call_SECONDS 1 /* Seconds. */
#define WSLUA_OPTARG_NSTime__call_NSECONDS 2 /* Nanoseconds. */
lua_remove(L,1); /* remove the table */
WSLUA_RETURN(NSTime_new(L)); /* The new NSTime object. */
}
WSLUA_METHOD NSTime_tonumber(lua_State* L) {
/* Returns a Lua number of the `NSTime` representing seconds from epoch
@since 2.4.0
*/
NSTime nstime = checkNSTime(L,1);
lua_pushnumber(L, (lua_Number)nstime_to_sec(nstime));
WSLUA_RETURN(1); /* The Lua number. */
}
WSLUA_METAMETHOD NSTime__tostring(lua_State* L) {
NSTime nstime = checkNSTime(L,1);
gchar *str;
long secs = (long)nstime->secs;
gint nsecs = nstime->nsecs;
gboolean negative_zero = FALSE;
/* Time is defined as sec + nsec/10^9, both parts can be negative.
* Translate this into the more familiar sec.nsec notation instead. */
if (secs > 0 && nsecs < 0) {
/* sign mismatch: (2, -3ns) -> 1.7 */
nsecs += NS_PER_S;
secs--;
} else if (secs < 0 && nsecs > 0) {
/* sign mismatch: (-2, 3ns) -> -1.7 */
nsecs = NS_PER_S - nsecs;
secs--;
} else if (nsecs < 0) {
/* Drop sign, the integer part already has it: (-2, -3ns) -> -2.3 */
nsecs = -nsecs;
/* In case the integer part is zero, it does not has a sign, so remember
* that it must be explicitly added. */
negative_zero = secs == 0;
}
if (negative_zero) {
str = wmem_strdup_printf(NULL, "-0.%09d", nsecs);
} else {
str = wmem_strdup_printf(NULL, "%ld.%09d", secs, nsecs);
}
lua_pushstring(L, str);
wmem_free(NULL, str);
WSLUA_RETURN(1); /* The string representing the nstime. */
}
WSLUA_METAMETHOD NSTime__add(lua_State* L) { /* Calculates the sum of two NSTimes. */
NSTime time1 = checkNSTime(L,1);
NSTime time2 = checkNSTime(L,2);
NSTime time3 = (NSTime)g_malloc (sizeof (nstime_t));
nstime_sum (time3, time1, time2);
pushNSTime (L, time3);
return 1;
}
WSLUA_METAMETHOD NSTime__sub(lua_State* L) { /* Calculates the diff of two NSTimes. */
NSTime time1 = checkNSTime(L,1);
NSTime time2 = checkNSTime(L,2);
NSTime time3 = (NSTime)g_malloc (sizeof (nstime_t));
nstime_delta (time3, time1, time2);
pushNSTime (L, time3);
return 1;
}
WSLUA_METAMETHOD NSTime__unm(lua_State* L) { /* Calculates the negative NSTime. */
NSTime time1 = checkNSTime(L,1);
NSTime time2 = (NSTime)g_malloc (sizeof (nstime_t));
nstime_set_zero (time2);
nstime_subtract (time2, time1);
pushNSTime (L, time2);
return 1;
}
WSLUA_METAMETHOD NSTime__eq(lua_State* L) { /* Compares two NSTimes. */
NSTime time1 = checkNSTime(L,1);
NSTime time2 = checkNSTime(L,2);
gboolean result = FALSE;
if (nstime_cmp(time1, time2) == 0)
result = TRUE;
lua_pushboolean(L,result);
return 1;
}
WSLUA_METAMETHOD NSTime__le(lua_State* L) { /* Compares two NSTimes. */
NSTime time1 = checkNSTime(L,1);
NSTime time2 = checkNSTime(L,2);
gboolean result = FALSE;
if (nstime_cmp(time1, time2) <= 0)
result = TRUE;
lua_pushboolean(L,result);
return 1;
}
WSLUA_METAMETHOD NSTime__lt(lua_State* L) { /* Compares two NSTimes. */
NSTime time1 = checkNSTime(L,1);
NSTime time2 = checkNSTime(L,2);
gboolean result = FALSE;
if (nstime_cmp(time1, time2) < 0)
result = TRUE;
lua_pushboolean(L,result);
return 1;
}
/* WSLUA_ATTRIBUTE NSTime_secs RW The NSTime seconds. */
WSLUA_ATTRIBUTE_NUMBER_GETTER(NSTime,secs);
WSLUA_ATTRIBUTE_NUMBER_SETTER(NSTime,secs,time_t);
/* WSLUA_ATTRIBUTE NSTime_nsecs RW The NSTime nano seconds. */
WSLUA_ATTRIBUTE_NUMBER_GETTER(NSTime,nsecs);
WSLUA_ATTRIBUTE_NUMBER_SETTER(NSTime,nsecs,int);
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int NSTime__gc(lua_State* L) {
NSTime nstime = toNSTime(L,1);
if (!nstime) return 0;
g_free (nstime);
return 0;
}
/* This table is ultimately registered as a sub-table of the class' metatable,
* and if __index/__newindex is invoked then it calls the appropriate function
* from this table for getting/setting the members.
*/
WSLUA_ATTRIBUTES NSTime_attributes[] = {
WSLUA_ATTRIBUTE_RWREG(NSTime,secs),
WSLUA_ATTRIBUTE_RWREG(NSTime,nsecs),
{ NULL, NULL, NULL }
};
WSLUA_METHODS NSTime_methods[] = {
WSLUA_CLASS_FNREG(NSTime,new),
WSLUA_CLASS_FNREG(NSTime,tonumber),
{ NULL, NULL }
};
WSLUA_META NSTime_meta[] = {
WSLUA_CLASS_MTREG(NSTime,tostring),
WSLUA_CLASS_MTREG(NSTime,add),
WSLUA_CLASS_MTREG(NSTime,sub),
WSLUA_CLASS_MTREG(NSTime,unm),
WSLUA_CLASS_MTREG(NSTime,eq),
WSLUA_CLASS_MTREG(NSTime,le),
WSLUA_CLASS_MTREG(NSTime,lt),
WSLUA_CLASS_MTREG(NSTime,call),
{ NULL, NULL }
};
int NSTime_register(lua_State* L) {
WSLUA_REGISTER_CLASS_WITH_ATTRS(NSTime);
return 0;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|