| 1 |
|
| /* pmcp_page.h - cursors and pages for the frozen lists. |
| 2 |
|
| * |
| 3 |
|
| * Needs: pmcp_rand.h (base64url), pmcp_decl.h. |
| 4 |
|
| * Gives: pmcp_cursor_make, pmcp_cursor_read, pmcp_page_bytes. |
| 5 |
|
| * |
| 6 |
|
| * A cursor is this server's own token: the index of the next item, as |
| 7 |
|
| * base64url of "c" and the decimal index. Opaque to the client, and a cursor |
| 8 |
|
| * this server did not issue is refused rather than guessed at. |
| 9 |
|
| * |
| 10 |
|
| * A page is spliced from the entries frozen at to_app, so the bytes of an item |
| 11 |
|
| * never change between calls. When an item has a guard, the caller's view is |
| 12 |
|
| * filtered: each guarded item runs its guard and is left out on a refusal. |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
|
| #ifndef PMCP_PAGE_H |
| 16 |
|
| #define PMCP_PAGE_H |
| 17 |
|
| |
| 18 |
33 |
| static SV *pmcp_cursor_make(pTHX_ IV index) |
| 19 |
|
| { |
| 20 |
|
| char raw[32], out[48]; |
| 21 |
33 |
| int rl = my_snprintf(raw, sizeof raw, "c%" IVdf, index); |
| 22 |
33 |
| size_t ol = pmcp_b64url((const unsigned char *)raw, (size_t)rl, out); |
| 23 |
33 |
| return newSVpvn(out, ol); |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
1643 |
| static int pmcp_b64url_val(char c) |
| 27 |
|
| { |
| 28 |
1643 |
▶ | if (c >= 'A' && c <= 'Z') return c - 'A'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') return c - 'A'; |
1441 |
202 |
1643 |
| if (c >= 'A' && c <= 'Z') return c - 'A'; |
761 |
680 |
1441 |
|
| 29 |
882 |
▶ | if (c >= 'a' && c <= 'z') return c - 'a' + 26; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'z') return c - 'a' + 26; |
667 |
215 |
882 |
| if (c >= 'a' && c <= 'z') return c - 'a' + 26; |
667 |
0 |
667 |
|
| 30 |
215 |
▶ | if (c >= '0' && c <= '9') return c - '0' + 52; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') return c - '0' + 52; |
155 |
60 |
215 |
| if (c >= '0' && c <= '9') return c - '0' + 52; |
141 |
14 |
155 |
|
| 31 |
74 |
▶ | if (c == '-') return 62; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '-') return 62; |
58 |
16 |
74 |
|
| 32 |
16 |
▶ | if (c == '_') return 63; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '_') return 63; |
13 |
3 |
16 |
|
| 33 |
3 |
| return -1; |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| /* The index a cursor names, or -1 when it is not one of ours, or past `n`. */ |
| 37 |
24 |
| static IV pmcp_cursor_read(pTHX_ SV *cursor, IV n) |
| 38 |
|
| { |
| 39 |
|
| STRLEN l, i; |
| 40 |
|
| const char *p; |
| 41 |
|
| char raw[32]; |
| 42 |
24 |
| size_t rl = 0; |
| 43 |
24 |
| U32 acc = 0; |
| 44 |
24 |
| int bits = 0; |
| 45 |
24 |
| IV idx = 0; |
| 46 |
|
| |
| 47 |
24 |
▶ | if (!cursor || SvROK(cursor) || !SvOK(cursor)) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cursor || SvROK(cursor) || !SvOK(cursor)) return -1; |
24 |
0 |
24 |
| if (!cursor || SvROK(cursor) || !SvOK(cursor)) return -1; |
24 |
0 |
24 |
| if (!cursor || SvROK(cursor) || !SvOK(cursor)) return -1; |
0 |
24 |
24 |
|
| 48 |
24 |
| p = SvPV_const(cursor, l); |
| 49 |
24 |
▶ | if (l == 0 || l > 40) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l == 0 || l > 40) return -1; |
23 |
1 |
24 |
| if (l == 0 || l > 40) return -1; |
0 |
23 |
23 |
|
| 50 |
102 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
81 |
21 |
102 |
|
| 51 |
81 |
| int v = pmcp_b64url_val(p[i]); |
| 52 |
81 |
▶ | if (v < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < 0) return -1; |
2 |
79 |
81 |
|
| 53 |
79 |
| acc = (acc << 6) | (U32)v; |
| 54 |
79 |
| bits += 6; |
| 55 |
79 |
▶ | if (bits >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits >= 8) { |
54 |
25 |
79 |
|
| 56 |
54 |
| bits -= 8; |
| 57 |
54 |
▶ | if (rl >= sizeof raw - 1) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rl >= sizeof raw - 1) return -1; |
0 |
54 |
54 |
|
| 58 |
54 |
| raw[rl++] = (char)((acc >> bits) & 0xFF); |
| 59 |
|
| } |
| 60 |
|
| } |
| 61 |
21 |
▶ | if (rl < 2 || raw[0] != 'c') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (rl < 2 || raw[0] != 'c') return -1; |
20 |
1 |
21 |
| if (rl < 2 || raw[0] != 'c') return -1; |
2 |
18 |
20 |
|
| 62 |
39 |
▶ | for (i = 1; i < rl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < rl; i++) { |
21 |
18 |
39 |
|
| 63 |
21 |
▶ | if (raw[i] < '0' || raw[i] > '9') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (raw[i] < '0' || raw[i] > '9') return -1; |
21 |
0 |
21 |
| if (raw[i] < '0' || raw[i] > '9') return -1; |
0 |
21 |
21 |
|
| 64 |
21 |
| idx = idx * 10 + (raw[i] - '0'); |
| 65 |
21 |
▶ | if (idx > n) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx > n) return -1; |
0 |
21 |
21 |
|
| 66 |
|
| } |
| 67 |
18 |
▶ | if (idx < 1 || idx >= n) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx < 1 || idx >= n) return -1; |
17 |
1 |
18 |
| if (idx < 1 || idx >= n) return -1; |
1 |
16 |
17 |
|
| 68 |
|
| /* the canonical spelling only, so one index has one cursor */ |
| 69 |
|
| { |
| 70 |
16 |
| SV *again = sv_2mortal(pmcp_cursor_make(aTHX_ idx)); |
| 71 |
16 |
▶ | if (!sv_eq(again, cursor)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_eq(again, cursor)) return -1; |
1 |
15 |
16 |
|
| 72 |
|
| } |
| 73 |
15 |
| return idx; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| /* {"nextCursor":C,"KEY":[E,...]} with the keys in canonical order, from the |
| 77 |
|
| * entries `entries[start..]`, at most `size` of them, skipping any `skip` |
| 78 |
|
| * refuses. Returns +1 bytes. `skip` may be NULL: everything is visible. */ |
| 79 |
|
| typedef int (*pmcp_visible_fn)(pTHX_ void *ctx, IV index); |
| 80 |
|
| |
| 81 |
46 |
| static SV *pmcp_page_bytes(pTHX_ const char *key, SV **entries, IV n, |
| 82 |
|
| IV start, IV size, pmcp_visible_fn visible, |
| 83 |
|
| void *ctx) |
| 84 |
|
| { |
| 85 |
46 |
| SV *items = sv_2mortal(newSVpvs("[")); |
| 86 |
46 |
| IV i, taken = 0, next = -1; |
| 87 |
|
| SV *out; |
| 88 |
|
| |
| 89 |
237 |
▶ | for (i = start; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < n; i++) { |
208 |
29 |
237 |
|
| 90 |
208 |
▶ | if (visible && !visible(aTHX_ ctx, i)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (visible && !visible(aTHX_ ctx, i)) continue; |
96 |
112 |
208 |
| if (visible && !visible(aTHX_ ctx, i)) continue; |
16 |
80 |
96 |
|
| 91 |
192 |
▶ | if (taken == size) { next = i; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (taken == size) { next = i; break; } |
17 |
175 |
192 |
|
| 92 |
175 |
▶ | if (taken) sv_catpvs(items, ","); |
Branch: 1 branch
| Branch | True | False | Total |
| if (taken) sv_catpvs(items, ","); |
130 |
45 |
175 |
|
| 93 |
175 |
| sv_catsv(items, entries[i]); |
| 94 |
175 |
| taken++; |
| 95 |
|
| } |
| 96 |
46 |
| sv_catpvs(items, "]"); |
| 97 |
|
| |
| 98 |
46 |
| out = newSVpvs("{"); |
| 99 |
46 |
▶ | if (next >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (next >= 0) { |
17 |
29 |
46 |
|
| 100 |
17 |
| SV *c = sv_2mortal(pmcp_cursor_make(aTHX_ next)); |
| 101 |
17 |
| sv_catpvs(out, "\"nextCursor\":\""); |
| 102 |
17 |
| sv_catsv(out, c); |
| 103 |
17 |
| sv_catpvs(out, "\","); |
| 104 |
|
| } |
| 105 |
46 |
| sv_catpvf(out, "\"%s\":", key); |
| 106 |
46 |
| sv_catsv(out, items); |
| 107 |
46 |
| sv_catpvs(out, "}"); |
| 108 |
46 |
| return out; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| #endif /* PMCP_PAGE_H */ |