format.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #pragma once
  2. #include <cctype>
  3. #include <cstddef>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <string>
  7. #include <string_view>
  8. #include <unordered_map>
  9. #include <utility>
  10. #if __has_include(<ada/idna/to_unicode.h>)
  11. #define JVALIDATE_HAS_IDNA
  12. #include <ada/idna/to_unicode.h>
  13. #include <ada/idna/validity.h>
  14. #endif
  15. #include <jvalidate/detail/idna_special_cases.h>
  16. #include <jvalidate/detail/pointer.h>
  17. #include <jvalidate/detail/relative_pointer.h>
  18. #include <jvalidate/detail/string.h>
  19. #include <jvalidate/forward.h>
  20. #define CONSTRUCTS(TYPE) format::ctor_as_valid<detail::TYPE>
  21. #define UTF32(FN) format::utf32<format::FN<char32_t>>
  22. namespace jvalidate::format::detail {
  23. struct result {
  24. ptrdiff_t consumed;
  25. bool valid;
  26. };
  27. inline bool is_leapyear(int y) { return (y % 400) == 0 || ((y % 4) == 0 && (y % 100) != 0); }
  28. inline bool illegal_date(int y, int m, int d) {
  29. static constexpr int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  30. if (is_leapyear(y) && m == 1) {
  31. --d;
  32. }
  33. return d > days[m];
  34. }
  35. inline result date(std::string_view dt) {
  36. struct tm tm;
  37. if (auto end = strptime(dt.data(), "%Y-%m-%d", &tm); end) {
  38. if ((end - dt.data()) != 10 || illegal_date(tm.tm_year + 1900, tm.tm_mon, tm.tm_mday)) {
  39. return {.consumed = 0, .valid = false};
  40. }
  41. return {.consumed = end - dt.data(), .valid = true};
  42. }
  43. return {.consumed = 0L, .valid = false};
  44. }
  45. }
  46. namespace jvalidate::format {
  47. inline bool date(std::string_view dt) {
  48. auto [consumed, valid] = detail::date(dt);
  49. return valid && consumed == dt.size();
  50. }
  51. inline bool time(std::string_view dt) {
  52. struct tm tm;
  53. char const * end = strptime(dt.data(), "%T", &tm);
  54. if (end == nullptr || end == dt.end() || (end - dt.data()) < 8) {
  55. return false;
  56. }
  57. dt.remove_prefix(end - dt.begin());
  58. if (dt[0] == '.') {
  59. dt.remove_prefix(1);
  60. if (dt.empty() || not std::isdigit(dt[0])) {
  61. return false;
  62. }
  63. while (std::isdigit(dt[0])) {
  64. dt.remove_prefix(1);
  65. }
  66. }
  67. if (dt[0] == 'Z' || dt[0] == 'z') {
  68. return dt.size() == 1;
  69. }
  70. if (std::strchr("+-", dt[0])) {
  71. return strptime(dt.data() + 1, "%R", &tm) == dt.end();
  72. }
  73. return false;
  74. }
  75. inline bool date_time(std::string_view dt) {
  76. auto [size, good] = detail::date(dt);
  77. if (not good || std::strchr("Tt", dt[size]) == nullptr) {
  78. return false;
  79. }
  80. dt.remove_prefix(size + 1);
  81. return time(dt);
  82. }
  83. inline bool uuid(std::string_view id) {
  84. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  85. constexpr size_t g_uuid_len = 36;
  86. constexpr size_t g_uuid_tokens = 5;
  87. char tok0[9], tok1[5], tok2[5], tok3[5], tok4[13];
  88. auto is_hex = [](std::string_view s) {
  89. return s.find_first_not_of(g_hex_digits) == std::string::npos;
  90. };
  91. return id.size() == g_uuid_len &&
  92. sscanf(id.data(), "%8s-%4s-%4s-%4s-%12s", tok0, tok1, tok2, tok3, tok4) == g_uuid_tokens &&
  93. is_hex(tok0) && is_hex(tok1) && is_hex(tok2) && is_hex(tok3) && is_hex(tok4);
  94. }
  95. inline bool duration(std::string_view dur) {
  96. auto eat = [&dur](std::string_view text) {
  97. char type;
  98. unsigned int rep;
  99. if (sscanf(dur.data(), "%u%c", &rep, &type) != 2 || text.find(type) == std::string::npos) {
  100. return std::string::npos;
  101. }
  102. dur.remove_prefix(dur.find(type) + 1);
  103. return text.find(type);
  104. };
  105. if (dur[0] != 'P' || dur.size() == 1) {
  106. return false;
  107. }
  108. dur.remove_prefix(1);
  109. if (dur[0] != 'T') {
  110. if (eat("W") != std::string::npos) {
  111. return dur.empty();
  112. }
  113. std::string_view ymd{"YMD"};
  114. while (not ymd.empty() && not dur.empty()) {
  115. if (size_t n = eat(ymd); n != std::string::npos) {
  116. ymd.remove_prefix(n + 1);
  117. } else {
  118. return false;
  119. }
  120. }
  121. if (dur.empty()) {
  122. return true;
  123. }
  124. }
  125. if (dur[0] != 'T' || dur.size() == 1) {
  126. return false;
  127. }
  128. dur.remove_prefix(1);
  129. std::string_view hms{"HMS"};
  130. while (not hms.empty() && not dur.empty()) {
  131. if (size_t n = eat(hms); n != std::string::npos) {
  132. hms.remove_prefix(n + 1);
  133. } else {
  134. return false;
  135. }
  136. }
  137. return dur.empty();
  138. }
  139. template <typename CharT> bool is_invalid_host_char(CharT c) {
  140. return c != '-' && not(std::isalnum(c) || c > 0x7F);
  141. }
  142. template <typename CharT>
  143. bool is_invalid_size_or_boundary_hostname(std::basic_string_view<CharT> name) {
  144. using delim = detail::char_delimiters<CharT>;
  145. return (name.empty() || detail::to_u8(name).size() >= 64 ||
  146. (name.size() >= 4 && name.substr(2).starts_with(delim::illegal_dashes_ulabel)) ||
  147. name[0] == '-' || name.back() == '-');
  148. }
  149. #ifndef JVALIDATE_HAS_IDNA
  150. inline bool hostname_part(std::string_view name) {
  151. using delim = detail::char_delimiters<char>;
  152. if (is_invalid_size_or_boundary_hostname(name)) {
  153. return false;
  154. }
  155. return std::ranges::none_of(name, [](char c) { return c != '-' && not std::isalnum(c); });
  156. }
  157. #else
  158. template <typename CharT> inline bool hostname_part(std::basic_string_view<CharT> name) {
  159. using delim = detail::char_delimiters<CharT>;
  160. if (name.starts_with(delim::punycode_prefix)) {
  161. std::u32string decoded = detail::to_u32(ada::idna::to_unicode(detail::to_u8(name)));
  162. return (decoded != detail::to_u32(name)) && hostname_part<char32_t>(decoded);
  163. }
  164. if (is_invalid_size_or_boundary_hostname(name)) {
  165. return false;
  166. }
  167. if constexpr (std::is_same_v<char, CharT>) {
  168. return std::ranges::none_of(name, [](char c) { return c != '-' && not std::isalnum(c); });
  169. } else {
  170. return ada::idna::is_label_valid(name);
  171. }
  172. }
  173. #endif
  174. // Limitation - does not inspect graphemes, so it cannot check idn-hostname
  175. // to fix this - we'd need to
  176. template <typename CharT = char> inline bool hostname(std::basic_string_view<CharT> name) {
  177. using delim = detail::char_delimiters<CharT>;
  178. if (name.find_first_of(delim::illegal_hostname_chars) != name.npos) {
  179. return false;
  180. }
  181. if (detail::to_u8(name).size() > (name.back() == '.' ? 254 : 253)) {
  182. return false;
  183. }
  184. if (not std::ranges::all_of(delim::special_cases,
  185. [name](auto & sc) { return sc.accepts(name); })) {
  186. return false;
  187. }
  188. for (size_t n = name.find('.'); n != std::string::npos;
  189. name.remove_prefix(n + 1), n = name.find('.')) {
  190. if (not hostname_part(name.substr(0, n))) {
  191. return false;
  192. }
  193. }
  194. return name.empty() || hostname_part(name);
  195. }
  196. inline bool ipv4(std::string_view ip) {
  197. unsigned int ip0, ip1, ip2, ip3;
  198. char eof;
  199. if (ip.find_first_not_of("0123456789.") != std::string::npos) {
  200. return false;
  201. }
  202. if (ip[0] == '0' && std::isdigit(ip[1])) {
  203. return false;
  204. }
  205. if (size_t n = ip.find(".0"); n != std::string::npos && std::isdigit(ip[n + 2])) {
  206. return false;
  207. }
  208. if (sscanf(std::string(ip).c_str(), "%3u.%3u.%3u.%3u%c", &ip0, &ip1, &ip2, &ip3, &eof) != 4) {
  209. return false;
  210. }
  211. return ip0 <= 0xFF && ip1 <= 0xFF && ip2 <= 0xFF && ip3 <= 0xFF;
  212. }
  213. inline bool ipv6(std::string_view ip) {
  214. int expected_spans = 8;
  215. if (size_t n = ip.find('.'); n != std::string::npos) {
  216. if (not ipv4(ip.substr(ip.find_last_of(':') + 1))) {
  217. return false;
  218. }
  219. // This is a cheat to allow e.g. ::127.0.0.1 to validate
  220. expected_spans = 7;
  221. ip = ip.substr(0, n);
  222. }
  223. if (ip.find_first_not_of("0123456789ABCDEFabcdef:") != std::string::npos) {
  224. return false;
  225. }
  226. if (ip.size() >= 40) {
  227. return false;
  228. }
  229. bool has_compressed = false;
  230. int groups = 0;
  231. if (ip.starts_with("::")) {
  232. has_compressed = true;
  233. ip.remove_prefix(2);
  234. }
  235. while (!ip.empty()) {
  236. int data;
  237. if (sscanf(ip.data(), "%4x", &data) != 1) {
  238. return false;
  239. }
  240. if (size_t n = ip.find(':'); std::min(n, ip.size()) > 4) {
  241. return false;
  242. } else if (n != std::string::npos) {
  243. ip.remove_prefix(n + 1);
  244. } else {
  245. ip = "";
  246. }
  247. ++groups;
  248. if (ip[0] == ':') {
  249. if (std::exchange(has_compressed, true)) {
  250. return false;
  251. }
  252. ip.remove_prefix(1);
  253. }
  254. }
  255. return groups == expected_spans || (has_compressed && groups < expected_spans);
  256. }
  257. // Let's be honest - no matter what RFC 5321 §4.1.2 or RFC 6531 say, the only
  258. // way to know if an email address is valid is to try and send a message to it.
  259. // Therefore, there's no point in trying to validate things according to a
  260. // complex grammar - as long as it has an '@' sign with at least one character
  261. // on each side, we ought to call it an email.
  262. template <typename CharT = char> inline bool email(std::basic_string_view<CharT> em) {
  263. using delim = detail::char_delimiters<CharT>;
  264. size_t n = em.find_last_of('@');
  265. if (n == 0 || n >= em.size() - 1) {
  266. return false;
  267. }
  268. auto const who = em.substr(0, n);
  269. if (who.starts_with('"') && who.ends_with('"')) {
  270. // No validation
  271. } else if (who.starts_with('.') || who.ends_with('.')) {
  272. return false;
  273. } else if (em.substr(0, n).find(delim::dotdot) != em.npos) {
  274. return false;
  275. }
  276. auto domain = em.substr(n + 1);
  277. if (not(domain.starts_with('[') && domain.ends_with(']'))) {
  278. return hostname(domain);
  279. }
  280. domain.remove_prefix(1);
  281. domain.remove_suffix(1);
  282. if (auto ip = detail::to_u8(domain); ip.starts_with("IPv6:")) {
  283. return ipv6(ip.substr(5));
  284. } else {
  285. return ipv4(ip);
  286. }
  287. }
  288. template <typename T> inline bool ctor_as_valid(std::string_view str) {
  289. try {
  290. [[maybe_unused]] auto _ = T(str);
  291. return true;
  292. } catch (std::exception const &) { return false; }
  293. }
  294. template <auto Predicate> bool utf32(std::string_view str) {
  295. return Predicate(detail::to_u32(str));
  296. }
  297. }
  298. namespace jvalidate {
  299. class FormatValidator {
  300. public:
  301. using Predicate = bool (*)(std::string_view);
  302. enum class Status { Unknown, Unimplemented, Valid, Invalid };
  303. private:
  304. std::unordered_map<std::string, Predicate> supported_formats_{
  305. {"date", &format::date},
  306. {"date-time", &format::date_time},
  307. {"duration", &format::duration},
  308. {"email", &format::email},
  309. {"hostname", &format::hostname},
  310. {"idn-email", UTF32(email)},
  311. {"idn-hostname", UTF32(hostname)},
  312. {"ipv4", &format::ipv4},
  313. {"ipv6", &format::ipv6},
  314. {"iri", nullptr},
  315. {"iri-reference", nullptr},
  316. {"json-pointer", CONSTRUCTS(Pointer)},
  317. {"relative-json-pointer", CONSTRUCTS(RelativePointer)},
  318. {"regex", nullptr},
  319. {"time", &format::time},
  320. {"uri", nullptr},
  321. {"uri-reference", nullptr},
  322. {"uri-template", nullptr},
  323. {"uuid", &format::uuid},
  324. };
  325. public:
  326. FormatValidator() = default;
  327. FormatValidator(Predicate is_regex) { supported_formats_.insert_or_assign("regex", is_regex); }
  328. Status operator()(std::string const & format, std::string_view text) const {
  329. if (auto it = supported_formats_.find(format); it != supported_formats_.end() && it->second) {
  330. if (not it->second) {
  331. return Status::Unimplemented;
  332. }
  333. return it->second(text) ? Status::Valid : Status::Invalid;
  334. }
  335. return Status::Unknown;
  336. }
  337. };
  338. }
  339. #undef CONSTRUCTS
  340. #undef UTF32