]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_util.h
Update German, French (#1962)
[Ventoy.git] / Plugson / src / Core / ventoy_util.h
1 /******************************************************************************
2 * ventoy_util.h
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #ifndef __VENTOY_UTIL_H__
21 #define __VENTOY_UTIL_H__
22
23 #define PLUGSON_TXZ "plugson.tar.xz"
24
25 #define check_free(p) if (p) free(p)
26 #define vtoy_safe_close_fd(fd) \
27 {\
28 if ((fd) >= 0) \
29 { \
30 close(fd); \
31 (fd) = -1; \
32 }\
33 }
34
35 extern char g_cur_dir[MAX_PATH];
36 extern char g_ventoy_dir[MAX_PATH];
37
38 #if defined(_MSC_VER) || defined(WIN32)
39
40 typedef HANDLE pthread_mutex_t;
41
42 static __inline int pthread_mutex_init(pthread_mutex_t *mutex, void *unused)
43 {
44 (void)unused;
45 *mutex = CreateMutex(NULL, FALSE, NULL);
46 return *mutex == NULL ? -1 : 0;
47 }
48
49 static __inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
50 {
51 return CloseHandle(*mutex) == 0 ? -1 : 0;
52 }
53
54 static __inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
55 {
56 return ReleaseMutex(*mutex) == 0 ? -1 : 0;
57 }
58
59 static __inline int pthread_mutex_lock(pthread_mutex_t *mutex)
60 {
61 return WaitForSingleObject(*mutex, INFINITE) == WAIT_OBJECT_0 ? 0 : -1;
62 }
63
64 int ventoy_path_case(char *path, int slash);
65
66 #else
67 int ventoy_get_sys_file_line(char *buffer, int buflen, const char *fmt, ...);
68
69 #define UINT8 uint8_t
70 #define UINT16 uint16_t
71 #define UINT32 uint32_t
72 #define UINT64 uint64_t
73
74 static inline int ventoy_path_case(char *path, int slash)
75 {
76 (void)path;
77 (void)slash;
78 return 0;
79 }
80 #endif
81
82
83 #define LANGUAGE_EN 0
84 #define LANGUAGE_CN 1
85
86 typedef struct SYSINFO
87 {
88 char buildtime[128];
89 int syntax_error;
90 int invalid_config;
91 int config_save_error;
92
93 int language;
94 int pathcase;
95 char cur_fsname[64];
96 char cur_capacity[64];
97 char cur_model[256];
98 char cur_ventoy_ver[64];
99 int cur_secureboot;
100 int cur_part_style;
101
102 char ip[32];
103 char port[16];
104 }SYSINFO;
105
106 extern SYSINFO g_sysinfo;
107
108
109
110
111 #define TMAGIC "ustar"
112
113 #define REGTYPE '0'
114 #define AREGTYPE '\0'
115 #define LNKTYPE '1'
116 #define CHRTYPE '3'
117 #define BLKTYPE '4'
118 #define DIRTYPE '5'
119 #define FIFOTYPE '6'
120 #define CONTTYPE '7'
121
122 #pragma pack(1)
123
124 typedef struct tag_tar_head
125 {
126 char name[100];
127 char mode[8];
128 char uid[8];
129 char gid[8];
130 char size[12];
131 char mtime[12];
132 char chksum[8];
133 char typeflag;
134 char linkname[100];
135 char magic[6];
136 char version[2];
137 char uname[32];
138 char gname[32];
139 char devmajor[8];
140 char devminor[8];
141 char prefix[155];
142 char padding[12];
143 }VENTOY_TAR_HEAD;
144
145
146 #pragma pack()
147
148 #define VENTOY_UP_ALIGN(N, align) (((N) + ((align) - 1)) / (align) * (align))
149 #define VENTOY_FILE_MAX 2048
150
151
152 #if defined(_MSC_VER) || defined(WIN32)
153 #define million_sleep(a) Sleep(a)
154 #else
155 #define million_sleep(a) usleep((a) * 1000)
156 #endif
157
158
159 typedef struct ventoy_file
160 {
161 int size;
162 char path[MAX_PATH];
163 int pathlen;
164 void *addr;
165 }ventoy_file;
166
167
168
169 int ventoy_is_file_exist(const char *fmt, ...);
170 int ventoy_is_directory_exist(const char *fmt, ...);
171 void ventoy_gen_preudo_uuid(void *uuid);
172 uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes);
173 void ventoy_md5(const void *data, uint32_t len, uint8_t *md5);
174 int ventoy_is_disk_mounted(const char *devpath);
175 int unxz(unsigned char *in, int in_size,
176 int (*fill)(void *dest, unsigned int size),
177 int (*flush)(void *src, unsigned int size),
178 unsigned char *out, int *in_used,
179 void (*error)(char *x));
180 int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen);
181 int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen);
182 const char * ventoy_get_os_language(void);
183 int ventoy_get_file_size(const char *file);
184 int ventoy_www_init(void);
185 void ventoy_www_exit(void);
186 int ventoy_decompress_tar(char *tarbuf, int buflen, int *tarsize);
187 ventoy_file * ventoy_tar_find_file(const char *path);
188 void ventoy_get_json_path(char *path, char *backup);
189 int ventoy_copy_file(const char *a, const char *b);
190
191 typedef int (*ventoy_http_writeback_pf)(void);
192
193 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback);
194 void ventoy_stop_writeback_thread(void);
195 void ventoy_set_writeback_event(void);
196
197
198 extern unsigned char *g_unxz_buffer;
199 extern int g_unxz_len;
200 void unxz_error(char *x);
201 int unxz_flush(void *src, unsigned int size);
202 char * ventoy_base64_encode(const char *data, int input_length, int *output_length);
203
204 #endif /* __VENTOY_UTIL_H__ */
205