namespace.c 5.4 KB

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