Build a path alias map programmatically (no file I/O), respecting the * specificity ordering invariant the loader establishes via qsort. */
| 20 | /* Build a path alias map programmatically (no file I/O), respecting the |
| 21 | * specificity ordering invariant the loader establishes via qsort. */ |
| 22 | static cbm_path_alias_map_t *make_map(const char *base_url, int count, ...) { |
| 23 | cbm_path_alias_map_t *map = calloc(1, sizeof(*map)); |
| 24 | map->base_url = base_url ? strdup(base_url) : NULL; |
| 25 | map->entries = calloc((size_t)count, sizeof(cbm_path_alias_t)); |
| 26 | map->count = count; |
| 27 | |
| 28 | va_list args; |
| 29 | va_start(args, count); |
| 30 | for (int i = 0; i < count; i++) { |
| 31 | const char *alias_pattern = va_arg(args, const char *); |
| 32 | const char *target_pattern = va_arg(args, const char *); |
| 33 | const char *star = strchr(alias_pattern, '*'); |
| 34 | if (star) { |
| 35 | map->entries[i].has_wildcard = true; |
| 36 | map->entries[i].alias_prefix = |
| 37 | cbm_strndup(alias_pattern, (size_t)(star - alias_pattern)); |
| 38 | map->entries[i].alias_suffix = strdup(star + 1); |
| 39 | } else { |
| 40 | map->entries[i].has_wildcard = false; |
| 41 | map->entries[i].alias_prefix = strdup(alias_pattern); |
| 42 | map->entries[i].alias_suffix = strdup(""); |
| 43 | } |
| 44 | const char *tstar = strchr(target_pattern, '*'); |
| 45 | if (tstar) { |
| 46 | map->entries[i].target_prefix = |
| 47 | cbm_strndup(target_pattern, (size_t)(tstar - target_pattern)); |
| 48 | map->entries[i].target_suffix = strdup(tstar + 1); |
| 49 | } else { |
| 50 | map->entries[i].target_prefix = strdup(target_pattern); |
| 51 | map->entries[i].target_suffix = strdup(""); |
| 52 | } |
| 53 | } |
| 54 | va_end(args); |
| 55 | |
| 56 | /* Mimic the loader's specificity sort. */ |
| 57 | for (int i = 0; i < count - 1; i++) { |
| 58 | for (int j = i + 1; j < count; j++) { |
| 59 | size_t li = strlen(map->entries[i].alias_prefix); |
| 60 | size_t lj = strlen(map->entries[j].alias_prefix); |
| 61 | if (lj > li) { |
| 62 | cbm_path_alias_t tmp = map->entries[i]; |
| 63 | map->entries[i] = map->entries[j]; |
| 64 | map->entries[j] = tmp; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return map; |
| 69 | } |
| 70 | |
| 71 | /* The map produced by make_map() owns all heap memory the same way the |
| 72 | * loader does, so the public free routine on the wrapping collection |
no test coverage detected