+static int ventoy_find_all_checksum
+(
+ grub_file_t file,
+ char *path,
+ int chktype,
+ int exists[VTOY_CHKSUM_NUM],
+ int *ptotexist
+)
+{
+ int i;
+ int ulen;
+ int tot = 0;
+ char c = 0;
+ char *pos = NULL;
+ char *pos1 = NULL;
+ char *pos2 = NULL;
+ char *buf = NULL;
+ char *currline = NULL;
+ char *nextline = NULL;
+ const char *uname = NULL;
+
+ tot = *ptotexist;
+
+ /* read file to buffer */
+ buf = grub_malloc(file->size + 4);
+ if (!buf)
+ {
+ return 1;
+ }
+ grub_file_read(file, buf, file->size);
+ buf[file->size] = 0;
+
+ /* parse each line */
+ for (currline = buf; currline; currline = nextline)
+ {
+ nextline = ventoy_get_line(currline);
+ VTOY_SKIP_SPACE(currline);
+
+ for (i = 0; i < VTOY_CHKSUM_NUM; i++)
+ {
+ if (exists[i])
+ {
+ continue;
+ }
+
+ uname = g_lower_chksum_name[i];
+ ulen = g_lower_chksum_namelen[i];
+
+ if (grub_strncasecmp(currline, uname, ulen) == 0)
+ {
+ pos = grub_strchr(currline, '=');
+ pos1 = grub_strchr(currline, '(');
+ pos2 = grub_strchr(currline, ')');
+
+ if (pos && pos1 && pos2)
+ {
+ c = *pos2;
+ *pos2 = 0;
+ if (ventoy_chksum_pathcmp(chktype, path, pos1 + 1) == 0)
+ {
+ exists[i] = 1;
+ tot++;
+ }
+ *pos2 = c;
+ }
+ }
+ else if (ventoy_str_len_alnum(currline, g_chksum_retlen[i]))
+ {
+ VTOY_SKIP_SPACE_NEXT_EX(pos, currline, g_chksum_retlen[i]);
+ if (ventoy_chksum_pathcmp(chktype, path, pos) == 0)
+ {
+ exists[i] = 1;
+ tot++;
+ }
+ }
+
+ if (tot >= VTOY_CHKSUM_NUM)
+ {
+ goto end;
+ }
+ }
+ }
+
+end:
+
+ *ptotexist = tot;
+ grub_free(buf);
+ return 0;
+}
+
+static grub_err_t ventoy_cmd_vtoychksum_exist(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int i = 0;
+ int cnt = 0;
+ char c = 0;
+ int tip = 0;
+ char *pos = NULL;
+ grub_file_t file = NULL;
+ const char *isopart = NULL;
+ int exists[VTOY_CHKSUM_NUM] = { 0, 0, 0, 0 };
+ int totexist = 0;
+
+ (void)argc;
+ (void)ctxt;
+
+ isopart = grub_env_get("vtoy_iso_part");
+
+ for (i = 0; i < VTOY_CHKSUM_NUM; i++)
+ {
+ if (ventoy_check_file_exist("%s%s.%s", isopart, args[0], g_lower_chksum_name[i]))
+ {
+ exists[i] = 1;
+ totexist++;
+ }
+ }
+
+ if (totexist == VTOY_CHKSUM_NUM)
+ {
+ goto end;
+ }
+
+ cnt = ventoy_str_chrcnt(args[0], '/');
+ if (cnt > 1)
+ {
+ pos = grub_strrchr(args[0], '/');
+ c = *pos;
+ *pos = 0;
+ file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s%s/VENTOY_CHECKSUM", isopart, args[0]);
+ *pos = c;
+
+ if (file)
+ {
+ if (tip == 0 && file->size > (32 * VTOY_SIZE_1KB))
+ {
+ tip = 1;
+ grub_printf("Reading checksum file...\n");
+ grub_refresh();
+ }
+
+ debug("parse local VENTOY_CHECKSUM\n");
+ ventoy_find_all_checksum(file, args[0], 2, exists, &totexist);
+ grub_file_close(file);
+ }
+ }
+
+ if (totexist == VTOY_CHKSUM_NUM)
+ {
+ goto end;
+ }
+
+ file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s/VENTOY_CHECKSUM", isopart);
+ if (file)
+ {
+ if (tip == 0 && file->size > (32 * VTOY_SIZE_1KB))
+ {
+ tip = 1;
+ grub_printf("Reading checksum file...\n");
+ grub_refresh();
+ }
+
+ debug("parse global VENTOY_CHECKSUM\n");
+ ventoy_find_all_checksum(file, args[0], (cnt > 1) ? 3 : 4, exists, &totexist);
+ grub_file_close(file);
+ }
+
+end:
+
+ ventoy_env_int_set("VT_EXIST_MD5", exists[0]);
+ ventoy_env_int_set("VT_EXIST_SHA1", exists[1]);
+ ventoy_env_int_set("VT_EXIST_SHA256", exists[2]);
+ ventoy_env_int_set("VT_EXIST_SHA512", exists[3]);
+
+ VENTOY_CMD_RETURN(0);
+}
+