]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_util.h
VentoyPlugson ---- A GUI ventoy.json configurator
[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 check_free(p) if (p) free(p)
24 #define vtoy_safe_close_fd(fd) \
25 {\
26 if ((fd) >= 0) \
27 { \
28 close(fd); \
29 (fd) = -1; \
30 }\
31 }
32
33 extern char g_cur_dir[MAX_PATH];
34 extern char g_ventoy_dir[MAX_PATH];
35
36 #if defined(_MSC_VER) || defined(WIN32)
37
38 typedef HANDLE pthread_mutex_t;
39
40 static __inline int pthread_mutex_init(pthread_mutex_t *mutex, void *unused)
41 {
42 (void)unused;
43 *mutex = CreateMutex(NULL, FALSE, NULL);
44 return *mutex == NULL ? -1 : 0;
45 }
46
47 static __inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
48 {
49 return CloseHandle(*mutex) == 0 ? -1 : 0;
50 }
51
52 static __inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
53 {
54 return ReleaseMutex(*mutex) == 0 ? -1 : 0;
55 }
56
57 static __inline int pthread_mutex_lock(pthread_mutex_t *mutex)
58 {
59 return WaitForSingleObject(*mutex, INFINITE) == WAIT_OBJECT_0 ? 0 : -1;
60 }
61
62 int ventoy_path_case(char *path, int slash);
63
64 #else
65 int ventoy_get_sys_file_line(char *buffer, int buflen, const char *fmt, ...);
66
67 #define UINT8 uint8_t
68 #define UINT16 uint16_t
69 #define UINT32 uint32_t
70 #define UINT64 uint64_t
71
72 static inline int ventoy_path_case(char *path, int slash)
73 {
74 (void)path;
75 (void)slash;
76 return 0;
77 }
78 #endif
79
80
81 #define LANGUAGE_EN 0
82 #define LANGUAGE_CN 1
83
84 typedef struct SYSINFO
85 {
86 char buildtime[128];
87 int syntax_error;
88
89 int language;
90 int pathcase;
91 char cur_fsname[64];
92 char cur_capacity[64];
93 char cur_model[256];
94 char cur_ventoy_ver[64];
95 int cur_secureboot;
96 int cur_part_style;
97
98 char ip[32];
99 char port[16];
100 }SYSINFO;
101
102 extern SYSINFO g_sysinfo;
103
104
105
106
107 #define TMAGIC "ustar"
108
109 #define REGTYPE '0'
110 #define AREGTYPE '\0'
111 #define LNKTYPE '1'
112 #define CHRTYPE '3'
113 #define BLKTYPE '4'
114 #define DIRTYPE '5'
115 #define FIFOTYPE '6'
116 #define CONTTYPE '7'
117
118 #pragma pack(1)
119
120 typedef struct tag_tar_head
121 {
122 char name[100];
123 char mode[8];
124 char uid[8];
125 char gid[8];
126 char size[12];
127 char mtime[12];
128 char chksum[8];
129 char typeflag;
130 char linkname[100];
131 char magic[6];
132 char version[2];
133 char uname[32];
134 char gname[32];
135 char devmajor[8];
136 char devminor[8];
137 char prefix[155];
138 char padding[12];
139 }VENTOY_TAR_HEAD;
140
141
142
143 typedef struct VENTOY_MAGIC
144 {
145 uint32_t magic1; // 0x51 0x52 0x53 0x54
146 uint32_t xzlen; //
147 uint32_t magic2; // 0xa1 0xa2 0xa3 0xa4
148 }VENTOY_MAGIC;
149
150
151
152 #pragma pack()
153
154 #define VENTOY_UP_ALIGN(N, align) (((N) + ((align) - 1)) / (align) * (align))
155 #define VENTOY_FILE_MAX 2048
156
157
158 #if defined(_MSC_VER) || defined(WIN32)
159 #define million_sleep(a) Sleep(a)
160 #else
161 #define million_sleep(a) usleep((a) * 1000)
162 #endif
163
164
165 typedef struct ventoy_file
166 {
167 int size;
168 char path[MAX_PATH];
169 int pathlen;
170 void *addr;
171 }ventoy_file;
172
173
174
175 int ventoy_is_file_exist(const char *fmt, ...);
176 int ventoy_is_directory_exist(const char *fmt, ...);
177 void ventoy_gen_preudo_uuid(void *uuid);
178 uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes);
179 void ventoy_md5(const void *data, uint32_t len, uint8_t *md5);
180 int ventoy_is_disk_mounted(const char *devpath);
181 int unxz(unsigned char *in, int in_size,
182 int (*fill)(void *dest, unsigned int size),
183 int (*flush)(void *src, unsigned int size),
184 unsigned char *out, int *in_used,
185 void (*error)(char *x));
186 int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen);
187 int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen);
188 const char * ventoy_get_os_language(void);
189 int ventoy_get_file_size(const char *file);
190 int ventoy_www_init(void);
191 void ventoy_www_exit(void);
192 int ventoy_decompress_tar(char *tarbuf, int buflen, int *tarsize);
193 ventoy_file * ventoy_tar_find_file(const char *path);
194 void ventoy_get_json_path(char *path, char *backup);
195 int ventoy_copy_file(const char *a, const char *b);
196
197 typedef int (*ventoy_http_writeback_pf)(void);
198
199 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback);
200 void ventoy_stop_writeback_thread(void);
201 void ventoy_set_writeback_event(void);
202
203
204 extern unsigned char *g_unxz_buffer;
205 extern int g_unxz_len;
206 void unxz_error(char *x);
207 int unxz_flush(void *src, unsigned int size);
208
209 #endif /* __VENTOY_UTIL_H__ */
210