ESAPI-C 1.0
The OWASP Enterprise Security API for C

access_ref.c

Go to the documentation of this file.
00001 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #include "access_ref.h"
00027 
00031 map_t *v_map = NULL;
00032 
00036 map_t *k_map = NULL;
00037 map_t *s, *f;
00038 
00039 bool _unique_keys(const char *k, const char *v) {
00040         map_t *loc_map1, *loc_map2;
00041 
00042         if (HASH_CNT(hh_k, k_map) != 0) { // initial entry, return true
00043                 HASH_FIND(hh_k, k_map, k, strlen(k), loc_map1);
00044                 HASH_FIND(hh_v, v_map, v, strlen(v), loc_map2);
00045                 return (loc_map1 && loc_map2) ? false : true;
00046         }
00047         return true;
00048 }
00049 
00050 char *esapi_get_indirect_reference(const char *direct) {
00051         char *key = NULL;
00052 
00053         if (!direct) {
00054                 return 0;
00055         } else {
00056                 HASH_FIND(hh_v, v_map, direct, strlen(direct), f);
00057                 if (f != NULL) {
00058                         key = f->key;
00059                 }
00060         }
00061         return key;
00062 }
00063 
00064 char *esapi_get_direct_reference(const char *indirect) {
00065         char *value = NULL;
00066 
00067         if (!indirect) {
00068                 return 0;
00069         } else {
00070                 HASH_FIND(hh_k, k_map, indirect, strlen(indirect), f);
00071                 if (f != NULL) {
00072                         value = f->val;
00073                 }
00074         }
00075         return value;
00076 }
00077 
00078 bool esapi_remove_indirect_reference(const char *indirect) {
00079         bool ret_val = false;
00080         map_t *loc_map;
00081 
00082         if (indirect) {
00083                 HASH_FIND(hh_v, v_map, indirect, strlen(indirect), loc_map);
00084 
00085                 if (loc_map) {
00086                         HASH_DELETE(hh_v, v_map, loc_map);
00087                         HASH_DELETE(hh_k, k_map, loc_map);
00088                         free(loc_map);
00089                         ret_val = true;
00090                 }
00091         }
00092         return ret_val;
00093 }
00094 
00095 bool esapi_remove_direct_reference(const char *direct) {
00096         bool ret_val = false;
00097         map_t *loc_map;
00098 
00099         HASH_FIND(hh_k, k_map, direct, strlen(direct), loc_map);
00100 
00101         if (loc_map) {
00102                 HASH_DELETE(hh_v, v_map, loc_map);
00103                 HASH_DELETE(hh_k, k_map, loc_map);
00104                 free(loc_map);
00105                 ret_val = true;
00106         }
00107         return ret_val;
00108 }
00109 
00110 bool esapi_put_reference(const char *k, const char *v) {
00111         bool ret_val = false;
00112 
00113         if (k && v) { // make sure the key and value are not NULL
00114                 // allocate space, will be freed in the remove functions.
00115                 if ((s = (map_t *) malloc(sizeof(map_t))) == 0) {
00116                         (void) fprintf(stderr, "malloc failure in %s\n", __func__);
00117                         exit(EXIT_FAILURE);
00118                 } else if (_unique_keys(k, v)) { // add only unique keys
00119                         strcpy(s->key, k);
00120                         strcpy(s->val, v);
00121                         // add key to structure
00122                         HASH_ADD(hh_k, k_map, key, strlen(s->key), s);
00123                         // add value as a key to structure
00124                         HASH_ADD(hh_v, v_map, val, strlen(s->val), s);
00125                         ret_val = true;
00126                 }
00127         }
00128         return ret_val;
00129 }
00130 
00131 char *esapi_get_unique_reference() {
00132         char *reference = (char *) malloc(KEY_LEN);
00133         esapi_fill_random_token(NULL, reference, KEY_LEN);
00134 
00135         return reference;
00136 }
 All Data Structures Files Functions Variables Typedefs Defines