]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_util.c
1 /******************************************************************************
2 * ventoy_util.c ---- ventoy util
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <ventoy_define.h>
23 #include <ventoy_util.h>
25 static int g_tar_filenum
= 0;
26 static char *g_tar_buffer
= NULL
;
27 static ventoy_file
*g_tar_filelist
= NULL
;
31 unsigned char *g_unxz_buffer
= NULL
;
34 void unxz_error(char *x
)
39 int unxz_flush(void *src
, unsigned int size
)
41 memcpy(g_unxz_buffer
+ g_unxz_len
, src
, size
);
42 g_unxz_len
+= (int)size
;
48 uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes
)
53 double GB
= SizeBytes
* 1.0 / 1000 / 1000 / 1000;
55 if ((SizeBytes
% SIZE_1GB
) == 0)
57 return (uint64_t)(SizeBytes
/ SIZE_1GB
);
60 for (i
= 0; i
< 12; i
++)
64 Delta
= (Pow2
- GB
) / Pow2
;
68 Delta
= (GB
- Pow2
) / Pow2
;
82 ventoy_file
* ventoy_tar_find_file(const char *path
)
86 ventoy_file
*node
= g_tar_filelist
;
88 len
= (int)strlen(path
);
90 for (i
= 0; i
< g_tar_filenum
; i
++, node
++)
92 if (node
->pathlen
== len
&& memcmp(node
->path
, path
, len
) == 0)
97 if (node
->pathlen
> len
)
107 int ventoy_decompress_tar(char *tarbuf
, int buflen
, int *tarsize
)
112 unsigned char *buffer
= NULL
;
113 char tarxz
[MAX_PATH
];
115 #if defined(_MSC_VER) || defined(WIN32)
116 scnprintf(tarxz
, sizeof(tarxz
), "%s\\ventoy\\%s", g_ventoy_dir
, PLUGSON_TXZ
);
118 scnprintf(tarxz
, sizeof(tarxz
), "%s/tool/%s", g_ventoy_dir
, PLUGSON_TXZ
);
121 if (ventoy_read_file_to_buf(tarxz
, 0, (void **)&buffer
, &BufLen
))
123 vlog("Failed to read file <%s>\n", tarxz
);
127 g_unxz_buffer
= (unsigned char *)tarbuf
;
130 unxz(buffer
, BufLen
, NULL
, unxz_flush
, NULL
, &inused
, unxz_error
);
131 vlog("xzlen:%u rawdata size:%d\n", BufLen
, g_unxz_len
);
133 if (inused
!= BufLen
)
135 vlog("Failed to unxz data %d %d\n", inused
, BufLen
);
140 *tarsize
= g_unxz_len
;
149 int ventoy_www_init(void)
156 ventoy_file
*node
= NULL
;
157 ventoy_file
*node2
= NULL
;
158 VENTOY_TAR_HEAD
*pHead
= NULL
;
163 g_tar_filelist
= malloc(VENTOY_FILE_MAX
* sizeof(ventoy_file
));
164 g_tar_buffer
= malloc(TAR_BUF_MAX
);
168 if ((!g_tar_filelist
) || (!g_tar_buffer
))
173 if (ventoy_decompress_tar(g_tar_buffer
, TAR_BUF_MAX
, &tarsize
))
175 vlog("Failed to decompress tar\n");
179 pHead
= (VENTOY_TAR_HEAD
*)g_tar_buffer
;
180 node
= g_tar_filelist
;
182 while (g_tar_filenum
< VENTOY_FILE_MAX
&& size
< tarsize
&& memcmp(pHead
->magic
, TMAGIC
, 5) == 0)
184 if (pHead
->typeflag
== REGTYPE
)
186 node
->size
= (int)strtol(pHead
->size
, NULL
, 8);
187 node
->pathlen
= (int)scnprintf(node
->path
, MAX_PATH
, "%s", pHead
->name
);
188 node
->addr
= pHead
+ 1;
190 if (node
->pathlen
== 13 && strcmp(pHead
->name
, "www/buildtime") == 0)
192 scnprintf(g_sysinfo
.buildtime
, sizeof(g_sysinfo
.buildtime
), "%s", (char *)node
->addr
);
193 vlog("Plugson buildtime %s\n", g_sysinfo
.buildtime
);
196 offset
= 512 + VENTOY_UP_ALIGN(node
->size
, 512);
206 pHead
= (VENTOY_TAR_HEAD
*)((char *)pHead
+ offset
);
212 for (i
= 0; i
< g_tar_filenum
; i
++)
213 for (j
= i
+ 1; j
< g_tar_filenum
; j
++)
215 node
= g_tar_filelist
+ i
;
216 node2
= g_tar_filelist
+ j
;
218 if (node
->pathlen
> node2
->pathlen
)
220 memcpy(&tmpnode
, node
, sizeof(ventoy_file
));
221 memcpy(node
, node2
, sizeof(ventoy_file
));
222 memcpy(node2
, &tmpnode
, sizeof(ventoy_file
));
226 vlog("Total extract %d files from tar file.\n", g_tar_filenum
);
231 void ventoy_www_exit(void)
233 check_free(g_tar_filelist
);
234 check_free(g_tar_buffer
);
235 g_tar_filelist
= NULL
;
241 void ventoy_get_json_path(char *path
, char *backup
)
243 #if defined(_MSC_VER) || defined(WIN32)
244 scnprintf(path
, 64, "%C:\\ventoy\\ventoy.json", g_cur_dir
[0]);
247 scnprintf(backup
, 64, "%C:\\ventoy\\ventoy_backup.json", g_cur_dir
[0]);
250 scnprintf(path
, 64, "%s/ventoy/ventoy.json", g_cur_dir
);
253 scnprintf(backup
, 64, "%s/ventoy/ventoy_backup.json", g_cur_dir
);
259 static const char g_encoding_table
[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
260 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
261 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
262 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
263 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
264 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
265 'w', 'x', 'y', 'z', '0', '1', '2', '3',
266 '4', '5', '6', '7', '8', '9', '+', '/'};
268 char * ventoy_base64_encode(const char *data
, int input_length
, int *output_length
)
272 char *encoded_data
= NULL
;
273 int mod_table
[] = {0, 2, 1};
275 *output_length
= 4 * ((input_length
+ 2) / 3);
276 encoded_data
= malloc(*output_length
+ 4);
282 while (i
< input_length
)
284 unsigned int octet_a
= i
< input_length
? (unsigned char)data
[i
++] : 0;
285 unsigned int octet_b
= i
< input_length
? (unsigned char)data
[i
++] : 0;
286 unsigned int octet_c
= i
< input_length
? (unsigned char)data
[i
++] : 0;
288 unsigned int triple
= (octet_a
<< 0x10) + (octet_b
<< 0x08) + octet_c
;
290 encoded_data
[j
++] = g_encoding_table
[(triple
>> 3 * 6) & 0x3F];
291 encoded_data
[j
++] = g_encoding_table
[(triple
>> 2 * 6) & 0x3F];
292 encoded_data
[j
++] = g_encoding_table
[(triple
>> 1 * 6) & 0x3F];
293 encoded_data
[j
++] = g_encoding_table
[(triple
>> 0 * 6) & 0x3F];
296 for (i
= 0; i
< mod_table
[input_length
% 3]; i
++)
298 encoded_data
[*output_length
- 1 - i
] = '=';