Fix memory leak in pspp-convert
[pspp] / utilities / pspp-convert.c
index ebd340ec3599e3cf942fa0e015a09d51c4c10e56..f53e28a83e1ca2262366e3ec75d256bb3b17a093 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2013, 2014 Free Software Foundation, Inc.
+   Copyright (C) 2013, 2014, 2015 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "data/casereader.h"
 #include "data/casewriter.h"
 #include "data/csv-file-writer.h"
+#include "data/encrypted-file.h"
 #include "data/file-name.h"
 #include "data/por-file-writer.h"
 #include "data/settings.h"
-#include "data/sys-file-encryption.h"
 #include "data/sys-file-writer.h"
 #include "data/file-handle-def.h"
 #include "libpspp/assertion.h"
 
 static void usage (void);
 
-static void decrypt_sav_file (struct encrypted_sys_file *enc,
-                              const char *input_filename,
-                              const char *output_filename,
-                              const char *password);
+static bool decrypt_file (struct encrypted_file *enc,
+                          const struct file_handle *input_filename,
+                          const struct file_handle *output_filename,
+                          const char *password);
 
 int
 main (int argc, char *argv[])
@@ -58,11 +58,11 @@ main (int argc, char *argv[])
   const char *output_filename;
 
   long long int max_cases = LLONG_MAX;
-  struct dictionary *dict;
+  struct dictionary *dict = NULL;
   struct casereader *reader;
   struct file_handle *input_fh;
   const char *encoding = NULL;
-  struct encrypted_sys_file *enc;
+  struct encrypted_file *enc;
 
   const char *output_format = NULL;
   struct file_handle *output_fh;
@@ -125,7 +125,7 @@ main (int argc, char *argv[])
           exit (EXIT_SUCCESS);
 
         default:
-          exit (EXIT_FAILURE);
+          goto error;
         }
     }
 
@@ -135,6 +135,8 @@ main (int argc, char *argv[])
 
   input_filename = argv[optind];
   output_filename = argv[optind + 1];
+  input_fh = fh_create_file (NULL, input_filename, NULL, fh_default_properties ());
+
   if (output_format == NULL)
     {
       const char *dot = strrchr (output_filename, '.');
@@ -145,22 +147,33 @@ main (int argc, char *argv[])
       output_format = dot + 1;
     }
 
-  if (encrypted_sys_file_open (&enc, input_filename) > 0)
+  output_fh = fh_create_file (NULL, output_filename, NULL, fh_default_properties ());
+  if (encrypted_file_open (&enc, input_fh) > 0)
     {
-      if (strcmp (output_format, "sav") && strcmp (output_format, "sys"))
-        error (1, 0, _("can only convert encrypted data file to sav or sys "
-                       "format"));
+      if (encrypted_file_is_sav (enc))
+        {
+          if (strcmp (output_format, "sav") && strcmp (output_format, "sys"))
+            error (1, 0, _("can only convert encrypted data file to sav or "
+                           "sys format"));
+        }
+      else
+        {
+          if (strcmp (output_format, "sps"))
+            error (1, 0, _("can only convert encrypted syntax file to sps "
+                           "format"));
+        }
 
-      decrypt_sav_file (enc, input_filename, output_filename, password);
+      if (! decrypt_file (enc, input_fh, output_fh, password))
+       goto error;
+         
       goto exit;
     }
 
-  input_fh = fh_create_file (NULL, input_filename, fh_default_properties ());
+
   reader = any_reader_open_and_decode (input_fh, encoding, &dict, NULL);
   if (reader == NULL)
-    exit (1);
+    goto error;
 
-  output_fh = fh_create_file (NULL, output_filename, fh_default_properties ());
   if (!strcmp (output_format, "csv") || !strcmp (output_format, "txt"))
     {
       struct csv_writer_options options;
@@ -207,32 +220,46 @@ main (int argc, char *argv[])
     error (1, 0, _("%s: error writing output file"), output_filename);
 
 exit:
+  dict_destroy (dict);
+  fh_unref (output_fh);
+  fh_unref (input_fh);
   fh_done ();
   i18n_done ();
 
   return 0;
+
+error:
+  dict_destroy (dict);
+  fh_unref (output_fh);
+  fh_unref (input_fh);
+  fh_done ();
+  i18n_done ();
+
+  return 1;
 }
 
-static void
-decrypt_sav_file (struct encrypted_sys_file *enc,
-                  const char *input_filename,
-                  const char *output_filename,
-                  const char *password)
+static bool
+decrypt_file (struct encrypted_file *enc,
+             const struct file_handle *ifh,
+             const struct file_handle *ofh,
+              const char *password)
 {
   FILE *out;
   int err;
+  const char *input_filename = fh_get_file_name (ifh);
+  const char *output_filename = fh_get_file_name (ofh);
 
   if (password == NULL)
     {
       password = getpass ("password: ");
       if (password == NULL)
-        exit (1);
+       return false;
     }
 
-  if (!encrypted_sys_file_unlock (enc, password))
+  if (!encrypted_file_unlock (enc, password))
     error (1, 0, _("sorry, wrong password"));
 
-  out = fn_open (output_filename, "wb");
+  out = fn_open (ofh, "wb");
   if (out == NULL)
     error (1, errno, ("%s: error opening output file"), output_filename);
 
@@ -241,7 +268,7 @@ decrypt_sav_file (struct encrypted_sys_file *enc,
       uint8_t buffer[1024];
       size_t n;
 
-      n = encrypted_sys_file_read (enc, buffer, sizeof buffer);
+      n = encrypted_file_read (enc, buffer, sizeof buffer);
       if (n == 0)
         break;
 
@@ -249,13 +276,15 @@ decrypt_sav_file (struct encrypted_sys_file *enc,
         error (1, errno, ("%s: write error"), output_filename);
     }
 
-  err = encrypted_sys_file_close (enc);
+  err = encrypted_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);
+  fn_close (ofh, out);
+
+  return true;
 }
 
 static void
@@ -264,20 +293,21 @@ usage (void)
   printf ("\
 %s, a utility for converting SPSS data files to other formats.\n\
 Usage: %s [OPTION]... INPUT OUTPUT\n\
-where INPUT is an SPSS system or portable file\n\
+where INPUT is an SPSS data file or encrypted syntax file\n\
   and OUTPUT is the name of the desired output file.\n\
 \n\
 The desired format of OUTPUT is by default inferred from its extension:\n\
   csv txt             comma-separated value\n\
   sav sys             SPSS system file\n\
   por                 SPSS portable file\n\
+  sps                 SPSS syntax file (encrypted syntax input files only)\n\
 \n\
 Options:\n\
   -O, --output-format=FORMAT  set specific output format, where FORMAT\n\
                       is one of the extensions listed above\n\
   -e, --encoding=CHARSET  override encoding of input data file\n\
   -c MAXCASES         limit number of cases to copy (default is all cases)\n\
-  -p PASSWORD         password for encrypted .sav files\n\
+  -p PASSWORD         password for encrypted files\n\
   --help              display this help and exit\n\
   --version           output version information and exit\n",
           program_name, program_name);