]> glassweightruler.freedombox.rocks Git - Ventoy.git/commitdiff
Show a warning message if ventoy.json is in UCS-2 encoding. (#1125)
authorlongpanda <admin@ventoy.net>
Mon, 8 Nov 2021 06:55:08 +0000 (14:55 +0800)
committerlongpanda <admin@ventoy.net>
Mon, 8 Nov 2021 06:55:08 +0000 (14:55 +0800)
ventoy.json must in UTF-8 encoding. The BOM is auto skipped if exist.

GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_plugin.c
INSTALL/grub/grub.cfg

index 77c833ce97cb3e5604969b051a94002f2a59e4ea..ca0e96dc1cc504191ad42397f003d929450ceb5a 100644 (file)
@@ -2399,7 +2399,9 @@ static int ventoy_parse_plugin_config(VTOY_JSON *json, const char *isodisk)
 grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **args)
 {
     int ret = 0;
+    int offset = 0;
     char *buf = NULL;
+    grub_uint8_t *code = NULL;
     grub_file_t file;
     VTOY_JSON *json = NULL;
     
@@ -2436,9 +2438,25 @@ grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **a
         return 1;
     }
 
-    
+    code = (grub_uint8_t *)buf;
+    if (code[0] == 0xef && code[1] == 0xbb && code[2] == 0xbf)
+    {
+        offset = 3; /* Skip UTF-8 BOM */
+    }
+    else if ((code[0] == 0xff && code[1] == 0xfe) || (code[0] == 0xfe && code[1] == 0xff))
+    {
+        grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
+        grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR");
 
-    ret = vtoy_json_parse(json, buf);
+        grub_env_set("VTOY_PLUGIN_ENCODE_ERROR", "1");
+        grub_env_export("VTOY_PLUGIN_ENCODE_ERROR");
+
+        debug("Failed to parse json string %d\n", ret);
+        grub_free(buf);
+        return 1;
+    }
+    
+    ret = vtoy_json_parse(json, buf + offset);
     if (ret)
     {
         grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
index 4acad0d4ea2511cb7405882a93022d1337f9749b..531faacd7f6c6f6db81f7689ca13caa683699351 100644 (file)
@@ -2178,12 +2178,19 @@ fi
 
 if [ -n "$VTOY_PLUGIN_SYNTAX_ERROR" ]; then
     clear
-    echo -e "\n Syntax error detected in ventoy.json, please check! \n"
-    echo -e " ventoy.json 文件中有语法错误,所有配置都不会生效,请检查!\n"
+    if [ -n "$VTOY_PLUGIN_ENCODE_ERROR" ]; then
+        echo -e "\n Encoding type for ventoy.json is not supported, please convert to UTF-8.\n"
+        echo -e " ventoy.json 文件编码格式不支持,请转换为 UTF-8 编码格式!\n"
+    else
+        echo -e "\n Syntax error detected in ventoy.json, please check! \n"
+        echo -e " ventoy.json 文件中有语法错误,所有配置都不会生效,请检查!\n"         
+    fi
+
     echo -e "\n press ENTER to continue (请按 回车 键继续) ..."
-    read vtInputKey 
+    read vtInputKey
 fi
 
+
 for vtTFile in ventoy.json ventoy_grub.cfg; do
     if [ -f $vtoy_efi_part/ventoy/$vtTFile ]; then
         clear