Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / wslua / wslua_nstime.c
blob94db190a76939707449b526f6015d81316d6ee31
1 /*
2 * wslua_nstime.c
4 * Wireshark's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
8 * (c) 2011, Stig Bjorlykke <stig@bjorlykke.org>
9 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include "config.h"
20 #include "wslua.h"
21 #include <wsutil/nstime.h>
23 /* 1s = 10^9 ns. */
24 #define NS_PER_S 1000000000
26 /* WSLUA_CONTINUE_MODULE Pinfo */
29 WSLUA_CLASS_DEFINE(NSTime,FAIL_ON_NULL("NSTime"));
30 /* NSTime represents a nstime_t. This is an object with seconds and nanoseconds. */
32 WSLUA_CONSTRUCTOR NSTime_new(lua_State *L) {
33 /* Creates a new NSTime object. */
34 #define WSLUA_OPTARG_NSTime_new_SECONDS 1 /* Seconds. */
35 #define WSLUA_OPTARG_NSTime_new_NSECONDS 2 /* Nano seconds. */
36 lua_Integer secs = luaL_optinteger(L,WSLUA_OPTARG_NSTime_new_SECONDS,0);
37 lua_Integer nsecs = luaL_optinteger(L,WSLUA_OPTARG_NSTime_new_NSECONDS,0);
39 NSTime nstime = g_new(nstime_t, 1);
40 if (!nstime) return 0;
41 nstime->secs = (time_t) secs;
42 nstime->nsecs = (int) nsecs;
44 pushNSTime(L,nstime);
46 WSLUA_RETURN(1); /* The new NSTime object. */
49 WSLUA_METAMETHOD NSTime__call(lua_State* L) { /* Creates a NSTime object. */
50 #define WSLUA_OPTARG_NSTime__call_SECONDS 1 /* Seconds. */
51 #define WSLUA_OPTARG_NSTime__call_NSECONDS 2 /* Nanoseconds. */
52 lua_remove(L,1); /* remove the table */
53 WSLUA_RETURN(NSTime_new(L)); /* The new NSTime object. */
56 WSLUA_METHOD NSTime_tonumber(lua_State* L) {
57 /* Returns a Lua number of the `NSTime` representing seconds from epoch. */
58 NSTime nstime = checkNSTime(L,1);
59 lua_pushnumber(L, (lua_Number)nstime_to_sec(nstime));
60 WSLUA_RETURN(1); /* The Lua number. */
63 WSLUA_METAMETHOD NSTime__tostring(lua_State* L) {
64 NSTime nstime = checkNSTime(L,1);
65 char *str;
66 long secs = (long)nstime->secs;
67 int nsecs = nstime->nsecs;
68 bool negative_zero = false;
70 /* Time is defined as sec + nsec/10^9, both parts can be negative.
71 * Translate this into the more familiar sec.nsec notation instead. */
72 if (secs > 0 && nsecs < 0) {
73 /* sign mismatch: (2, -3ns) -> 1.7 */
74 nsecs += NS_PER_S;
75 secs--;
76 } else if (secs < 0 && nsecs > 0) {
77 /* sign mismatch: (-2, 3ns) -> -1.7 */
78 nsecs = NS_PER_S - nsecs;
79 secs--;
80 } else if (nsecs < 0) {
81 /* Drop sign, the integer part already has it: (-2, -3ns) -> -2.3 */
82 nsecs = -nsecs;
83 /* In case the integer part is zero, it does not has a sign, so remember
84 * that it must be explicitly added. */
85 negative_zero = secs == 0;
88 if (negative_zero) {
89 str = wmem_strdup_printf(NULL, "-0.%09d", nsecs);
90 } else {
91 str = wmem_strdup_printf(NULL, "%ld.%09d", secs, nsecs);
93 lua_pushstring(L, str);
94 wmem_free(NULL, str);
96 WSLUA_RETURN(1); /* The string representing the nstime. */
98 WSLUA_METAMETHOD NSTime__add(lua_State* L) { /* Calculates the sum of two NSTimes. */
99 NSTime time1 = checkNSTime(L,1);
100 NSTime time2 = checkNSTime(L,2);
101 NSTime time3 = (NSTime)g_malloc (sizeof (nstime_t));
103 nstime_sum (time3, time1, time2);
104 pushNSTime (L, time3);
106 return 1;
109 WSLUA_METAMETHOD NSTime__sub(lua_State* L) { /* Calculates the diff of two NSTimes. */
110 NSTime time1 = checkNSTime(L,1);
111 NSTime time2 = checkNSTime(L,2);
112 NSTime time3 = (NSTime)g_malloc (sizeof (nstime_t));
114 nstime_delta (time3, time1, time2);
115 pushNSTime (L, time3);
117 return 1;
120 WSLUA_METAMETHOD NSTime__unm(lua_State* L) { /* Calculates the negative NSTime. */
121 NSTime time1 = checkNSTime(L,1);
122 NSTime time2 = (NSTime)g_malloc (sizeof (nstime_t));
124 nstime_set_zero (time2);
125 nstime_subtract (time2, time1);
126 pushNSTime (L, time2);
128 return 1;
131 WSLUA_METAMETHOD NSTime__eq(lua_State* L) { /* Compares two NSTimes. */
132 NSTime time1 = checkNSTime(L,1);
133 NSTime time2 = checkNSTime(L,2);
134 bool result = false;
136 if (nstime_cmp(time1, time2) == 0)
137 result = true;
139 lua_pushboolean(L,result);
141 return 1;
144 WSLUA_METAMETHOD NSTime__le(lua_State* L) { /* Compares two NSTimes. */
145 NSTime time1 = checkNSTime(L,1);
146 NSTime time2 = checkNSTime(L,2);
147 bool result = false;
149 if (nstime_cmp(time1, time2) <= 0)
150 result = true;
152 lua_pushboolean(L,result);
154 return 1;
157 WSLUA_METAMETHOD NSTime__lt(lua_State* L) { /* Compares two NSTimes. */
158 NSTime time1 = checkNSTime(L,1);
159 NSTime time2 = checkNSTime(L,2);
160 bool result = false;
162 if (nstime_cmp(time1, time2) < 0)
163 result = true;
165 lua_pushboolean(L,result);
167 return 1;
171 /* WSLUA_ATTRIBUTE NSTime_secs RW The NSTime seconds. */
172 WSLUA_ATTRIBUTE_INTEGER_GETTER(NSTime,secs);
173 WSLUA_ATTRIBUTE_INTEGER_SETTER(NSTime,secs,time_t);
175 /* WSLUA_ATTRIBUTE NSTime_nsecs RW The NSTime nano seconds. */
176 WSLUA_ATTRIBUTE_INTEGER_GETTER(NSTime,nsecs);
177 WSLUA_ATTRIBUTE_INTEGER_SETTER(NSTime,nsecs,int);
179 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
180 static int NSTime__gc(lua_State* L) {
181 NSTime nstime = toNSTime(L,1);
183 if (!nstime) return 0;
185 g_free (nstime);
186 return 0;
189 /* This table is ultimately registered as a sub-table of the class' metatable,
190 * and if __index/__newindex is invoked then it calls the appropriate function
191 * from this table for getting/setting the members.
193 WSLUA_ATTRIBUTES NSTime_attributes[] = {
194 WSLUA_ATTRIBUTE_RWREG(NSTime,secs),
195 WSLUA_ATTRIBUTE_RWREG(NSTime,nsecs),
196 { NULL, NULL, NULL }
199 WSLUA_METHODS NSTime_methods[] = {
200 WSLUA_CLASS_FNREG(NSTime,new),
201 WSLUA_CLASS_FNREG(NSTime,tonumber),
202 { NULL, NULL }
205 WSLUA_META NSTime_meta[] = {
206 WSLUA_CLASS_MTREG(NSTime,tostring),
207 WSLUA_CLASS_MTREG(NSTime,add),
208 WSLUA_CLASS_MTREG(NSTime,sub),
209 WSLUA_CLASS_MTREG(NSTime,unm),
210 WSLUA_CLASS_MTREG(NSTime,eq),
211 WSLUA_CLASS_MTREG(NSTime,le),
212 WSLUA_CLASS_MTREG(NSTime,lt),
213 WSLUA_CLASS_MTREG(NSTime,call),
214 { NULL, NULL }
217 int NSTime_register(lua_State* L) {
218 WSLUA_REGISTER_CLASS_WITH_ATTRS(NSTime);
219 return 0;
224 * Editor modelines - https://www.wireshark.org/tools/modelines.html
226 * Local variables:
227 * c-basic-offset: 4
228 * tab-width: 8
229 * indent-tabs-mode: nil
230 * End:
232 * vi: set shiftwidth=4 tabstop=8 expandtab:
233 * :indentSize=4:tabSize=8:noTabs=true: