namespace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "lsup/core.h"
  2. #include "lsup/namespace.h"
  3. /** @brief Prefix / Namespace pair.
  4. */
  5. typedef struct ns_entry_t {
  6. LSUP_ns_pfx pfx; // Namespace prefix.
  7. char * ns; // Fully qualified NS.
  8. } NSEntry;
  9. /** @brief Iterator for dumping NS map.
  10. */
  11. struct dump_iter_t {
  12. size_t i; // Iterator counter.
  13. const char *** data; // Stored data.
  14. };
  15. /**
  16. * Callbacks.
  17. */
  18. static int nsmap_comp_fn (const void *a, const void *b, void *udata)
  19. {
  20. const NSEntry *nsa = a;
  21. const NSEntry *nsb = b;
  22. return strncmp (nsa->pfx, nsb->pfx, PFX_LEN);
  23. }
  24. static uint64_t nsmap_hash_fn (
  25. const void *item, uint64_t seed0, uint64_t seed1)
  26. {
  27. const NSEntry *nse = item;
  28. return (uint64_t) LSUP_HASH64 (nse->pfx, strlen (nse->pfx), seed0);
  29. }
  30. static void nsmap_free_fn (void *item)
  31. { free (((NSEntry *) item)->ns); }
  32. // TODO expose in API as first-class iterator function.
  33. static bool nsmap_dump_ns_iter_fn (const void *item, void *udata)
  34. {
  35. const NSEntry *entry = item;
  36. struct dump_iter_t *cur = udata;
  37. //LOG_TRACE ("Dumping NS to string: %s %s", entry->pfx, entry->ns);
  38. cur->data[cur->i][0] = (const char *)entry->pfx;
  39. cur->data[cur->i++][1] = (const char *)entry->ns;
  40. return true;
  41. }
  42. /**
  43. * API.
  44. */
  45. LSUP_NSMap *
  46. LSUP_nsmap_new (void)
  47. {
  48. return hashmap_new (
  49. sizeof (NSEntry), 0, LSUP_HASH_SEED, 0,
  50. nsmap_hash_fn, nsmap_comp_fn, nsmap_free_fn, NULL);
  51. }
  52. void
  53. LSUP_nsmap_free (LSUP_NSMap *map)
  54. { hashmap_free (map); }
  55. LSUP_rc
  56. LSUP_nsmap_add (LSUP_NSMap *map, const char *pfx, const char *nsstr)
  57. {
  58. NSEntry entry_s = {};
  59. if (strlen(pfx) >= PFX_LEN)
  60. log_warn(
  61. "Prefix `%s` is longer than the maximum allowed size "
  62. "(%d characters). Truncating.", pfx, PFX_LEN - 1);
  63. strncpy (entry_s.pfx, pfx, PFX_LEN -1);
  64. entry_s.ns = strdup (nsstr);
  65. const NSEntry *ret = hashmap_delete (map, &entry_s);
  66. if (!ret) LOG_DEBUG("Adding prefix '%s' to NS map @%p.", entry_s.pfx, map);
  67. else {
  68. LOG_DEBUG(
  69. "Replacing NS '%s' with '%s' for prefix '%s'.",
  70. ret->ns, entry_s.ns, entry_s.pfx);
  71. // Free replaced NS string.
  72. free (ret->ns);
  73. }
  74. hashmap_set (map, &entry_s);
  75. if (hashmap_oom (map)) return LSUP_MEM_ERR;
  76. return LSUP_OK;
  77. }
  78. LSUP_rc
  79. LSUP_nsmap_remove (LSUP_NSMap *map, const char *pfx)
  80. {
  81. NSEntry entry_s = {};
  82. strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
  83. const NSEntry *entry = hashmap_delete (map, &entry_s);
  84. if (!entry) return LSUP_NOACTION;
  85. free (entry->ns);
  86. return LSUP_OK;
  87. }
  88. const char *
  89. LSUP_nsmap_get_ns (const LSUP_NSMap *map, const char *pfx)
  90. {
  91. NSEntry entry_s = {};
  92. strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
  93. const NSEntry *entry = hashmap_get ((LSUP_NSMap *)map, &entry_s);
  94. return (entry) ? entry->ns : NULL;
  95. }
  96. const char *
  97. LSUP_nsmap_get_pfx (const LSUP_NSMap *map, const char *ns)
  98. {
  99. const NSEntry *entry;
  100. size_t i = 0;
  101. while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
  102. if (strncmp (entry->ns, ns, strlen (ns)) == 0)
  103. return entry->pfx;
  104. }
  105. return NULL;
  106. }
  107. LSUP_rc
  108. LSUP_nsmap_normalize_uri (
  109. const LSUP_NSMap *map, const char *pfx_uri, char **fq_uri_p)
  110. {
  111. LSUP_rc rc;
  112. char *fq_uri = NULL;
  113. size_t pfx_len = strcspn (pfx_uri, ":");
  114. if (pfx_len >= PFX_LEN || pfx_len == strlen (pfx_uri)) {
  115. log_warn (
  116. "No prefix separator detected in URI `%s` within maximum "
  117. "prefix length (%d characters).", pfx_uri, PFX_LEN - 1);
  118. goto no_prefix;
  119. }
  120. LSUP_ns_pfx pfx;
  121. strncpy (pfx, pfx_uri, pfx_len);
  122. pfx[pfx_len] = '\0';
  123. const char *ns = LSUP_nsmap_get_ns ((LSUP_NSMap *)map, pfx);
  124. if (ns) {
  125. // -1 for :, +1 for terminator.
  126. size_t fq_size = strlen (ns) + strlen (pfx_uri) - pfx_len;
  127. fq_uri = malloc (fq_size);
  128. if (UNLIKELY (! (fq_uri))) return LSUP_MEM_ERR;
  129. strcpy (fq_uri, ns);
  130. strcat (fq_uri, pfx_uri + pfx_len + 1);
  131. rc = LSUP_OK;
  132. } else {
  133. log_warn ("No NS prefix found in map to normalize %s", pfx_uri);
  134. no_prefix:
  135. fq_uri = malloc (strlen(pfx_uri) + 1);
  136. if (UNLIKELY (! (fq_uri))) return LSUP_MEM_ERR;
  137. strcpy (fq_uri, pfx_uri);
  138. rc = LSUP_NORESULT;
  139. }
  140. *fq_uri_p = fq_uri;
  141. return rc;
  142. }
  143. LSUP_rc
  144. LSUP_nsmap_denormalize_uri (
  145. const LSUP_NSMap *map, const char *fq_uri, char **pfx_uri_p)
  146. {
  147. /*
  148. * This is different from LSUP_nsmap_get_ns, in that the URI being looked
  149. * at will unlikely match exactly the full namespace stored in the map.
  150. * This function has to count the characters left over from the match in
  151. * order to add the URI suffix.
  152. */
  153. LSUP_rc rc;
  154. const NSEntry *entry;
  155. const char *pfx = NULL;
  156. size_t i = 0, offset;
  157. while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
  158. offset = strlen (entry->ns);
  159. if (strncmp (entry->ns, fq_uri, offset) == 0) {
  160. pfx = entry->pfx;
  161. break;
  162. }
  163. }
  164. char *pfx_uri = NULL;
  165. if (pfx) {
  166. // +2: one for terminating \x00, one for the colon.
  167. pfx_uri = malloc (strlen (pfx) + strlen (fq_uri) - offset + 2);
  168. if (UNLIKELY (! (pfx_uri))) return LSUP_MEM_ERR;
  169. sprintf (pfx_uri, "%s:%s", pfx, fq_uri + offset);
  170. rc = LSUP_OK;
  171. } else {
  172. pfx_uri = strdup (fq_uri);
  173. rc = LSUP_NORESULT;
  174. }
  175. *pfx_uri_p = pfx_uri;
  176. return rc;
  177. }
  178. const char ***
  179. LSUP_nsmap_dump (const LSUP_NSMap *map)
  180. {
  181. size_t i = hashmap_count ((LSUP_NSMap *) map);
  182. const char ***data = malloc (2 * (i + 1) * sizeof (char *));
  183. if (UNLIKELY (!data)) return NULL;
  184. for (size_t j = 0; j < i; j++) {
  185. data[j] = malloc (2 * sizeof (char *));
  186. if (UNLIKELY (!data[j])) return NULL;
  187. }
  188. struct dump_iter_t cur = {.i = 0, .data = data};
  189. hashmap_scan ((LSUP_NSMap *) map, nsmap_dump_ns_iter_fn, &cur);
  190. data[i] = NULL; // Sentinel
  191. return data;
  192. }