2017-09-14 23:11:56 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2017-03-04 21:16:59 +08:00
|
|
|
|
2017-09-14 23:11:56 +08:00
|
|
|
#include "utils.h"
|
2017-03-05 01:50:36 +08:00
|
|
|
|
2017-03-13 05:05:51 +08:00
|
|
|
void write_zero(int fd, size_t size) {
|
|
|
|
size_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
ftruncate(fd, pos + size);
|
|
|
|
lseek(fd, pos + size, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2017-03-04 21:16:59 +08:00
|
|
|
void mem_align(size_t *pos, size_t align) {
|
|
|
|
size_t mask = align - 1;
|
|
|
|
if (*pos & mask) {
|
|
|
|
*pos += align - (*pos & mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:54:23 +08:00
|
|
|
void file_align(int fd, size_t align, int out) {
|
2017-03-04 21:16:59 +08:00
|
|
|
size_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
size_t mask = align - 1;
|
2017-03-13 04:19:30 +08:00
|
|
|
size_t off;
|
2017-03-04 21:16:59 +08:00
|
|
|
if (pos & mask) {
|
2017-03-13 04:19:30 +08:00
|
|
|
off = align - (pos & mask);
|
2017-03-08 00:54:23 +08:00
|
|
|
if (out) {
|
2017-03-13 05:05:51 +08:00
|
|
|
write_zero(fd, off);
|
2017-03-13 04:19:30 +08:00
|
|
|
} else {
|
|
|
|
lseek(fd, pos + off, SEEK_SET);
|
2017-03-08 00:54:23 +08:00
|
|
|
}
|
2017-03-04 21:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_new(const char *filename) {
|
2017-04-28 21:48:38 +08:00
|
|
|
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
2017-03-04 21:16:59 +08:00
|
|
|
}
|
2017-09-15 01:45:39 +08:00
|
|
|
|
|
|
|
int check_verity_pattern(const char *s) {
|
|
|
|
int pos = 0;
|
|
|
|
if (s[0] == ',') ++pos;
|
|
|
|
if (strncmp(s + pos, "verify", 6) != 0) return -1;
|
|
|
|
pos += 6;
|
|
|
|
if (s[pos] == '=') {
|
2017-09-15 02:52:27 +08:00
|
|
|
while (s[pos] != '\0' && s[pos] != ' ' && s[pos] != '\n' && s[pos] != ',') ++pos;
|
2017-09-15 01:45:39 +08:00
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_encryption_pattern(const char *s) {
|
2017-09-26 02:04:07 +08:00
|
|
|
const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", NULL };
|
2017-09-15 01:45:39 +08:00
|
|
|
for (int i = 0 ; encrypt_list[i]; ++i) {
|
|
|
|
int len = strlen(encrypt_list[i]);
|
|
|
|
if (strncmp(s, encrypt_list[i], len) == 0)
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|