pc+-file-reader, sys-file-reader: Fix misuses of zero as null pointer.
[pspp] / src / data / pc+-file-reader.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-2000, 2006-2007, 2009-2016 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <float.h>
21 #include <inttypes.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24
25 #include "data/any-reader.h"
26 #include "data/case.h"
27 #include "data/casereader-provider.h"
28 #include "data/casereader.h"
29 #include "data/dictionary.h"
30 #include "data/file-handle-def.h"
31 #include "data/file-name.h"
32 #include "data/format.h"
33 #include "data/identifier.h"
34 #include "data/missing-values.h"
35 #include "data/value-labels.h"
36 #include "data/value.h"
37 #include "data/variable.h"
38 #include "libpspp/float-format.h"
39 #include "libpspp/i18n.h"
40 #include "libpspp/integer-format.h"
41 #include "libpspp/message.h"
42 #include "libpspp/misc.h"
43 #include "libpspp/pool.h"
44 #include "libpspp/str.h"
45
46 #include "gl/localcharset.h"
47 #include "gl/minmax.h"
48 #include "gl/xalloc.h"
49 #include "gl/xsize.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) (msgid)
54
55 struct pcp_dir_entry
56   {
57     unsigned int ofs;
58     unsigned int len;
59   };
60
61 struct pcp_directory
62   {
63     struct pcp_dir_entry main;
64     struct pcp_dir_entry variables;
65     struct pcp_dir_entry labels;
66     struct pcp_dir_entry data;
67   };
68
69 struct pcp_main_header
70   {
71     char product[63];           /* "PCSPSS SYSTEM FILE..." */
72     unsigned int nominal_case_size; /* Number of var positions. */
73     char creation_date[9];      /* "[m]m/dd/yy". */
74     char creation_time[9];      /* "[H]H:MM:SS". */
75     char file_label[65];        /* File label. */
76     unsigned int weight_index;  /* Index of weighting variable, 0 if none. */
77   };
78
79 struct pcp_var_record
80   {
81     unsigned int pos;
82
83     char name[9];
84     int width;
85     struct fmt_spec format;
86     uint8_t missing[8];
87     char *label;
88
89     bool weight;
90
91     struct pcp_value_label *val_labs;
92     size_t n_val_labs;
93
94     struct variable *var;
95   };
96
97 struct pcp_value_label
98   {
99     uint8_t value[8];
100     char *label;
101   };
102
103 /* System file reader. */
104 struct pcp_reader
105   {
106     struct any_reader any_reader;
107
108     /* Resource tracking. */
109     struct pool *pool;          /* All system file state. */
110
111     /* File data. */
112     unsigned int file_size;
113     struct any_read_info info;
114     struct pcp_directory directory;
115     struct pcp_main_header header;
116     struct pcp_var_record *vars;
117     size_t n_vars;
118
119     /* File state. */
120     struct file_handle *fh;     /* File handle. */
121     struct fh_lock *lock;       /* Mutual exclusion for file handle. */
122     FILE *file;                 /* File stream. */
123     unsigned int pos;           /* Position in file. */
124     bool error;                 /* I/O or corruption error? */
125     struct caseproto *proto;    /* Format of output cases. */
126
127     /* File format. */
128     unsigned int n_cases;       /* Number of cases */
129     const char *encoding;       /* String encoding. */
130
131     /* Decompression. */
132     bool compressed;
133     uint8_t opcodes[8];         /* Current block of opcodes. */
134     size_t opcode_idx;          /* Next opcode to interpret, 8 if none left. */
135     bool corruption_warning;    /* Warned about possible corruption? */
136   };
137
138 static struct pcp_reader *
139 pcp_reader_cast (const struct any_reader *r_)
140 {
141   assert (r_->klass == &pcp_file_reader_class);
142   return UP_CAST (r_, struct pcp_reader, any_reader);
143 }
144
145 static const struct casereader_class pcp_file_casereader_class;
146
147 static bool pcp_close (struct any_reader *);
148
149 static bool read_variables_record (struct pcp_reader *);
150
151 static void pcp_msg (struct pcp_reader *r, off_t, int class,
152                      const char *format, va_list args)
153      PRINTF_FORMAT (4, 0);
154 static void pcp_warn (struct pcp_reader *, off_t, const char *, ...)
155      PRINTF_FORMAT (3, 4);
156 static void pcp_error (struct pcp_reader *, off_t, const char *, ...)
157      PRINTF_FORMAT (3, 4);
158
159 static bool read_bytes (struct pcp_reader *, void *, size_t)
160   WARN_UNUSED_RESULT;
161 static int try_read_bytes (struct pcp_reader *, void *, size_t)
162   WARN_UNUSED_RESULT;
163 static bool read_uint16 (struct pcp_reader *, unsigned int *)
164   WARN_UNUSED_RESULT;
165 static bool read_uint32 (struct pcp_reader *, unsigned int *)
166   WARN_UNUSED_RESULT;
167 static bool read_float (struct pcp_reader *, double *)
168   WARN_UNUSED_RESULT;
169 static double parse_float (const uint8_t number[8]);
170 static bool read_string (struct pcp_reader *, char *, size_t)
171   WARN_UNUSED_RESULT;
172 static bool skip_bytes (struct pcp_reader *, size_t) WARN_UNUSED_RESULT;
173
174 static bool pcp_seek (struct pcp_reader *, off_t);
175
176 static bool pcp_is_sysmis(const uint8_t *);
177 \f
178 /* Dictionary reader. */
179
180 static bool read_dictionary (struct pcp_reader *);
181 static bool read_main_header (struct pcp_reader *, struct pcp_main_header *);
182 static void parse_header (struct pcp_reader *,
183                           const struct pcp_main_header *,
184                           struct any_read_info *, struct dictionary *);
185 static bool parse_variable_records (struct pcp_reader *, struct dictionary *,
186                                     struct pcp_var_record *, size_t n);
187
188 /* Tries to open FH for reading as an SPSS/PC+ system file.  Returns a
189    pcp_reader if successful, otherwise NULL. */
190 static struct any_reader *
191 pcp_open (struct file_handle *fh)
192 {
193   struct pcp_reader *r;
194   struct stat s;
195
196   /* Create and initialize reader. */
197   r = xzalloc (sizeof *r);
198   r->any_reader.klass = &pcp_file_reader_class;
199   r->pool = pool_create ();
200   pool_register (r->pool, free, r);
201   r->fh = fh_ref (fh);
202   r->opcode_idx = sizeof r->opcodes;
203
204   /* TRANSLATORS: this fragment will be interpolated into
205      messages in fh_lock() that identify types of files. */
206   r->lock = fh_lock (fh, FH_REF_FILE, N_("SPSS/PC+ system file"),
207                      FH_ACC_READ, false);
208   if (r->lock == NULL)
209     goto error;
210
211   /* Open file. */
212   r->file = fn_open (fh, "rb");
213   if (r->file == NULL)
214     {
215       msg (ME, _("Error opening `%s' for reading as an SPSS/PC+ "
216                  "system file: %s."),
217            fh_get_file_name (r->fh), strerror (errno));
218       goto error;
219     }
220
221   /* Fetch file size. */
222   if (fstat (fileno (r->file), &s))
223     {
224       pcp_error (r, 0, _("%s: stat failed (%s)."),
225                  fh_get_file_name (r->fh), strerror (errno));
226       goto error;
227     }
228   if (s.st_size > UINT_MAX)
229     {
230       pcp_error (r, 0, _("%s: file too large."), fh_get_file_name (r->fh));
231       goto error;
232     }
233   r->file_size = s.st_size;
234
235   /* Read dictionary. */
236   if (!read_dictionary (r))
237     goto error;
238
239   if (!pcp_seek (r, r->directory.data.ofs))
240     goto error;
241
242   return &r->any_reader;
243
244 error:
245   pcp_close (&r->any_reader);
246   return NULL;
247 }
248
249 static bool
250 pcp_read_dir_entry (struct pcp_reader *r, struct pcp_dir_entry *de)
251 {
252   if (!read_uint32 (r, &de->ofs) || !read_uint32 (r, &de->len))
253     return false;
254
255   if (de->len > r->file_size || de->ofs > r->file_size - de->len)
256     {
257       pcp_error (r, r->pos - 8, _("Directory entry is for a %u-byte record "
258                                   "starting at offset %u but file is only "
259                                   "%u bytes long."),
260                  de->ofs, de->len, r->file_size);
261       return false;
262     }
263
264   return true;
265 }
266
267 static bool
268 read_dictionary (struct pcp_reader *r)
269 {
270   unsigned int two, zero;
271
272   if (!read_uint32 (r, &two) || !read_uint32 (r, &zero))
273     return false;
274   if (two != 2 || zero != 0)
275     pcp_warn (r, 0, _("Directory fields have unexpected values "
276                       "(%u,%u)."), two, zero);
277
278   if (!pcp_read_dir_entry (r, &r->directory.main)
279       || !pcp_read_dir_entry (r, &r->directory.variables)
280       || !pcp_read_dir_entry (r, &r->directory.labels)
281       || !pcp_read_dir_entry (r, &r->directory.data))
282     return false;
283
284   if (!read_main_header (r, &r->header))
285     return false;
286
287   read_variables_record (r);
288
289   return true;
290 }
291
292 struct get_strings_aux
293   {
294     struct pool *pool;
295     char **titles;
296     char **strings;
297     bool *ids;
298     size_t allocated;
299     size_t n;
300   };
301
302 static void
303 add_string__ (struct get_strings_aux *aux,
304               const char *string, bool id, char *title)
305 {
306   if (aux->n >= aux->allocated)
307     {
308       aux->allocated = 2 * (aux->allocated + 1);
309       aux->titles = pool_realloc (aux->pool, aux->titles,
310                                   aux->allocated * sizeof *aux->titles);
311       aux->strings = pool_realloc (aux->pool, aux->strings,
312                                    aux->allocated * sizeof *aux->strings);
313       aux->ids = pool_realloc (aux->pool, aux->ids,
314                                aux->allocated * sizeof *aux->ids);
315     }
316
317   aux->titles[aux->n] = title;
318   aux->strings[aux->n] = pool_strdup (aux->pool, string);
319   aux->ids[aux->n] = id;
320   aux->n++;
321 }
322
323 static void PRINTF_FORMAT (3, 4)
324 add_string (struct get_strings_aux *aux,
325             const char *string, const char *title, ...)
326 {
327   va_list args;
328
329   va_start (args, title);
330   add_string__ (aux, string, false, pool_vasprintf (aux->pool, title, args));
331   va_end (args);
332 }
333
334 static void PRINTF_FORMAT (3, 4)
335 add_id (struct get_strings_aux *aux, const char *id, const char *title, ...)
336 {
337   va_list args;
338
339   va_start (args, title);
340   add_string__ (aux, id, true, pool_vasprintf (aux->pool, title, args));
341   va_end (args);
342 }
343
344 /* Retrieves significant string data from R in its raw format, to allow the
345    caller to try to detect the encoding in use.
346
347    Returns the number of strings retrieved N.  Sets each of *TITLESP, *IDSP,
348    and *STRINGSP to an array of N elements allocated from POOL.  For each I in
349    0...N-1, UTF-8 string *TITLESP[I] describes *STRINGSP[I], which is in
350    whatever encoding system file R uses.  *IDS[I] is true if *STRINGSP[I] must
351    be a valid PSPP language identifier, false if *STRINGSP[I] is free-form
352    text. */
353 static size_t
354 pcp_get_strings (const struct any_reader *r_, struct pool *pool,
355                  char ***titlesp, bool **idsp, char ***stringsp)
356 {
357   struct pcp_reader *r = pcp_reader_cast (r_);
358   struct get_strings_aux aux;
359   size_t var_idx;
360   size_t i, j;
361
362   aux.pool = pool;
363   aux.titles = NULL;
364   aux.strings = NULL;
365   aux.ids = NULL;
366   aux.allocated = 0;
367   aux.n = 0;
368
369   var_idx = 0;
370   for (i = 0; i < r->n_vars; i++)
371     if (r->vars[i].width != -1)
372       add_id (&aux, r->vars[i].name, _("Variable %zu"), ++var_idx);
373
374   var_idx = 0;
375   for (i = 0; i < r->n_vars; i++)
376     if (r->vars[i].width != -1)
377       {
378         var_idx++;
379         if (r->vars[i].label)
380           add_string (&aux, r->vars[i].label, _("Variable %zu Label"),
381                       var_idx);
382
383         for (j = 0; j < r->vars[i].n_val_labs; j++)
384           add_string (&aux, r->vars[i].label,
385                       _("Variable %zu Value Label %zu"), var_idx, j);
386       }
387
388   add_string (&aux, r->header.creation_date, _("Creation Date"));
389   add_string (&aux, r->header.creation_time, _("Creation Time"));
390   add_string (&aux, r->header.product, _("Product"));
391   add_string (&aux, r->header.file_label, _("File Label"));
392
393   *titlesp = aux.titles;
394   *idsp = aux.ids;
395   *stringsp = aux.strings;
396   return aux.n;
397 }
398
399 static void
400 find_and_delete_var (struct dictionary *dict, const char *name)
401 {
402   struct variable *var = dict_lookup_var (dict, name);
403   if (var)
404     dict_delete_var (dict, var);
405 }
406
407 /* Decodes the dictionary read from R, saving it into into *DICT.  Character
408    strings in R are decoded using ENCODING, or an encoding obtained from R if
409    ENCODING is null, or the locale encoding if R specifies no encoding.
410
411    If INFOP is non-null, then it receives additional info about the system
412    file, which the caller must eventually free with any_read_info_destroy()
413    when it is no longer needed.
414
415    This function consumes R.  The caller must use it again later, even to
416    destroy it with pcp_close(). */
417 static struct casereader *
418 pcp_decode (struct any_reader *r_, const char *encoding,
419             struct dictionary **dictp, struct any_read_info *infop)
420 {
421   struct pcp_reader *r = pcp_reader_cast (r_);
422   struct dictionary *dict;
423
424   if (encoding == NULL)
425     {
426       encoding = locale_charset ();
427       pcp_warn (r, -1, _("Using default encoding %s to read this SPSS/PC+ "
428                          "system file.  For best results, specify an "
429                          "encoding explicitly.  Use SYSFILE INFO with "
430                          "ENCODING=\"DETECT\" to analyze the possible "
431                          "encodings."),
432                 encoding);
433     }
434
435   dict = dict_create (encoding);
436   r->encoding = dict_get_encoding (dict);
437
438   parse_header (r, &r->header, &r->info, dict);
439   if (!parse_variable_records (r, dict, r->vars, r->n_vars))
440     goto error;
441
442   /* Create an index of dictionary variable widths for
443      pcp_read_case to use.  We cannot use the `struct variable's
444      from the dictionary we created, because the caller owns the
445      dictionary and may destroy or modify its variables. */
446   r->proto = caseproto_ref_pool (dict_get_proto (dict), r->pool);
447
448   find_and_delete_var (dict, "CASENUM_");
449   find_and_delete_var (dict, "DATE_");
450   find_and_delete_var (dict, "WEIGHT_");
451
452   *dictp = dict;
453   if (infop)
454     {
455       *infop = r->info;
456       memset (&r->info, 0, sizeof r->info);
457     }
458
459   return casereader_create_sequential
460     (NULL, r->proto, r->n_cases, &pcp_file_casereader_class, r);
461
462 error:
463   pcp_close (&r->any_reader);
464   dict_destroy (dict);
465   *dictp = NULL;
466   return NULL;
467 }
468
469 /* Closes R, which should have been returned by pcp_open() but not already
470    closed with pcp_decode() or this function.
471    Returns true if an I/O error has occurred on READER, false
472    otherwise. */
473 static bool
474 pcp_close (struct any_reader *r_)
475 {
476   struct pcp_reader *r = pcp_reader_cast (r_);
477   bool error;
478
479   if (r->file)
480     {
481       if (fn_close (r->fh, r->file) == EOF)
482         {
483           msg (ME, _("Error closing system file `%s': %s."),
484                fh_get_file_name (r->fh), strerror (errno));
485           r->error = true;
486         }
487       r->file = NULL;
488     }
489
490   any_read_info_destroy (&r->info);
491   fh_unlock (r->lock);
492   fh_unref (r->fh);
493
494   error = r->error;
495   pool_destroy (r->pool);
496
497   return !error;
498 }
499
500 /* Destroys READER. */
501 static void
502 pcp_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
503 {
504   struct pcp_reader *r = r_;
505   pcp_close (&r->any_reader);
506 }
507
508 /* Detects whether FILE is an SPSS/PC+ system file.  Returns 1 if so, 0 if
509    not, and a negative errno value if there is an error reading FILE. */
510 static int
511 pcp_detect (FILE *file)
512 {
513   static const char signature[4] = "SPSS";
514   char buf[sizeof signature];
515
516   if (fseek (file, 0x104, SEEK_SET))
517     return -errno;
518
519   if (fread (buf, sizeof buf, 1, file) != 1)
520     return ferror (file) ? -errno : 0;
521
522   return !memcmp (buf, signature, sizeof buf);
523 }
524 \f
525 /* Reads the main header of the SPSS/PC+ system file.  Initializes *HEADER and
526    *INFO, except for the string fields in *INFO, which parse_header() will
527    initialize later once the file's encoding is known. */
528 static bool
529 read_main_header (struct pcp_reader *r, struct pcp_main_header *header)
530 {
531   unsigned int base_ofs = r->directory.main.ofs;
532   unsigned int zero0, zero1, zero2, zero3;
533   size_t min_values, min_data_size;
534   unsigned int one0, one1;
535   unsigned int compressed;
536   unsigned int n_cases1;
537   uint8_t sysmis[8];
538
539   if (!pcp_seek (r, base_ofs))
540     return false;
541
542   if (r->directory.main.len < 0xb0)
543     {
544       pcp_error (r, r->pos, _("This is not an SPSS/PC+ system file."));
545       return false;
546     }
547   else if (r->directory.main.len > 0xb0)
548     pcp_warn (r, r->pos, _("Record 0 has unexpected length %u."),
549               r->directory.main.len);
550
551   if (!read_uint16 (r, &one0)
552       || !read_string (r, header->product, sizeof header->product)
553       || !read_bytes (r, sysmis, sizeof sysmis)
554       || !read_uint32 (r, &zero0)
555       || !read_uint32 (r, &zero1)
556       || !read_uint16 (r, &one1)
557       || !read_uint16 (r, &compressed)
558       || !read_uint16 (r, &header->nominal_case_size)
559       || !read_uint16 (r, &r->n_cases)
560       || !read_uint16 (r, &header->weight_index)
561       || !read_uint16 (r, &zero2)
562       || !read_uint16 (r, &n_cases1)
563       || !read_uint16 (r, &zero3)
564       || !read_string (r, header->creation_date, sizeof header->creation_date)
565       || !read_string (r, header->creation_time, sizeof header->creation_time)
566       || !read_string (r, header->file_label, sizeof header->file_label))
567     return false;
568
569   if (!pcp_is_sysmis (sysmis))
570     {
571       double d = parse_float (sysmis);
572       pcp_warn (r, base_ofs, _("Record 0 specifies unexpected system missing "
573                                "value %g (%a)."), d, d);
574     }
575   if (one0 != 1 || one1 != 1
576       || zero0 != 0 || zero1 != 0 || zero2 != 0 || zero3 != 0)
577     pcp_warn (r, base_ofs, _("Record 0 reserved fields have unexpected values "
578                              "(%u,%u,%u,%u,%u,%u)."),
579               one0, one1, zero0, zero1, zero2, zero3);
580   if (n_cases1 != r->n_cases)
581     pcp_warn (r, base_ofs, _("Record 0 case counts differ (%u versus %u)."),
582               r->n_cases, n_cases1);
583   if (compressed != 0 && compressed != 1)
584     {
585       pcp_error (r, base_ofs, _("Invalid compression type %u."), compressed);
586       return false;
587     }
588
589   r->compressed = compressed != 0;
590
591   min_values = xtimes (header->nominal_case_size, r->n_cases);
592   min_data_size = xtimes (compressed ? 1 : 8, min_values);
593   if (r->directory.data.len < min_data_size
594       || size_overflow_p (min_data_size))
595     {
596       pcp_warn (r, base_ofs, _("Record 0 claims %u cases with %u values per "
597                                "case (requiring at least %zu bytes) but data "
598                                "record is only %u bytes long."),
599                 r->n_cases, header->nominal_case_size, min_data_size,
600                 r->directory.data.len);
601       return true;
602     }
603
604   return true;
605 }
606
607 static bool
608 read_value_labels (struct pcp_reader *r, struct pcp_var_record *var,
609                    unsigned int start, unsigned int end)
610 {
611   size_t allocated_val_labs = 0;
612
613   start += 7;
614   end += 7;
615   if (end > r->directory.labels.len)
616     {
617       pcp_warn (r, r->pos - 32,
618                 _("Value labels claimed to end at offset %u in labels record "
619                   "but labels record is only %u bytes."),
620                 end, r->directory.labels.len);
621       return true;
622     }
623
624   start += r->directory.labels.ofs;
625   end += r->directory.labels.ofs;
626   if (start > end || end > r->file_size)
627     {
628       pcp_warn (r, r->pos - 32,
629                 _("Value labels claimed to be at offset %u with length %u "
630                   "but file size is only %u bytes."),
631                 start, end - start, r->file_size);
632       return true;
633     }
634
635   if (!pcp_seek (r, start))
636     return false;
637
638   while (r->pos < end && end - r->pos > 8)
639     {
640       struct pcp_value_label *vl;
641       uint8_t len;
642
643       if (var->n_val_labs >= allocated_val_labs)
644         var->val_labs = pool_2nrealloc (r->pool, var->val_labs,
645                                         &allocated_val_labs,
646                                         sizeof *var->val_labs);
647       vl = &var->val_labs[var->n_val_labs];
648
649       if (!read_bytes (r, vl->value, sizeof vl->value)
650           || !read_bytes (r, &len, 1))
651         return false;
652
653       if (end - r->pos < len)
654         {
655           pcp_warn (r, r->pos,
656                     _("Value labels end with partial label (%u bytes left in "
657                       "record, label length %"PRIu8")."),
658                     end - r->pos, len);
659           return true;
660         }
661       vl->label = pool_malloc (r->pool, len + 1);
662       if (!read_bytes (r, vl->label, len))
663         return false;
664
665       vl->label[len] = '\0';
666       var->n_val_labs++;
667     }
668   if (r->pos < end)
669     pcp_warn (r, r->pos, _("%u leftover bytes following value labels."),
670               end - r->pos);
671
672   return true;
673 }
674
675 static bool
676 read_var_label (struct pcp_reader *r, struct pcp_var_record *var,
677                 unsigned int ofs)
678 {
679   uint8_t len;
680
681   ofs += 7;
682   if (ofs >= r->directory.labels.len)
683     {
684       pcp_warn (r, r->pos - 32,
685                 _("Variable label claimed to start at offset %u in labels "
686                   "record but labels record is only %u bytes."),
687                 ofs, r->directory.labels.len);
688       return true;
689     }
690
691   if (!pcp_seek (r, ofs + r->directory.labels.ofs) || !read_bytes (r, &len, 1))
692     return false;
693
694   if (len >= r->directory.labels.len - ofs)
695     {
696       pcp_warn (r, r->pos - 1,
697                 _("Variable label with length %u starting at offset %u in "
698                   "labels record overruns end of %u-byte labels record."),
699                 len, ofs + 1, r->directory.labels.len);
700       return false;
701     }
702
703   var->label = pool_malloc (r->pool, len + 1);
704   var->label[len] = '\0';
705   return read_bytes (r, var->label, len);
706 }
707
708 /* Reads the variables record (record 1) into R. */
709 static bool
710 read_variables_record (struct pcp_reader *r)
711 {
712   unsigned int i;
713   bool weighted;
714
715   if (!pcp_seek (r, r->directory.variables.ofs))
716     return false;
717   if (r->directory.variables.len != r->header.nominal_case_size * 32)
718     {
719       pcp_error (r, r->pos, _("Record 1 has length %u (expected %u)."),
720                  r->directory.variables.len, r->header.nominal_case_size * 32);
721       return false;
722     }
723
724   r->vars = pool_calloc (r->pool,
725                          r->header.nominal_case_size, sizeof *r->vars);
726   weighted = false;
727   for (i = 0; i < r->header.nominal_case_size; i++)
728     {
729       struct pcp_var_record *var = &r->vars[r->n_vars++];
730       unsigned int value_label_start, value_label_end;
731       unsigned int var_label_ofs;
732       unsigned int format;
733       uint8_t raw_type;
734
735       var->pos = r->pos;
736       if (!read_uint32 (r, &value_label_start)
737           || !read_uint32 (r, &value_label_end)
738           || !read_uint32 (r, &var_label_ofs)
739           || !read_uint32 (r, &format)
740           || !read_string (r, var->name, sizeof var->name)
741           || !read_bytes (r, var->missing, sizeof var->missing))
742         return false;
743
744       var->weight = r->header.weight_index && i == r->header.weight_index - 1;
745       if (var->weight)
746         weighted = true;
747
748       raw_type = format >> 16;
749       if (!fmt_from_io (raw_type, &var->format.type))
750         {
751           pcp_error (r, var->pos, _("Variable %u has invalid type %"PRIu8"."),
752                      i, raw_type);
753           return false;
754         }
755
756       var->format.w = (format >> 8) & 0xff;
757       var->format.d = format & 0xff;
758       fmt_fix_output (&var->format);
759       var->width = fmt_var_width (&var->format);
760
761       if (var_label_ofs)
762         {
763           unsigned int save_pos = r->pos;
764           if (!read_var_label (r, var, var_label_ofs)
765               || !pcp_seek (r, save_pos))
766             return false;
767         }
768
769       if (value_label_end > value_label_start && var->width <= 8)
770         {
771           unsigned int save_pos = r->pos;
772           if (!read_value_labels (r, var, value_label_start, value_label_end)
773               || !pcp_seek (r, save_pos))
774             return false;
775         }
776
777       if (var->width > 8)
778         {
779           int extra = DIV_RND_UP (var->width - 8, 8);
780           i += extra;
781           if (!skip_bytes (r, 32 * extra))
782             return false;
783         }
784     }
785
786   if (r->header.weight_index && !weighted)
787     pcp_warn (r, -1, _("Invalid weight index %u."), r->header.weight_index);
788
789   return true;
790 }
791
792 static char *
793 recode_and_trim_string (struct pool *pool, const char *from, const char *in)
794 {
795   struct substring out;
796
797   out = recode_substring_pool ("UTF-8", from, ss_cstr (in), pool);
798   ss_trim (&out, ss_cstr (" "));
799   return ss_xstrdup (out);
800 }
801
802 static void
803 parse_header (struct pcp_reader *r, const struct pcp_main_header *header,
804               struct any_read_info *info, struct dictionary *dict)
805 {
806   const char *dict_encoding = dict_get_encoding (dict);
807   char *label;
808
809   memset (info, 0, sizeof *info);
810
811   info->integer_format = INTEGER_LSB_FIRST;
812   info->float_format = FLOAT_IEEE_DOUBLE_LE;
813   info->compression = r->compressed ? ANY_COMP_SIMPLE : ANY_COMP_NONE;
814   info->case_cnt = r->n_cases;
815
816   /* Convert file label to UTF-8 and put it into DICT. */
817   label = recode_and_trim_string (r->pool, dict_encoding, header->file_label);
818   dict_set_label (dict, label);
819   free (label);
820
821   /* Put creation date, time, and product in UTF-8 into INFO. */
822   info->creation_date = recode_and_trim_string (r->pool, dict_encoding,
823                                                 header->creation_date);
824   info->creation_time = recode_and_trim_string (r->pool, dict_encoding,
825                                                 header->creation_time);
826   info->product = recode_and_trim_string (r->pool, dict_encoding,
827                                           header->product);
828 }
829
830 /* Reads a variable (type 2) record from R and adds the
831    corresponding variable to DICT.
832    Also skips past additional variable records for long string
833    variables. */
834 static bool
835 parse_variable_records (struct pcp_reader *r, struct dictionary *dict,
836                         struct pcp_var_record *var_recs, size_t n_var_recs)
837 {
838   const char *dict_encoding = dict_get_encoding (dict);
839   struct pcp_var_record *rec;
840
841   for (rec = var_recs; rec < &var_recs[n_var_recs]; rec++)
842     {
843       struct variable *var;
844       char *name;
845       size_t i;
846
847       name = recode_string_pool ("UTF-8", dict_encoding,
848                                  rec->name, -1, r->pool);
849       name[strcspn (name, " ")] = '\0';
850
851       /* Transform $DATE => DATE_, $WEIGHT => WEIGHT_, $CASENUM => CASENUM_. */
852       if (name[0] == '$')
853         name = pool_asprintf (r->pool, "%s_", name + 1);
854
855       if (!dict_id_is_valid (dict, name, false) || name[0] == '#')
856         {
857           pcp_error (r, rec->pos, _("Invalid variable name `%s'."), name);
858           return false;
859         }
860
861       var = rec->var = dict_create_var (dict, name, rec->width);
862       if (var == NULL)
863         {
864           char *new_name = dict_make_unique_var_name (dict, NULL, NULL);
865           pcp_warn (r, rec->pos, _("Renaming variable with duplicate name "
866                                    "`%s' to `%s'."),
867                     name, new_name);
868           var = rec->var = dict_create_var_assert (dict, new_name, rec->width);
869           free (new_name);
870         }
871       if (rec->weight)
872         {
873           if (!rec->width)
874             dict_set_weight (dict, var);
875           else
876             pcp_warn (r, rec->pos,
877                       _("Cannot weight by string variable `%s'."), name);
878         }
879
880       /* Set the short name the same as the long name. */
881       var_set_short_name (var, 0, name);
882
883       /* Get variable label, if any. */
884       if (rec->label)
885         {
886           char *utf8_label;
887
888           utf8_label = recode_string ("UTF-8", dict_encoding, rec->label, -1);
889           var_set_label (var, utf8_label);
890           free (utf8_label);
891         }
892
893       /* Add value labels. */
894       for (i = 0; i < rec->n_val_labs; i++)
895         {
896           union value value;
897           char *utf8_label;
898
899           value_init (&value, rec->width);
900           if (var_is_numeric (var))
901             value.f = parse_float (rec->val_labs[i].value);
902           else
903             memcpy (value_str_rw (&value, rec->width),
904                     rec->val_labs[i].value, rec->width);
905
906           utf8_label = recode_string ("UTF-8", dict_encoding,
907                                       rec->val_labs[i].label, -1);
908           var_add_value_label (var, &value, utf8_label);
909           free (utf8_label);
910
911           value_destroy (&value, rec->width);
912         }
913
914       /* Set missing values. */
915       if (rec->width <= 8 && !pcp_is_sysmis (rec->missing))
916         {
917           int width = var_get_width (var);
918           struct missing_values mv;
919
920           mv_init_pool (r->pool, &mv, width);
921           if (var_is_numeric (var))
922             mv_add_num (&mv, parse_float (rec->missing));
923           else
924             mv_add_str (&mv, rec->missing, MIN (width, 8));
925           var_set_missing_values (var, &mv);
926         }
927
928       /* Set formats. */
929       var_set_both_formats (var, &rec->format);
930     }
931
932   return true;
933 }
934 \f
935 /* Case reader. */
936
937 static void read_error (struct casereader *, const struct pcp_reader *);
938
939 static bool read_case_number (struct pcp_reader *, double *);
940 static int read_case_string (struct pcp_reader *, uint8_t *, size_t);
941 static int read_opcode (struct pcp_reader *);
942 static bool read_compressed_number (struct pcp_reader *, double *);
943 static int read_compressed_string (struct pcp_reader *, uint8_t *);
944 static int read_whole_strings (struct pcp_reader *, uint8_t *, size_t);
945
946 /* Reads and returns one case from READER's file.  Returns a null
947    pointer if not successful. */
948 static struct ccase *
949 pcp_file_casereader_read (struct casereader *reader, void *r_)
950 {
951   struct pcp_reader *r = r_;
952   unsigned int start_pos = r->pos;
953   struct ccase *c;
954   int retval;
955   int i;
956
957   if (r->error || !r->n_cases)
958     return NULL;
959   r->n_cases--;
960
961   c = case_create (r->proto);
962   for (i = 0; i < r->n_vars; i++)
963     {
964       struct pcp_var_record *var = &r->vars[i];
965       union value *v = case_data_rw_idx (c, i);
966
967       if (var->width == 0)
968         retval = read_case_number (r, &v->f);
969       else
970         retval = read_case_string (r, value_str_rw (v, var->width),
971                                    var->width);
972
973       if (retval != 1)
974         {
975           pcp_error (r, r->pos, _("File ends in partial case."));
976           goto error;
977         }
978     }
979   if (r->pos > r->directory.data.ofs + r->directory.data.len)
980     {
981       pcp_error (r, r->pos, _("Case beginning at offset 0x%08x extends past "
982                               "end of data record at offset 0x%08x."),
983                  start_pos, r->directory.data.ofs + r->directory.data.len);
984       goto error;
985     }
986
987   return c;
988
989 error:
990   read_error (reader, r);
991   case_unref (c);
992   return NULL;
993 }
994
995 /* Issues an error that an unspecified error occurred PCP, and
996    marks R tainted. */
997 static void
998 read_error (struct casereader *r, const struct pcp_reader *pcp)
999 {
1000   msg (ME, _("Error reading case from file %s."), fh_get_name (pcp->fh));
1001   casereader_force_error (r);
1002 }
1003
1004 /* Reads a number from R and stores its value in *D.
1005    If R is compressed, reads a compressed number;
1006    otherwise, reads a number in the regular way.
1007    Returns true if successful, false if end of file is
1008    reached immediately. */
1009 static bool
1010 read_case_number (struct pcp_reader *r, double *d)
1011 {
1012   if (!r->compressed)
1013     {
1014       uint8_t number[8];
1015       if (!try_read_bytes (r, number, sizeof number))
1016         return false;
1017       *d = parse_float (number);
1018       return true;
1019     }
1020   else
1021     return read_compressed_number (r, d);
1022 }
1023
1024 /* Reads LENGTH string bytes from R into S.  Always reads a multiple of 8
1025    bytes; if LENGTH is not a multiple of 8, then extra bytes are read and
1026    discarded without being written to S.  Reads compressed strings if S is
1027    compressed.  Returns 1 if successful, 0 if end of file is reached
1028    immediately, or -1 for some kind of error. */
1029 static int
1030 read_case_string (struct pcp_reader *r, uint8_t *s, size_t length)
1031 {
1032   size_t whole = ROUND_DOWN (length, 8);
1033   size_t partial = length % 8;
1034
1035   if (whole)
1036     {
1037       int retval = read_whole_strings (r, s, whole);
1038       if (retval != 1)
1039         return retval;
1040     }
1041
1042   if (partial)
1043     {
1044       uint8_t bounce[8];
1045       int retval = read_whole_strings (r, bounce, sizeof bounce);
1046       if (retval <= 0)
1047         return -1;
1048       memcpy (s + whole, bounce, partial);
1049     }
1050
1051   return 1;
1052 }
1053
1054 /* Reads and returns the next compression opcode from R. */
1055 static int
1056 read_opcode (struct pcp_reader *r)
1057 {
1058   assert (r->compressed);
1059   if (r->opcode_idx >= sizeof r->opcodes)
1060     {
1061       int retval = try_read_bytes (r, r->opcodes, sizeof r->opcodes);
1062       if (retval != 1)
1063         return -1;
1064       r->opcode_idx = 0;
1065     }
1066   return r->opcodes[r->opcode_idx++];
1067 }
1068
1069 /* Reads a compressed number from R and stores its value in D.
1070    Returns true if successful, false if end of file is
1071    reached immediately. */
1072 static bool
1073 read_compressed_number (struct pcp_reader *r, double *d)
1074 {
1075   int opcode = read_opcode (r);
1076   switch (opcode)
1077     {
1078     case -1:
1079       return false;
1080
1081     case 0:
1082       *d = SYSMIS;
1083       return true;
1084
1085     case 1:
1086       return read_float (r, d);
1087
1088     default:
1089       *d = opcode - 105.0;
1090       return true;
1091     }
1092 }
1093
1094 /* Reads a compressed 8-byte string segment from R and stores it in DST. */
1095 static int
1096 read_compressed_string (struct pcp_reader *r, uint8_t *dst)
1097 {
1098   int opcode;
1099   int retval;
1100
1101   opcode = read_opcode (r);
1102   switch (opcode)
1103     {
1104     case -1:
1105       return 0;
1106
1107     case 1:
1108       retval = read_bytes (r, dst, 8);
1109       return retval == 1 ? 1 : -1;
1110
1111     default:
1112       if (!r->corruption_warning)
1113         {
1114           r->corruption_warning = true;
1115           pcp_warn (r, r->pos,
1116                     _("Possible compressed data corruption: "
1117                       "string contains compressed integer (opcode %d)."),
1118                     opcode);
1119       }
1120       memset (dst, ' ', 8);
1121       return 1;
1122     }
1123 }
1124
1125 /* Reads LENGTH string bytes from R into S.  LENGTH must be a multiple of 8.
1126    Reads compressed strings if S is compressed.  Returns 1 if successful, 0 if
1127    end of file is reached immediately, or -1 for some kind of error. */
1128 static int
1129 read_whole_strings (struct pcp_reader *r, uint8_t *s, size_t length)
1130 {
1131   assert (length % 8 == 0);
1132   if (!r->compressed)
1133     return try_read_bytes (r, s, length);
1134   else
1135     {
1136       size_t ofs;
1137
1138       for (ofs = 0; ofs < length; ofs += 8)
1139         {
1140           int retval = read_compressed_string (r, s + ofs);
1141           if (retval != 1)
1142             return -1;
1143           }
1144       return 1;
1145     }
1146 }
1147 \f
1148 /* Messages. */
1149
1150 /* Displays a corruption message. */
1151 static void
1152 pcp_msg (struct pcp_reader *r, off_t offset,
1153          int class, const char *format, va_list args)
1154 {
1155   struct msg m;
1156   struct string text;
1157
1158   ds_init_empty (&text);
1159   if (offset >= 0)
1160     ds_put_format (&text, _("`%s' near offset 0x%llx: "),
1161                    fh_get_file_name (r->fh), (long long int) offset);
1162   else
1163     ds_put_format (&text, _("`%s': "), fh_get_file_name (r->fh));
1164   ds_put_vformat (&text, format, args);
1165
1166   m.category = msg_class_to_category (class);
1167   m.severity = msg_class_to_severity (class);
1168   m.file_name = NULL;
1169   m.first_line = 0;
1170   m.last_line = 0;
1171   m.first_column = 0;
1172   m.last_column = 0;
1173   m.text = ds_cstr (&text);
1174
1175   msg_emit (&m);
1176 }
1177
1178 /* Displays a warning for offset OFFSET in the file. */
1179 static void
1180 pcp_warn (struct pcp_reader *r, off_t offset, const char *format, ...)
1181 {
1182   va_list args;
1183
1184   va_start (args, format);
1185   pcp_msg (r, offset, MW, format, args);
1186   va_end (args);
1187 }
1188
1189 /* Displays an error for the current file position,
1190    marks it as in an error state,
1191    and aborts reading it using longjmp. */
1192 static void
1193 pcp_error (struct pcp_reader *r, off_t offset, const char *format, ...)
1194 {
1195   va_list args;
1196
1197   va_start (args, format);
1198   pcp_msg (r, offset, ME, format, args);
1199   va_end (args);
1200
1201   r->error = true;
1202 }
1203 \f
1204 /* Reads BYTE_CNT bytes into BUF.
1205    Returns 1 if exactly BYTE_CNT bytes are successfully read.
1206    Returns -1 if an I/O error or a partial read occurs.
1207    Returns 0 for an immediate end-of-file and, if EOF_IS_OK is false, reports
1208    an error. */
1209 static inline int
1210 read_bytes_internal (struct pcp_reader *r, bool eof_is_ok,
1211                      void *buf, size_t byte_cnt)
1212 {
1213   size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1214   r->pos += bytes_read;
1215   if (bytes_read == byte_cnt)
1216     return 1;
1217   else if (ferror (r->file))
1218     {
1219       pcp_error (r, r->pos, _("System error: %s."), strerror (errno));
1220       return -1;
1221     }
1222   else if (!eof_is_ok || bytes_read != 0)
1223     {
1224       pcp_error (r, r->pos, _("Unexpected end of file."));
1225       return -1;
1226     }
1227   else
1228     return 0;
1229 }
1230
1231 /* Reads BYTE_CNT into BUF.
1232    Returns true if successful.
1233    Returns false upon I/O error or if end-of-file is encountered. */
1234 static bool
1235 read_bytes (struct pcp_reader *r, void *buf, size_t byte_cnt)
1236 {
1237   return read_bytes_internal (r, false, buf, byte_cnt) == 1;
1238 }
1239
1240 /* Reads BYTE_CNT bytes into BUF.
1241    Returns 1 if exactly BYTE_CNT bytes are successfully read.
1242    Returns 0 if an immediate end-of-file is encountered.
1243    Returns -1 if an I/O error or a partial read occurs. */
1244 static int
1245 try_read_bytes (struct pcp_reader *r, void *buf, size_t byte_cnt)
1246 {
1247   return read_bytes_internal (r, true, buf, byte_cnt);
1248 }
1249
1250 /* Reads a 16-bit signed integer from R and stores its value in host format in
1251    *X.  Returns true if successful, otherwise false. */
1252 static bool
1253 read_uint16 (struct pcp_reader *r, unsigned int *x)
1254 {
1255   uint8_t integer[2];
1256   if (read_bytes (r, integer, sizeof integer) != 1)
1257     return false;
1258   *x = integer_get (INTEGER_LSB_FIRST, integer, sizeof integer);
1259   return true;
1260 }
1261
1262 /* Reads a 32-bit signed integer from R and stores its value in host format in
1263    *X.  Returns true if successful, otherwise false. */
1264 static bool
1265 read_uint32 (struct pcp_reader *r, unsigned int *x)
1266 {
1267   uint8_t integer[4];
1268   if (read_bytes (r, integer, sizeof integer) != 1)
1269     return false;
1270   *x = integer_get (INTEGER_LSB_FIRST, integer, sizeof integer);
1271   return true;
1272 }
1273
1274 /* Reads exactly SIZE - 1 bytes into BUFFER
1275    and stores a null byte into BUFFER[SIZE - 1]. */
1276 static bool
1277 read_string (struct pcp_reader *r, char *buffer, size_t size)
1278 {
1279   bool ok;
1280
1281   assert (size > 0);
1282   ok = read_bytes (r, buffer, size - 1);
1283   if (ok)
1284     buffer[size - 1] = '\0';
1285   return ok;
1286 }
1287
1288 /* Skips BYTES bytes forward in R. */
1289 static bool
1290 skip_bytes (struct pcp_reader *r, size_t bytes)
1291 {
1292   while (bytes > 0)
1293     {
1294       char buffer[1024];
1295       size_t chunk = MIN (sizeof buffer, bytes);
1296       if (!read_bytes (r, buffer, chunk))
1297         return false;
1298       bytes -= chunk;
1299     }
1300
1301   return true;
1302 }
1303 \f
1304 static bool
1305 pcp_seek (struct pcp_reader *r, off_t offset)
1306 {
1307   if (fseeko (r->file, offset, SEEK_SET))
1308     {
1309       pcp_error (r, 0, _("%s: seek failed (%s)."),
1310                  fh_get_file_name (r->fh), strerror (errno));
1311       return false;
1312     }
1313   r->pos = offset;
1314   return true;
1315 }
1316
1317 /* Reads a 64-bit floating-point number from R and returns its
1318    value in host format. */
1319 static bool
1320 read_float (struct pcp_reader *r, double *d)
1321 {
1322   uint8_t number[8];
1323
1324   if (!read_bytes (r, number, sizeof number))
1325     return false;
1326   else
1327     {
1328       *d = parse_float (number);
1329       return true;
1330     }
1331 }
1332
1333 static double
1334 parse_float (const uint8_t number[8])
1335 {
1336   return (pcp_is_sysmis (number)
1337           ? SYSMIS
1338           : float_get_double (FLOAT_IEEE_DOUBLE_LE, number));
1339 }
1340
1341 static bool
1342 pcp_is_sysmis(const uint8_t *p)
1343 {
1344   static const uint8_t sysmis[8]
1345     = { 0xf5, 0x1e, 0x26, 0x02, 0x8a, 0x8c, 0xed, 0xff };
1346   return !memcmp (p, sysmis, 8);
1347 }
1348 \f
1349 static const struct casereader_class pcp_file_casereader_class =
1350   {
1351     pcp_file_casereader_read,
1352     pcp_file_casereader_destroy,
1353     NULL,
1354     NULL,
1355   };
1356
1357 const struct any_reader_class pcp_file_reader_class =
1358   {
1359     N_("SPSS/PC+ System File"),
1360     pcp_detect,
1361     pcp_open,
1362     pcp_close,
1363     pcp_decode,
1364     pcp_get_strings,
1365   };