openssl stack

提供了用宏来帮助用户实现接口。用户可以参考safestack.h 中的宏SKM_DEFINE_STACK_OF(t1, t2, t3)来定义自己的上层堆栈操作函数

# define STACK_OF(type) struct stack_st_##type

# define SKM_DEFINE_STACK_OF(t1, t2, t3) \
    STACK_OF(t1); \
    typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
    typedef void (*sk_##t1##_freefunc)(t3 *a); \
    typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
    static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
    { \
        return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
    { \
        return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \
    } \
    static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \
    { \
        return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \
    } \
    static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
    { \
        return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
    } \
    static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \
    { \
        return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \
    { \
        return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \
    } \
    static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
    { \
        OPENSSL_sk_free((OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
    { \
        OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
    { \
        return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \
    } \
    static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
    { \
        return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
                                           (const void *)ptr); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
    { \
        return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
    { \
        return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \
    } \
    static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
    { \
        return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
    { \
        return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
    { \
        OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
    { \
        return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \
    } \
    static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
    { \
        return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
    { \
        return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
    { \
        return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \
    } \
    static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
    { \
        OPENSSL_sk_sort((OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
    { \
        return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
    { \
        return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
    } \
    static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
                                                    sk_##t1##_copyfunc copyfunc, \
                                                    sk_##t1##_freefunc freefunc) \
    { \
        return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \
                                            (OPENSSL_sk_copyfunc)copyfunc, \
                                            (OPENSSL_sk_freefunc)freefunc); \
    } \
    static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
    { \
        return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \
    }

实例:
s=sk_Student_new_null();
相当于 return (STACK_OF(t1) *)OPENSSL_sk_new_null();
即 return (struct stack_st_Student *)OPENSSL_sk_new_null();
这是分配一个stack

#include <stdio.h>
	#include <stdlib.h>
	#include <string.h>
	#include <openssl/safestack.h>
	
	
/*
	#define sk_Student_new(st) SKM_sk_new(Student, (st))
	#define sk_Student_new_null() SKM_sk_new_null(Student)
	#define sk_Student_free(st) SKM_sk_free(Student, (st))
	#define sk_Student_num(st) SKM_sk_num(Student, (st))
	#define sk_Student_value(st, i) SKM_sk_value(Student, (st), (i))
	#define sk_Student_set(st, i, val) SKM_sk_set(Student, (st), (i), (val))
	#define sk_Student_zero(st) SKM_sk_zero(Student, (st))
	#define sk_Student_push(st, val) SKM_sk_push(Student, (st), (val))
	#define sk_Student_unshift(st, val) SKM_sk_unshift(Student, (st), (val))
	#define sk_Student_find(st, val) SKM_sk_find(Student, (st), (val))
	#define sk_Student_delete(st, i) SKM_sk_delete(Student, (st), (i))
	#define sk_Student_delete_ptr(st, ptr) SKM_sk_delete_ptr(Student, (st), (ptr))
	#define sk_Student_insert(st, val, i) SKM_sk_insert(Student, (st), (val), (i))
	#define sk_Student_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(Student, (st), (cmp))
	#define sk_Student_dup(st) SKM_sk_dup(Student, st)
	#define sk_Student_pop_free(st, free_func) SKM_sk_pop_free(Student, (st), (free_func))
	#define sk_Student_shift(st) SKM_sk_shift(Student, (st))
	#define sk_Student_pop(st) SKM_sk_pop(Student, (st))
	#define sk_Student_sort(st) SKM_sk_sort(Student, (st))
*/	
	typedef	struct	Student_st
	{
		char		*name;
		int		age;
		char		*otherInfo;
	}Student;

	typedef	STACK_OF(Student)	Students;//struct stack_st_##Student -> struct stack_st_Student
	SKM_DEFINE_STACK_OF(Student,Student,Student) // 展开上面一堆函数
	
	Student *Student_Malloc()
	{
		Student *a=malloc(sizeof(Student));
		a->name=malloc(20);
		strcpy(a->name,"zcp");
		a->otherInfo=malloc(20);
		strcpy(a->otherInfo,"no info");
		return a;
	}
	
	void	Student_Free(Student *a)
	{
		free(a->name);
		free(a->otherInfo);
		free(a);
	}

	static	int Student_cmp(Student **a,Student **b)
	{
		int		ret;

		ret=strcmp((*a)->name,(*b)->name);
		return ret;
	}
	
	int	main()
	{
		Students *s,*snew;
		Student	*s1,*one,*s2;
		int i,num;
	
		//@1
		s=sk_Student_new_null();

		snew=sk_Student_new(Student_cmp);

		//@2
		s2=Student_Malloc();
		sk_Student_push(snew,s2);


		s1=Student_Malloc();
		sk_Student_push(s,s1);

		//@3
		i=sk_Student_find(snew,s2);

		num=sk_Student_num(s);
		for(i=0;i<num;i++)
		{
			one=sk_Student_value(s,i);
			printf("student name :	%s\n",one->name);
			printf("sutdent	age  :	%d\n",one->age);
			printf("student otherinfo :	%s\n\n\n",one->otherInfo);
		}

		//@4
		sk_Student_pop_free(s,Student_Free);
		sk_Student_pop_free(snew,Student_Free);
		return 0;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值