3.1 openssl堆栈
堆栈是一种先进先出的数据结构。openssl大量采用堆栈来存放数据。他实现一个通用的堆栈,可以方便的存储任意数据。他实现了许多基本的堆栈操作。主要有:
a.堆栈拷贝(sk_dup)
b.构建新堆栈(sk_new_null, sk_new)
c.插入数据(sk_insert)
d.删除数据(sk_delete)
e.查找数据(sk_find,sk_find_ex)
f.入栈(sk_push)
h.出栈(sk_pop)
i.获取堆栈元素个数(sk_num)
j.获取堆栈值(sk_value)
k.设置堆栈值(sk_set)
l.堆栈排序(sk_sort)
3.2 数据结构
openssl堆栈数据结构在stack.h中定义
typedef struct stack_st
{
int num; 堆栈中存放数据的个数
char **data; 用于存放数据地址,每个数据地址存放在data[0]到data[num-1]中
int sorted; 堆栈是否已排序,如果排序则为1,否则为0,堆栈数据一般是无序的,只有当用户调用了sk_sort操作,其值才为1.
int num_alloc;
int (*comp)(const char *const *, const char *const *); 堆栈内存放数据的比较函数地址,此函数用于排序和查找操作;当用户生成一个新堆栈时,可以指定comp为函数实现的一个比较函数;或当堆栈已经存在时通过调用sk_set_cmp_func函数来重新指定比较函数。
}
备注:用户不需要调用底层的堆栈函数(sk_sort\sk_set_comp_func等),而是调用他通过宏实现的各个函数。
3.3 源码
openssl堆栈实现源码位于crypto/stack目录下。
1)sk_set_cmp_func
此函数用于设置堆栈存放数据的比较函数。由于堆栈不知道用户存放的是什么数据,所以,比较函数必须由用户自己实现。
2)sk_find
根据数据地址来查找它在堆栈中的位置。当堆栈设置比较函数时,他首先对堆栈进行排序,然后通过二分法进行查找。如果堆栈没有设置比较半数,他只是简单的比较数据地址来查找。
3)sk_sort
本函数对堆栈数据排序。他首先根据sorted来判断是否已经排序,如果未排序则调用了C函数qsort进行快速排序
4)sk_pop_free
本函数用于释放堆栈内存放的数据以堆栈本身,他需要一个由用户指定的针对具体数据的释放函数。如果用户仅调用sk_free函数,则只会释放堆栈本身所用的内存,而不会释放数据内存。
3.4 定义用户自己的堆栈函数
用户直接调用最底层的堆栈操作函数。用户可以参考safesstack.h来定义自己上层堆栈操作函数
#define sk_GENERAL_NAMES_new(st) SKM_sk_new(GENERAL_NAME, (st))
#define sk_GENERAL_NAMES_new_null() SKM_sk_new_new_null(GENERAL_NAME)
#define sk_GENERAL_NAMES_free(st) SKM_sk_free(GENERAL_NAME, (st))
#define sk_GENERAL_NAMES_num(st) SKM_sk_num(GENERAL_NAMES, (st))
#define sk_GENERAL_NAMES_value(st, i) SKM_sk_value(GENERAL_NAMES, (st), (i))
#define sk_GENERAL_NAMES_set(st, i, val) SKM_sk_set(GENERAL_NAMES, (st), (i), (val))
#define sk_GENERAL_NAMES_zero(st) SKM_sk_zero(GENERAL_NAMES, (st))
#define sk_GENERAL_NAMES_push(st, val) SKM_sk_push(GENERAL_NAMES, (st))
#define sk_GENERAL_NAMES_unshift(st, val) SKM_sk_unshift(GENERAL_NAMES, (st), (val))
#define sk_GENERAL_NAMES_find(st, val) SKM_sk_find(GENERAL_NAMES, (st), (val))
#define sk_GENERAL_NAMES_find_ex(st, val) SKM_sk_find_ex(GENERAL_NAMES, (st), (val))
#define sk_GENERAL_NAMES_delete(st, i) SKM_sk_delete(GENERAL_NAMES, (st), (i))
#define sk_GENERAL_NAMES_delete_ptr(st, ptr) SKM_sk_delete_ptr(GENERAL_NAMES, (st), (ptr))
#define sk_GENERAL_NAMES_insert(st, val, i) SKM_sk_insert(GENERAL_NAMES, (st), (val), (i))
#define sk_GENERAL_NAMES_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(GENERAL_NAMES, (st), (cmp))
#define sk_GENERAL_NAMES_dup(st) SKM_sk_dup(GENERAL_NAMES, st)
#define sk_GENERAL_NAMES_pop_free(st, free_func) SKM_sk_pop_free(GENERAL_NAMES,(st), (free_func))
#define sk_GENERAL_NAMES_deep_copy(st, copy_func, free_func) SKM_sk_deep_copy(GENERAL_NAMES, (st), (copy_func), (free_func))
#define sk_GENERAL_NAMES_shift(st) SKM_sk_shift(GENERAL_NAMES, (st))
#define sk_GENERAL_NAMES_pop(st) SKM_sk_pop(GENERAL_NAMES, (st))
#define sk_GENERAL_NAMES_is_sorted(st) SKM_sk_is_sorted(GENERAL_NAMES, (st))
3.5 编程示例
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <openssl/safestack.h> 5 6 #define sk_student_new(st) SKM_sk_new(student, (st)) 7 #define sk_student_new_null() SKM_sk_new_null(student) 8 #define sk_student_free(st) SKM_sk_free(student, (st)) 9 #define sk_student_num(st) SKM_sk_num(student, (st)) 10 #define sk_student_value(st, i) SKM_sk_value(student, (st), (i)) 11 #define sk_student_set(st, i, val) SKM_sk_set(student, (st), (i), (val)) 12 #define sk_student_zero(st) SKM_sk_zero(student, (st)) 13 #define sk_student_push(st, val) SKM_sk_push(student, (st), (val)) 14 #define sk_student_unshift(st, val) SKM_sk_unshift(student, (st), (val)) 15 #define sk_student_find(st, val) SKM_sk_find(student, (st), (val)) 16 #define sk_student_delete(st, i) SKM_sk_delete(student, (st), (i)) 17 #define sk_student_delete_ptr(st, ptr) SKM_sk_delete_ptr(student, (st), (ptr)) 18 #define sk_student_insert(st, val, i) SKM_sk_insert(student, (st), (val), (i)) 19 #define sk_student_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(student, (st), (cmp)) 20 #define sk_student_dup(st) SKM_sk_dup(student, st) 21 #define sk_student_pop_free(st, free_func) SKM_sk_pop_free(student, (st), (free_func)) 22 #define sk_student_shift(st) SKM_sk_shift(student, (st)) 23 #define sk_student_pop(st) SKM_sk_pop(student, (st)) 24 #define sk_student_sort(st) SKM_sk_sort(student, (st)) 25 26 27 typedef struct student_st 28 { 29 char *name; 30 int age; 31 char *otherInfo; 32 }student; 33 34 typedef STACK_OF(student) students; 35 36 student *student_malloc() 37 { 38 student *a = malloc(sizeof(student)); 39 a->name = malloc(20); 40 strcpy(a->name, "zcp"); 41 a->otherInfo = malloc(20); 42 strcpy(a->otherInfo, "no info"); 43 return a; 44 } 45 46 47 void student_free(student *a) 48 { 49 free(a->name); 50 free(a->otherInfo); 51 free(a); 52 } 53 54 static int student_cmp(student *a, student *b) 55 { 56 int ret; 57 ret = strcmp(a->name, b->name); 58 return ret; 59 } 60 61 int main(int argc, char *argv) 62 { 63 students *s, *snew; 64 student *s1, *one, *s2; 65 int i, num; 66 67 s = sk_student_new_null(); 68 s2 = student_malloc(); 69 sk_student_push(snew, s2); 70 i = sk_student_find(snew, s2); 71 s1 = student_malloc(); 72 sk_student_push(s, s1); 73 num = sk_student_num(s); 74 75 for (i = 0; i < num; i++) 76 { 77 one = sk_student_value(s,i); 78 printf("student name: %s \n", one->name); 79 printf("student age: %d \n", one->age); 80 printf("student otherinfo: %s\n\n\n", one->otherInfo); 81 } 82 83 sk_student_pop_free(s, student_free); 84 sk_student_pop_free(snew, student_free); 85 return 0; 86 }