Debian Codegraph @

src/common/bnettime.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999 Rob Crittenden ([email protected])
3 * Copyright (C) 1999,2000 Ross Combs ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #define BNETTIME_INTERNAL_ACCESScxx . . $ `src/common/bnettime.cpp:19:9`!
20 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
21 #include "common/bnettime.h"cxx . . $ `<file>/src/common/bnettime.h`/
22
23 #include <cstdio>
24 #include <cstring>
25 #include <cerrno>
26 #include <ctime>
27
28 #include "compat/gettimeofday.h"cxx . . $ `<file>/src/compat/gettimeofday.h`/
29 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
30 #include "common/bn_type.h"cxx . . $ `<file>/src/common/bn_type.h`/
31 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
32
33
34 namespace pvpgncxx . . $ pvpgn/
35 {
36
37 /*
38 * By comparing the hex numbers from packet dumps with the times displayed
39 * on the login screen, bnettime seems to be in units of 10E-7 seconds. It
40 * is stored in a 64bit value, and represented as two distinct numbers when
41 * it is used as a string in the protocol.
42 *
43 * examples (client in GMT timezone):
44 * 11/04/99 05:16 0x01 "29304451 3046165090"
45 * 01/03/99 11:19 0x01 "29243146 3825346784"
46 * 08/02/99 21:28 0x01 "29285677 4283311890"
47 * 12/12/99 01:55 0x01 "29312068 151587081"
48 *
49 * The top number seems to be in units of about 7 minutes because the bottom
50 * number rolls over every 2^32 10E-7 seconds, which is:
51 * (2^32)*.0000001
52 * 429.4967296 // seconds
53 * 429/60
54 * 7.15827882666666666666 // minutes
55 *
56 * The epoch was determined using a binary search looking for Jan 1, 1970
57 * (GMT) to be displayed after setting the last game time to a pair of
58 * numbers. It was determined through all 64 bits by looking for the exact
59 * number where adding one pushes it over to 00:01. It was determined to be:
60 * "27111902 3577643008"
61 *
62 * It appears that this is the same format as is used in MS-DOS to store file
63 * times. The format is a 64 bit number telling how many 100 nanosecond
64 * intervals since Jan 1, 1601.
65 *
66 * If I was clever I should be able to do this without floating point math.
67 * I don't feel like being clever though :) (Refer to the function
68 * DOSFS_UnixTimeToFileTime() in dosfs.c in the WINE source code for an
69 * example that I wish I had seen before writing this).
70 *
71 * Update... from looking at the WINE sources VAP has determined that these
72 * are actually in MS-Windows FileTime format.
73 *
74 * "In the WINE listing, you can see that
75 * FileTime = (UnixTime * 10 000 000) + 116 444 736 000 000 000 + remainder
76 * These dates are represented from 1 Jan 1601."
77 */
78
79 #define SEC_PER_USECcxx . . $ `src/common/bnettime.cpp:79:9`! .0000001
80
81 #define UNIX_EPOCHcxx . . $ `src/common/bnettime.cpp:81:9`! 11644473600. /* seconds the Unix epoch is from the bnettime epoch */
82
83 #define SEC_PER_UPPERcxx . . $ `src/common/bnettime.cpp:83:9`! 429.4967296 /* (2^32)*10E-7 */
84 #define SEC_PER_LOWERcxx . . $ `src/common/bnettime.cpp:84:9`! .0000001
85
86 #define UPPER_PER_SECcxx . . $ `src/common/bnettime.cpp:86:9`! .00232830643653869628 /* 10E7/(2^32) */
87 #define LOWER_PER_SECcxx . . $ `src/common/bnettime.cpp:87:9`! 10000000.
88
89
90 extern t_bnettimecxx . . $ pvpgn/t_bnettime# secs_to_bnettimecxx . . $ pvpgn/secs_to_bnettime(9aaee16a40fcc070).(double secs)
91 {
92 t_bnettimecxx . . $ pvpgn/t_bnettime# temp;
93
94 temp.ucxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#u. = (unsigned int)(secs*UPPER_PER_SECcxx . . $ `src/common/bnettime.cpp:86:9`!);
95 temp.lcxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#l. = (unsigned int)(secs*LOWER_PER_SECcxx . . $ `src/common/bnettime.cpp:87:9`!);
96
97 return temp;
98 }
99
100
101 extern double bnettime_to_secscxx . . $ pvpgn/bnettime_to_secs(7d4cfa203a0dd875).(t_bnettimecxx . . $ pvpgn/t_bnettime# bntime)
102 {
103 return ((double)bntime.ucxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#u.)*SEC_PER_UPPERcxx . . $ `src/common/bnettime.cpp:83:9`! +
104 ((double)bntime.lcxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#l.)*SEC_PER_LOWERcxx . . $ `src/common/bnettime.cpp:84:9`!;
105 }
106
107
108 extern t_bnettimecxx . . $ pvpgn/t_bnettime# time_to_bnettimecxx . . $ pvpgn/time_to_bnettime(f96d2c3d06d968c9).(stdcxx . . $ std/::time_tcxx . . $ std/time_t# stdtime, unsigned int usec)
109 {
110 return secs_to_bnettimecxx . . $ pvpgn/secs_to_bnettime(9aaee16a40fcc070).((double)stdtime + (double)usec*SEC_PER_USECcxx . . $ `src/common/bnettime.cpp:79:9`! + UNIX_EPOCHcxx . . $ `src/common/bnettime.cpp:81:9`!);
111 }
112
113
114 extern stdcxx . . $ std/::time_tcxx . . $ std/time_t# bnettime_to_timecxx . . $ pvpgn/bnettime_to_time(f37c7c75a9c2d829).(t_bnettimecxx . . $ pvpgn/t_bnettime# bntime)
115 {
116 return (stdcxx . . $ std/::time_tcxx . . $ std/time_t#)(bnettime_to_secscxx . . $ pvpgn/bnettime_to_secs(7d4cfa203a0dd875).(bntime) - UNIX_EPOCHcxx . . $ `src/common/bnettime.cpp:81:9`!);
117 }
118
119
120 /* return current time as bnettime */
121 extern t_bnettimecxx . . $ pvpgn/t_bnettime# bnettimecxx . . $ pvpgn/bnettime(c08f2632e82bc0c).(void)
122 {
123 struct timevalcxx . . $ timeval# tv;
124
125 if (gettimeofday(&tv, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) < 0)
126 {
127 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not get time (gettimeofday: {})", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
128 return time_to_bnettimecxx . . $ pvpgn/time_to_bnettime(f96d2c3d06d968c9).(stdcxx . . $ std/::timecxx . . $ std/time(a7cd306b2d326b09).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!), 0);
129 }
130 return time_to_bnettimecxx . . $ pvpgn/time_to_bnettime(f96d2c3d06d968c9).((stdcxx . . $ std/::time_tcxx . . $ std/time_t#)tv.tv_seccxx . . $ timeval#tv_sec., tv.tv_useccxx . . $ timeval#tv_usec.);
131 }
132
133
134 /* FIXME: the string functions here should probably go in account_wrap */
135 extern char const * bnettime_get_strcxx . . $ pvpgn/bnettime_get_str(3c7c6f20d7c4fe28).(t_bnettimecxx . . $ pvpgn/t_bnettime# bntime)
136 {
137 static char temp[1024];
138
139 stdcxx . . $ std/::sprintfcxx . . $ std/sprintf(bb743b22d71e45d5).(temp, "%u %u", bntime.ucxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#u., bntime.lcxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#l.);
140
141 return temp;
142 }
143
144
145 extern int bnettime_set_strcxx . . $ pvpgn/bnettime_set_str(faeb564d2075bce7).(t_bnettimecxx . . $ pvpgn/t_bnettime# * bntime, char const * timestr)
146 {
147 if (!bntime)
148 {
149 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL bntime");
150 return -1;
151 }
152 if (!timestr)
153 {
154 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL timestr");
155 return -1;
156 }
157
158 if (stdcxx . . $ std/::sscanfcxx . . $ std/sscanf(6246fabbf6b5312d).(timestr, "%u %u", &bntime->ucxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#u., &bntime->lcxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#l.) != 2)
159 return -1;
160
161 return 0;
162 }
163
164
165 extern void bnettime_to_bn_longcxx . . $ pvpgn/bnettime_to_bn_long(5cee0b6c516bd57b).(t_bnettimecxx . . $ pvpgn/t_bnettime# in, bn_longcxx . . $ pvpgn/bn_long# * out)
166 {
167 if (!out)
168 {
169 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL out");
170 return;
171 }
172
173 bn_long_set_a_bcxx . . $ pvpgn/bn_long_set_a_b(45bff462d07d60c).(out, in.ucxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#u., in.lcxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#l.);
174 }
175
176
177 extern void bn_long_to_bnettimecxx . . $ pvpgn/bn_long_to_bnettime(42c8eeb10f75b1bc).(bn_longcxx . . $ pvpgn/bn_long# in, t_bnettimecxx . . $ pvpgn/t_bnettime# * out)
178 {
179 if (!out)
180 {
181 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL out");
182 return;
183 }
184
185 out->ucxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#u. = bn_long_get_acxx . . $ pvpgn/bn_long_get_a(ca1608e2c87725e2).(in);
186 out->lcxx . . $ pvpgn/$anonymous_type_c83edbf674c01669_0#l. = bn_long_get_bcxx . . $ pvpgn/bn_long_get_b(ca1608e2c87725e2).(in);
187 }
188
189
190 extern int local_tzbiascxx . . $ pvpgn/local_tzbias(b126dc7c1de90089).(void) /* in minutes */
191 {
192 stdcxx . . $ std/::time_tcxx . . $ std/time_t# now;
193 stdcxx . . $ std/::time_tcxx . . $ std/time_t# test;
194 stdcxx . . $ std/::time_tcxx . . $ std/time_t# testloc;
195 struct stdcxx . . $ std/::tmcxx . . $ tm# * temp;
196
197 now = stdcxx . . $ std/::timecxx . . $ std/time(a7cd306b2d326b09).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
198
199 if (!(temp = stdcxx . . $ std/::gmtimecxx . . $ std/gmtime(7d91c9e5f6bb884b).(&now)))
200 return 0;
201 if ((test = stdcxx . . $ std/::mktimecxx . . $ std/mktime(77f55e735876b1c8).(temp)) == (stdcxx . . $ std/::time_tcxx . . $ std/time_t#)(-1))
202 return 0;
203
204 if (!(temp = stdcxx . . $ std/::localtimecxx . . $ std/localtime(7d91c9e5f6bb884b).(&now)))
205 return 0;
206 if ((testloc = stdcxx . . $ std/::mktimecxx . . $ std/mktime(77f55e735876b1c8).(temp)) == (stdcxx . . $ std/::time_tcxx . . $ std/time_t#)(-1))
207 return 0;
208
209 if (testloc > test) /* std::time_t is probably unsigned... */
210 return -(int)(testloc - test) / 60;
211 return (int)(test - testloc) / 60;
212 }
213
214
215 extern t_bnettimecxx . . $ pvpgn/t_bnettime# bnettime_add_tzbiascxx . . $ pvpgn/bnettime_add_tzbias(f1f1310dc900a45f).(t_bnettimecxx . . $ pvpgn/t_bnettime# bntime, int tzbias)
216 {
217 return secs_to_bnettimecxx . . $ pvpgn/secs_to_bnettime(9aaee16a40fcc070).(bnettime_to_secscxx . . $ pvpgn/bnettime_to_secs(7d4cfa203a0dd875).(bntime) - (double)tzbias*60.0);
218 }
219
220 }