format.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #pragma once
  2. #include <jvalidate/_macro.h>
  3. #include <cctype>
  4. #include <chrono>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <ctime>
  8. #include <string>
  9. #include <string_view>
  10. #include <unordered_map>
  11. #include <utility>
  12. #ifdef JVALIDATE_HAS_IDNA
  13. #include <ada/idna/to_unicode.h>
  14. #include <ada/idna/validity.h>
  15. #endif
  16. #include <jvalidate/detail/expect.h>
  17. #include <jvalidate/detail/idna_special_cases.h>
  18. #include <jvalidate/detail/pointer.h>
  19. #include <jvalidate/detail/relative_pointer.h>
  20. #include <jvalidate/detail/string.h>
  21. #include <jvalidate/forward.h>
  22. #define CONSTRUCTS(TYPE) format::ctor_as_valid<detail::TYPE>
  23. #define UTF32(FN) JVALIDATE_IIF(JVALIDATE_HAS_IDNA, format::utf32<format::FN<char32_t>>, nullptr)
  24. namespace jvalidate::format {
  25. bool date(std::string_view dt);
  26. bool time(std::string_view dt);
  27. bool date_time(std::string_view dt);
  28. bool duration(std::string_view dur);
  29. template <typename CharT = char> bool uri(std::basic_string_view<CharT> uri);
  30. template <typename CharT = char> bool uri_reference(std::basic_string_view<CharT> uri);
  31. bool uuid(std::string_view id);
  32. template <typename CharT = char> bool hostname(std::basic_string_view<CharT> name);
  33. bool ipv4(std::string_view ip);
  34. bool ipv6(std::string_view ip);
  35. template <typename CharT = char> bool email(std::basic_string_view<CharT> em);
  36. }
  37. namespace jvalidate::format::detail {
  38. struct result {
  39. ptrdiff_t consumed;
  40. bool valid;
  41. };
  42. inline bool is_leapyear(int y) { return (y % 400) == 0 || ((y % 4) == 0 && (y % 100) != 0); }
  43. inline bool illegal_date(int y, int m, int d) {
  44. static constexpr int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  45. if (is_leapyear(y) && m == 1) {
  46. --d;
  47. }
  48. return d > days[m];
  49. }
  50. inline result date(std::string_view dt) {
  51. struct tm tm;
  52. if (auto end = strptime(dt.data(), "%Y-%m-%d", &tm); end) {
  53. if ((end - dt.data()) != 10 || illegal_date(tm.tm_year + 1900, tm.tm_mon, tm.tm_mday)) {
  54. return {.consumed = 0, .valid = false};
  55. }
  56. return {.consumed = end - dt.data(), .valid = true};
  57. }
  58. return {.consumed = 0L, .valid = false};
  59. }
  60. inline bool is_leapsecond(std::tm tm) {
  61. if (tm.tm_sec != 60) {
  62. return true;
  63. }
  64. #if __cpp_lib_chrono >= 201907L
  65. tm.tm_isdst = -1;
  66. std::chrono::seconds time(std::mktime(&tm));
  67. auto const & leap_seconds = std::chrono::get_tzdb().leap_seconds;
  68. return std::ranges::find(leap_seconds, time) != leap_seconds.end();
  69. #else
  70. return false;
  71. #endif
  72. }
  73. // https://www.rfc-editor.org/rfc/rfc6570.html#section-1.5
  74. inline bool is_uschar(int c) {
  75. using P = std::pair<int, int>;
  76. constexpr std::array data{
  77. P{0xA0, 0xD7FF}, P{0xF900, 0xFDCF}, P{0xFDF0, 0xFFEF}, P{0x10000, 0x1FFFD},
  78. P{0x20000, 0x2FFFD}, P{0x30000, 0x3FFFD}, P{0x40000, 0x4FFFD}, P{0x50000, 0x5FFFD},
  79. P{0x60000, 0x6FFFD}, P{0x70000, 0x7FFFD}, P{0x80000, 0x8FFFD}, P{0x90000, 0x9FFFD},
  80. P{0xA0000, 0xAFFFD}, P{0xB0000, 0xBFFFD}, P{0xC0000, 0xCFFFD}, P{0xD0000, 0xDFFFD},
  81. P{0xE0000, 0xEFFFD},
  82. };
  83. return std::ranges::any_of(data,
  84. [c](auto & pair) { return c >= pair.first && c <= pair.second; });
  85. }
  86. template <typename CharT>
  87. inline bool is_pchar(std::basic_string_view<CharT> part, size_t & pos,
  88. std::string_view extra_valid_chars = ":@") {
  89. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  90. if (std::isalnum(part[pos]) || is_uschar(part[pos]) ||
  91. std::strchr("-._~!$&'()*+,;=", part[pos])) {
  92. return true;
  93. }
  94. if (part[pos] == '%') {
  95. return std::strchr(g_hex_digits, part[++pos]) && std::strchr(g_hex_digits, part[++pos]);
  96. }
  97. return extra_valid_chars.find(part[pos]) != part.npos;
  98. };
  99. template <typename CharT> inline bool is_uri_authority(std::basic_string_view<CharT> uri) {
  100. if (size_t pos = uri.find('@'); pos != uri.npos && pos < uri.find('/')) {
  101. for (size_t i = 0; i < pos; ++i) {
  102. if (not is_pchar(uri, i, ":")) {
  103. return false;
  104. }
  105. }
  106. uri.remove_prefix(pos + 1);
  107. }
  108. if (uri[0] == '[') {
  109. size_t pos = uri.find(']');
  110. auto ip = uri.substr(1, pos - 1);
  111. uri.remove_prefix(pos + 1);
  112. if (not ipv6(to_u8(ip))) {
  113. return false;
  114. }
  115. }
  116. if (size_t pos = uri.find(':'); pos != uri.npos) {
  117. if (not std::ranges::all_of(uri.substr(pos + 1), [](auto c) { return std::isdigit(c); })) {
  118. return false;
  119. }
  120. uri.remove_suffix(uri.size() - pos + 1);
  121. }
  122. return ipv4(to_u8(uri)) || hostname(uri);
  123. }
  124. template <typename CharT> bool test_uri_part(std::basic_string_view<CharT> & uri, char delim) {
  125. size_t const pos = uri.find(delim);
  126. if (pos == uri.npos) {
  127. return true;
  128. }
  129. auto part = uri.substr(pos + 1);
  130. uri = uri.substr(0, pos);
  131. for (size_t pos = 0; pos < part.size(); ++pos) {
  132. RETURN_UNLESS(detail::is_pchar(part, pos, ":@/?"), false);
  133. }
  134. return true;
  135. };
  136. }
  137. namespace jvalidate::format {
  138. inline bool date(std::string_view dt) {
  139. auto [consumed, valid] = detail::date(dt);
  140. return valid && consumed == dt.size();
  141. }
  142. inline bool time(std::string_view dt) {
  143. std::tm tm;
  144. char const * end = strptime(dt.data(), "%T", &tm);
  145. if (end == nullptr || end == dt.end() || (end - dt.data()) < 8) {
  146. return false;
  147. }
  148. dt.remove_prefix(end - dt.begin());
  149. if (dt[0] == '.') {
  150. dt.remove_prefix(1);
  151. if (dt.empty() || not std::isdigit(dt[0])) {
  152. return false;
  153. }
  154. while (std::isdigit(dt[0])) {
  155. dt.remove_prefix(1);
  156. }
  157. }
  158. if (dt[0] == 'Z' || dt[0] == 'z') {
  159. return dt.size() == 1 && detail::is_leapsecond(tm);
  160. }
  161. if (std::strchr("+-", dt[0])) {
  162. return strptime(dt.data() + 1, "%R", &tm) == dt.end() && detail::is_leapsecond(tm);
  163. }
  164. return false;
  165. }
  166. inline bool date_time(std::string_view dt) {
  167. auto [size, good] = detail::date(dt);
  168. if (not good || std::strchr("Tt", dt[size]) == nullptr) {
  169. return false;
  170. }
  171. dt.remove_prefix(size + 1);
  172. return time(dt);
  173. }
  174. template <typename CharT> inline bool uri(std::basic_string_view<CharT> uri) {
  175. using delim = detail::char_delimiters<CharT>;
  176. // https://www.rfc-editor.org/rfc/rfc3986.html#appendix-A
  177. if (size_t const pos = uri.find(':'); pos != uri.npos) {
  178. RETURN_UNLESS(std::isalpha(uri[0]), false);
  179. for (size_t i = 1; i < pos; ++i) {
  180. RETURN_UNLESS(std::isalnum(uri[i]) || std::strchr("+-.", uri[i]), false);
  181. }
  182. uri.remove_prefix(pos + 1);
  183. } else {
  184. return false;
  185. }
  186. RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
  187. RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
  188. auto path = uri;
  189. if (uri.starts_with(delim::double_slash)) {
  190. uri.remove_prefix(2);
  191. path = uri.substr(std::min(uri.size(), uri.find('/')));
  192. uri.remove_suffix(path.size());
  193. RETURN_UNLESS(detail::is_uri_authority(uri), false);
  194. }
  195. for (size_t i = 0; i < path.size(); ++i) {
  196. RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
  197. }
  198. return true;
  199. }
  200. template <typename CharT> inline bool uri_reference(std::basic_string_view<CharT> uri) {
  201. using delim = detail::char_delimiters<CharT>;
  202. if (jvalidate::format::uri(uri)) {
  203. return true;
  204. }
  205. RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
  206. RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
  207. auto path = uri;
  208. if (uri.starts_with(delim::double_slash)) {
  209. uri.remove_prefix(2);
  210. path = uri.substr(std::min(uri.size(), uri.find('/')));
  211. uri.remove_suffix(path.size());
  212. RETURN_UNLESS(detail::is_uri_authority(uri), false);
  213. }
  214. if (size_t const pos = path.find('/'); pos != path.npos) {
  215. for (size_t i = 0; i < pos; ++i) {
  216. RETURN_UNLESS(detail::is_pchar(path, i, "@"), false);
  217. }
  218. path.remove_prefix(pos);
  219. }
  220. for (size_t i = 0; i < path.size(); ++i) {
  221. RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
  222. }
  223. return true;
  224. }
  225. inline bool uuid(std::string_view id) {
  226. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  227. constexpr size_t g_uuid_len = 36;
  228. constexpr size_t g_uuid_tokens = 5;
  229. char tok0[9], tok1[5], tok2[5], tok3[5], tok4[13];
  230. auto is_hex = [](std::string_view s) {
  231. return s.find_first_not_of(g_hex_digits) == std::string::npos;
  232. };
  233. return id.size() == g_uuid_len &&
  234. sscanf(id.data(), "%8s-%4s-%4s-%4s-%12s", tok0, tok1, tok2, tok3, tok4) == g_uuid_tokens &&
  235. is_hex(tok0) && is_hex(tok1) && is_hex(tok2) && is_hex(tok3) && is_hex(tok4);
  236. }
  237. inline bool duration(std::string_view dur) {
  238. auto eat = [&dur](std::string_view text) {
  239. char type;
  240. unsigned int rep;
  241. if (sscanf(dur.data(), "%u%c", &rep, &type) != 2 || text.find(type) == std::string::npos) {
  242. return std::string::npos;
  243. }
  244. dur.remove_prefix(dur.find(type) + 1);
  245. return text.find(type);
  246. };
  247. if (dur[0] != 'P' || dur.size() == 1) {
  248. return false;
  249. }
  250. dur.remove_prefix(1);
  251. if (dur[0] != 'T') {
  252. if (eat("W") != std::string::npos) {
  253. return dur.empty();
  254. }
  255. std::string_view ymd{"YMD"};
  256. while (not ymd.empty() && not dur.empty()) {
  257. if (size_t n = eat(ymd); n != std::string::npos) {
  258. ymd.remove_prefix(n + 1);
  259. } else {
  260. return false;
  261. }
  262. }
  263. if (dur.empty()) {
  264. return true;
  265. }
  266. }
  267. if (dur[0] != 'T' || dur.size() == 1) {
  268. return false;
  269. }
  270. dur.remove_prefix(1);
  271. std::string_view hms{"HMS"};
  272. while (not hms.empty() && not dur.empty()) {
  273. if (size_t n = eat(hms); n != std::string::npos) {
  274. hms.remove_prefix(n + 1);
  275. } else {
  276. return false;
  277. }
  278. }
  279. return dur.empty();
  280. }
  281. template <typename CharT> bool is_invalid_host_char(CharT c) {
  282. return c != '-' && not(std::isalnum(c) || c > 0x7F);
  283. }
  284. template <typename CharT>
  285. bool is_invalid_size_or_boundary_hostname(std::basic_string_view<CharT> name) {
  286. using delim = detail::char_delimiters<CharT>;
  287. return (name.empty() || detail::length_u8(name) >= 64 ||
  288. (name.size() >= 4 && name.substr(2).starts_with(delim::illegal_dashes_ulabel)) ||
  289. name[0] == '-' || name.back() == '-');
  290. }
  291. #if !JVALIDATE_HAS_IDNA
  292. inline bool hostname_part(std::string_view name) {
  293. using delim = detail::char_delimiters<char>;
  294. if (is_invalid_size_or_boundary_hostname(name)) {
  295. return false;
  296. }
  297. return std::ranges::none_of(name, [](char c) { return c != '-' && not std::isalnum(c); });
  298. }
  299. #else
  300. template <typename CharT> inline bool hostname_part(std::basic_string_view<CharT> name) {
  301. using delim = detail::char_delimiters<CharT>;
  302. if (name.starts_with(delim::punycode_prefix)) {
  303. std::u32string decoded = detail::to_u32(ada::idna::to_unicode(detail::to_u8(name)));
  304. return (decoded != detail::to_u32(name)) && hostname_part<char32_t>(decoded);
  305. }
  306. if (is_invalid_size_or_boundary_hostname(name)) {
  307. return false;
  308. }
  309. if constexpr (std::is_same_v<char, CharT>) {
  310. return std::ranges::none_of(name, [](char c) { return c != '-' && not std::isalnum(c); });
  311. } else {
  312. return ada::idna::is_label_valid(name);
  313. }
  314. }
  315. #endif
  316. template <typename CharT> inline bool hostname(std::basic_string_view<CharT> name) {
  317. using delim = detail::char_delimiters<CharT>;
  318. if (name.find_first_of(delim::illegal_hostname_chars) != name.npos) {
  319. return false;
  320. }
  321. if (detail::to_u8(name).size() > (name.back() == '.' ? 254 : 253)) {
  322. return false;
  323. }
  324. if (not std::ranges::all_of(delim::special_cases,
  325. [name](auto & sc) { return sc.accepts(name); })) {
  326. return false;
  327. }
  328. for (size_t n = name.find('.'); n != std::string::npos;
  329. name.remove_prefix(n + 1), n = name.find('.')) {
  330. if (not hostname_part(name.substr(0, n))) {
  331. return false;
  332. }
  333. }
  334. return name.empty() || hostname_part(name);
  335. }
  336. inline bool ipv4(std::string_view ip) {
  337. unsigned int ip0, ip1, ip2, ip3;
  338. char eof;
  339. if (ip.find_first_not_of("0123456789.") != std::string::npos) {
  340. return false;
  341. }
  342. if (ip[0] == '0' && std::isdigit(ip[1])) {
  343. return false;
  344. }
  345. if (size_t n = ip.find(".0"); n != std::string::npos && std::isdigit(ip[n + 2])) {
  346. return false;
  347. }
  348. if (sscanf(std::string(ip).c_str(), "%3u.%3u.%3u.%3u%c", &ip0, &ip1, &ip2, &ip3, &eof) != 4) {
  349. return false;
  350. }
  351. return ip0 <= 0xFF && ip1 <= 0xFF && ip2 <= 0xFF && ip3 <= 0xFF;
  352. }
  353. inline bool ipv6(std::string_view ip) {
  354. int expected_spans = 8;
  355. if (size_t n = ip.find('.'); n != std::string::npos) {
  356. if (not ipv4(ip.substr(ip.find_last_of(':') + 1))) {
  357. return false;
  358. }
  359. // This is a cheat to allow e.g. ::127.0.0.1 to validate
  360. expected_spans = 7;
  361. ip = ip.substr(0, n);
  362. }
  363. if (ip.find_first_not_of("0123456789ABCDEFabcdef:") != std::string::npos) {
  364. return false;
  365. }
  366. if (ip.size() >= 40) {
  367. return false;
  368. }
  369. bool has_compressed = false;
  370. int groups = 0;
  371. if (ip.starts_with("::")) {
  372. has_compressed = true;
  373. ip.remove_prefix(2);
  374. }
  375. while (!ip.empty()) {
  376. int data;
  377. if (sscanf(ip.data(), "%4x", &data) != 1) {
  378. return false;
  379. }
  380. if (size_t n = ip.find(':'); std::min(n, ip.size()) > 4) {
  381. return false;
  382. } else if (n != std::string::npos) {
  383. ip.remove_prefix(n + 1);
  384. } else {
  385. ip = "";
  386. }
  387. ++groups;
  388. if (ip[0] == ':') {
  389. if (std::exchange(has_compressed, true)) {
  390. return false;
  391. }
  392. ip.remove_prefix(1);
  393. }
  394. }
  395. return groups == expected_spans || (has_compressed && groups < expected_spans);
  396. }
  397. // Let's be honest - no matter what RFC 5321 §4.1.2 or RFC 6531 say, the only
  398. // way to know if an email address is valid is to try and send a message to it.
  399. // Therefore, there's no point in trying to validate things according to a
  400. // complex grammar - as long as it has an '@' sign with at least one character
  401. // on each side, we ought to call it an email.
  402. template <typename CharT> inline bool email(std::basic_string_view<CharT> em) {
  403. using delim = detail::char_delimiters<CharT>;
  404. size_t n = em.find_last_of('@');
  405. if (n == 0 || n >= em.size() - 1) {
  406. return false;
  407. }
  408. auto const who = em.substr(0, n);
  409. if (who.starts_with('"') && who.ends_with('"')) {
  410. // No validation
  411. } else if (who.starts_with('.') || who.ends_with('.')) {
  412. return false;
  413. } else if (em.substr(0, n).find(delim::dotdot) != em.npos) {
  414. return false;
  415. }
  416. auto domain = em.substr(n + 1);
  417. if (not(domain.starts_with('[') && domain.ends_with(']'))) {
  418. return hostname(domain);
  419. }
  420. domain.remove_prefix(1);
  421. domain.remove_suffix(1);
  422. if (auto ip = detail::to_u8(domain); ip.starts_with("IPv6:")) {
  423. return ipv6(ip.substr(5));
  424. } else {
  425. return ipv4(ip);
  426. }
  427. }
  428. template <typename T> inline bool ctor_as_valid(std::string_view str) {
  429. try {
  430. [[maybe_unused]] auto _ = T(str);
  431. return true;
  432. } catch (std::exception const &) { return false; }
  433. }
  434. template <auto Predicate> bool utf32(std::string_view str) {
  435. return Predicate(detail::to_u32(str));
  436. }
  437. }
  438. namespace jvalidate {
  439. class FormatValidator {
  440. public:
  441. using Predicate = bool (*)(std::string_view);
  442. enum class Status { Unknown, Unimplemented, Valid, Invalid };
  443. private:
  444. std::unordered_map<std::string, Predicate> supported_formats_{
  445. {"date", &format::date},
  446. {"date-time", &format::date_time},
  447. {"duration", &format::duration},
  448. {"email", &format::email},
  449. {"hostname", &format::hostname},
  450. {"idn-email", UTF32(email)},
  451. {"idn-hostname", UTF32(hostname)},
  452. {"ipv4", &format::ipv4},
  453. {"ipv6", &format::ipv6},
  454. {"iri", UTF32(uri)},
  455. {"iri-reference", UTF32(uri_reference)},
  456. {"json-pointer", CONSTRUCTS(Pointer)},
  457. {"relative-json-pointer", CONSTRUCTS(RelativePointer)},
  458. {"regex", nullptr},
  459. {"time", &format::time},
  460. {"uri", &format::uri},
  461. {"uri-reference", &format::uri_reference},
  462. {"uri-template", nullptr},
  463. {"uuid", &format::uuid},
  464. };
  465. public:
  466. FormatValidator() = default;
  467. FormatValidator(Predicate is_regex) { supported_formats_.insert_or_assign("regex", is_regex); }
  468. Status operator()(std::string const & format, std::string_view text) const {
  469. if (auto it = supported_formats_.find(format); it != supported_formats_.end() && it->second) {
  470. if (not it->second) {
  471. return Status::Unimplemented;
  472. }
  473. return it->second(text) ? Status::Valid : Status::Invalid;
  474. }
  475. return Status::Unknown;
  476. }
  477. };
  478. }
  479. #undef CONSTRUCTS
  480. #undef UTF32