forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar.c
196 lines (162 loc) · 4.63 KB
/
avatar.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "main.h"
/* gets avatar filepath for given client id string and stores it in dest,
* returns number of chars written */
int get_avatar_location(char_t *dest, const char_t *id)
{
char_t *p = dest + datapath_subdir(dest, AVATAR_DIRECTORY);
memcpy((char *)p, id, TOX_PUBLIC_KEY_SIZE * 2); p += TOX_PUBLIC_KEY_SIZE * 2;
strcpy((char *)p, ".png"); p += sizeof(".png") - 1;
return p - dest;
}
/* gets avatar hash filepath for given client id string and stores it in dest,
* returns number of chars written */
int get_avatar_hash_location(char_t *dest, const char_t *id)
{
char_t *p = dest + datapath_subdir(dest, AVATAR_DIRECTORY);
memcpy((char *)p, id, TOX_PUBLIC_KEY_SIZE * 2); p += TOX_PUBLIC_KEY_SIZE * 2;
strcpy((char *)p, ".hash"); p += sizeof(".hash") - 1;
return p - dest;
}
/* frees the image of an avatar, does nothing if image is NULL */
void avatar_free_image(AVATAR *avatar)
{
if (avatar->image) {
image_free(avatar->image);
avatar->image = NULL;
}
}
int load_avatar(const char_t *id, uint8_t *dest, uint32_t *size_out)
{
char_t path[512];
uint32_t size;
get_avatar_location(path, id);
uint8_t *avatar_data = file_raw((char *)path, &size);
if (!avatar_data) {
return 0;
}
if (size > TOX_AVATAR_MAX_DATA_LENGTH) {
free(avatar_data);
debug("warning: saved avatar file(%s) too large for tox\n", path);
return 0;
}
memcpy(dest, avatar_data, size);
free(avatar_data);
if (size_out) {
*size_out = size;
}
return 1;
}
int save_avatar(const char_t *id, const uint8_t *data, uint32_t size)
{
char_t path[512];
get_avatar_location(path, id);
FILE *file = fopen((char*)path, "wb");
if (file) {
fwrite(data, size, 1, file);
flush_file(file);
fclose(file);
return 1;
} else {
debug("error opening avatar file (%s) for writing\n", (char *)path);
return 0;
}
}
int delete_saved_avatar(const char_t *id)
{
char_t path[512];
get_avatar_location(path, id);
return remove((char *)path);
}
int load_avatar_hash(const char_t *id, uint8_t *dest)
{
char_t path[512];
uint32_t size;
get_avatar_hash_location(path, id);
uint8_t *hash_data = file_raw((char *)path, &size);
if (!hash_data) {
return 0;
}
if (size != TOX_HASH_LENGTH) {
debug("warning: saved avatar hash (%s) does not have TOX_HASH_LENGTH bytes\n", path);
free(hash_data);
return 0;
}
memcpy(dest, hash_data, TOX_HASH_LENGTH);
free(hash_data);
return 1;
}
int save_avatar_hash(const char_t *id, const uint8_t *hash)
{
char_t path[512];
get_avatar_hash_location(path, id);
FILE *file = fopen((char*)path, "wb");
if (file) {
fwrite(hash, TOX_HASH_LENGTH, 1, file);
flush_file(file);
fclose(file);
return 1;
} else {
debug("error opening avatar hash file (%s) for writing\n", (char *)path);
return 0;
}
}
int delete_avatar_hash(const char_t *id)
{
char_t path[512];
get_avatar_hash_location(path, id);
return remove((char *)path);
}
int set_avatar(AVATAR *avatar, const uint8_t *data, uint32_t size, _Bool create_hash)
{
if (size > TOX_AVATAR_MAX_DATA_LENGTH) {
debug("warning: avatar too large\n");
return 0;
}
uint16_t w, h;
UTOX_NATIVE_IMAGE *image = png_to_image((UTOX_PNG_IMAGE)data, size, &w, &h, 1);
if(!UTOX_NATIVE_IMAGE_IS_VALID(image)) {
debug("warning: avatar is invalid\n");
return 0;
} else {
avatar_free_image(avatar);
avatar->image = image;
avatar->width = w;
avatar->height = h;
avatar->format = TOX_AVATAR_FORMAT_PNG;
if (create_hash) {
tox_hash(avatar->hash, data, size);
}
return 1;
}
}
void unset_avatar(AVATAR *avatar)
{
avatar->format = TOX_AVATAR_FORMAT_NONE;
avatar_free_image(avatar);
}
/* sets self avatar, see self_set_and_save_avatar */
int self_set_avatar(const uint8_t *data, uint32_t size)
{
if (!set_avatar(&self.avatar, data, size, 1)) {
return 0;
}
uint8_t *png_data = malloc(size);
memcpy(png_data, data, size);
tox_postmessage(TOX_SETAVATAR, TOX_AVATAR_FORMAT_PNG, size, png_data);
return 1;
}
int self_set_and_save_avatar(const uint8_t *data, uint32_t size)
{
if (self_set_avatar(data, size)) {
save_avatar(self.id, data, size);
return 1;
} else {
return 0;
}
}
void self_remove_avatar()
{
unset_avatar(&self.avatar);
delete_saved_avatar(self.id);
tox_postmessage(TOX_UNSETAVATAR, 0, 0, NULL);
}