| ... | ... | @@ -1,6 +1,9 @@ |
| 1 | | const std = @import("std.zig"); |
| 2 | 1 | const builtin = @import("builtin"); |
| 3 | 2 | |
| 3 | const std = @import("std.zig"); |
| 4 | const Reader = std.Io.Reader; |
| 5 | const Allocator = std.mem.Allocator; |
| 6 | |
| 4 | 7 | pub const Transition = struct { |
| 5 | 8 | ts: i64, |
| 6 | 9 | timetype: *Timetype, |
| ... | ... | @@ -34,7 +37,7 @@ pub const Leapsecond = struct { |
| 34 | 37 | }; |
| 35 | 38 | |
| 36 | 39 | pub const Tz = struct { |
| 37 | | allocator: std.mem.Allocator, |
| 40 | allocator: Allocator, |
| 38 | 41 | transitions: []const Transition, |
| 39 | 42 | timetypes: []const Timetype, |
| 40 | 43 | leapseconds: []const Leapsecond, |
| ... | ... | @@ -54,8 +57,8 @@ pub const Tz = struct { |
| 54 | 57 | }, |
| 55 | 58 | }; |
| 56 | 59 | |
| 57 | | pub fn parse(allocator: std.mem.Allocator, reader: anytype) !Tz { |
| 58 | | var legacy_header = try reader.readStruct(Header); |
| 60 | pub fn parse(allocator: Allocator, reader: *Reader) !Tz { |
| 61 | var legacy_header = try reader.takeStruct(Header, .little); |
| 59 | 62 | if (!std.mem.eql(u8, &legacy_header.magic, "TZif")) return error.BadHeader; |
| 60 | 63 | if (legacy_header.version != 0 and legacy_header.version != '2' and legacy_header.version != '3') return error.BadVersion; |
| 61 | 64 | |
| ... | ... | @@ -68,9 +71,9 @@ pub const Tz = struct { |
| 68 | 71 | } else { |
| 69 | 72 | // If the format is modern, just skip over the legacy data |
| 70 | 73 | const skipv = legacy_header.counts.timecnt * 5 + legacy_header.counts.typecnt * 6 + legacy_header.counts.charcnt + legacy_header.counts.leapcnt * 8 + legacy_header.counts.isstdcnt + legacy_header.counts.isutcnt; |
| 71 | | try reader.skipBytes(skipv, .{}); |
| 74 | try reader.discardAll(skipv); |
| 72 | 75 | |
| 73 | | var header = try reader.readStruct(Header); |
| 76 | var header = try reader.takeStruct(Header, .little); |
| 74 | 77 | if (!std.mem.eql(u8, &header.magic, "TZif")) return error.BadHeader; |
| 75 | 78 | if (header.version != '2' and header.version != '3') return error.BadVersion; |
| 76 | 79 | if (builtin.target.cpu.arch.endian() != std.builtin.Endian.big) { |
| ... | ... | @@ -81,7 +84,7 @@ pub const Tz = struct { |
| 81 | 84 | } |
| 82 | 85 | } |
| 83 | 86 | |
| 84 | | fn parseBlock(allocator: std.mem.Allocator, reader: anytype, header: Header, legacy: bool) !Tz { |
| 87 | fn parseBlock(allocator: Allocator, reader: *Reader, header: Header, legacy: bool) !Tz { |
| 85 | 88 | if (header.counts.isstdcnt != 0 and header.counts.isstdcnt != header.counts.typecnt) return error.Malformed; // rfc8536: isstdcnt [...] MUST either be zero or equal to "typecnt" |
| 86 | 89 | if (header.counts.isutcnt != 0 and header.counts.isutcnt != header.counts.typecnt) return error.Malformed; // rfc8536: isutcnt [...] MUST either be zero or equal to "typecnt" |
| 87 | 90 | if (header.counts.typecnt == 0) return error.Malformed; // rfc8536: typecnt [...] MUST NOT be zero |
| ... | ... | @@ -98,12 +101,12 @@ pub const Tz = struct { |
| 98 | 101 | // Parse transition types |
| 99 | 102 | var i: usize = 0; |
| 100 | 103 | while (i < header.counts.timecnt) : (i += 1) { |
| 101 | | transitions[i].ts = if (legacy) try reader.readInt(i32, .big) else try reader.readInt(i64, .big); |
| 104 | transitions[i].ts = if (legacy) try reader.takeInt(i32, .big) else try reader.takeInt(i64, .big); |
| 102 | 105 | } |
| 103 | 106 | |
| 104 | 107 | i = 0; |
| 105 | 108 | while (i < header.counts.timecnt) : (i += 1) { |
| 106 | | const tt = try reader.readByte(); |
| 109 | const tt = try reader.takeByte(); |
| 107 | 110 | if (tt >= timetypes.len) return error.Malformed; // rfc8536: Each type index MUST be in the range [0, "typecnt" - 1] |
| 108 | 111 | transitions[i].timetype = &timetypes[tt]; |
| 109 | 112 | } |
| ... | ... | @@ -111,11 +114,11 @@ pub const Tz = struct { |
| 111 | 114 | // Parse time types |
| 112 | 115 | i = 0; |
| 113 | 116 | while (i < header.counts.typecnt) : (i += 1) { |
| 114 | | const offset = try reader.readInt(i32, .big); |
| 117 | const offset = try reader.takeInt(i32, .big); |
| 115 | 118 | if (offset < -2147483648) return error.Malformed; // rfc8536: utoff [...] MUST NOT be -2**31 |
| 116 | | const dst = try reader.readByte(); |
| 119 | const dst = try reader.takeByte(); |
| 117 | 120 | if (dst != 0 and dst != 1) return error.Malformed; // rfc8536: (is)dst [...] The value MUST be 0 or 1. |
| 118 | | const idx = try reader.readByte(); |
| 121 | const idx = try reader.takeByte(); |
| 119 | 122 | if (idx > header.counts.charcnt - 1) return error.Malformed; // rfc8536: (desig)idx [...] Each index MUST be in the range [0, "charcnt" - 1] |
| 120 | 123 | timetypes[i] = .{ |
| 121 | 124 | .offset = offset, |
| ... | ... | @@ -128,7 +131,7 @@ pub const Tz = struct { |
| 128 | 131 | } |
| 129 | 132 | |
| 130 | 133 | var designators_data: [256 + 6]u8 = undefined; |
| 131 | | try reader.readNoEof(designators_data[0..header.counts.charcnt]); |
| 134 | try reader.readSliceAll(designators_data[0..header.counts.charcnt]); |
| 132 | 135 | const designators = designators_data[0..header.counts.charcnt]; |
| 133 | 136 | if (designators[designators.len - 1] != 0) return error.Malformed; // rfc8536: charcnt [...] includes the trailing NUL (0x00) octet |
| 134 | 137 | |
| ... | ... | @@ -144,12 +147,12 @@ pub const Tz = struct { |
| 144 | 147 | // Parse leap seconds |
| 145 | 148 | i = 0; |
| 146 | 149 | while (i < header.counts.leapcnt) : (i += 1) { |
| 147 | | const occur: i64 = if (legacy) try reader.readInt(i32, .big) else try reader.readInt(i64, .big); |
| 150 | const occur: i64 = if (legacy) try reader.takeInt(i32, .big) else try reader.takeInt(i64, .big); |
| 148 | 151 | if (occur < 0) return error.Malformed; // rfc8536: occur [...] MUST be nonnegative |
| 149 | 152 | if (i > 0 and leapseconds[i - 1].occurrence + 2419199 > occur) return error.Malformed; // rfc8536: occur [...] each later value MUST be at least 2419199 greater than the previous value |
| 150 | 153 | if (occur > std.math.maxInt(i48)) return error.Malformed; // Unreasonably far into the future |
| 151 | 154 | |
| 152 | | const corr = try reader.readInt(i32, .big); |
| 155 | const corr = try reader.takeInt(i32, .big); |
| 153 | 156 | if (i == 0 and corr != -1 and corr != 1) return error.Malformed; // rfc8536: The correction value in the first leap-second record, if present, MUST be either one (1) or minus one (-1) |
| 154 | 157 | if (i > 0 and leapseconds[i - 1].correction != corr + 1 and leapseconds[i - 1].correction != corr - 1) return error.Malformed; // rfc8536: The correction values in adjacent leap-second records MUST differ by exactly one (1) |
| 155 | 158 | if (corr > std.math.maxInt(i16)) return error.Malformed; // Unreasonably large correction |
| ... | ... | @@ -163,7 +166,7 @@ pub const Tz = struct { |
| 163 | 166 | // Parse standard/wall indicators |
| 164 | 167 | i = 0; |
| 165 | 168 | while (i < header.counts.isstdcnt) : (i += 1) { |
| 166 | | const stdtime = try reader.readByte(); |
| 169 | const stdtime = try reader.takeByte(); |
| 167 | 170 | if (stdtime == 1) { |
| 168 | 171 | timetypes[i].flags |= 0x02; |
| 169 | 172 | } |
| ... | ... | @@ -172,7 +175,7 @@ pub const Tz = struct { |
| 172 | 175 | // Parse UT/local indicators |
| 173 | 176 | i = 0; |
| 174 | 177 | while (i < header.counts.isutcnt) : (i += 1) { |
| 175 | | const ut = try reader.readByte(); |
| 178 | const ut = try reader.takeByte(); |
| 176 | 179 | if (ut == 1) { |
| 177 | 180 | timetypes[i].flags |= 0x04; |
| 178 | 181 | if (!timetypes[i].standardTimeIndicator()) return error.Malformed; // rfc8536: standard/wall value MUST be one (1) if the UT/local value is one (1) |
| ... | ... | @@ -182,9 +185,8 @@ pub const Tz = struct { |
| 182 | 185 | // Footer |
| 183 | 186 | var footer: ?[]u8 = null; |
| 184 | 187 | if (!legacy) { |
| 185 | | if ((try reader.readByte()) != '\n') return error.Malformed; // An rfc8536 footer must start with a newline |
| 186 | | var footerdata_buf: [128]u8 = undefined; |
| 187 | | const footer_mem = reader.readUntilDelimiter(&footerdata_buf, '\n') catch |err| switch (err) { |
| 188 | if ((try reader.takeByte()) != '\n') return error.Malformed; // An rfc8536 footer must start with a newline |
| 189 | const footer_mem = reader.takeSentinel('\n') catch |err| switch (err) { |
| 188 | 190 | error.StreamTooLong => return error.OverlargeFooter, // Read more than 128 bytes, much larger than any reasonable POSIX TZ string |
| 189 | 191 | else => return err, |
| 190 | 192 | }; |
| ... | ... | @@ -194,7 +196,7 @@ pub const Tz = struct { |
| 194 | 196 | } |
| 195 | 197 | errdefer if (footer) |ft| allocator.free(ft); |
| 196 | 198 | |
| 197 | | return Tz{ |
| 199 | return .{ |
| 198 | 200 | .allocator = allocator, |
| 199 | 201 | .transitions = transitions, |
| 200 | 202 | .timetypes = timetypes, |
| ... | ... | @@ -215,9 +217,9 @@ pub const Tz = struct { |
| 215 | 217 | |
| 216 | 218 | test "slim" { |
| 217 | 219 | const data = @embedFile("tz/asia_tokyo.tzif"); |
| 218 | | var in_stream = std.io.fixedBufferStream(data); |
| 220 | var in_stream: Reader = .fixed(data); |
| 219 | 221 | |
| 220 | | var tz = try std.Tz.parse(std.testing.allocator, in_stream.reader()); |
| 222 | var tz = try std.Tz.parse(std.testing.allocator, &in_stream); |
| 221 | 223 | defer tz.deinit(); |
| 222 | 224 | |
| 223 | 225 | try std.testing.expectEqual(tz.transitions.len, 9); |
| ... | ... | @@ -228,9 +230,9 @@ test "slim" { |
| 228 | 230 | |
| 229 | 231 | test "fat" { |
| 230 | 232 | const data = @embedFile("tz/antarctica_davis.tzif"); |
| 231 | | var in_stream = std.io.fixedBufferStream(data); |
| 233 | var in_stream: Reader = .fixed(data); |
| 232 | 234 | |
| 233 | | var tz = try std.Tz.parse(std.testing.allocator, in_stream.reader()); |
| 235 | var tz = try std.Tz.parse(std.testing.allocator, &in_stream); |
| 234 | 236 | defer tz.deinit(); |
| 235 | 237 | |
| 236 | 238 | try std.testing.expectEqual(tz.transitions.len, 8); |
| ... | ... | @@ -241,9 +243,9 @@ test "fat" { |
| 241 | 243 | test "legacy" { |
| 242 | 244 | // Taken from Slackware 8.0, from 2001 |
| 243 | 245 | const data = @embedFile("tz/europe_vatican.tzif"); |
| 244 | | var in_stream = std.io.fixedBufferStream(data); |
| 246 | var in_stream: Reader = .fixed(data); |
| 245 | 247 | |
| 246 | | var tz = try std.Tz.parse(std.testing.allocator, in_stream.reader()); |
| 248 | var tz = try std.Tz.parse(std.testing.allocator, &in_stream); |
| 247 | 249 | defer tz.deinit(); |
| 248 | 250 | |
| 249 | 251 | try std.testing.expectEqual(tz.transitions.len, 170); |