+static void
+decrypt_sav_file (struct encrypted_sys_file *enc,
+ const char *input_filename,
+ const char *output_filename,
+ const char *password)
+{
+ FILE *out;
+ int err;
+
+ if (password == NULL)
+ {
+ password = getpass ("password: ");
+ if (password == NULL)
+ exit (1);
+ }
+
+ if (!encrypted_sys_file_unlock (enc, password))
+ error (1, 0, _("sorry, wrong password"));
+
+ out = fn_open (output_filename, "wb");
+ if (out == NULL)
+ error (1, errno, ("%s: error opening output file"), output_filename);
+
+ for (;;)
+ {
+ uint8_t buffer[1024];
+ size_t n;
+
+ n = encrypted_sys_file_read (enc, buffer, sizeof buffer);
+ if (n == 0)
+ break;
+
+ if (fwrite (buffer, 1, n, out) != n)
+ error (1, errno, ("%s: write error"), output_filename);
+ }
+
+ err = encrypted_sys_file_close (enc);
+ if (err)
+ error (1, err, ("%s: read error"), input_filename);
+
+ if (fflush (out) == EOF)
+ error (1, errno, ("%s: write error"), output_filename);
+ fn_close (output_filename, out);
+}
+