namespace.c 6.0 KB

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