Coverage Report

Created: 2023-09-25 06:17

/src/znc/third_party/cctz/src/time_zone_posix.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//   https://www.apache.org/licenses/LICENSE-2.0
8
//
9
//   Unless required by applicable law or agreed to in writing, software
10
//   distributed under the License is distributed on an "AS IS" BASIS,
11
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//   See the License for the specific language governing permissions and
13
//   limitations under the License.
14
15
#include "time_zone_posix.h"
16
17
#include <cstddef>
18
#include <cstring>
19
#include <limits>
20
#include <string>
21
22
namespace cctz {
23
24
namespace {
25
26
const char kDigits[] = "0123456789";
27
28
0
const char* ParseInt(const char* p, int min, int max, int* vp) {
29
0
  int value = 0;
30
0
  const char* op = p;
31
0
  const int kMaxInt = std::numeric_limits<int>::max();
32
0
  for (; const char* dp = strchr(kDigits, *p); ++p) {
33
0
    int d = static_cast<int>(dp - kDigits);
34
0
    if (d >= 10) break;  // '\0'
35
0
    if (value > kMaxInt / 10) return nullptr;
36
0
    value *= 10;
37
0
    if (value > kMaxInt - d) return nullptr;
38
0
    value += d;
39
0
  }
40
0
  if (p == op || value < min || value > max) return nullptr;
41
0
  *vp = value;
42
0
  return p;
43
0
}
44
45
// abbr = <.*?> | [^-+,\d]{3,}
46
0
const char* ParseAbbr(const char* p, std::string* abbr) {
47
0
  const char* op = p;
48
0
  if (*p == '<') {  // special zoneinfo <...> form
49
0
    while (*++p != '>') {
50
0
      if (*p == '\0') return nullptr;
51
0
    }
52
0
    abbr->assign(op + 1, static_cast<std::size_t>(p - op) - 1);
53
0
    return ++p;
54
0
  }
55
0
  while (*p != '\0') {
56
0
    if (strchr("-+,", *p)) break;
57
0
    if (strchr(kDigits, *p)) break;
58
0
    ++p;
59
0
  }
60
0
  if (p - op < 3) return nullptr;
61
0
  abbr->assign(op, static_cast<std::size_t>(p - op));
62
0
  return p;
63
0
}
64
65
// offset = [+|-]hh[:mm[:ss]] (aggregated into single seconds value)
66
const char* ParseOffset(const char* p, int min_hour, int max_hour, int sign,
67
0
                        std::int_fast32_t* offset) {
68
0
  if (p == nullptr) return nullptr;
69
0
  if (*p == '+' || *p == '-') {
70
0
    if (*p++ == '-') sign = -sign;
71
0
  }
72
0
  int hours = 0;
73
0
  int minutes = 0;
74
0
  int seconds = 0;
75
76
0
  p = ParseInt(p, min_hour, max_hour, &hours);
77
0
  if (p == nullptr) return nullptr;
78
0
  if (*p == ':') {
79
0
    p = ParseInt(p + 1, 0, 59, &minutes);
80
0
    if (p == nullptr) return nullptr;
81
0
    if (*p == ':') {
82
0
      p = ParseInt(p + 1, 0, 59, &seconds);
83
0
      if (p == nullptr) return nullptr;
84
0
    }
85
0
  }
86
0
  *offset = sign * ((((hours * 60) + minutes) * 60) + seconds);
87
0
  return p;
88
0
}
89
90
// datetime = ( Jn | n | Mm.w.d ) [ / offset ]
91
0
const char* ParseDateTime(const char* p, PosixTransition* res) {
92
0
  if (p != nullptr && *p == ',') {
93
0
    if (*++p == 'M') {
94
0
      int month = 0;
95
0
      if ((p = ParseInt(p + 1, 1, 12, &month)) != nullptr && *p == '.') {
96
0
        int week = 0;
97
0
        if ((p = ParseInt(p + 1, 1, 5, &week)) != nullptr && *p == '.') {
98
0
          int weekday = 0;
99
0
          if ((p = ParseInt(p + 1, 0, 6, &weekday)) != nullptr) {
100
0
            res->date.fmt = PosixTransition::M;
101
0
            res->date.m.month = static_cast<std::int_fast8_t>(month);
102
0
            res->date.m.week = static_cast<std::int_fast8_t>(week);
103
0
            res->date.m.weekday = static_cast<std::int_fast8_t>(weekday);
104
0
          }
105
0
        }
106
0
      }
107
0
    } else if (*p == 'J') {
108
0
      int day = 0;
109
0
      if ((p = ParseInt(p + 1, 1, 365, &day)) != nullptr) {
110
0
        res->date.fmt = PosixTransition::J;
111
0
        res->date.j.day = static_cast<std::int_fast16_t>(day);
112
0
      }
113
0
    } else {
114
0
      int day = 0;
115
0
      if ((p = ParseInt(p, 0, 365, &day)) != nullptr) {
116
0
        res->date.fmt = PosixTransition::N;
117
0
        res->date.n.day = static_cast<std::int_fast16_t>(day);
118
0
      }
119
0
    }
120
0
  }
121
0
  if (p != nullptr) {
122
0
    res->time.offset = 2 * 60 * 60;  // default offset is 02:00:00
123
0
    if (*p == '/') p = ParseOffset(p + 1, -167, 167, 1, &res->time.offset);
124
0
  }
125
0
  return p;
126
0
}
127
128
}  // namespace
129
130
// spec = std offset [ dst [ offset ] , datetime , datetime ]
131
0
bool ParsePosixSpec(const std::string& spec, PosixTimeZone* res) {
132
0
  const char* p = spec.c_str();
133
0
  if (*p == ':') return false;
134
135
0
  p = ParseAbbr(p, &res->std_abbr);
136
0
  p = ParseOffset(p, 0, 24, -1, &res->std_offset);
137
0
  if (p == nullptr) return false;
138
0
  if (*p == '\0') return true;
139
140
0
  p = ParseAbbr(p, &res->dst_abbr);
141
0
  if (p == nullptr) return false;
142
0
  res->dst_offset = res->std_offset + (60 * 60);  // default
143
0
  if (*p != ',') p = ParseOffset(p, 0, 24, -1, &res->dst_offset);
144
145
0
  p = ParseDateTime(p, &res->dst_start);
146
0
  p = ParseDateTime(p, &res->dst_end);
147
148
0
  return p != nullptr && *p == '\0';
149
0
}
150
151
}  // namespace cctz