Coverage Report

Created: 2024-05-20 06:11

/src/FreeRDP/libfreerdp/core/timezone.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Time Zone Redirection
4
 *
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <freerdp/config.h>
21
22
#include <winpr/crt.h>
23
#include <winpr/assert.h>
24
#include <winpr/timezone.h>
25
26
#include "settings.h"
27
#include "timezone.h"
28
29
#include <freerdp/log.h>
30
#define TAG FREERDP_TAG("core.timezone")
31
32
static BOOL rdp_read_system_time(wStream* s, SYSTEMTIME* system_time);
33
static BOOL rdp_write_system_time(wStream* s, const SYSTEMTIME* system_time);
34
35
/**
36
 * Read SYSTEM_TIME structure (TS_SYSTEMTIME).
37
 * msdn{cc240478}
38
 * @param s stream
39
 * @param system_time system time structure
40
 */
41
42
BOOL rdp_read_system_time(wStream* s, SYSTEMTIME* system_time)
43
1.58k
{
44
1.58k
  WINPR_ASSERT(system_time);
45
46
1.58k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 16ull))
47
0
    return FALSE;
48
49
1.58k
  Stream_Read_UINT16(s, system_time->wYear);         /* wYear, must be set to 0 */
50
1.58k
  Stream_Read_UINT16(s, system_time->wMonth);        /* wMonth */
51
1.58k
  Stream_Read_UINT16(s, system_time->wDayOfWeek);    /* wDayOfWeek */
52
1.58k
  Stream_Read_UINT16(s, system_time->wDay);          /* wDay */
53
1.58k
  Stream_Read_UINT16(s, system_time->wHour);         /* wHour */
54
1.58k
  Stream_Read_UINT16(s, system_time->wMinute);       /* wMinute */
55
1.58k
  Stream_Read_UINT16(s, system_time->wSecond);       /* wSecond */
56
1.58k
  Stream_Read_UINT16(s, system_time->wMilliseconds); /* wMilliseconds */
57
1.58k
  return TRUE;
58
1.58k
}
59
60
/**
61
 * Write SYSTEM_TIME structure (TS_SYSTEMTIME).
62
 * msdn{cc240478}
63
 * @param s stream
64
 * @param system_time system time structure
65
 */
66
67
BOOL rdp_write_system_time(wStream* s, const SYSTEMTIME* system_time)
68
0
{
69
0
  WINPR_ASSERT(system_time);
70
0
  if (!Stream_EnsureRemainingCapacity(s, 16ull))
71
0
    return FALSE;
72
73
0
  Stream_Write_UINT16(s, system_time->wYear);         /* wYear, must be set to 0 */
74
0
  Stream_Write_UINT16(s, system_time->wMonth);        /* wMonth */
75
0
  Stream_Write_UINT16(s, system_time->wDayOfWeek);    /* wDayOfWeek */
76
0
  Stream_Write_UINT16(s, system_time->wDay);          /* wDay */
77
0
  Stream_Write_UINT16(s, system_time->wHour);         /* wHour */
78
0
  Stream_Write_UINT16(s, system_time->wMinute);       /* wMinute */
79
0
  Stream_Write_UINT16(s, system_time->wSecond);       /* wSecond */
80
0
  Stream_Write_UINT16(s, system_time->wMilliseconds); /* wMilliseconds */
81
0
  DEBUG_TIMEZONE("Time: y=%" PRIu16 ",m=%" PRIu16 ",dow=%" PRIu16 ",d=%" PRIu16 ", %02" PRIu16
82
0
                 ":%02" PRIu16 ":%02" PRIu16 ".%03" PRIu16 "",
83
0
                 system_time->wYear, system_time->wMonth, system_time->wDayOfWeek,
84
0
                 system_time->wDay, system_time->wHour, system_time->wMinute,
85
0
                 system_time->wSecond, system_time->wMilliseconds);
86
0
  return TRUE;
87
0
}
88
89
/**
90
 * Read client time zone information (TS_TIME_ZONE_INFORMATION).
91
 * msdn{cc240477}
92
 * @param s stream
93
 * @param settings settings
94
 *
95
 * @return \b TRUE for success, \b FALSE otherwise
96
 */
97
98
BOOL rdp_read_client_time_zone(wStream* s, rdpSettings* settings)
99
830
{
100
830
  LPTIME_ZONE_INFORMATION tz = { 0 };
101
102
830
  if (!s || !settings)
103
0
    return FALSE;
104
105
830
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 172))
106
40
    return FALSE;
107
108
790
  tz = settings->ClientTimeZone;
109
110
790
  if (!tz)
111
0
    return FALSE;
112
113
790
  Stream_Read_UINT32(s, tz->Bias); /* Bias */
114
  /* standardName (64 bytes) */
115
790
  Stream_Read(s, tz->StandardName, sizeof(tz->StandardName));
116
790
  if (!rdp_read_system_time(s, &tz->StandardDate)) /* StandardDate */
117
0
    return FALSE;
118
790
  Stream_Read_UINT32(s, tz->StandardBias);    /* StandardBias */
119
  /* daylightName (64 bytes) */
120
790
  Stream_Read(s, tz->DaylightName, sizeof(tz->DaylightName));
121
790
  if (!rdp_read_system_time(s, &tz->DaylightDate)) /* DaylightDate */
122
0
    return FALSE;
123
790
  Stream_Read_UINT32(s, tz->DaylightBias);    /* DaylightBias */
124
790
  return TRUE;
125
790
}
126
127
/**
128
 * Write client time zone information (TS_TIME_ZONE_INFORMATION).
129
 * msdn{cc240477}
130
 * @param s stream
131
 * @param settings settings
132
 *
133
 * @return \b TRUE for success, \b FALSE otherwise
134
 */
135
136
BOOL rdp_write_client_time_zone(wStream* s, rdpSettings* settings)
137
0
{
138
0
  LPTIME_ZONE_INFORMATION tz = { 0 };
139
140
0
  WINPR_ASSERT(settings);
141
0
  tz = settings->ClientTimeZone;
142
143
0
  if (!tz)
144
0
    return FALSE;
145
146
0
  if (!Stream_EnsureRemainingCapacity(s, 4ull + sizeof(tz->StandardName)))
147
0
    return FALSE;
148
149
  /* Bias */
150
0
  Stream_Write_UINT32(s, tz->Bias);
151
  /* standardName (64 bytes) */
152
0
  Stream_Write(s, tz->StandardName, sizeof(tz->StandardName));
153
  /* StandardDate */
154
0
  if (!rdp_write_system_time(s, &tz->StandardDate))
155
0
    return FALSE;
156
157
#ifdef WITH_DEBUG_TIMEZONE
158
  WLog_DBG(TIMEZONE_TAG, "bias=%" PRId32 "", tz->Bias);
159
  WLog_DBG(TIMEZONE_TAG, "StandardName:");
160
  winpr_HexDump(TIMEZONE_TAG, WLOG_DEBUG, (const BYTE*)tz->StandardName,
161
                sizeof(tz->StandardName));
162
  WLog_DBG(TIMEZONE_TAG, "DaylightName:");
163
  winpr_HexDump(TIMEZONE_TAG, WLOG_DEBUG, (const BYTE*)tz->DaylightName,
164
                sizeof(tz->DaylightName));
165
#endif
166
  /* Note that StandardBias is ignored if no valid standardDate is provided. */
167
  /* StandardBias */
168
0
  if (!Stream_EnsureRemainingCapacity(s, 4ull + sizeof(tz->DaylightName)))
169
0
    return FALSE;
170
0
  Stream_Write_UINT32(s, tz->StandardBias);
171
0
  DEBUG_TIMEZONE("StandardBias=%" PRId32 "", tz->StandardBias);
172
  /* daylightName (64 bytes) */
173
0
  Stream_Write(s, tz->DaylightName, sizeof(tz->DaylightName));
174
  /* DaylightDate */
175
0
  if (!rdp_write_system_time(s, &tz->DaylightDate))
176
0
    return FALSE;
177
  /* Note that DaylightBias is ignored if no valid daylightDate is provided. */
178
  /* DaylightBias */
179
0
  if (!Stream_EnsureRemainingCapacity(s, 4ull))
180
0
    return FALSE;
181
0
  Stream_Write_UINT32(s, tz->DaylightBias);
182
0
  DEBUG_TIMEZONE("DaylightBias=%" PRId32 "", tz->DaylightBias);
183
0
  return TRUE;
184
0
}