symbols.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /*
  39. * Description (symbols.c)
  40. *
  41. * This module implements a symbol table for ACL-related structures.
  42. * The symbol table associates string names and types with pointers
  43. * to various kinds of structures.
  44. */
  45. /*
  46. #include <base/systems.h>
  47. */
  48. #include <plhash.h>
  49. #include <base/util.h>
  50. #include <netsite.h>
  51. #define __PRIVATE_SYMBOLS
  52. #include "libaccess/symbols.h"
  53. #include <ctype.h>
  54. static PLHashEntry * symAllocEntry(void * pool, const void *unused);
  55. static void * symAllocTable(void * pool, PRSize size);
  56. static int symCmpName(const void * name1, const void * name2);
  57. static int symCmpValue(const void * value1, const void * value2);
  58. static PLHashNumber symHash(const void * symkey);
  59. static void symFreeEntry(void * pool, PLHashEntry * he, PRUintn flag);
  60. static void symFreeTable(void * pool, void * item);
  61. /* Table of pointers to functions associated with the hash table */
  62. static PLHashAllocOps SymAllocOps = {
  63. symAllocTable, /* allocate the hash table */
  64. symFreeTable, /* free the hash table */
  65. symAllocEntry, /* allocate a table entry */
  66. symFreeEntry, /* free a table entry */
  67. };
  68. static void * symAllocTable(void * pool, PRSize size)
  69. {
  70. return (void *)PERM_MALLOC(size);
  71. }
  72. static void symFreeTable(void * pool, void * item)
  73. {
  74. PERM_FREE(item);
  75. }
  76. static PLHashEntry * symAllocEntry(void * pool, const void *ignored)
  77. {
  78. PLHashEntry * he;
  79. he = (PLHashEntry *) PERM_MALLOC(sizeof(PLHashEntry));
  80. return he;
  81. }
  82. static void symFreeEntry(void * pool, PLHashEntry * he, PRUintn flag)
  83. {
  84. if (flag == HT_FREE_ENTRY) {
  85. /* Just free the hash entry, not anything it references */
  86. PERM_FREE(he);
  87. }
  88. }
  89. static int symCmpName(const void * name1, const void * name2)
  90. {
  91. Symbol_t * sym1 = (Symbol_t *)name1;
  92. Symbol_t * sym2 = (Symbol_t *)name2;
  93. return ((sym1->sym_type == sym2->sym_type) &&
  94. !strcasecmp(sym1->sym_name, sym2->sym_name));
  95. }
  96. static int symCmpValue(const void * value1, const void * value2)
  97. {
  98. return (value1 == value2);
  99. }
  100. static PLHashNumber symHash(const void * symkey)
  101. {
  102. Symbol_t * sym = (Symbol_t *)symkey;
  103. char * cp;
  104. PLHashNumber h;
  105. h = sym->sym_type;
  106. cp = sym->sym_name;
  107. if (cp) {
  108. while (*cp) {
  109. h = (h << 3) ^ tolower(*cp);
  110. ++cp;
  111. }
  112. }
  113. return h;
  114. }
  115. /* Helper function for symTableEnumerate() */
  116. typedef struct {
  117. int (*func)(Symbol_t * sym, void * parg);
  118. void * argp;
  119. } SymTableEnum_t;
  120. static int symTableEnumHelp(PLHashEntry * he, int n, void * step)
  121. {
  122. SymTableEnum_t * ste = (SymTableEnum_t *)step;
  123. int ret = 0;
  124. int rv;
  125. rv = (*ste->func)((Symbol_t *)(he->key), ste->argp);
  126. if (rv != 0) {
  127. if (rv & SYMENUMREMOVE) ret = HT_ENUMERATE_REMOVE;
  128. if (rv & SYMENUMSTOP) ret |= HT_ENUMERATE_STOP;
  129. }
  130. return ret;
  131. }
  132. NSPR_BEGIN_EXTERN_C
  133. /*
  134. * Description (symTableAddSym)
  135. *
  136. * This function adds a symbol definition to the symbol table.
  137. * The symbol definition includes a name string, a type, and a
  138. * reference to a structure.
  139. *
  140. * Arguments:
  141. *
  142. * table - handle for symbol table
  143. * newsym - pointer to new symbol name and type
  144. * symref - pointer to structure named by symbol
  145. *
  146. * Returns:
  147. *
  148. * If successful, the return code is zero. An error is indicated
  149. * by a negative return code (SYMERRxxxx - see symbols.h).
  150. */
  151. int symTableAddSym(void * table, Symbol_t * newsym, void * symref)
  152. {
  153. SymTable_t * st = (SymTable_t *)table;
  154. PLHashEntry * he;
  155. PLHashEntry **hep;
  156. PLHashNumber keyhash;
  157. int rv = 0;
  158. /* Compute the hash value for this symbol */
  159. keyhash = symHash((const void *)newsym);
  160. crit_enter(st->stb_crit);
  161. /* See if another symbol already has the same name and type */
  162. hep = PL_HashTableRawLookup(st->stb_ht, keyhash, (void *)newsym);
  163. if (*hep == 0) {
  164. /* Expand the hash table if necessary and allocate an entry */
  165. he = PL_HashTableRawAdd(st->stb_ht,
  166. hep, keyhash, (void *)newsym, symref);
  167. }
  168. else {
  169. /* The symbol is already there. It's an error */
  170. rv = SYMERRDUPSYM;
  171. }
  172. crit_exit(st->stb_crit);
  173. return rv;
  174. }
  175. /*
  176. * Description (symTableRemoveSym)
  177. *
  178. * This function removes an entry from a symbol table. It does
  179. * not free the entry itself, just the hash entry that references
  180. * it.
  181. *
  182. * Arguments:
  183. *
  184. * table - symbol table handle
  185. * sym - pointer to symbol structure
  186. */
  187. void symTableRemoveSym(void * table, Symbol_t * sym)
  188. {
  189. SymTable_t * st = (SymTable_t *)table;
  190. if (sym->sym_name != 0) {
  191. crit_enter(st->stb_crit);
  192. PL_HashTableRemove(st->stb_ht, (void *)sym);
  193. crit_exit(st->stb_crit);
  194. }
  195. }
  196. /*
  197. * Description (symTableEnumerate)
  198. *
  199. * This function enumerates all of the entries in a symbol table,
  200. * calling a specified function for each entry. The function
  201. * specified by the caller may return flags indicating actions
  202. * to be taken for each entry or whether to terminate the
  203. * enumeration. These flags are defined in symbols.h as
  204. * SYMENUMxxxx.
  205. *
  206. * Arguments:
  207. *
  208. * table - symbol table handle
  209. * argp - argument for caller function
  210. * func - function to be called for each entry
  211. */
  212. void symTableEnumerate(void * table, void * argp,
  213. #ifdef UnixWare /* Fix bug in UnixWare compiler for name mangeling - [email protected] */
  214. ArgFn_symTableEnum func)
  215. #else
  216. int (*func)(Symbol_t * sym, void * parg))
  217. #endif
  218. {
  219. SymTable_t * st = (SymTable_t *)table;
  220. SymTableEnum_t ste; /* enumeration arguments */
  221. ste.func = func;
  222. ste.argp = argp;
  223. crit_enter(st->stb_crit);
  224. (void)PL_HashTableEnumerateEntries(st->stb_ht,
  225. symTableEnumHelp, (void *)&ste);
  226. crit_exit(st->stb_crit);
  227. }
  228. /*
  229. * Description (symTableFindSym)
  230. *
  231. * This function locates a symbol with a specified name and type
  232. * in a given symbol table. It returns a pointer to the structure
  233. * named by the symbol.
  234. *
  235. * Arguments:
  236. *
  237. * table - symbol table handle
  238. * symname - symbol name string pointer
  239. * symtype - symbol type code
  240. * psymref - pointer to returned structure pointer
  241. *
  242. * Returns:
  243. *
  244. * If successful, the return code is zero and the structure pointer
  245. * associated with the symbol name and type is returned in the
  246. * location specified by 'psymref'. An error is indicated by a
  247. * negative return code (SYMERRxxxx - see symbols.h).
  248. */
  249. int symTableFindSym(void * table, char * symname,
  250. int symtype, void **psymref)
  251. {
  252. SymTable_t * st = (SymTable_t *)table;
  253. Symbol_t sym;
  254. void * symref;
  255. /* Create temporary entry with fields needed by symHash() */
  256. sym.sym_name = symname;
  257. sym.sym_type = symtype;
  258. crit_enter(st->stb_crit);
  259. symref = PL_HashTableLookup(st->stb_ht, (void *)&sym);
  260. crit_exit(st->stb_crit);
  261. *psymref = symref;
  262. return (symref) ? 0 : SYMERRNOSYM;
  263. }
  264. /*
  265. * Description (symTableDestroy)
  266. *
  267. * This function destroys a symbol table created by symTableNew().
  268. *
  269. * Arguments:
  270. *
  271. * table - symbol table handle from symTableNew()
  272. * flags - bit flags (unused - must be zero)
  273. */
  274. void symTableDestroy(void * table, int flags)
  275. {
  276. SymTable_t * st = (SymTable_t *)table;
  277. if (st) {
  278. if (st->stb_crit) {
  279. crit_terminate(st->stb_crit);
  280. }
  281. if (st->stb_ht) {
  282. PL_HashTableDestroy(st->stb_ht);
  283. }
  284. PERM_FREE(st);
  285. }
  286. }
  287. /*
  288. * Description (symTableNew)
  289. *
  290. * This function creates a new symbol table, and returns a handle
  291. * for it.
  292. *
  293. * Arguments:
  294. *
  295. * ptable - pointer to returned symbol table handle
  296. *
  297. * Returns:
  298. *
  299. * If successful, the return code is zero and a handle for the new
  300. * symbol table is returned in the location specified by 'ptable'.
  301. * An error is indicated by a negative return code (SYMERRxxxx
  302. * - see symbols.h).
  303. */
  304. int symTableNew(void **ptable)
  305. {
  306. SymTable_t * st;
  307. /* Allocate the symbol table object */
  308. st = (SymTable_t *)PERM_MALLOC(sizeof(SymTable_t));
  309. if (st == 0) goto err_nomem;
  310. /* Get a monitor for it */
  311. st->stb_crit = crit_init();
  312. st->stb_ht = PL_NewHashTable(0, symHash, symCmpName, symCmpValue,
  313. &SymAllocOps, 0);
  314. if (st->stb_ht == 0) goto err_nomem;
  315. *ptable = st;
  316. return 0;
  317. err_nomem:
  318. if (st) {
  319. symTableDestroy(st, 0);
  320. }
  321. return SYMERRNOMEM;
  322. }
  323. NSPR_END_EXTERN_C