| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <time.h> |
| 4 |
|
| 5 |
#include "iso9660.h" |
| 6 |
#include "log.h" |
| 7 |
#include "stdio-util.h" |
| 8 |
#include "string-util.h" |
| 9 |
#include "time-util.h" |
| 10 |
|
| 11 |
void iso9660_datetime_zero(struct iso9660_datetime *ret) { |
| 12 |
assert(ret); |
| 13 |
|
| 14 |
memcpy(ret->year, "0000", 4); |
| 15 |
memcpy(ret->month, "00", 2); |
| 16 |
memcpy(ret->day, "00", 2); |
| 17 |
memcpy(ret->hour, "00", 2); |
| 18 |
memcpy(ret->minute, "00", 2); |
| 19 |
memcpy(ret->second, "00", 2); |
| 20 |
memcpy(ret->deci, "00", 2); |
| 21 |
ret->zone = 0; |
| 22 |
} |
| 23 |
|
| 24 |
static int validate_tm(const struct tm *t) { |
| 25 |
assert(t); |
| 26 |
|
| 27 |
/* Safety checks on bounded fields of struct tm, ranges as per tm(3type). Mostly in place because |
| 28 |
* ISO9660 date/time ranges and struct tm ranges differ. */ |
| 29 |
|
| 30 |
if (t->tm_mon < 0 || t->tm_mon > 11) |
| 31 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Month out of range."); |
| 32 |
if (t->tm_mday < 1 || t->tm_mday > 31) |
| 33 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Day of month out of range."); |
| 34 |
if (t->tm_hour < 0 || t->tm_hour > 23) |
| 35 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Hour out of range."); |
| 36 |
if (t->tm_min < 0 || t->tm_min > 59) |
| 37 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Minute out of range."); |
| 38 |
if (t->tm_sec < 0 || t->tm_sec > 60) |
| 39 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Seconds out of range."); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int iso9660_datetime_from_usec(usec_t usec, bool utc, struct iso9660_datetime *ret) { |
| 45 |
struct tm t; |
| 46 |
int r; |
| 47 |
|
| 48 |
assert(ret); |
| 49 |
|
| 50 |
r = localtime_or_gmtime_usec(usec, utc, &t); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
r = validate_tm(&t); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
if (t.tm_year >= 10000 - 1900) |
| 59 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Year has more than 4 digits and is incompatible with ISO9660."); |
| 60 |
if (t.tm_year + 1900 < 0) |
| 61 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Year is negative and is incompatible with ISO9660."); |
| 62 |
|
| 63 |
long offset = t.tm_gmtoff / (15*60); /* The time zone is encoded by 15 minutes increments */ |
| 64 |
if (offset < INT8_MIN || offset > INT8_MAX) |
| 65 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "GMT offset out of range."); |
| 66 |
|
| 67 |
char buf[17]; |
| 68 |
assert_cc(sizeof(buf)-1 == offsetof(struct iso9660_datetime, zone)); |
| 69 |
/* Ignore leap seconds, no real hope for hardware. Deci-seconds always zero. */ |
| 70 |
xsprintf(buf, "%04d%02d%02d%02d%02d%02d00", |
| 71 |
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, |
| 72 |
t.tm_hour, t.tm_min, MIN(t.tm_sec, 59)); |
| 73 |
memcpy(ret, buf, sizeof(buf)-1); |
| 74 |
|
| 75 |
ret->zone = offset; |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
int iso9660_dir_datetime_from_usec(usec_t usec, bool utc, struct iso9660_dir_time *ret) { |
| 81 |
struct tm t; |
| 82 |
int r; |
| 83 |
|
| 84 |
assert(ret); |
| 85 |
|
| 86 |
r = localtime_or_gmtime_usec(usec, utc, &t); |
| 87 |
if (r < 0) |
| 88 |
return r; |
| 89 |
|
| 90 |
r = validate_tm(&t); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
|
| 94 |
if (t.tm_year < 0 || t.tm_year > UINT8_MAX) |
| 95 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Year is incompatible with ISO9660."); |
| 96 |
|
| 97 |
long offset = t.tm_gmtoff / (15*60); /* The time zone is encoded by 15 minutes increments */ |
| 98 |
if (offset < INT8_MIN || offset > INT8_MAX) |
| 99 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "GMT offset out of range."); |
| 100 |
|
| 101 |
*ret = (struct iso9660_dir_time) { |
| 102 |
.year = t.tm_year, |
| 103 |
.month = t.tm_mon + 1, |
| 104 |
.day = t.tm_mday, |
| 105 |
.hour = t.tm_hour, |
| 106 |
.minute = t.tm_min, |
| 107 |
.second = MIN(t.tm_sec, 59), |
| 108 |
/* The time zone is encoded by 15 minutes increments */ |
| 109 |
.offset = offset, |
| 110 |
}; |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
static bool iso9660_valid_string(const char *str, bool allow_a_chars) { |
| 116 |
/* note that a-chars are not supposed to accept lower case letters, but it looks like common practice |
| 117 |
* to use them |
| 118 |
*/ |
| 119 |
return in_charset(str, allow_a_chars ? UPPERCASE_LETTERS LOWERCASE_LETTERS DIGITS " _!\"%&'()*+,-./:;<=>?" : UPPERCASE_LETTERS DIGITS "_"); |
| 120 |
} |
| 121 |
|
| 122 |
int iso9660_set_string(char target[], size_t len, const char *source, bool allow_a_chars) { |
| 123 |
assert(target || len == 0); |
| 124 |
|
| 125 |
if (source) { |
| 126 |
if (!iso9660_valid_string(source, allow_a_chars)) |
| 127 |
return -EINVAL; |
| 128 |
|
| 129 |
size_t slen = strlen(source); |
| 130 |
if (slen > len) |
| 131 |
return -EINVAL; |
| 132 |
|
| 133 |
memset(mempcpy(target, source, slen), ' ', len - slen); |
| 134 |
} else |
| 135 |
memset(target, ' ', len); |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
bool iso9660_volume_name_valid(const char *name) { |
| 141 |
/* In theory the volume identifier should be d-chars, but in practice, a-chars are allowed */ |
| 142 |
return iso9660_valid_string(name, /* allow_a_chars= */ true) && |
| 143 |
strlen(name) <= 32; |
| 144 |
} |
| 145 |
|
| 146 |
bool iso9660_system_name_valid(const char *name) { |
| 147 |
return iso9660_valid_string(name, /* allow_a_chars= */ true) && |
| 148 |
strlen(name) <= 32; |
| 149 |
} |
| 150 |
|
| 151 |
bool iso9660_publisher_name_valid(const char *name) { |
| 152 |
return iso9660_valid_string(name, /* allow_a_chars= */ true) && |
| 153 |
strlen(name) <= 128; |
| 154 |
} |