symbols.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * Description (symbols.c)
  43. *
  44. * This module implements a symbol table for ACL-related structures.
  45. * The symbol table associates string names and types with pointers
  46. * to various kinds of structures.
  47. */
  48. /*
  49. #include <base/systems.h>
  50. */
  51. #include <plhash.h>
  52. #include <base/util.h>
  53. #include <netsite.h>
  54. #define __PRIVATE_SYMBOLS
  55. #include "libaccess/symbols.h"
  56. #include <ctype.h>
  57. static PLHashEntry * symAllocEntry(void * pool, const void *unused);
  58. static void * symAllocTable(void * pool, PRSize size);
  59. static int symCmpName(const void * name1, const void * name2);
  60. static int symCmpValue(const void * value1, const void * value2);
  61. static PLHashNumber symHash(const void * symkey);
  62. static void symFreeEntry(void * pool, PLHashEntry * he, PRUintn flag);
  63. static void symFreeTable(void * pool, void * item);
  64. /* Table of pointers to functions associated with the hash table */
  65. static PLHashAllocOps SymAllocOps = {
  66. symAllocTable, /* allocate the hash table */
  67. symFreeTable, /* free the hash table */
  68. symAllocEntry, /* allocate a table entry */
  69. symFreeEntry, /* free a table entry */
  70. };
  71. static void * symAllocTable(void * pool, PRSize size)
  72. {
  73. return (void *)PERM_MALLOC(size);
  74. }
  75. static void symFreeTable(void * pool, void * item)
  76. {
  77. PERM_FREE(item);
  78. }
  79. static PLHashEntry * symAllocEntry(void * pool, const void *ignored)
  80. {
  81. PLHashEntry * he;
  82. he = (PLHashEntry *) PERM_MALLOC(sizeof(PLHashEntry));
  83. return he;
  84. }
  85. static void symFreeEntry(void * pool, PLHashEntry * he, PRUintn flag)
  86. {
  87. if (flag == HT_FREE_ENTRY) {
  88. /* Just free the hash entry, not anything it references */
  89. PERM_FREE(he);
  90. }
  91. }
  92. static int symCmpName(const void * name1, const void * name2)
  93. {
  94. Symbol_t * sym1 = (Symbol_t *)name1;
  95. Symbol_t * sym2 = (Symbol_t *)name2;
  96. return ((sym1->sym_type == sym2->sym_type) &&
  97. !strcasecmp(sym1->sym_name, sym2->sym_name));
  98. }
  99. static int symCmpValue(const void * value1, const void * value2)
  100. {
  101. return (value1 == value2);
  102. }
  103. static PLHashNumber symHash(const void * symkey)
  104. {
  105. Symbol_t * sym = (Symbol_t *)symkey;
  106. const char * cp;
  107. PLHashNumber h;
  108. h = sym->sym_type;
  109. cp = sym->sym_name;
  110. if (cp) {
  111. while (*cp) {
  112. h = (h << 3) ^ tolower(*cp);
  113. ++cp;
  114. }
  115. }
  116. return h;
  117. }
  118. /* Helper function for symTableEnumerate() */
  119. typedef struct {
  120. int (*func)(Symbol_t * sym, void * parg);
  121. void * argp;
  122. } SymTableEnum_t;
  123. static int symTableEnumHelp(PLHashEntry * he, int n, void * step)
  124. {
  125. SymTableEnum_t * ste = (SymTableEnum_t *)step;
  126. int ret = 0;
  127. int rv;
  128. rv = (*ste->func)((Symbol_t *)(he->key), ste->argp);
  129. if (rv != 0) {
  130. if (rv & SYMENUMREMOVE) ret = HT_ENUMERATE_REMOVE;
  131. if (rv & SYMENUMSTOP) ret |= HT_ENUMERATE_STOP;
  132. }
  133. return ret;
  134. }
  135. NSPR_BEGIN_EXTERN_C
  136. /*
  137. * Description (symTableAddSym)
  138. *
  139. * This function adds a symbol definition to the symbol table.
  140. * The symbol definition includes a name string, a type, and a
  141. * reference to a structure.
  142. *
  143. * Arguments:
  144. *
  145. * table - handle for symbol table
  146. * newsym - pointer to new symbol name and type
  147. * symref - pointer to structure named by symbol
  148. *
  149. * Returns:
  150. *
  151. * If successful, the return code is zero. An error is indicated
  152. * by a negative return code (SYMERRxxxx - see symbols.h).
  153. */
  154. int symTableAddSym(void * table, Symbol_t * newsym, void * symref)
  155. {
  156. SymTable_t * st = (SymTable_t *)table;
  157. PLHashEntry **hep;
  158. PLHashNumber keyhash;
  159. int rv = 0;
  160. /* Compute the hash value for this symbol */
  161. keyhash = symHash((const void *)newsym);
  162. crit_enter(st->stb_crit);
  163. /* See if another symbol already has the same name and type */
  164. hep = PL_HashTableRawLookup(st->stb_ht, keyhash, (void *)newsym);
  165. if (*hep == 0) {
  166. /* Expand the hash table if necessary and allocate an entry */
  167. PL_HashTableRawAdd(st->stb_ht,
  168. hep, keyhash, (void *)newsym, symref);
  169. }
  170. else {
  171. /* The symbol is already there. It's an error */
  172. rv = SYMERRDUPSYM;
  173. }
  174. crit_exit(st->stb_crit);
  175. return rv;
  176. }
  177. /*
  178. * Description (symTableRemoveSym)
  179. *
  180. * This function removes an entry from a symbol table. It does
  181. * not free the entry itself, just the hash entry that references
  182. * it.
  183. *
  184. * Arguments:
  185. *
  186. * table - symbol table handle
  187. * sym - pointer to symbol structure
  188. */
  189. void symTableRemoveSym(void * table, Symbol_t * sym)
  190. {
  191. SymTable_t * st = (SymTable_t *)table;
  192. if (sym->sym_name != 0) {
  193. crit_enter(st->stb_crit);
  194. PL_HashTableRemove(st->stb_ht, (void *)sym);
  195. crit_exit(st->stb_crit);
  196. }
  197. }
  198. /*
  199. * Description (symTableEnumerate)
  200. *
  201. * This function enumerates all of the entries in a symbol table,
  202. * calling a specified function for each entry. The function
  203. * specified by the caller may return flags indicating actions
  204. * to be taken for each entry or whether to terminate the
  205. * enumeration. These flags are defined in symbols.h as
  206. * SYMENUMxxxx.
  207. *
  208. * Arguments:
  209. *
  210. * table - symbol table handle
  211. * argp - argument for caller function
  212. * func - function to be called for each entry
  213. */
  214. void symTableEnumerate(void * table, void * argp,
  215. #ifdef UnixWare /* Fix bug in UnixWare compiler for name mangeling - [email protected] */
  216. ArgFn_symTableEnum func)
  217. #else
  218. int (*func)(Symbol_t * sym, void * parg))
  219. #endif
  220. {
  221. SymTable_t * st = (SymTable_t *)table;
  222. SymTableEnum_t ste; /* enumeration arguments */
  223. ste.func = func;
  224. ste.argp = argp;
  225. crit_enter(st->stb_crit);
  226. (void)PL_HashTableEnumerateEntries(st->stb_ht,
  227. symTableEnumHelp, (void *)&ste);
  228. crit_exit(st->stb_crit);
  229. }
  230. /*
  231. * Description (symTableFindSym)
  232. *
  233. * This function locates a symbol with a specified name and type
  234. * in a given symbol table. It returns a pointer to the structure
  235. * named by the symbol.
  236. *
  237. * Arguments:
  238. *
  239. * table - symbol table handle
  240. * symname - symbol name string pointer
  241. * symtype - symbol type code
  242. * psymref - pointer to returned structure pointer
  243. *
  244. * Returns:
  245. *
  246. * If successful, the return code is zero and the structure pointer
  247. * associated with the symbol name and type is returned in the
  248. * location specified by 'psymref'. An error is indicated by a
  249. * negative return code (SYMERRxxxx - see symbols.h).
  250. */
  251. int symTableFindSym(void * table, const char * symname,
  252. int symtype, void **psymref)
  253. {
  254. SymTable_t * st = (SymTable_t *)table;
  255. Symbol_t sym;
  256. void * symref;
  257. /* Create temporary entry with fields needed by symHash() */
  258. sym.sym_name = symname;
  259. sym.sym_type = symtype;
  260. crit_enter(st->stb_crit);
  261. symref = PL_HashTableLookup(st->stb_ht, (void *)&sym);
  262. crit_exit(st->stb_crit);
  263. *psymref = symref;
  264. return (symref) ? 0 : SYMERRNOSYM;
  265. }
  266. /*
  267. * Description (symTableDestroy)
  268. *
  269. * This function destroys a symbol table created by symTableNew().
  270. *
  271. * Arguments:
  272. *
  273. * table - symbol table handle from symTableNew()
  274. * flags - bit flags (unused - must be zero)
  275. */
  276. void symTableDestroy(void * table, int flags)
  277. {
  278. SymTable_t * st = (SymTable_t *)table;
  279. if (st) {
  280. if (st->stb_crit) {
  281. crit_terminate(st->stb_crit);
  282. }
  283. if (st->stb_ht) {
  284. PL_HashTableDestroy(st->stb_ht);
  285. }
  286. PERM_FREE(st);
  287. }
  288. }
  289. /*
  290. * Description (symTableNew)
  291. *
  292. * This function creates a new symbol table, and returns a handle
  293. * for it.
  294. *
  295. * Arguments:
  296. *
  297. * ptable - pointer to returned symbol table handle
  298. *
  299. * Returns:
  300. *
  301. * If successful, the return code is zero and a handle for the new
  302. * symbol table is returned in the location specified by 'ptable'.
  303. * An error is indicated by a negative return code (SYMERRxxxx
  304. * - see symbols.h).
  305. */
  306. int symTableNew(void **ptable)
  307. {
  308. SymTable_t * st;
  309. /* Allocate the symbol table object */
  310. st = (SymTable_t *)PERM_MALLOC(sizeof(SymTable_t));
  311. if (st == 0) goto err_nomem;
  312. /* Get a monitor for it */
  313. st->stb_crit = crit_init();
  314. st->stb_ht = PL_NewHashTable(0, symHash, symCmpName, symCmpValue,
  315. &SymAllocOps, 0);
  316. if (st->stb_ht == 0) goto err_nomem;
  317. *ptable = st;
  318. return 0;
  319. err_nomem:
  320. if (st) {
  321. symTableDestroy(st, 0);
  322. }
  323. return SYMERRNOMEM;
  324. }
  325. NSPR_END_EXTERN_C