namespace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "core.h"
  2. #include "namespace.h"
  3. /** @brief Prefix / Namespace pair.
  4. */
  5. typedef struct ns_entry_t {
  6. 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, PFX_LEN, seed0);
  30. }
  31. static void nsmap_free_fn (void *item)
  32. { free (((NSEntry *) item)->ns); }
  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. cur->data[cur->i][0] = (const char *)entry->pfx;
  38. cur->data[cur->i++][1] = (const char *)entry->ns;
  39. return true;
  40. }
  41. /**
  42. * API.
  43. */
  44. NSMap *
  45. LSUP_nsmap_new (void)
  46. {
  47. return hashmap_new (
  48. sizeof (NSEntry), 0, LSUP_HASH_SEED, 0,
  49. nsmap_hash_fn, nsmap_comp_fn, nsmap_free_fn, NULL);
  50. }
  51. void
  52. LSUP_nsmap_free (NSMap *map)
  53. { hashmap_free (map); }
  54. LSUP_rc
  55. LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
  56. {
  57. NSEntry entry_s;
  58. if (strlen(pfx) >= PFX_LEN)
  59. log_warn(
  60. "Prefix `%s` is longer than the maximum allowed size "
  61. "(%d characters). Truncating.", pfx, PFX_LEN - 1);
  62. strncpy (entry_s.pfx, pfx, sizeof (entry_s.pfx));
  63. char *ns = strdup (nsstr);
  64. entry_s.ns = ns;
  65. NSEntry *ret = hashmap_set (map, &entry_s);
  66. if (hashmap_oom (map)) return LSUP_MEM_ERR;
  67. // Free replaced NS string.
  68. if (ret) {
  69. free (ret->ns);
  70. free (ret);
  71. }
  72. return LSUP_OK;
  73. }
  74. LSUP_rc
  75. LSUP_nsmap_remove (NSMap *map, const char *pfx)
  76. {
  77. NSEntry entry_s;
  78. strncpy (entry_s.pfx, pfx, PFX_LEN);
  79. NSEntry *entry = hashmap_delete (map, &entry_s);
  80. if (!entry) return LSUP_NOACTION;
  81. free (entry->ns);
  82. return LSUP_OK;
  83. }
  84. const char *
  85. LSUP_nsmap_get_ns (NSMap *map, const char *pfx)
  86. {
  87. NSEntry entry_s;
  88. strncpy (entry_s.pfx, pfx, PFX_LEN);
  89. NSEntry *entry = hashmap_get (map, &entry_s);
  90. return (entry) ? entry->ns : NULL;
  91. }
  92. const char *
  93. LSUP_nsmap_get_pfx (NSMap *map, const char *ns)
  94. {
  95. const NSEntry *entry;
  96. size_t i = 0;
  97. while (hashmap_iter (map, &i, (void **) &entry)) {
  98. if (strncmp (entry->ns, ns, strlen (ns)) == 0)
  99. return entry->pfx;
  100. }
  101. return NULL;
  102. }
  103. LSUP_rc
  104. LSUP_nsmap_normalize_uri (
  105. NSMap *map, const char *pfx_uri, char **fq_uri_p)
  106. {
  107. char *fq_uri = NULL;
  108. size_t pfx_len = strcspn (pfx_uri, ":");
  109. if (pfx_len >= PFX_LEN) {
  110. log_warn(
  111. "Prefix in `%s` is longer than the maximum allowed size "
  112. "(%d characters). Truncating.", pfx_uri, PFX_LEN - 1);
  113. pfx_len = PFX_LEN - 1;
  114. }
  115. ns_pfx pfx;
  116. strncpy (pfx, pfx_uri, pfx_len);
  117. pfx[pfx_len] = 0;
  118. /*
  119. Namespace *entry;
  120. for (entry = map; entry != NULL; entry = entry->hh.next) {
  121. if (strncmp (entry->pfx, pfx_uri, strlen (entry->pfx)) == 0)
  122. break;
  123. }
  124. */
  125. const char *ns = LSUP_nsmap_get_ns (map, pfx);
  126. if (ns) {
  127. // -1 for :, +1 for terminator.
  128. size_t fq_size = strlen (ns) + strlen (pfx_uri) - pfx_len;
  129. fq_uri = malloc (fq_size);
  130. if (UNLIKELY (! (fq_uri))) return LSUP_MEM_ERR;
  131. strcpy (fq_uri, ns);
  132. strcat (fq_uri, pfx_uri + pfx_len + 1);
  133. }
  134. else fq_uri = strdup (pfx_uri);
  135. *fq_uri_p = fq_uri;
  136. return LSUP_OK;
  137. }
  138. LSUP_rc
  139. LSUP_nsmap_denormalize_uri (
  140. NSMap *map, const char *fq_uri, char **pfx_uri_p)
  141. {
  142. /*
  143. * This is different from LSUP_nsmap_get_ns, in that the URI being looked
  144. * at will unlikely match exactly the full namespace stored in the map.
  145. * This function has to count the characters left over from the match in
  146. * order to add the URI suffix.
  147. */
  148. const NSEntry *entry;
  149. const char *pfx = NULL;
  150. size_t i = 0, offset;
  151. while (hashmap_iter (map, &i, (void **) &entry)) {
  152. offset = strlen(entry->ns);
  153. if (strncmp (entry->ns, fq_uri, offset) == 0) {
  154. pfx = entry->pfx;
  155. break;
  156. }
  157. }
  158. char *pfx_uri = NULL;
  159. if (pfx) {
  160. // +2: one for terminating \x00, one for the colon.
  161. pfx_uri = malloc (strlen (pfx) + strlen (fq_uri) - offset + 2);
  162. if (UNLIKELY (! (pfx_uri))) return LSUP_MEM_ERR;
  163. sprintf (pfx_uri, "%s:%s", pfx, fq_uri + offset);
  164. }
  165. else pfx_uri = strdup (fq_uri);
  166. *pfx_uri_p = pfx_uri;
  167. return LSUP_OK;
  168. }
  169. const char ***
  170. LSUP_nsmap_dump (const NSMap *map)
  171. {
  172. size_t i = hashmap_count ((NSMap *) map);
  173. const char ***data = malloc (2 * (i + 1) * sizeof (char *));
  174. if (UNLIKELY (!data)) return NULL;
  175. for (size_t j = 0; j < i; j++) {
  176. data[j] = malloc (2 * sizeof (char *));
  177. if (UNLIKELY (!data[j])) return NULL;
  178. }
  179. struct dump_iter_t cur = {.i = 0, .data = data};
  180. hashmap_scan ((NSMap *) map, nsmap_dump_ns_iter_fn, &cur);
  181. data[i] = NULL; // Sentinel
  182. return data;
  183. }