+static int ventoy_plugin_parse_pwdstr(char *pwdstr, vtoy_password *pwd)
+{
+ int i;
+ int len;
+ char ch;
+ char *pos;
+ char bytes[3];
+ vtoy_password tmpPwd;
+
+ len = (int)grub_strlen(pwdstr);
+ if (len > 64)
+ {
+ if (NULL == pwd) grub_printf("Password too long %d\n", len);
+ return 1;
+ }
+
+ grub_memset(&tmpPwd, 0, sizeof(tmpPwd));
+
+ if (grub_strncmp(pwdstr, "txt#", 4) == 0)
+ {
+ tmpPwd.type = VTOY_PASSWORD_TXT;
+ grub_snprintf(tmpPwd.text, sizeof(tmpPwd.text), "%s", pwdstr + 4);
+ }
+ else if (grub_strncmp(pwdstr, "md5#", 4) == 0)
+ {
+ if ((len - 4) == 32)
+ {
+ for (i = 0; i < 16; i++)
+ {
+ bytes[0] = pwdstr[4 + i * 2];
+ bytes[1] = pwdstr[4 + i * 2 + 1];
+ bytes[2] = 0;
+
+ if (grub_isxdigit(bytes[0]) && grub_isxdigit(bytes[1]))
+ {
+ tmpPwd.md5[i] = (grub_uint8_t)grub_strtoul(bytes, NULL, 16);
+ }
+ else
+ {
+ if (NULL == pwd) grub_printf("Invalid md5 hex format %s %d\n", pwdstr, i);
+ return 1;
+ }
+ }
+ tmpPwd.type = VTOY_PASSWORD_MD5;
+ }
+ else if ((len - 4) > 32)
+ {
+ pos = grub_strchr(pwdstr + 4, '#');
+ if (!pos)
+ {
+ if (NULL == pwd) grub_printf("Invalid md5 password format %s\n", pwdstr);
+ return 1;
+ }
+
+ if (len - 1 - (int)(long)(pos - pwdstr) != 32)
+ {
+ if (NULL == pwd) grub_printf("Invalid md5 salt password format %s\n", pwdstr);
+ return 1;
+ }
+
+ ch = *pos;
+ *pos = 0;
+ grub_snprintf(tmpPwd.salt, sizeof(tmpPwd.salt), "%s", pwdstr + 4);
+ *pos = ch;
+
+ pos++;
+ for (i = 0; i < 16; i++)
+ {
+ bytes[0] = pos[i * 2];
+ bytes[1] = pos[i * 2 + 1];
+ bytes[2] = 0;
+
+ if (grub_isxdigit(bytes[0]) && grub_isxdigit(bytes[1]))
+ {
+ tmpPwd.md5[i] = (grub_uint8_t)grub_strtoul(bytes, NULL, 16);
+ }
+ else
+ {
+ if (NULL == pwd) grub_printf("Invalid md5 hex format %s %d\n", pwdstr, i);
+ return 1;
+ }
+ }
+
+ tmpPwd.type = VTOY_PASSWORD_SALT_MD5;
+ }
+ else
+ {
+ if (NULL == pwd) grub_printf("Invalid md5 password format %s\n", pwdstr);
+ return 1;
+ }
+ }
+ else
+ {
+ if (NULL == pwd) grub_printf("Invalid password format %s\n", pwdstr);
+ return 1;
+ }
+
+ if (pwd)
+ {
+ grub_memcpy(pwd, &tmpPwd, sizeof(tmpPwd));
+ }
+
+ return 0;
+}
+