Decode and validate every token field that does not require executing the * query. The materialization digest itself is checked immediately after the * full query has been re-executed. */
| 5735 | * query. The materialization digest itself is checked immediately after the |
| 5736 | * full query has been re-executed. */ |
| 5737 | static const char *query_graph_cursor_decode(const char *token, const char *current_generation, |
| 5738 | uint64_t expected_params_hash, |
| 5739 | query_graph_cursor_t *out) { |
| 5740 | static const char invalid[] = |
| 5741 | "invalid_cursor: unrecognized or modified token — re-run the original query without " |
| 5742 | "'cursor'"; |
| 5743 | memset(out, 0, sizeof(*out)); |
| 5744 | if (!token || strncmp(token, "q1.", 3) != 0) { |
| 5745 | return invalid; |
| 5746 | } |
| 5747 | const char *generation_start = token + 3; |
| 5748 | const char *generation_end = strchr(generation_start, '.'); |
| 5749 | if (!generation_end || generation_end == generation_start || |
| 5750 | (size_t)(generation_end - generation_start) >= sizeof(out->generation)) { |
| 5751 | return invalid; |
| 5752 | } |
| 5753 | memcpy(out->generation, generation_start, (size_t)(generation_end - generation_start)); |
| 5754 | out->generation[generation_end - generation_start] = '\0'; |
| 5755 | |
| 5756 | const char *hash_start = generation_end + 1; |
| 5757 | const char *hash_end = strchr(hash_start, '.'); |
| 5758 | if (!query_graph_hex_span(hash_start, hash_end, 16)) { |
| 5759 | return invalid; |
| 5760 | } |
| 5761 | errno = 0; |
| 5762 | char *parsed_end = NULL; |
| 5763 | unsigned long long parsed_hash = strtoull(hash_start, &parsed_end, 16); |
| 5764 | if (errno == ERANGE || parsed_end != hash_end) { |
| 5765 | return invalid; |
| 5766 | } |
| 5767 | out->params_hash = (uint64_t)parsed_hash; |
| 5768 | |
| 5769 | const char *digest_start = hash_end + 1; |
| 5770 | const char *digest_end = strchr(digest_start, '.'); |
| 5771 | if (!query_graph_hex_span(digest_start, digest_end, QUERY_GRAPH_CURSOR_DIGEST_HEX_LEN)) { |
| 5772 | return invalid; |
| 5773 | } |
| 5774 | memcpy(out->result_digest, digest_start, QUERY_GRAPH_CURSOR_DIGEST_HEX_LEN); |
| 5775 | out->result_digest[QUERY_GRAPH_CURSOR_DIGEST_HEX_LEN] = '\0'; |
| 5776 | |
| 5777 | const char *offset_start = digest_end + 1; |
| 5778 | const char *offset_end = strchr(offset_start, '.'); |
| 5779 | errno = 0; |
| 5780 | long parsed_offset = offset_end ? strtol(offset_start, &parsed_end, 10) : -1; |
| 5781 | if (!offset_end || offset_end == offset_start || errno == ERANGE || parsed_end != offset_end || |
| 5782 | parsed_offset <= 0 || parsed_offset > INT_MAX) { |
| 5783 | return invalid; |
| 5784 | } |
| 5785 | out->offset = (int)parsed_offset; |
| 5786 | |
| 5787 | const char *seal_start = offset_end + 1; |
| 5788 | const char *seal_end = token + strlen(token); |
| 5789 | if (!query_graph_hex_span(seal_start, seal_end, 16)) { |
| 5790 | return invalid; |
| 5791 | } |
| 5792 | char expected_seal[17]; |
| 5793 | query_graph_cursor_seal(out, expected_seal); |
| 5794 | if (memcmp(seal_start, expected_seal, 16) != 0) { |
no test coverage detected