-
Notifications
You must be signed in to change notification settings - Fork 0
/
voucher.m
104 lines (79 loc) · 3.21 KB
/
voucher.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>
#include "helper.h"
#include "voucher.h"
mach_voucher_attr_recipe_t create_voucher_recipe(size_t content_size, uint64_t id) {
mach_voucher_attr_recipe_t user_data_recipe;
size_t user_data_recipe_size;
user_data_recipe_size = sizeof(mach_voucher_attr_recipe_data_t) + content_size;
user_data_recipe = calloc(1, user_data_recipe_size);
user_data_recipe->key = MACH_VOUCHER_ATTR_KEY_USER_DATA;
user_data_recipe->command = MACH_VOUCHER_ATTR_USER_DATA_STORE;
user_data_recipe->content_size = content_size;
*(uint64_t *)user_data_recipe->content = id;
return user_data_recipe;
}
mach_port_t create_voucher(size_t content_size, mach_voucher_attr_recipe_t recipe) {
mach_port_t new_voucher, host;
kern_return_t kr;
size_t user_data_recipe_size;
host = mach_host_self();
user_data_recipe_size = sizeof(mach_voucher_attr_recipe_data_t) + content_size;
new_voucher = MACH_PORT_NULL;
kr = host_create_mach_voucher(
host,
(mach_voucher_attr_raw_recipe_array_t) recipe,
user_data_recipe_size,
&new_voucher);
return new_voucher;
}
mach_port_t redeem_voucher(mach_port_t previous_voucher) {
mach_port_t host, new_voucher;
kern_return_t kr;
mach_voucher_attr_recipe_t user_data_recipe;
size_t user_data_recipe_size;
host = mach_host_self();
user_data_recipe_size = sizeof(mach_voucher_attr_recipe_data_t);
user_data_recipe = calloc(1, user_data_recipe_size);
user_data_recipe->key = MACH_VOUCHER_ATTR_KEY_USER_DATA;
user_data_recipe->command = MACH_VOUCHER_ATTR_REDEEM;
user_data_recipe->previous_voucher = previous_voucher;
new_voucher = MACH_PORT_NULL;
kr = host_create_mach_voucher(
host,
(mach_voucher_attr_raw_recipe_array_t) user_data_recipe,
user_data_recipe_size,
&new_voucher);
free(user_data_recipe);
return new_voucher;
}
mach_voucher_attr_recipe_t multiple_redeem_voucher_recipe(mach_port_t previous_voucher, int number_of_recipes) {
mach_voucher_attr_recipe_data_t *user_data_recipe;
int i;
user_data_recipe = calloc(number_of_recipes, sizeof(mach_voucher_attr_recipe_data_t));
for (i = 0; i < number_of_recipes; i++) {
user_data_recipe[i].key = MACH_VOUCHER_ATTR_KEY_USER_DATA;
user_data_recipe[i].command = MACH_VOUCHER_ATTR_REDEEM;
user_data_recipe[i].content_size = 0;
user_data_recipe[i].previous_voucher = previous_voucher;
}
return user_data_recipe;
}
mach_port_t multiple_redeem_voucher(mach_voucher_attr_recipe_t recipe, int number_of_recipes) {
mach_port_t host, new_voucher;
kern_return_t kr;
int i;
size_t user_data_recipe_size;
host = mach_host_self();
user_data_recipe_size = sizeof(mach_voucher_attr_recipe_data_t) * number_of_recipes;
new_voucher = MACH_PORT_NULL;
kr = host_create_mach_voucher(
host,
(mach_voucher_attr_raw_recipe_array_t) recipe,
user_data_recipe_size,
&new_voucher);
return new_voucher;
}
void release_voucher(mach_port_t voucher_port) {
kern_return_t kr;
kr = mach_port_destroy(mach_task_self(), voucher_port);
}