-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.c
160 lines (140 loc) · 5.13 KB
/
preview.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
/*
* This file is part of cclip, clipboard manager for wayland
* Copyright (C) 2024 heather7283
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fnmatch.h>
#include <inttypes.h>
#include "preview.h"
#include "common.h"
static void generate_text_preview(char* out_buf, const char* in_buf,
size_t preview_len, size_t data_len) {
size_t in_pos = 0;
size_t out_pos = 0;
bool last_was_space = true;
preview_len -= 1; /* null terminator */
while (in_pos < data_len && out_pos < preview_len) {
unsigned char c = (unsigned char)in_buf[in_pos];
/* ASCII control characters (also space) */
if (c <= 0x20 || c == 0x7F) {
if (c == '\t' || c == '\n' || c == '\r' || c == ' ') {
if (!last_was_space) {
out_buf[out_pos++] = ' ';
last_was_space = true;
}
}
in_pos++;
continue;
}
/* ASCII printable characters */
if (c <= 0x7F) {
out_buf[out_pos++] = c;
last_was_space = false;
in_pos++;
continue;
}
/* UTF-8 from here */
size_t seq_len = 0;
uint32_t code_point = 0;
if ((c & 0xE0) == 0xC0) {
seq_len = 2;
code_point = c & 0x1F;
} else if ((c & 0xF0) == 0xE0) {
seq_len = 3;
code_point = c & 0x0F;
} else if ((c & 0xF8) == 0xF0) {
seq_len = 4;
code_point = c & 0x07;
} else {
/* invalid UTF-8 */
out_buf[out_pos++] = '?';
in_pos++;
continue;
}
/* check if UTF-8 sequence fits into both buffers */
if (in_pos + seq_len > data_len || out_pos + seq_len > preview_len) {
out_buf[out_pos++] = '?';
break;
}
/* validate continuation bytes and build codepoint */
bool seq_valid = true;
for (size_t i = 1; i < seq_len; i++) {
unsigned char b = (unsigned char)in_buf[in_pos + i];
if ((b & 0xC0) != 0x80) {
seq_valid = false;
break;
}
/* I heckin love bitwise operations (no I don't) */
code_point = (code_point << 6) | (b & 0x3F);
}
/* check for illegal overlong encodings and invalid code points
* I'm getting serious brain damage from this shit and I hate UTF-8 */
bool valid_range = false;
if (seq_len == 2) {
valid_range = code_point >= 0x80 && code_point <= 0x7FF;
} else if (seq_len == 3) {
valid_range = code_point >= 0x800 && code_point <= 0xFFFF;
} else if (seq_len == 4) {
valid_range = code_point >= 0x10000 && code_point <= 0x10FFFF;
}
/* surrogate pairs (whatever that means, some windows bullshit) */
if (code_point >= 0xD800 && code_point <= 0xDFFF) {
valid_range = false;
}
if (seq_valid && valid_range) {
for (size_t i = 0; i < seq_len; i++) {
out_buf[out_pos++] = in_buf[in_pos++];
}
} else {
out_buf[out_pos++] = '?';
in_pos++;
}
last_was_space = false;
}
out_buf[out_pos] = '\0';
}
static void generate_binary_preview(char* out_buf, size_t preview_len,
size_t data_size, const char* const mime_type) {
const char* units[] = {"B", "KiB", "MiB"};
int units_index = 0;
double size = data_size;
while (size >= 1024 && units_index < 2) {
size /= 1024;
units_index += 1;
}
if (units_index == 0) {
snprintf(out_buf, preview_len, "%s | %" PRIu64 " B", mime_type, data_size);
} else {
snprintf(out_buf, preview_len, "%s | %" PRIu64 " B (%.2f %s)",
mime_type, data_size, size, units[units_index]);
}
}
char* generate_preview(const void* const data, size_t preview_len,
const size_t data_size, const char* const mime_type) {
char* preview = calloc(preview_len, sizeof(char));
if (preview == NULL) {
die("failed to allocate memory for preview string\n");
}
if (fnmatch("text/*", mime_type, 0) == 0) {
generate_text_preview(preview, data, preview_len, data_size);
} else {
generate_binary_preview(preview, preview_len, data_size, mime_type);
}
debug("generated preview: %s\n", preview);
return preview;
}