📄 ssl_util_table.c
字号:
*data_buf_p = data_copy_p; /* returning from the section where we were overwriting table data */ return TABLE_ERROR_NONE; } /* * It is a new entry. */ /* allocate a new entry */ entry_p = (table_entry_t *) table_p->ta_malloc(entry_size(table_p, ksize, dsize)); if (entry_p == NULL) return TABLE_ERROR_ALLOC; /* copy key into storage */ entry_p->te_key_size = ksize; key_copy_p = ENTRY_KEY_BUF(entry_p); memcpy(key_copy_p, key_buf, ksize); /* copy data in */ entry_p->te_data_size = dsize; if (dsize > 0) { if (table_p->ta_data_align == 0) data_copy_p = ENTRY_DATA_BUF(table_p, entry_p); else data_copy_p = entry_data_buf(table_p, entry_p); if (data_buf != NULL) memcpy(data_copy_p, data_buf, dsize); } else data_copy_p = NULL; if (key_buf_p != NULL) *key_buf_p = key_copy_p; if (data_buf_p != NULL) *data_buf_p = data_copy_p; /* insert into list, no need to append */ entry_p->te_next_p = table_p->ta_buckets[bucket]; table_p->ta_buckets[bucket] = entry_p; table_p->ta_entry_n++; /* do we need auto-adjust? */ if (table_p->ta_flags & TABLE_FLAG_AUTO_ADJUST && SHOULD_TABLE_GROW(table_p)) return table_adjust(table_p, table_p->ta_entry_n); return TABLE_ERROR_NONE;}/* * int table_insert * * DESCRIPTION: * * Exactly the same as table_insert_kd except it does not pass back a * pointer to the key after they have been inserted into the table * structure. This is still here for backwards compatibility. * * See table_insert_kd for more information. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer into which we will be inserting a * new key/data pair. * * key_buf - Buffer of bytes of the key that we are inserting. If you * are storing an (int) as the key (for example) then key_buf should * be a (int *). * * key_size - Size of the key_buf buffer. If set to < 0 then the * library will do a strlen of key_buf and add 1 for the '\0'. If you * are storing an (int) as the key (for example) then key_size should * be sizeof(int). * * data_buf - Buffer of bytes of the data that we are inserting. If * it is NULL then the library will allocate space for the data in the * table without copying in any information. If data_buf is NULL and * data_size is 0 then the library will associate a NULL data pointer * with the key. If you are storing a (long) as the data (for * example) then data_buf should be a (long *). * * data_size - Size of the data_buf buffer. If set to < 0 then the * library will do a strlen of data_buf and add 1 for the '\0'. If * you are storing an (long) as the key (for example) then key_size * should be sizeof(long). * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that was allocated in the table. If you are * storing an (long) as the data (for example) then data_buf_p should * be (long **) i.e. the address of a (long *). * * overwrite - Flag which, if set to 1, will allow the overwriting of * the data in the table with the new data if the key already exists * in the table. */int table_insert(table_t * table_p, const void *key_buf, const int key_size, const void *data_buf, const int data_size, void **data_buf_p, const char overwrite_b){ return table_insert_kd(table_p, key_buf, key_size, data_buf, data_size, NULL, data_buf_p, overwrite_b);}/* * int table_retrieve * * DESCRIPTION: * * This routine looks up a key made up of a buffer of bytes and an * associated size in the table. If found then it returns the * associated data information. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer into which we will be searching * for the key. * * key_buf - Buffer of bytes of the key that we are searching for. If * you are looking for an (int) as the key (for example) then key_buf * should be a (int *). * * key_size - Size of the key_buf buffer. If set to < 0 then the * library will do a strlen of key_buf and add 1 for the '\0'. If you * are looking for an (int) as the key (for example) then key_size * should be sizeof(int). * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that was allocated in the table and that is * associated with the key. If a (long) was stored as the data (for * example) then data_buf_p should be (long **) i.e. the address of a * (long *). * * data_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the data stored in the table that is associated with * the key. */int table_retrieve(table_t * table_p, const void *key_buf, const int key_size, void **data_buf_p, int *data_size_p){ int bucket; unsigned int ksize; table_entry_t *entry_p, **buckets; if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; if (key_buf == NULL) return TABLE_ERROR_ARG_NULL; /* find key size */ if (key_size < 0) ksize = strlen((char *) key_buf) + sizeof(char); else ksize = key_size; /* get the bucket number via a has function */ bucket = hash(key_buf, ksize, 0) % table_p->ta_bucket_n; /* look for the entry in this bucket, only check keys of the same size */ buckets = table_p->ta_buckets; for (entry_p = buckets[bucket]; entry_p != NULL; entry_p = entry_p->te_next_p) { entry_p = TABLE_POINTER(table_p, table_entry_t *, entry_p); if (entry_p->te_key_size == ksize && memcmp(ENTRY_KEY_BUF(entry_p), key_buf, ksize) == 0) break; } /* not found? */ if (entry_p == NULL) return TABLE_ERROR_NOT_FOUND; if (data_buf_p != NULL) { if (entry_p->te_data_size == 0) *data_buf_p = NULL; else { if (table_p->ta_data_align == 0) *data_buf_p = ENTRY_DATA_BUF(table_p, entry_p); else *data_buf_p = entry_data_buf(table_p, entry_p); } } if (data_size_p != NULL) *data_size_p = entry_p->te_data_size; return TABLE_ERROR_NONE;}/* * int table_delete * * DESCRIPTION: * * This routine looks up a key made up of a buffer of bytes and an * associated size in the table. If found then it will be removed * from the table. The associated data can be passed back to the user * if requested. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * NOTE: this could be an allocation error if the library is to return * the data to the user. * * ARGUMENTS: * * table_p - Table structure pointer from which we will be deleteing * the key. * * key_buf - Buffer of bytes of the key that we are searching for to * delete. If you are deleting an (int) key (for example) then * key_buf should be a (int *). * * key_size - Size of the key_buf buffer. If set to < 0 then the * library will do a strlen of key_buf and add 1 for the '\0'. If you * are deleting an (int) key (for example) then key_size should be * sizeof(int). * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that was allocated in the table and that was * associated with the key. If a (long) was stored as the data (for * example) then data_buf_p should be (long **) i.e. the address of a * (long *). If a pointer is passed in, the caller is responsible for * freeing it after use. If data_buf_p is NULL then the library will * free up the data allocation itself. * * data_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the data that was stored in the table and that was * associated with the key. */int table_delete(table_t * table_p, const void *key_buf, const int key_size, void **data_buf_p, int *data_size_p){ int bucket; unsigned int ksize; unsigned char *data_copy_p; table_entry_t *entry_p, *last_p; if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; if (key_buf == NULL) return TABLE_ERROR_ARG_NULL; /* get the key size */ if (key_size < 0) ksize = strlen((char *) key_buf) + sizeof(char); else ksize = key_size; /* find our bucket */ bucket = hash(key_buf, ksize, 0) % table_p->ta_bucket_n; /* look for the entry in this bucket, only check keys of the same size */ for (last_p = NULL, entry_p = table_p->ta_buckets[bucket]; entry_p != NULL; last_p = entry_p, entry_p = entry_p->te_next_p) { if (entry_p->te_key_size == ksize && memcmp(ENTRY_KEY_BUF(entry_p), key_buf, ksize) == 0) break; } /* did we find it? */ if (entry_p == NULL) return TABLE_ERROR_NOT_FOUND; /* * NOTE: we may want to adjust the linear counters here if the entry * we are deleting is the one we are pointing on or is ahead of the * one in the bucket list */ /* remove entry from the linked list */ if (last_p == NULL) table_p->ta_buckets[bucket] = entry_p->te_next_p; else last_p->te_next_p = entry_p->te_next_p; /* free entry */ if (data_buf_p != NULL) { if (entry_p->te_data_size == 0) *data_buf_p = NULL; else { /* * if we were storing it compacted, we now need to malloc some * space if the user wants the value after the delete. */ *data_buf_p = table_p->ta_malloc(entry_p->te_data_size); if (*data_buf_p == NULL) return TABLE_ERROR_ALLOC; if (table_p->ta_data_align == 0) data_copy_p = ENTRY_DATA_BUF(table_p, entry_p); else data_copy_p = entry_data_buf(table_p, entry_p); memcpy(*data_buf_p, data_copy_p, entry_p->te_data_size); } } if (data_size_p != NULL) *data_size_p = entry_p->te_data_size; table_p->ta_free(entry_p); table_p->ta_entry_n--; /* do we need auto-adjust down? */ if ((table_p->ta_flags & TABLE_FLAG_AUTO_ADJUST) && (table_p->ta_flags & TABLE_FLAG_ADJUST_DOWN) && SHOULD_TABLE_SHRINK(table_p)) return table_adjust(table_p, table_p->ta_entry_n); return TABLE_ERROR_NONE;}/* * int table_delete_first * * DESCRIPTION: * * This is like the table_delete routines except it deletes the first * key/data pair in the table instead of an entry corresponding to a * particular key. The associated key and data information can be * passed back to the user if requested. This routines is handy to * clear out a table. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * NOTE: this could be an allocation error if the library is to return * the data to the user. * * ARGUMENTS: * * table_p - Table structure pointer from which we will be deleteing * the first key. * * key_buf_p - Pointer which, if not NULL, will be set to the address * of the storage of the first key that was allocated in the table. * If an (int) was stored as the first key (for example) then * key_buf_p should be (int **) i.e. the address of a (int *). If a * pointer is passed in, the caller is responsible for freeing it * after use. If key_buf_p is NULL then the library will free up the * key allocation itself. * * key_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the key that was stored in the table and that was * associated with the key. * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that was allocated in the table and that was * associated with the key. If a (long) was stored as the data (for * example) then data_buf_p should be (long **) i.e. the address of a * (long *). If a pointer is passed in, the caller is responsible for * freeing it after use. If data_buf_p is NULL then the library will * free up the data allocation itself. * * data_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the data that was stored in the table and that was * associated with the key. */int table_delete_first(table_t * table_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p){ unsigned char *data_copy_p; table_entry_t *entry_p; table_linear_t linear; if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; /* take the first entry */ entry_p = first_entry(table_p, &linear); if (entry_p == NULL) return TABLE_ERROR_NOT_FOUND; /* * NOTE: we may want to adjust the linear counters here if the entry * we are deleting is the one we are pointing on or is ahead of the * one in the bucket list */ /* remove entry from the linked list */ table_p->ta_buckets[linear.tl_bucket_c] = entry_p->te_next_p; /* free entry */ if (key_buf_p != NULL) { if (entry_p->te_key_size == 0) *key_buf_p = NULL; else { /* * if we were storing it compacted, we now need to malloc some * space if the user wants the value after the delete. */ *key_buf_p = table_p->ta_malloc(entry_p->te_key_size); if (*key_buf_p == NULL) return TABLE_ERROR_ALLOC; memcpy(*key_buf_p, ENTRY_KEY_BUF(entry_p), entry_p->te_key_size);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -