summaryrefslogtreecommitdiff
path: root/src/tz_info.cc
blob: 7c3361ee63be259ada91990c3bec2b45211d09cb (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "common.hh"

#include "io.hh"
#include "logger.hh"
#include "tz_info.hh"
#include "tz_str.hh"

#include <byteswap.h>
#include <errno.h>
#include <string.h>
#include <vector>
#include <unistd.h>

namespace {

struct Header {
  char magic[4];
  char ver;
  char unused[15];
  uint32_t isutcnt;
  uint32_t isstdcnt;
  uint32_t leapcnt;
  uint32_t timecnt;
  uint32_t typecnt;
  uint32_t charcnt;
};

#if !HAVE_ATTRIBUTE_PACKED
#pragma pack(push)
#endif
struct
#if HAVE_ATTRIBUTE_PACKED
__attribute__((packed))
#endif
LocalTimeType {
  int32_t utoff;
  uint8_t dst;
  uint8_t idx;
};

struct
#if HAVE_ATTRIBUTE_PACKED
__attribute__((packed))
#endif
LeapSecond {
  int64_t occur;
  int32_t corr;
};
#if !HAVE_ATTRIBUTE_PACKED
#pragma pack(pop)
#endif

struct Data {
  std::vector<int64_t> transition_times;
  std::vector<uint8_t> transition_types;
  std::vector<LocalTimeType> local_time_type_records;
  std::vector<char> time_zone_designations;
  std::vector<LeapSecond> leap_second_records;
  std::vector<uint8_t> standard_or_wall_indicators;
  std::vector<uint8_t> ut_local_indicators;
};

inline uint32_t ntoh(uint32_t value) {
#ifdef WORDS_BIGENDIAN
  return value;
#else
  return bswap_32(value);
#endif
}

inline int32_t ntoh(int32_t value) {
#ifdef WORDS_BIGENDIAN
  return value;
#else
  return bswap_32(value);
#endif
}

inline int64_t ntoh(int64_t value) {
#ifdef WORDS_BIGENDIAN
  return value;
#else
  return bswap_64(value);
#endif
}

class TzInfoImpl : public TzInfo {
public:
  TzInfoImpl(std::shared_ptr<Logger> logger,
             std::filesystem::path tzinfo_dir)
    : logger_(std::move(logger)), base_(std::move(tzinfo_dir)) {
    static_assert(sizeof(Header) == 44, "Header must be packed");
  }

  std::optional<time_t> get_local_time(std::string_view tzname,
                                       time_t utc_time) const override {
    if (!base_.empty()) {
      std::filesystem::path zone = base_ / tzname;
      auto fd = io::open(zone, io::open_flags::rdonly);
      if (fd) {
        return read(zone, fd.get(), utc_time);
      } else {
        logger_->warn("Unable to open %s for reading: %s.", zone.c_str(),
                      strerror(errno));
      }
    }
    return std::nullopt;
  }

private:
  std::optional<time_t> read(std::filesystem::path const& zone, int fd,
                             time_t utc_time) const {
    Header header;
    if (!io::read_all(fd, &header, sizeof(header))) {
      logger_->warn("%s: Error reading: %s.", zone.c_str(),
                    strerror(errno));
      return std::nullopt;
    }
    if (!check_and_fix_header(header)) {
      logger_->warn("%s: Not a TZinfo file.", zone.c_str());
      return std::nullopt;
    }
    // Skip V1, 32bit time stamps are just so 90's.
    auto ret = lseek(fd, data_size(header, '\0'), SEEK_CUR);
    if (ret == -1) {
      logger_->warn("%s: Error seeking: %s.", zone.c_str(), strerror(errno));
      return std::nullopt;
    }
    if (!io::read_all(fd, &header, sizeof(header))) {
      logger_->warn("%s: Error reading: %s.", zone.c_str(),
                    strerror(errno));
      return std::nullopt;
    }
    if (header.ver < '2' || !check_and_fix_header(header)) {
      logger_->warn("%s: Not a TZinfo V2+ file.", zone.c_str());
      return std::nullopt;
    }
    Data data;
    if (!read_data(fd, header, data)) {
      logger_->warn("%s: Error reading: %s.", zone.c_str(),
                    strerror(errno));
      return std::nullopt;
    }
    std::string footer;
    // Note that read_footer might read past the footer, which is currently
    // fine (as it is a footer) but good to know for future developers.
    if (!read_footer(fd, footer)) {
      logger_->warn("%s: Error reading (4): %s.", zone.c_str(),
                    strerror(errno));
      return std::nullopt;
    }

    return utc_to_local(data, footer, utc_time);
  }

  static std::optional<time_t> utc_to_local(Data const& data,
                                            std::string const& footer,
                                            time_t utc) {
    if (data.transition_times.empty())
      return utc_to_local(footer, utc);
    if (utc < data.transition_times.front())
      return std::nullopt;
    size_t i = 0;
    while (i < data.transition_times.size() && utc > data.transition_times[i])
      ++i;

    if (i == 0)
      return std::nullopt;
    if (i == data.transition_times.size() && !footer.empty())
      return utc_to_local(footer, utc);
    --i;

    return utc_to_local(data.local_time_type_records[
                            data.transition_types[i]], utc);
  }

  static std::optional<time_t> utc_to_local(LocalTimeType const& local_time,
                                     time_t utc) {
    return utc += local_time.utoff;
  }

  static std::optional<time_t> utc_to_local(std::string const& tz, time_t utc) {
    return tz::get_local_time(tz, utc);
  }

  static size_t data_size(Header const& header, char version) {
    auto time_size = version >= '2' ? 8 : 4;
    return header.timecnt * time_size + header.timecnt + header.typecnt * 6 +
      header.charcnt + header.leapcnt * (time_size + 4) + header.isstdcnt +
      header.isutcnt;
  }

  static bool read_data(int fd, Header const& header, Data& data) {
    assert(header.ver >= '2');
    data.transition_times.resize(header.timecnt);
    data.transition_types.resize(header.timecnt);
    static_assert(sizeof(LocalTimeType) == 6, "LocalTimeType must be packed");
    data.local_time_type_records.resize(header.typecnt);
    data.time_zone_designations.resize(header.charcnt);
    static_assert(sizeof(LeapSecond) == 12, "LeapSecond must be packed");
    data.leap_second_records.resize(header.leapcnt);
    data.standard_or_wall_indicators.resize(header.isstdcnt);
    data.ut_local_indicators.resize(header.isutcnt);
    if (!io::read_all(fd, data.transition_times.data(),
                      header.timecnt * sizeof(int64_t)) ||
        !io::read_all(fd, data.transition_types.data(), header.timecnt) ||
        !io::read_all(fd, data.local_time_type_records.data(),
                      header.typecnt * sizeof(LocalTimeType)) ||
        !io::read_all(fd, data.time_zone_designations.data(), header.charcnt) ||
        !io::read_all(fd, data.leap_second_records.data(),
                      header.leapcnt * sizeof(LeapSecond)) ||
        !io::read_all(fd, data.standard_or_wall_indicators.data(),
                      header.isstdcnt) ||
        !io::read_all(fd, data.ut_local_indicators.data(), header.isutcnt))
      return false;

    {
      int64_t last = std::numeric_limits<int64_t>::min();
      for (auto& time : data.transition_times) {
        time = ntoh(time);
        if (time <= last)
          return false;
        last = time;
      }
    }

    for (auto const& type : data.transition_types)
      if (type >= header.typecnt)
        return false;

    for (auto& local : data.local_time_type_records) {
      local.utoff = ntoh(local.utoff);
      if (local.utoff == -2147483648)
        return false;
      if (local.dst != 0 && local.dst != 1)
        return false;
      if (local.idx >= header.charcnt)
        return false;
    }

    if (data.time_zone_designations.empty() ||
        data.time_zone_designations.back() != '\0')
      return false;

    {
      LeapSecond last;
      last.occur = -1;
      last.corr = 0;
      for (auto& leap : data.leap_second_records) {
        leap.occur = ntoh(leap.occur);
        leap.corr = ntoh(leap.corr);
        if (last.occur == -1) {
          if (leap.occur < 0)
            return false;
        } else {
          if (leap.occur <= last.occur)
            return false;
        }
        auto diff = last.corr - leap.corr;
        if (diff != 1 && diff != -1)
          return false;
        last.occur = leap.occur + 2419199;
        last.corr = leap.corr;
      }
    }

    for (auto const& standard_or_wall : data.standard_or_wall_indicators)
      if (standard_or_wall != 1 && standard_or_wall != 0)
        return false;

    for (auto const& ut_or_local : data.ut_local_indicators)
      if (ut_or_local != 1 && ut_or_local != 0)
        return false;

    return true;
  }

  static bool read_footer(int fd, std::string& footer) {
    size_t offset = 0;
    footer.resize(128);
    while (true) {
      auto got = io::read(fd, footer.data() + offset, footer.size() - offset);
      if (got <= 0)
        return false;
      if (footer.front() != '\n')
        return false;
      auto end = footer.substr(0, got).find('\n', std::max(1lu, offset));
      if (end != std::string::npos) {
        footer = footer.substr(1, end - 1);
        return true;
      }
      offset += got;
      footer.resize(offset + 128);
    }
  }

  static bool check_and_fix_header(Header& header) {
    if (memcmp(header.magic, "TZif", 4))
      return false;
    header.isutcnt = ntoh(header.isutcnt);
    header.isstdcnt = ntoh(header.isstdcnt);
    header.leapcnt = ntoh(header.leapcnt);
    header.timecnt = ntoh(header.timecnt);
    header.typecnt = ntoh(header.typecnt);
    header.charcnt = ntoh(header.charcnt);

    if (header.isutcnt != 0 && header.isutcnt != header.typecnt)
      return false;
    if (header.isstdcnt != 0 && header.isstdcnt != header.typecnt)
      return false;
    if (header.typecnt == 0)
      return false;
    if (header.charcnt == 0)
      return false;

    return true;
  }

  std::shared_ptr<Logger> logger_;
  std::filesystem::path base_;
};

}  // namespace

std::unique_ptr<TzInfo> TzInfo::create(std::shared_ptr<Logger> logger,
                                       std::filesystem::path tzinfo_dir) {
  return std::make_unique<TzInfoImpl>(std::move(logger), std::move(tzinfo_dir));
}