Coverage Report

Created: 2026-11-13 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/main/serialize.c
Line
Count
Source
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1995--2026  The R Core Team
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, a copy is available at
17
 *  https://www.R-project.org/Licenses/
18
 */
19
20
/* <UTF8> byte-level access is only to compare with chars <= 0x7F */
21
22
#ifdef HAVE_CONFIG_H
23
#include <config.h>
24
#endif
25
26
#define NEED_CONNECTION_PSTREAMS
27
#define R_USE_SIGNALS 1
28
#include <Defn.h>
29
#include <Rmath.h>
30
#include <Fileio.h>
31
#include <Rversion.h>
32
#include <R_ext/Riconv.h>
33
#include <R_ext/RS.h>           /* for CallocCharBuf, R_Free */
34
#include <errno.h>
35
#include <ctype.h>    /* for isspace */
36
#include <stdarg.h>
37
#ifdef Win32
38
#include <trioremap.h>
39
#endif
40
41
/* From time to time changes in R, such as the addition of a new SXP,
42
 * may require changes in the save file format.  Here are some
43
 * guidelines on handling format changes:
44
 *
45
 *    Starting with 1.4 there is a version number associated with save
46
 *    file formats.  This version number should be incremented when
47
 *    the format is changed so older versions of R can recognize and
48
 *    reject the new format with a meaningful error message.
49
 *
50
 *    R should remain able to write older workspace formats.  An error
51
 *    should be signaled if the contents to be saved is not compatible
52
 *    with the requested format.
53
 *
54
 *    To allow older versions of R to give useful error messages, the
55
 *    header now contains the version of R that wrote the workspace
56
 *    and the oldest version that can read the workspace.  These
57
 *    versions are stored as an integer packed by the R_Version macro
58
 *    from Rversion.h.  Some workspace formats may only exist
59
 *    temporarily in the development stage.  If readers are not
60
 *    provided in a released version, then these should specify the
61
 *    oldest reader R version as -1.
62
 */
63
64
 /* It is now customary that the version (1, 2, 3) of the format is
65
  * reflected also in magic numbers (such as RDX2, RDX3, ...), together with
66
  * type (xdr/ascii/binary).  Adding a new serialization format thus now
67
  * also requires adding a new set of magic numbers, yet in principle this
68
  * could be changed in the future.  The code in this file does not need the
69
  * magic numbers, it relies on version and type information in the
70
  * serialization header (version 2 and 3).
71
  */
72
73
/* ----- V e r s i o n -- T w o -- S a v e / R e s t o r e ----- */
74
75
/* Adapted from Chris Young and Ross Ihaka's Version One by Luke
76
   Tierney.  Copyright Assigned to the R Project.
77
78
   The approach used here uses a single pass over the node tree to be
79
   serialized.  Sharing of reference objects is preserved, but sharing
80
   among other objects is ignored.  The first time a reference object
81
   is encountered it is entered in a hash table; the value stored with
82
   the object is the index in the sequence of reference objects (1 for
83
   first reference object, 2 for second, etc.).  When an object is
84
   seen again, i.e. it is already in the hash table, a reference
85
   marker along with the index is written out.  The unserialize code
86
   does not know in advance how many reference objects it will see, so
87
   it starts with an initial array of some reasonable size and doubles
88
   it each time space runs out.  Reference objects are entered as they
89
   are encountered.
90
91
   This means the serialize and unserialize code needs to agree on
92
   what is a reference object.  Making a non-reference object into
93
   a reference object requires a version change in the format.  An
94
   alternate design would be to precede each reference object with a
95
   marker that says the next thing is a possibly shared object and
96
   needs to be entered into the reference table.
97
98
   Adding new SXP types is easy, whether they are reference objects or
99
   not.  The unserialize code will signal an error if it sees a type
100
   value it does not know.  It is of course better to increment the
101
   serialization format number when a new SXP is added, but if that
102
   SXP is unlikely to be saved by users then it may be simpler to keep
103
   the version number and let the error handling code deal with it.
104
105
   The output format for dotted pairs writes the ATTRIB value first
106
   rather than last.  This allows CDR's to be processed by iterative
107
   tail calls to avoid recursion stack overflows when processing long
108
   lists.
109
110
   CHARSXPs are now handled in a way that preserves both embedded null
111
   characters and NA_STRING values.
112
113
   The XDR save format now only uses the in-memory xdr facility for
114
   converting integers and doubles to a portable format.
115
116
   The output format packs the type flag and other flags into a single
117
   integer.  This produces more compact output for code; it has little
118
   effect on data.
119
120
   Environments recognized as package or namespace environments are
121
   not saved directly. Instead, a STRSXP is saved that is then used to
122
   attempt to find the package/namespace when unserialized.  The
123
   exact mechanism for choosing the name and finding the package/name
124
   space from the name still has to be developed, but the
125
   serialization format should be able to accommodate any reasonable
126
   mechanism.
127
128
   The mechanism assumes that user code supplies one routine for
129
   handling single characters and one for handling an array of bytes.
130
   Higher level interfaces that serialize to/from a FILE * pointer or
131
   an Rconnection pointer are provided in this file; others can be
132
   built easily.
133
134
   A mechanism is provided to allow special handling of non-system
135
   reference objects (all weak references and external pointers, and
136
   all environments other than package environments, namespace
137
   environments, and the global environment).  The hook function
138
   consists of a function pointer and a data value.  The serialization
139
   function pointer is called with the reference object and the data
140
   value as arguments.  It should return R_NilValue for standard
141
   handling and an STRSXP for special handling.  If an STRSXP is
142
   returned, then a special handing mark is written followed by the
143
   strings in the STRSXP (attributes are ignored).  On unserializing,
144
   any specially marked entry causes a call to the hook function with
145
   the reconstructed STRSXP and data value as arguments.  This should
146
   return the value to use for the reference object.  A reasonable
147
   convention on how to use this mechanism is needed, but again the
148
   format should be compatible with any reasonable convention.
149
150
   Eventually it may be useful to use these hooks to allow objects
151
   with a class to have a class-specific serialization mechanism.  The
152
   serialization format should support this.  It is trickier than in
153
   Java and other reference based languages where creation and
154
   initialization can be separated--we don't really have that option
155
   at the R level.  */
156
157
/* ----- V e r s i o n -- T h r e e -- S a v e / R e s t o r e ----- */
158
159
/* This format extends version 2 format by adding an identifier of the
160
   current native encoding to the serialization header.  On deserialization,
161
   strings without an encoding flag will be converted to the current native
162
   encoding, if possible, or to (flagged) UTF-8.  The conversion may fail
163
   when the original encoding is not supported by iconv (unlikely) or when
164
   the string is not valid in its declared encoding, which unfortunately is
165
   not uncommon.  The conversion code now deliberately does not check
166
   whether strings are valid when no conversion is needed, but such check
167
   could be added in the future without changing the format.
168
169
   Version 3 also adds support for custom ALTREP serialization. Under
170
   version 2 ALTREP objects are serialied like non-ALTREP ones. */
171
172
/*
173
 * Forward Declarations
174
 */
175
176
static void OutStringVec(R_outpstream_t stream, SEXP s, SEXP ref_table);
177
static void WriteItem (SEXP s, SEXP ref_table, R_outpstream_t stream);
178
static SEXP ReadItem(SEXP ref_table, R_inpstream_t stream);
179
static void WriteBC(SEXP s, SEXP ref_table, R_outpstream_t stream);
180
static SEXP ReadBC(SEXP ref_table, R_inpstream_t stream);
181
182
/*
183
 * Constants
184
 */
185
186
/* The default version used when a stream Init function is called with
187
   version = 0 */
188
189
static int defaultSerializeVersion(void)
190
2
{
191
2
    static int dflt = -1;
192
193
2
    if (dflt < 0) {
194
2
  char *valstr = getenv("R_DEFAULT_SERIALIZE_VERSION");
195
2
  int val = -1;
196
2
  if (valstr != NULL)
197
0
      val = atoi(valstr);
198
2
  if (val == 2 || val == 3)
199
0
      dflt = val;
200
2
  else
201
2
      dflt = 3; /* the default */
202
2
    }
203
2
    return dflt;
204
2
}
205
206
/*
207
 * Utility Functions
208
 *
209
 * An assert function which doesn't crash the program.
210
 * Something like this might be useful in an R header file
211
 */
212
213
#ifdef NDEBUG
214
#define R_assert(e) ((void) 0)
215
#else
216
/* The line below requires an ANSI C preprocessor (stringify operator) */
217
53.4M
#define R_assert(e) ((e) ? (void) 0 : error("assertion '%s' failed: file '%s', line %d\n", #e, __FILE__, __LINE__))
218
#endif /* NDEBUG */
219
220
/* Rsnprintf: like snprintf, but guaranteed to null-terminate. See
221
   errors.c::Rvsnprintf_mbcs for a multi-byte safe version. */
222
static int Rsnprintf(char *buf, size_t size, const char *format, ...)
223
0
{
224
0
    int val;
225
0
    va_list(ap);
226
0
    va_start(ap, format);
227
    /* On Windows this no longer uses the non-C99 MSVCRT.dll version */
228
0
    val = vsnprintf(buf, size, format, ap);
229
0
    if(size) buf[size-1] = '\0';
230
0
    va_end(ap);
231
0
    return val;
232
0
}
233
234
235
/*
236
 * Basic Output Routines
237
 */
238
239
static void OutInteger(R_outpstream_t stream, int i)
240
10
{
241
10
    char buf[128];
242
10
    switch (stream->type) {
243
0
    case R_pstream_ascii_format:
244
0
    case R_pstream_asciihex_format:
245
0
  if (i == NA_INTEGER)
246
0
      Rsnprintf(buf, sizeof(buf), "NA\n");
247
0
  else
248
0
      Rsnprintf(buf, sizeof(buf), "%d\n", i);
249
0
  stream->OutBytes(stream, buf, (int)strlen(buf));
250
0
  break;
251
0
    case R_pstream_binary_format:
252
0
  stream->OutBytes(stream, &i, sizeof(int));
253
0
  break;
254
10
    case R_pstream_xdr_format:
255
10
  R_XDREncodeInteger(i, buf);
256
10
  stream->OutBytes(stream, buf, R_XDR_INTEGER_SIZE);
257
10
  break;
258
0
    default:
259
0
  error(_("unknown or inappropriate output format"));
260
10
    }
261
10
}
262
263
static void OutReal(R_outpstream_t stream, double d)
264
0
{
265
0
    char buf[128];
266
0
    switch (stream->type) {
267
0
    case R_pstream_ascii_format:
268
0
  if (! R_FINITE(d)) {
269
0
      if (ISNA(d))
270
0
    Rsnprintf(buf, sizeof(buf), "NA\n");
271
0
      else if (ISNAN(d))
272
0
    Rsnprintf(buf, sizeof(buf), "NaN\n");
273
0
      else if (d < 0)
274
0
    Rsnprintf(buf, sizeof(buf), "-Inf\n");
275
0
      else
276
0
    Rsnprintf(buf, sizeof(buf), "Inf\n");
277
0
  }
278
0
  else
279
      /* 16: full precision; 17 gives 999, 000 &c */
280
0
      Rsnprintf(buf, sizeof(buf), "%.16g\n", d);
281
0
  stream->OutBytes(stream, buf, (int)strlen(buf));
282
0
  break;
283
0
    case R_pstream_asciihex_format:
284
0
  if (! R_FINITE(d)) {
285
0
      if (ISNA(d))
286
0
    Rsnprintf(buf, sizeof(buf), "NA\n");
287
0
      else if (ISNAN(d))
288
0
    Rsnprintf(buf, sizeof(buf), "NaN\n");
289
0
      else if (d < 0)
290
0
    Rsnprintf(buf, sizeof(buf), "-Inf\n");
291
0
      else
292
0
    Rsnprintf(buf, sizeof(buf), "Inf\n");
293
0
  }
294
0
  else
295
0
      Rsnprintf(buf, sizeof(buf), "%a\n", d);
296
0
  stream->OutBytes(stream, buf, (int)strlen(buf));
297
0
  break;
298
0
    case R_pstream_binary_format:
299
0
  stream->OutBytes(stream, &d, sizeof(double));
300
0
  break;
301
0
    case R_pstream_xdr_format:
302
0
  R_XDREncodeDouble(d, buf);
303
0
  stream->OutBytes(stream, buf, R_XDR_DOUBLE_SIZE);
304
0
  break;
305
0
    default:
306
0
  error(_("unknown or inappropriate output format"));
307
0
    }
308
0
}
309
310
static void OutComplex(R_outpstream_t stream, Rcomplex c)
311
0
{
312
0
    OutReal(stream, c.r);
313
0
    OutReal(stream, c.i);
314
0
}
315
316
static void OutByte(R_outpstream_t stream, Rbyte i)
317
0
{
318
0
    char buf[128];
319
0
    switch (stream->type) {
320
0
    case R_pstream_ascii_format:
321
0
    case R_pstream_asciihex_format:
322
0
  Rsnprintf(buf, sizeof(buf), "%02x\n", i);
323
0
  stream->OutBytes(stream, buf, (int)strlen(buf));
324
0
  break;
325
0
    case R_pstream_binary_format:
326
0
    case R_pstream_xdr_format:
327
0
  stream->OutBytes(stream, &i, 1);
328
0
  break;
329
0
    default:
330
0
  error(_("unknown or inappropriate output format"));
331
0
    }
332
0
}
333
334
/* This assumes CHARSXPs remain limited to 2^31-1 bytes */
335
static void OutString(R_outpstream_t stream, const char *s, int length)
336
2
{
337
2
    if (stream->type == R_pstream_ascii_format ||
338
2
  stream->type == R_pstream_asciihex_format) {
339
0
  int i;
340
0
  char buf[128];
341
0
  for (i = 0; i < length; i++) {
342
0
      switch(s[i]) {
343
0
      case '\n': snprintf(buf, 128, "\\n");  break;
344
0
      case '\t': snprintf(buf, 128, "\\t");  break;
345
0
      case '\v': snprintf(buf, 128, "\\v");  break;
346
0
      case '\b': snprintf(buf, 128, "\\b");  break;
347
0
      case '\r': snprintf(buf, 128, "\\r");  break;
348
0
      case '\f': snprintf(buf, 128, "\\f");  break;
349
0
      case '\a': snprintf(buf, 128, "\\a");  break;
350
0
      case '\\': snprintf(buf, 128, "\\\\"); break;
351
0
      case '\?': snprintf(buf, 128, "\\?");  break;
352
0
      case '\'': snprintf(buf, 128, "\\'");  break;
353
0
      case '\"': snprintf(buf, 128, "\\\""); break;
354
0
      default  :
355
    /* cannot print char in octal mode -> cast to unsigned
356
       char first */
357
    /* actually, since s is signed char and '\?' == 127
358
       is handled above, s[i] > 126 can't happen, but
359
       I'm superstitious...  -pd */
360
0
    if (s[i] <= 32 || s[i] > 126)
361
0
        snprintf(buf, 128, "\\%03o", (unsigned char) s[i]);
362
0
    else
363
0
        snprintf(buf, 128, "%c", s[i]);
364
0
      }
365
0
      stream->OutBytes(stream, buf, (int)strlen(buf));
366
0
  }
367
0
  stream->OutChar(stream, '\n');
368
0
    }
369
2
    else
370
2
  stream->OutBytes(stream, (void *)s, length); /* FIXME: is this case right? */
371
2
}
372
373
374
/*
375
 * Basic Input Routines
376
 */
377
378
static void InWord(R_inpstream_t stream, char * buf, int size)
379
3.73M
{
380
3.73M
    int c, i;
381
3.73M
    i = 0;
382
4.15M
    do {
383
4.15M
  c = stream->InChar(stream);
384
4.15M
  if (c == EOF)
385
87
      error(_("read error"));
386
4.15M
    } while (isspace(c));
387
11.0M
    while (! isspace(c) && i < size) {
388
7.29M
  buf[i++] = (char) c;
389
7.29M
  c = stream->InChar(stream);
390
7.29M
    }
391
3.73M
    if (i == size)
392
118
  error(_("read error"));
393
3.73M
    buf[i] = 0;
394
3.73M
}
395
396
static int InInteger(R_inpstream_t stream)
397
40.4M
{
398
40.4M
    char word[128];
399
40.4M
    char buf[128];
400
40.4M
    int i;
401
402
40.4M
    switch (stream->type) {
403
3.43M
    case R_pstream_ascii_format:
404
3.43M
  InWord(stream, word, sizeof(word));
405
3.43M
  if(sscanf(word, "%127s", buf) != 1) error(_("read error"));
406
3.43M
  if (strcmp(buf, "NA") == 0)
407
1.97k
      return NA_INTEGER;
408
3.43M
  else
409
3.43M
      if(sscanf(buf, "%d", &i) != 1) error(_("read error"));
410
3.43M
  return i;
411
1.20k
    case R_pstream_binary_format:
412
1.20k
  stream->InBytes(stream, &i, sizeof(int));
413
1.20k
  return i;
414
36.9M
    case R_pstream_xdr_format:
415
36.9M
  stream->InBytes(stream, buf, R_XDR_INTEGER_SIZE);
416
36.9M
  return R_XDRDecodeInteger(buf);
417
0
    default:
418
0
  return NA_INTEGER;
419
40.4M
    }
420
40.4M
}
421
422
#ifdef Win32
423
extern int trio_sscanf(const char *buffer, const char *format, ...);
424
425
#endif
426
427
static double InReal(R_inpstream_t stream)
428
102k
{
429
102k
    char word[128];
430
102k
    char buf[128];
431
102k
    double d;
432
433
102k
    switch (stream->type) {
434
102k
    case R_pstream_ascii_format:
435
102k
  InWord(stream, word, sizeof(word));
436
102k
  if(sscanf(word, "%127s", buf) != 1) error(_("read error"));
437
102k
  if (strcmp(buf, "NA") == 0)
438
551
      return NA_REAL;
439
101k
  else if (strcmp(buf, "NaN") == 0)
440
40
      return R_NaN;
441
101k
  else if (strcmp(buf, "Inf") == 0)
442
416
      return R_PosInf;
443
101k
  else if (strcmp(buf, "-Inf") == 0)
444
86
      return R_NegInf;
445
101k
  else
446
101k
      if(
447
#ifdef Win32
448
    trio_sscanf(buf, "%lg", &d)
449
#else
450
101k
    sscanf(buf, "%lg", &d)
451
101k
#endif
452
101k
    != 1) error(_("read error"));
453
101k
  return d;
454
0
    case R_pstream_binary_format:
455
0
  stream->InBytes(stream, &d, sizeof(double));
456
0
  return d;
457
0
    case R_pstream_xdr_format:
458
0
  stream->InBytes(stream, buf, R_XDR_DOUBLE_SIZE);
459
0
  return R_XDRDecodeDouble(buf);
460
0
    default:
461
0
  return NA_REAL;
462
102k
    }
463
102k
}
464
465
static Rcomplex InComplex(R_inpstream_t stream)
466
3.35k
{
467
3.35k
    Rcomplex c;
468
3.35k
    c.r = InReal(stream);
469
3.35k
    c.i = InReal(stream);
470
3.35k
    return c;
471
3.35k
}
472
473
/* These utilities for reading characters with an unget option are
474
   defined so the code in InString can match the code in
475
   saveload.c:InStringAscii--that way it is easier to match changes in
476
   one to the other. */
477
typedef struct R_instring_stream_st {
478
    int last;
479
    R_inpstream_t stream;
480
} *R_instring_stream_t;
481
482
static void InitInStringStream(R_instring_stream_t s, R_inpstream_t stream)
483
307k
{
484
307k
    s->last = EOF;
485
307k
    s->stream = stream;
486
307k
}
487
488
static int GetChar(R_instring_stream_t s)
489
3.03M
{
490
3.03M
    int c;
491
3.03M
    if (s->last != EOF) {
492
319k
  c = s->last;
493
319k
  s->last = EOF;
494
319k
    }
495
2.71M
    else c = s->stream->InChar(s->stream);
496
3.03M
    return c;
497
3.03M
}
498
499
static void UngetChar(R_instring_stream_t s, int c)
500
319k
{
501
319k
    s->last = c;
502
319k
}
503
504
505
static void InString(R_inpstream_t stream, char *buf, int length)
506
2.98M
{
507
2.98M
    if (stream->type == R_pstream_ascii_format) {
508
436k
  if (length > 0) {
509
307k
      int c, d, i, j;
510
307k
      struct R_instring_stream_st iss;
511
512
307k
      InitInStringStream(&iss, stream);
513
307k
      while(isspace(c = GetChar(&iss)))
514
1.12k
    ;
515
307k
      UngetChar(&iss, c);
516
2.95M
      for (i = 0; i < length; i++) {
517
    /* fgetc() and text connections return EOF without an
518
       error: stop here rather than fill the rest of the
519
       string with EOF bytes */
520
2.64M
    if ((c = GetChar(&iss)) == EOF)
521
201
        error(_("read error"));
522
2.64M
    if (c == '\\') {
523
63.8k
        if ((c = GetChar(&iss)) == EOF)
524
9
      error(_("read error"));
525
63.8k
        switch(c) {
526
496
        case 'n' : buf[i] = '\n'; break;
527
7.31k
        case 't' : buf[i] = '\t'; break;
528
1.52k
        case 'v' : buf[i] = '\v'; break;
529
309
        case 'b' : buf[i] = '\b'; break;
530
620
        case 'r' : buf[i] = '\r'; break;
531
346
        case 'f' : buf[i] = '\f'; break;
532
647
        case 'a' : buf[i] = '\a'; break;
533
26.9k
        case '\\': buf[i] = '\\'; break;
534
273
        case '?' : buf[i] = '\?'; break;
535
854
        case '\'': buf[i] = '\''; break;
536
7.36k
        case '\"': buf[i] = '\"'; break; /* closing " for emacs */
537
7.98k
        case '0': case '1': case '2': case '3':
538
12.4k
        case '4': case '5': case '6': case '7':
539
12.4k
      d = 0; j = 0;
540
30.8k
      while('0' <= c && c < '8' && j < 3) {
541
18.3k
          d = d * 8 + (c - '0');
542
18.3k
          c = GetChar(&iss);
543
18.3k
          j++;
544
18.3k
      }
545
12.4k
      buf[i] = (char) d;
546
12.4k
      UngetChar(&iss, c);
547
12.4k
      break;
548
4.67k
        default  : buf[i] = (char) c;
549
63.8k
        }
550
63.8k
    }
551
2.58M
    else buf[i] = (char) c;
552
2.64M
      }
553
307k
  }
554
436k
    }
555
2.55M
    else  /* this limits the string length: used for CHARSXPs */
556
2.55M
  stream->InBytes(stream, buf, length);
557
2.98M
}
558
559
560
/*
561
 * Format Header Reading and Writing
562
 *
563
 * The header starts with one of three characters, A for ascii, B for
564
 * binary, or X for xdr.
565
 */
566
567
static void OutFormat(R_outpstream_t stream)
568
2
{
569
/*    if (stream->type == R_pstream_binary_format) {
570
  warning(_("binary format is deprecated; using xdr instead"));
571
  stream->type = R_pstream_xdr_format;
572
  } */
573
2
    switch (stream->type) {
574
0
    case R_pstream_ascii_format:
575
0
    case R_pstream_asciihex_format:
576
0
  stream->OutBytes(stream, "A\n", 2); break;
577
  /* on deserialization, asciihex_format is treated exactly the same
578
     way as ascii_format; the distinction is handled inside scanf %lg */
579
0
    case R_pstream_binary_format: stream->OutBytes(stream, "B\n", 2); break;
580
2
    case R_pstream_xdr_format:    stream->OutBytes(stream, "X\n", 2); break;
581
0
    case R_pstream_any_format:
582
0
  error(_("must specify ascii, binary, or xdr format"));
583
0
    default: error(_("unknown output format"));
584
2
    }
585
2
}
586
587
static void InFormat(R_inpstream_t stream)
588
37.7k
{
589
37.7k
    char buf[2];
590
37.7k
    R_pstream_format_t type;
591
37.7k
    stream->InBytes(stream, buf, 2);
592
37.7k
    switch (buf[0]) {
593
4.30k
    case 'A': type = R_pstream_ascii_format; break; /* also for asciihex */
594
169
    case 'B': type = R_pstream_binary_format; break;
595
32.9k
    case 'X': type = R_pstream_xdr_format; break;
596
28
    case '\n':
597
  /* GROSS HACK: ASCII unserialize may leave a trailing newline
598
     in the stream.  If the stream contains a second
599
     serialization, then a second unserialize will fail if such
600
     a newline is present.  The right fix is to make sure
601
     unserialize consumes exactly what serialize produces.  But
602
     this seems hard because of the current use of whitespace
603
     skipping in unserialize.  So a temporary hack to cure the
604
     symptom is to deal with a possible leading newline.  I
605
     don't think more than one is possible, but I'm not sure.
606
     LT */
607
28
  if (buf[1] == 'A') {
608
18
      type = R_pstream_ascii_format;
609
18
      stream->InBytes(stream, buf, 1);
610
18
      break;
611
18
  }
612
59
    default:
613
59
  type = R_pstream_any_format;  /* keep compiler happy */
614
59
  error(_("unknown input format"));
615
37.7k
    }
616
37.4k
    if (stream->type == R_pstream_any_format)
617
35.5k
  stream->type = type;
618
1.86k
    else if (type != stream->type)
619
1
  error(_("input format does not match specified format"));
620
37.4k
}
621
622
623
/*
624
 * Hash Table Functions
625
 *
626
 * Hashing functions for hashing reference objects during writing.
627
 * Objects are entered, and the order in which they are encountered is
628
 * recorded.  HashGet returns this number, a positive integer, if the
629
 * object was seen before, and zero if not.  A fixed hash table size
630
 * is used; this is not ideal but seems adequate for now.  The hash
631
 * table representation consists of a (R_NilValue . vector) pair.  The
632
 * hash buckets are in the vector.  This indirect representation
633
 * should allow resizing the table at some point.
634
 */
635
636
#define HASHSIZE 1099
637
638
0
#define PTRHASH(obj) (((R_size_t) (obj)) >> 2)
639
640
0
#define HASH_TABLE_COUNT(ht) ((int) TRUELENGTH(CDR(ht)))
641
2
#define SET_HASH_TABLE_COUNT(ht, val) SET_TRUELENGTH(CDR(ht), ((int) (val)))
642
643
0
#define HASH_TABLE_SIZE(ht) LENGTH(CDR(ht))
644
645
0
#define HASH_BUCKET(ht, pos) VECTOR_ELT(CDR(ht), pos)
646
0
#define SET_HASH_BUCKET(ht, pos, val) SET_VECTOR_ELT(CDR(ht), pos, val)
647
648
static SEXP MakeHashTable(void)
649
2
{
650
2
    SEXP val = CONS(R_NilValue, allocVector(VECSXP, HASHSIZE));
651
2
    SET_HASH_TABLE_COUNT(val, 0);
652
2
    return val;
653
2
}
654
655
static void HashAdd(SEXP obj, SEXP ht)
656
0
{
657
0
    R_size_t pos = PTRHASH(obj) % HASH_TABLE_SIZE(ht);
658
0
    int count = HASH_TABLE_COUNT(ht) + 1;
659
0
    SEXP val = ScalarInteger(count);
660
0
    SEXP cell = CONS(val, HASH_BUCKET(ht, pos));
661
662
0
    SET_HASH_TABLE_COUNT(ht, count);
663
0
    SET_HASH_BUCKET(ht, pos, cell);
664
0
    SET_TAG(cell, obj);
665
0
}
666
667
static int HashGet(SEXP item, SEXP ht)
668
0
{
669
0
    R_size_t pos = PTRHASH(item) % HASH_TABLE_SIZE(ht);
670
0
    SEXP cell;
671
0
    for (cell = HASH_BUCKET(ht, pos); cell != R_NilValue; cell = CDR(cell))
672
0
  if (item == TAG(cell))
673
0
      return INTEGER(CAR(cell))[0];
674
0
    return 0;
675
0
}
676
677
678
/*
679
 * Administrative SXP values
680
 *
681
 * These macros define SXP "type" for specifying special object, such
682
 * as R_NilValue, or control information, like REFSXP or NAMESPACESXP.
683
 * The range of SXP types is limited to 5 bit by the current sxpinfo
684
 * layout, but just in case these values are placed at the top of the
685
 * 8 bit range.
686
 */
687
688
4.45M
#define REFSXP            255
689
5.82M
#define NILVALUE_SXP      254
690
9.22k
#define GLOBALENV_SXP     253
691
9.20k
#define UNBOUNDVALUE_SXP  252
692
215k
#define MISSINGARG_SXP    251
693
18.8k
#define BASENAMESPACE_SXP 250
694
11.9k
#define NAMESPACESXP      249
695
493
#define PACKAGESXP        248
696
25.2k
#define PERSISTSXP        247
697
/* the following are speculative--we may or may not need them soon */
698
3
#define CLASSREFSXP       246
699
9
#define GENERICREFSXP     245
700
4.66M
#define BCREPDEF          244
701
2.16M
#define BCREPREF          243
702
1.68k
#define EMPTYENV_SXP    242
703
1.60k
#define BASEENV_SXP   241
704
705
/* The following are needed to preserve attribute information on
706
   expressions in the constant pool of byte code objects. This is
707
   mainly for preserving source references attributes.  The original
708
   implementation of the sharing-preserving writing and reading of byte
709
   code objects did not account for the need to preserve attributes,
710
   so there is now a work-around using these SXP types to flag when
711
   the ATTRIB field has been written out. Object bits and S4 bits are
712
   still not preserved.  In the long run it might be better to change
713
   to a scheme in which all sharing is preserved and byte code objects
714
   don't need to be handled as a special case.  LT */
715
4.80M
#define ATTRLANGSXP       240
716
4.80M
#define ATTRLISTSXP       239
717
718
35.1k
#define ALTREP_SXP    238
719
720
/*
721
 * Type/Flag Packing and Unpacking
722
 *
723
 * To reduce space consumption for serializing code (lots of list
724
 * structure) the type (at most 8 bits), several single bit flags,
725
 * and the sxpinfo gp field (LEVELS, 16 bits) are packed into a single
726
 * integer.  The integer is signed, so this shouldn't be pushed too
727
 * far.  It assumes at least 28 bits, but that should be no problem.
728
 */
729
730
23.7M
#define IS_OBJECT_BIT_MASK (1 << 8)
731
23.7M
#define HAS_ATTR_BIT_MASK (1 << 9)
732
23.7M
#define HAS_TAG_BIT_MASK (1 << 10)
733
0
#define ENCODE_LEVELS(v) ((v) << 12)
734
23.7M
#define DECODE_LEVELS(v) ((v) >> 12)
735
30.2M
#define DECODE_TYPE(v) ((v) & 255)
736
737
static int PackFlags(int type, int levs, int isobj, int hasattr, int hastag)
738
0
{
739
    /* Bit 5 of gp is internal bookkeeping that should not be
740
       serialized. For CHARSXP it is the cache bit (stripped since R
741
       2.8.0). For vectors it is the growable/resizable bit set by
742
       `R_allocResizableVector()`. For closures it is the NOJIT bit
743
       which is still serialized here as this is preexisting and has
744
       not caused problems in practice.
745
    */
746
0
    int val;
747
    /* Also make sure the HASHASH bit of CHARSXP is not written out. */
748
0
    if (type == CHARSXP)
749
0
  levs &= (~(CACHED_MASK | HASHASH_MASK));
750
0
    else
751
0
  switch (type) {
752
0
        case LGLSXP:
753
0
        case INTSXP:
754
0
        case REALSXP:
755
0
        case CPLXSXP:
756
0
        case STRSXP:
757
0
        case VECSXP:
758
0
        case EXPRSXP:
759
0
        case RAWSXP:
760
0
            levs &= ~GROWABLE_MASK;
761
0
            break;
762
0
        default:
763
0
            break;
764
0
        }
765
0
    val = type | ENCODE_LEVELS(levs);
766
0
    if (isobj) val |= IS_OBJECT_BIT_MASK;
767
0
    if (hasattr) val |= HAS_ATTR_BIT_MASK;
768
0
    if (hastag) val |= HAS_TAG_BIT_MASK;
769
0
    return val;
770
0
}
771
772
static void UnpackFlags(int flags, SEXPTYPE *ptype, int *plevs,
773
      int *pisobj, int *phasattr, int *phastag)
774
23.7M
{
775
23.7M
    *ptype = DECODE_TYPE(flags);
776
23.7M
    *plevs = DECODE_LEVELS(flags);
777
23.7M
    *pisobj = flags & IS_OBJECT_BIT_MASK ? true : FALSE;
778
23.7M
    *phasattr = flags & HAS_ATTR_BIT_MASK ? true : FALSE;
779
23.7M
    *phastag = flags & HAS_TAG_BIT_MASK ? true : FALSE;
780
23.7M
}
781
782
783
/*
784
 * Reference/Index Packing and Unpacking
785
 *
786
 * Code will contain many references to symbols. As long as there are
787
 * not too many references, the index ant the REFSXP flag indicating a
788
 * reference can be packed in a single integer.  Since the index is
789
 * 1-based, a 0 is used to indicate an index that doesn't fit and
790
 * therefore follows.
791
 */
792
793
0
#define PACK_REF_INDEX(i) (((i) << 8) | REFSXP)
794
4.45M
#define UNPACK_REF_INDEX(i) ((i) >> 8)
795
0
#define MAX_PACKED_INDEX (INT_MAX >> 8)
796
797
static void OutRefIndex(R_outpstream_t stream, int i)
798
0
{
799
0
    if (i > MAX_PACKED_INDEX) {
800
0
  OutInteger(stream, REFSXP);
801
0
  OutInteger(stream, i);
802
0
    }
803
0
    else OutInteger(stream, PACK_REF_INDEX(i));
804
0
}
805
806
static int InRefIndex(R_inpstream_t stream, int flags)
807
4.45M
{
808
4.45M
    int i = UNPACK_REF_INDEX(flags);
809
4.45M
    if (i == 0)
810
2.24k
  return InInteger(stream);
811
4.45M
    else
812
4.45M
  return i;
813
4.45M
}
814
815
816
/*
817
 * Persistent Name Hooks
818
 *
819
 * These routines call the appropriate hook functions for allowing
820
 * customized handling of reference objects.
821
 */
822
823
static SEXP GetPersistentName(R_outpstream_t stream, SEXP s)
824
2
{
825
2
    if (stream->OutPersistHookFunc != NULL) {
826
0
  switch (TYPEOF(s)) {
827
0
  case WEAKREFSXP:
828
0
  case EXTPTRSXP: break;
829
0
  case ENVSXP:
830
0
      if (s == R_GlobalEnv ||
831
0
    s == R_BaseEnv ||
832
0
    s == R_EmptyEnv ||
833
0
    R_IsNamespaceEnv(s) ||
834
0
    R_IsPackageEnv(s))
835
0
    return R_NilValue;
836
0
      else
837
0
    break;
838
0
  default: return R_NilValue;
839
0
  }
840
0
  return stream->OutPersistHookFunc(s, stream->OutPersistHookData);
841
0
    }
842
2
    else
843
2
  return R_NilValue;
844
2
}
845
846
static SEXP PersistentRestore(R_inpstream_t stream, SEXP s)
847
25.2k
{
848
25.2k
    if (stream->InPersistHookFunc == NULL)
849
2
  error(_("no restore method available"));
850
25.2k
    return stream->InPersistHookFunc(s, stream->InPersistHookData);
851
25.2k
}
852
853
854
/*
855
 * Serialization Code
856
 */
857
858
static int SaveSpecialHook(SEXP item)
859
2
{
860
2
    if (item == R_NilValue)      return NILVALUE_SXP;
861
0
    if (item == R_EmptyEnv)  return EMPTYENV_SXP;
862
0
    if (item == R_BaseEnv)  return BASEENV_SXP;
863
0
    if (item == R_GlobalEnv)     return GLOBALENV_SXP;
864
0
    if (item == R_UnboundValue)  return UNBOUNDVALUE_SXP;
865
0
    if (item == R_MissingArg)    return MISSINGARG_SXP;
866
0
    if (item == R_BaseNamespace) return BASENAMESPACE_SXP;
867
0
    return 0;
868
0
}
869
870
static void WriteLENGTH(R_outpstream_t stream, SEXP s)
871
0
{
872
0
#ifdef LONG_VECTOR_SUPPORT
873
0
    if (IS_LONG_VEC(s)) {
874
0
  OutInteger(stream, -1);
875
0
  R_xlen_t len = XLENGTH(s);
876
0
  OutInteger(stream, (int)(len / 4294967296L));
877
0
  OutInteger(stream, (int)(len % 4294967296L));
878
0
   } else OutInteger(stream, LENGTH(s));
879
#else
880
    OutInteger(stream, LENGTH(s));
881
#endif
882
0
}
883
884
#define IF_IC_R_CheckUserInterrupt()    \
885
2
    if(!(--ic)) {       \
886
0
  R_CheckUserInterrupt();     \
887
0
  ic = 9999;        \
888
0
    }
889
890
static void OutStringVec(R_outpstream_t stream, SEXP s, SEXP ref_table)
891
0
{
892
0
    R_assert(TYPEOF(s) == STRSXP);
893
894
#ifdef WARN_ABOUT_NAMES_IN_PERSISTENT_STRINGS
895
    SEXP names = getAttrib(s, R_NamesSymbol);
896
    if (names != R_NilValue)
897
  warning(_("names in persistent strings are currently ignored"));
898
#endif
899
900
0
    R_xlen_t len = XLENGTH(s);
901
0
    OutInteger(stream, 0); /* place holder to allow names if we want to */
902
0
    WriteLENGTH(stream, s);
903
0
    int ic = 9;
904
0
    for (R_xlen_t i = 0; i < len; i++) {
905
0
  IF_IC_R_CheckUserInterrupt();
906
0
  WriteItem(STRING_ELT(s, i), ref_table, stream);
907
0
    }
908
0
}
909
910
#include <rpc/types.h>
911
#include <rpc/xdr.h>
912
913
#define CHUNK_SIZE 8096
914
915
1.25M
#define min2(a, b) ((a) < (b)) ? (a) : (b)
916
917
918
static R_INLINE void
919
OutIntegerVec(R_outpstream_t stream, SEXP s, R_xlen_t length)
920
0
{
921
0
    int ic = 9999;
922
0
    switch (stream->type) {
923
0
    case R_pstream_xdr_format:
924
0
    {
925
0
  static char buf[CHUNK_SIZE * sizeof(int)];
926
0
  R_xlen_t done, this;
927
0
  XDR xdrs;
928
0
  for (done = 0; done < length; done += this) {
929
0
      IF_IC_R_CheckUserInterrupt();
930
0
      this = min2(CHUNK_SIZE, length - done);
931
0
      xdrmem_create(&xdrs, buf, (int)(this * sizeof(int)), XDR_ENCODE);
932
0
      for(int cnt = 0; cnt < this; cnt++)
933
0
    if(!xdr_int(&xdrs, INTEGER(s) + done + cnt))
934
0
        error(_("XDR write failed"));
935
0
      xdr_destroy(&xdrs);
936
0
      stream->OutBytes(stream, buf, (int)(sizeof(int) * this));
937
0
  }
938
0
  break;
939
0
    }
940
0
    case R_pstream_binary_format:
941
0
    {
942
  /* write in chunks to avoid overflowing ints */
943
0
  R_xlen_t done, this;
944
0
  for (done = 0; done < length; done += this) {
945
0
      IF_IC_R_CheckUserInterrupt();
946
0
      this = min2(CHUNK_SIZE, length - done);
947
0
      stream->OutBytes(stream, INTEGER(s) + done,
948
0
           (int)(sizeof(int) * this));
949
0
  }
950
0
  break;
951
0
    }
952
0
    default:
953
0
  for (R_xlen_t cnt = 0; cnt < length; cnt++) {
954
0
      OutInteger(stream, INTEGER(s)[cnt]);
955
0
      IF_IC_R_CheckUserInterrupt();
956
0
  }
957
0
    }
958
0
}
959
960
static R_INLINE void
961
OutRealVec(R_outpstream_t stream, SEXP s, R_xlen_t length)
962
0
{
963
0
    int ic = 9999;
964
0
    switch (stream->type) {
965
0
    case R_pstream_xdr_format:
966
0
    {
967
0
  static char buf[CHUNK_SIZE * sizeof(double)];
968
0
  R_xlen_t done, this;
969
0
  XDR xdrs;
970
0
  for (done = 0; done < length; done += this) {
971
0
      IF_IC_R_CheckUserInterrupt();
972
0
      this = min2(CHUNK_SIZE, length - done);
973
0
      xdrmem_create(&xdrs, buf, (int)(this * sizeof(double)), XDR_ENCODE);
974
0
      for(int cnt = 0; cnt < this; cnt++)
975
0
    if(!xdr_double(&xdrs, REAL(s) + done + cnt))
976
0
        error(_("XDR write failed"));
977
0
      xdr_destroy(&xdrs);
978
0
      stream->OutBytes(stream, buf, (int)(sizeof(double) * this));
979
0
  }
980
0
  break;
981
0
    }
982
0
    case R_pstream_binary_format:
983
0
    {
984
0
  R_xlen_t done, this;
985
0
  for (done = 0; done < length; done += this) {
986
0
      IF_IC_R_CheckUserInterrupt();
987
0
      this = min2(CHUNK_SIZE, length - done);
988
0
      stream->OutBytes(stream, REAL(s) + done,
989
0
           (int)(sizeof(double) * this));
990
0
  }
991
0
  break;
992
0
    }
993
0
    default:
994
0
  for (R_xlen_t cnt = 0; cnt < length; cnt++) {
995
0
      IF_IC_R_CheckUserInterrupt();
996
0
      OutReal(stream, REAL(s)[cnt]);
997
0
  }
998
0
    }
999
0
}
1000
1001
static R_INLINE void
1002
OutComplexVec(R_outpstream_t stream, SEXP s, R_xlen_t length)
1003
0
{
1004
0
    int ic = 9999;
1005
0
    switch (stream->type) {
1006
0
    case R_pstream_xdr_format:
1007
0
    {
1008
0
  static char buf[CHUNK_SIZE * sizeof(Rcomplex)];
1009
0
  R_xlen_t done, this;
1010
0
  XDR xdrs;
1011
0
  Rcomplex *c = COMPLEX(s);
1012
0
  for (done = 0; done < length; done += this) {
1013
0
      IF_IC_R_CheckUserInterrupt();
1014
0
      this = min2(CHUNK_SIZE, length - done);
1015
0
      xdrmem_create(&xdrs, buf, (int)(this * sizeof(Rcomplex)), XDR_ENCODE);
1016
0
      for(int cnt = 0; cnt < this; cnt++) {
1017
0
    if(!xdr_double(&xdrs, &(c[done+cnt].r)) ||
1018
0
       !xdr_double(&xdrs, &(c[done+cnt].i)))
1019
0
        error(_("XDR write failed"));
1020
0
      }
1021
0
      stream->OutBytes(stream, buf, (int)(sizeof(Rcomplex) * this));
1022
0
      xdr_destroy(&xdrs);
1023
0
  }
1024
0
  break;
1025
0
    }
1026
0
    case R_pstream_binary_format:
1027
0
    {
1028
0
  R_xlen_t done, this;
1029
0
  for (done = 0; done < length; done += this) {
1030
0
      IF_IC_R_CheckUserInterrupt();
1031
0
      this = min2(CHUNK_SIZE, length - done);
1032
0
      stream->OutBytes(stream, COMPLEX(s) + done,
1033
0
           (int)(sizeof(Rcomplex) * this));
1034
0
  }
1035
0
  break;
1036
0
    }
1037
0
    default:
1038
0
  for (R_xlen_t cnt = 0; cnt < length; cnt++) {
1039
0
      IF_IC_R_CheckUserInterrupt();
1040
0
      OutComplex(stream, COMPLEX(s)[cnt]);
1041
0
  }
1042
0
    }
1043
0
}
1044
1045
static void WriteItem (SEXP s, SEXP ref_table, R_outpstream_t stream)
1046
2
{
1047
2
    if (R_compile_pkgs && TYPEOF(s) == CLOSXP && TYPEOF(BODY(s)) != BCODESXP &&
1048
0
        !R_disable_bytecode &&
1049
0
        (!IS_S4_OBJECT(s) || (!inherits(s, "refMethodDef") &&
1050
0
                              !inherits(s, "defaultBindingFunction")))) {
1051
1052
  /* Do not compile reference class methods in their generators, because
1053
     the byte-code is dropped as soon as the method is installed into a
1054
     new environment. This is a performance optimization but it also
1055
     prevents byte-compiler warnings about no visible binding for super
1056
     assignment to a class field.
1057
1058
     Do not compile default binding functions, because the byte-code is
1059
     dropped as fields are set in constructors (just an optimization).
1060
  */
1061
1062
0
  SEXP new_s;
1063
0
  R_compile_pkgs = FALSE;
1064
0
  PROTECT(new_s = R_cmpfun1(s));
1065
0
  WriteItem (new_s, ref_table, stream);
1066
0
  UNPROTECT(1);
1067
0
  R_compile_pkgs = true;
1068
0
  return;
1069
0
    }
1070
1071
2
    int ic = 9999;
1072
2
 tailcall:
1073
2
    R_CheckStack();
1074
2
    IF_IC_R_CheckUserInterrupt();
1075
2
    if (ALTREP(s) && stream->version >= 3) {
1076
0
  SEXP info = ALTREP_SERIALIZED_CLASS(s);
1077
0
  SEXP state = ALTREP_SERIALIZED_STATE(s);
1078
0
  if (info != NULL && state != NULL) {
1079
0
      int flags = PackFlags(ALTREP_SXP, LEVELS(s), OBJECT(s), 0, 0);
1080
0
      PROTECT(state);
1081
0
      PROTECT(info);
1082
0
      OutInteger(stream, flags);
1083
0
      WriteItem(info, ref_table, stream);
1084
0
      WriteItem(state, ref_table, stream);
1085
0
      WriteItem(ATTRIB(s), ref_table, stream);
1086
0
      UNPROTECT(2); /* state, info */
1087
0
      return;
1088
0
  }
1089
  /* else fall through to standard processing */
1090
0
    }
1091
2
    int i;
1092
2
    SEXP t;
1093
2
    if ((t = GetPersistentName(stream, s)) != R_NilValue) {
1094
0
  R_assert(TYPEOF(t) == STRSXP && LENGTH(t) > 0);
1095
0
  PROTECT(t);
1096
0
  HashAdd(s, ref_table);
1097
0
  OutInteger(stream, PERSISTSXP);
1098
0
  OutStringVec(stream, t, ref_table);
1099
0
  UNPROTECT(1);
1100
0
    }
1101
2
    else if ((i = SaveSpecialHook(s)) != 0)
1102
2
  OutInteger(stream, i);
1103
0
    else if ((i = HashGet(s, ref_table)) != 0)
1104
0
  OutRefIndex(stream, i);
1105
0
    else if (TYPEOF(s) == SYMSXP) {
1106
  /* Note : NILSXP can't occur here */
1107
0
  HashAdd(s, ref_table);
1108
0
  OutInteger(stream, SYMSXP);
1109
0
  WriteItem(PRINTNAME(s), ref_table, stream);
1110
0
    }
1111
0
    else if (TYPEOF(s) == ENVSXP) {
1112
0
  HashAdd(s, ref_table);
1113
0
  if (R_IsPackageEnv(s)) {
1114
0
      SEXP name = R_PackageEnvName(s);
1115
0
      const void *vmax = vmaxget();
1116
0
      warning(_("'%s' may not be available when loading"),
1117
0
        translateChar(STRING_ELT(name, 0)));
1118
0
      vmaxset(vmax);
1119
0
      OutInteger(stream, PACKAGESXP);
1120
0
      OutStringVec(stream, name, ref_table);
1121
0
  }
1122
0
  else if (R_IsNamespaceEnv(s)) {
1123
#ifdef WARN_ABOUT_NAME_SPACES_MAYBE_NOT_AVAILABLE
1124
      warning(_("namespaces may not be available when loading"));
1125
#endif
1126
0
      OutInteger(stream, NAMESPACESXP);
1127
0
      OutStringVec(stream, PROTECT(R_NamespaceEnvSpec(s)), ref_table);
1128
0
      UNPROTECT(1);
1129
0
  }
1130
0
  else {
1131
0
      OutInteger(stream, ENVSXP);
1132
0
      OutInteger(stream, R_EnvironmentIsLocked(s) ? 1 : 0);
1133
0
      WriteItem(ENCLOS(s), ref_table, stream);
1134
0
      WriteItem(FRAME(s), ref_table, stream);
1135
0
      WriteItem(HASHTAB(s), ref_table, stream);
1136
0
      WriteItem(ATTRIB(s), ref_table, stream);
1137
0
  }
1138
0
    }
1139
0
    else {
1140
0
  int flags, hastag, hasattr;
1141
0
  R_xlen_t len;
1142
0
  int ic = 999;
1143
0
  switch(TYPEOF(s)) {
1144
0
  case LISTSXP:
1145
0
  case LANGSXP:
1146
0
  case PROMSXP:
1147
0
  case DOTSXP: hastag = TAG(s) != R_NilValue; break;
1148
0
  case CLOSXP: hastag = true; break;
1149
0
  default: hastag = false;
1150
0
  }
1151
  /* With the CHARSXP cache chains maintained through the ATTRIB
1152
     field the content of that field must not be serialized, so
1153
     we treat it as not there. */
1154
0
  hasattr = (TYPEOF(s) != CHARSXP && ATTRIB(s) != R_NilValue);
1155
0
  flags = PackFlags(TYPEOF(s), LEVELS(s), OBJECT(s),
1156
0
        hasattr, hastag);
1157
0
  OutInteger(stream, flags);
1158
0
  switch (TYPEOF(s)) {
1159
0
  case LISTSXP:
1160
0
  case LANGSXP:
1161
0
  case PROMSXP:
1162
0
  case DOTSXP:
1163
      /* Dotted pair objects */
1164
      /* These write their ATTRIB fields first to allow us to avoid
1165
         recursion on the CDR */
1166
0
      if (hasattr)
1167
0
    WriteItem(ATTRIB(s), ref_table, stream);
1168
0
      if (TAG(s) != R_NilValue)
1169
0
    WriteItem(TAG(s), ref_table, stream);
1170
0
      if (BNDCELL_TAG(s))
1171
0
    R_expand_binding_value(s);
1172
0
      WriteItem(CAR(s), ref_table, stream);
1173
      /* now do a tail call to WriteItem to handle the CDR */
1174
0
      s = CDR(s);
1175
0
      goto tailcall;
1176
0
  case CLOSXP:
1177
      /* Like a dotted pair object */
1178
      /* Write the ATTRIB field first to allow us to avoid
1179
         recursion on the CDR/BODY */
1180
0
      if (hasattr)
1181
0
    WriteItem(ATTRIB(s), ref_table, stream);
1182
0
      WriteItem(CLOENV(s), ref_table, stream);
1183
0
      WriteItem(FORMALS(s), ref_table, stream);
1184
      /* now do a tail call to WriteItem to handle the CDR/BODY */
1185
0
      s = BODY(s);
1186
0
      goto tailcall;
1187
0
  case EXTPTRSXP:
1188
      /* external pointers */
1189
0
      HashAdd(s, ref_table);
1190
0
      WriteItem(EXTPTR_PROT(s), ref_table, stream);
1191
0
      WriteItem(EXTPTR_TAG(s), ref_table, stream);
1192
0
      break;
1193
0
  case WEAKREFSXP:
1194
      /* Weak references */
1195
0
      HashAdd(s, ref_table);
1196
0
      break;
1197
0
  case SPECIALSXP:
1198
0
  case BUILTINSXP:
1199
      /* Builtin functions */
1200
0
      OutInteger(stream, (int)strlen(PRIMNAME(s)));
1201
0
      OutString(stream, PRIMNAME(s), (int)strlen(PRIMNAME(s)));
1202
0
      break;
1203
0
  case CHARSXP:
1204
0
      if (s == NA_STRING)
1205
0
    OutInteger(stream, -1);
1206
0
      else {
1207
0
    OutInteger(stream, LENGTH(s));
1208
0
    OutString(stream, CHAR(s), LENGTH(s));
1209
0
      }
1210
0
      break;
1211
0
  case LGLSXP:
1212
0
  case INTSXP:
1213
0
      len = XLENGTH(s);
1214
0
      WriteLENGTH(stream, s);
1215
0
      OutIntegerVec(stream, s, len);
1216
0
      break;
1217
0
  case REALSXP:
1218
0
      len = XLENGTH(s);
1219
0
      WriteLENGTH(stream, s);
1220
0
      OutRealVec(stream, s, len);
1221
0
      break;
1222
0
  case CPLXSXP:
1223
0
      len = XLENGTH(s);
1224
0
      WriteLENGTH(stream, s);
1225
0
      OutComplexVec(stream, s, len);
1226
0
      break;
1227
0
  case STRSXP:
1228
0
      len = XLENGTH(s);
1229
0
      WriteLENGTH(stream, s);
1230
0
      for (R_xlen_t ix = 0; ix < len; ix++) {
1231
0
    IF_IC_R_CheckUserInterrupt();
1232
0
    WriteItem(STRING_ELT(s, ix), ref_table, stream);
1233
0
      }
1234
0
      break;
1235
0
  case VECSXP:
1236
0
  case EXPRSXP:
1237
0
      len = XLENGTH(s);
1238
0
      WriteLENGTH(stream, s);
1239
0
      for (R_xlen_t ix = 0; ix < len; ix++) {
1240
0
    IF_IC_R_CheckUserInterrupt();
1241
0
    WriteItem(VECTOR_ELT(s, ix), ref_table, stream);
1242
0
      }
1243
0
      break;
1244
0
  case BCODESXP:
1245
0
      WriteBC(s, ref_table, stream);
1246
0
      break;
1247
0
  case RAWSXP:
1248
0
      len = XLENGTH(s);
1249
0
      WriteLENGTH(stream, s);
1250
0
      switch (stream->type) {
1251
0
      case R_pstream_xdr_format:
1252
0
      case R_pstream_binary_format:
1253
0
      {
1254
0
    R_xlen_t done, this;
1255
0
    for (done = 0; done < len; done += this) {
1256
0
        IF_IC_R_CheckUserInterrupt();
1257
0
        this = min2(CHUNK_SIZE, len - done);
1258
0
        stream->OutBytes(stream, RAW(s) + done, (int) this);
1259
0
    }
1260
0
    break;
1261
0
      }
1262
0
      default:
1263
0
    for (R_xlen_t ix = 0; ix < len; ix++) {
1264
0
        IF_IC_R_CheckUserInterrupt();
1265
0
        OutByte(stream, RAW(s)[ix]);
1266
0
    }
1267
0
      }
1268
0
      break;
1269
0
  case OBJSXP:
1270
0
    break; /* only attributes (i.e., slots) count */
1271
0
  default:
1272
0
      error(_("WriteItem: unknown type %i"), TYPEOF(s));
1273
0
  }
1274
0
  if (hasattr)
1275
0
      WriteItem(ATTRIB(s), ref_table, stream);
1276
0
    }
1277
2
}
1278
1279
static SEXP MakeCircleHashTable(void)
1280
0
{
1281
0
    return CONS(R_NilValue, allocVector(VECSXP, HASHSIZE));
1282
0
}
1283
1284
static bool AddCircleHash(SEXP item, SEXP ct)
1285
0
{
1286
0
    SEXP table, bucket, list;
1287
1288
0
    table = CDR(ct);
1289
0
    R_size_t pos = PTRHASH(item) % LENGTH(table);
1290
0
    bucket = VECTOR_ELT(table, pos);
1291
0
    for (list = bucket; list != R_NilValue; list = CDR(list))
1292
0
  if (TAG(list) == item) {
1293
0
      if (CAR(list) == R_NilValue) {
1294
    /* this is the second time; enter in list and mark */
1295
0
    SETCAR(list, R_UnboundValue); /* anything different will do */
1296
0
    SETCAR(ct, CONS(item, CAR(ct)));
1297
0
      }
1298
0
      return true;
1299
0
  }
1300
1301
    /* If we get here then this is a new item; enter in the table */
1302
0
    bucket = CONS(R_NilValue, bucket);
1303
0
    SET_TAG(bucket, item);
1304
0
    SET_VECTOR_ELT(table, pos, bucket);
1305
0
    return false;
1306
0
}
1307
1308
static void ScanForCircles1(SEXP s, SEXP ct)
1309
0
{
1310
0
    switch (TYPEOF(s)) {
1311
0
    case LANGSXP:
1312
0
    case LISTSXP:
1313
0
  if (! AddCircleHash(s, ct)) {
1314
0
      ScanForCircles1(CAR(s), ct);
1315
0
      ScanForCircles1(CDR(s), ct);
1316
0
  }
1317
0
  break;
1318
0
    case BCODESXP:
1319
0
  {
1320
0
      int i, n;
1321
0
      SEXP consts = BCODE_CONSTS(s);
1322
0
      n = LENGTH(consts);
1323
0
      for (i = 0; i < n; i++)
1324
0
    ScanForCircles1(VECTOR_ELT(consts, i), ct);
1325
0
  }
1326
0
  break;
1327
0
    default: break;
1328
0
    }
1329
0
}
1330
1331
static SEXP ScanForCircles(SEXP s)
1332
0
{
1333
0
    SEXP ct;
1334
0
    PROTECT(ct = MakeCircleHashTable());
1335
0
    ScanForCircles1(s, ct);
1336
0
    UNPROTECT(1);
1337
0
    return CAR(ct);
1338
0
}
1339
1340
static SEXP findrep(SEXP x, SEXP reps)
1341
0
{
1342
0
    for (; reps != R_NilValue; reps = CDR(reps))
1343
0
  if (x == CAR(reps))
1344
0
      return reps;
1345
0
    return R_NilValue;
1346
0
}
1347
1348
static void WriteBCLang(SEXP s, SEXP ref_table, SEXP reps,
1349
      R_outpstream_t stream)
1350
0
{
1351
0
    int type = TYPEOF(s);
1352
0
    if (type == LANGSXP || type == LISTSXP) {
1353
0
  SEXP r = findrep(s, reps);
1354
0
  int output = TRUE;
1355
0
  if (r != R_NilValue) {
1356
      /* we have a cell referenced more than once */
1357
0
      if (TAG(r) == R_NilValue) {
1358
    /* this is the first reference, so update and register
1359
       the counter */
1360
0
    int i = INTEGER(CAR(reps))[0]++;
1361
0
    SET_TAG(r, allocVector(INTSXP, 1));
1362
0
    INTEGER(TAG(r))[0] = i;
1363
0
    OutInteger(stream, BCREPDEF);
1364
0
    OutInteger(stream, i);
1365
0
      }
1366
0
      else {
1367
    /* we've seen it before, so just put out the index */
1368
0
    OutInteger(stream, BCREPREF);
1369
0
    OutInteger(stream, INTEGER(TAG(r))[0]);
1370
0
    output = false;
1371
0
      }
1372
0
  }
1373
0
  if (output) {
1374
0
      SEXP attr = ATTRIB(s);
1375
0
      if (attr != R_NilValue) {
1376
0
    switch(type) {
1377
0
    case LANGSXP: type = ATTRLANGSXP; break;
1378
0
    case LISTSXP: type = ATTRLISTSXP; break;
1379
0
    }
1380
0
      }
1381
0
      OutInteger(stream, type);
1382
0
      if (attr != R_NilValue)
1383
0
    WriteItem(attr, ref_table, stream);
1384
0
      WriteItem(TAG(s), ref_table, stream);
1385
0
      WriteBCLang(CAR(s), ref_table, reps, stream);
1386
0
      WriteBCLang(CDR(s), ref_table, reps, stream);
1387
0
  }
1388
0
    }
1389
0
    else {
1390
0
  OutInteger(stream, 0); /* pad */
1391
0
  WriteItem(s, ref_table, stream);
1392
0
    }
1393
0
}
1394
1395
static void WriteBC1(SEXP s, SEXP ref_table, SEXP reps, R_outpstream_t stream)
1396
0
{
1397
0
    int i, n;
1398
0
    SEXP code, consts;
1399
0
    PROTECT(code = R_bcDecode(BCODE_CODE(s)));
1400
0
    WriteItem(code, ref_table, stream);
1401
0
    consts = BCODE_CONSTS(s);
1402
0
    n = LENGTH(consts);
1403
0
    OutInteger(stream, n);
1404
0
    for (i = 0; i < n; i++) {
1405
0
  SEXP c = VECTOR_ELT(consts, i);
1406
0
  int type = TYPEOF(c);
1407
0
  switch (type) {
1408
0
  case BCODESXP:
1409
0
      OutInteger(stream, type);
1410
0
      WriteBC1(c, ref_table, reps, stream);
1411
0
      break;
1412
0
  case LANGSXP:
1413
0
  case LISTSXP:
1414
0
      WriteBCLang(c, ref_table, reps, stream);
1415
0
      break;
1416
0
  default:
1417
0
      OutInteger(stream, type);
1418
0
      WriteItem(c, ref_table, stream);
1419
0
  }
1420
0
    }
1421
0
    UNPROTECT(1);
1422
0
}
1423
1424
static void WriteBC(SEXP s, SEXP ref_table, R_outpstream_t stream)
1425
0
{
1426
0
    SEXP reps = ScanForCircles(s);
1427
0
    PROTECT(reps = CONS(R_NilValue, reps));
1428
0
    OutInteger(stream, length(reps));
1429
0
    SETCAR(reps, allocVector(INTSXP, 1));
1430
0
    INTEGER(CAR(reps))[0] = 0;
1431
0
    WriteBC1(s, ref_table, reps, stream);
1432
0
    UNPROTECT(1);
1433
0
}
1434
1435
void R_Serialize(SEXP s, R_outpstream_t stream)
1436
2
{
1437
2
    int version = stream->version;
1438
1439
2
    OutFormat(stream);
1440
1441
2
    switch(version) {
1442
0
    case 2:
1443
0
  OutInteger(stream, version);
1444
0
  OutInteger(stream, R_VERSION);
1445
0
  OutInteger(stream, R_Version(2,3,0));
1446
0
  break;
1447
2
    case 3:
1448
2
    {
1449
2
  OutInteger(stream, version);
1450
2
  OutInteger(stream, R_VERSION);
1451
2
  OutInteger(stream, R_Version(3,5,0));
1452
2
  const char *natenc = R_nativeEncoding();
1453
2
  int nelen = (int) strlen(natenc);
1454
2
  OutInteger(stream, nelen);
1455
2
  OutString(stream, natenc, nelen);
1456
2
  break;
1457
0
    }
1458
0
    default: error(_("version %d not supported"), version);
1459
2
    }
1460
1461
2
    SEXP ref_table = PROTECT(MakeHashTable());
1462
2
    WriteItem(s, ref_table, stream);
1463
2
    UNPROTECT(1);
1464
2
}
1465
1466
1467
/*
1468
 * Unserialize Code
1469
 */
1470
1471
// used in saveload.c
1472
attribute_hidden int R_ReadItemDepth = 0, R_InitReadItemDepth;
1473
1474
static char lastname[8192] = "<unknown>";
1475
1476
36.8k
#define INITIAL_REFREAD_TABLE_SIZE 128
1477
1478
static SEXP MakeReadRefTable(void)
1479
36.8k
{
1480
36.8k
    SEXP data = allocVector(VECSXP, INITIAL_REFREAD_TABLE_SIZE);
1481
36.8k
    SET_TRUELENGTH(data, 0);
1482
36.8k
    return CONS(data, R_NilValue);
1483
36.8k
}
1484
1485
static SEXP GetReadRef(SEXP table, int index)
1486
4.45M
{
1487
4.45M
    int i = index - 1;
1488
4.45M
    SEXP data = CAR(table);
1489
1490
4.45M
    if (i < 0 || i >= LENGTH(data))
1491
210
  error(_("reference index out of range"));
1492
4.45M
    return VECTOR_ELT(data, i);
1493
4.45M
}
1494
1495
static void AddReadRef(SEXP table, SEXP value)
1496
580k
{
1497
580k
    SEXP data = CAR(table);
1498
580k
    R_xlen_t count = TRUELENGTH(data) + 1;
1499
580k
    if (count >= LENGTH(data)) {
1500
1.08k
  R_xlen_t i, len;
1501
1.08k
  SEXP newdata;
1502
1503
1.08k
  PROTECT(value);
1504
1.08k
  len = 2 * count;
1505
1.08k
  newdata = allocVector(VECSXP, len);
1506
205k
  for (i = 0; i < LENGTH(data); i++)
1507
204k
      SET_VECTOR_ELT(newdata, i, VECTOR_ELT(data, i));
1508
1.08k
  SETCAR(table, newdata);
1509
1.08k
  data = newdata;
1510
1.08k
  UNPROTECT(1);
1511
1.08k
    }
1512
580k
    SET_TRUELENGTH(data, count);
1513
580k
    SET_VECTOR_ELT(data, count - 1, value);
1514
580k
}
1515
1516
static SEXP InStringVec(R_inpstream_t stream, SEXP ref_table)
1517
37.7k
{
1518
37.7k
    if (InInteger(stream) != 0)
1519
59
  error(_("names in persistent strings are not supported yet"));
1520
37.7k
    int len = InInteger(stream);
1521
37.7k
    SEXP s = PROTECT(allocVector(STRSXP, len));
1522
37.7k
    R_ReadItemDepth++;
1523
87.3k
    for (int i = 0; i < len; i++)
1524
49.5k
  SET_STRING_ELT(s, i, ReadItem(ref_table, stream));
1525
37.7k
    R_ReadItemDepth--;
1526
37.7k
    UNPROTECT(1);
1527
37.7k
    return s;
1528
37.7k
}
1529
1530
/* use static buffer to reuse storage */
1531
static R_INLINE void
1532
InIntegerVec(R_inpstream_t stream, SEXP obj, R_xlen_t length)
1533
1.23M
{
1534
1.23M
    switch (stream->type) {
1535
1.20M
    case R_pstream_xdr_format:
1536
1.20M
    {
1537
1.20M
  static char buf[CHUNK_SIZE * sizeof(int)];
1538
1.20M
  R_xlen_t done, this;
1539
1.20M
  XDR xdrs;
1540
2.40M
  for (done = 0; done < length; done += this) {
1541
1.20M
      this = min2(CHUNK_SIZE, length - done);
1542
1.20M
      stream->InBytes(stream, buf, (int)(sizeof(int) * this));
1543
1.20M
      xdrmem_create(&xdrs, buf, (int)(this * sizeof(int)), XDR_DECODE);
1544
15.0M
      for(int cnt = 0; cnt < this; cnt++)
1545
13.8M
    if(!xdr_int(&xdrs, INTEGER(obj) + done + cnt))
1546
0
        error(_("XDR read failed"));
1547
1.20M
      xdr_destroy(&xdrs);
1548
1.20M
  }
1549
1.20M
  break;
1550
0
    }
1551
70
    case R_pstream_binary_format:
1552
70
    {
1553
70
  R_xlen_t done, this;
1554
123
  for (done = 0; done < length; done += this) {
1555
53
      this = min2(CHUNK_SIZE, length - done);
1556
53
      stream->InBytes(stream, INTEGER(obj) + done,
1557
53
          (int)(sizeof(int) * this));
1558
53
  }
1559
70
  break;
1560
0
    }
1561
32.2k
    default:
1562
66.8k
  for (R_xlen_t cnt = 0; cnt < length; cnt++)
1563
34.5k
      INTEGER(obj)[cnt] = InInteger(stream);
1564
1.23M
    }
1565
1.23M
}
1566
1567
static R_INLINE void
1568
InRealVec(R_inpstream_t stream, SEXP obj, R_xlen_t length)
1569
85.0k
{
1570
85.0k
    switch (stream->type) {
1571
52.2k
    case R_pstream_xdr_format:
1572
52.2k
    {
1573
52.2k
  static char buf[CHUNK_SIZE * sizeof(double)];
1574
52.2k
  R_xlen_t done, this;
1575
52.2k
  XDR xdrs;
1576
104k
  for (done = 0; done < length; done += this) {
1577
51.9k
      this = min2(CHUNK_SIZE, length - done);
1578
51.9k
      stream->InBytes(stream, buf, (int)(sizeof(double) * this));
1579
51.9k
      xdrmem_create(&xdrs, buf, (int)(this * sizeof(double)), XDR_DECODE);
1580
115k
      for(R_xlen_t cnt = 0; cnt < this; cnt++)
1581
63.5k
    if(!xdr_double(&xdrs, REAL(obj) + done + cnt))
1582
0
        error(_("XDR read failed"));
1583
51.9k
      xdr_destroy(&xdrs);
1584
51.9k
  }
1585
52.2k
  break;
1586
0
    }
1587
44
    case R_pstream_binary_format:
1588
44
    {
1589
44
  R_xlen_t done, this;
1590
87
  for (done = 0; done < length; done += this) {
1591
43
      this = min2(CHUNK_SIZE, length - done);
1592
43
      stream->InBytes(stream, REAL(obj) + done,
1593
43
          (int)(sizeof(double) * this));
1594
43
  }
1595
44
  break;
1596
0
    }
1597
32.7k
    default:
1598
128k
  for (R_xlen_t cnt = 0; cnt < length; cnt++)
1599
95.8k
      REAL(obj)[cnt] = InReal(stream);
1600
85.0k
    }
1601
85.0k
}
1602
1603
static R_INLINE void
1604
InComplexVec(R_inpstream_t stream, SEXP obj, R_xlen_t length)
1605
1.14k
{
1606
1.14k
    switch (stream->type) {
1607
413
    case R_pstream_xdr_format:
1608
413
    {
1609
413
  static char buf[CHUNK_SIZE * sizeof(Rcomplex)];
1610
413
  R_xlen_t done, this;
1611
413
  XDR xdrs;
1612
413
  Rcomplex *output = COMPLEX(obj);
1613
771
  for (done = 0; done < length; done += this) {
1614
358
      this = min2(CHUNK_SIZE, length - done);
1615
358
      stream->InBytes(stream, buf, (int)(sizeof(Rcomplex) * this));
1616
358
      xdrmem_create(&xdrs, buf, (int)(this * sizeof(Rcomplex)), XDR_DECODE);
1617
1.32k
      for(R_xlen_t cnt = 0; cnt < this; cnt++) {
1618
971
    if(!xdr_double(&xdrs, &(output[done+cnt].r)) ||
1619
971
       !xdr_double(&xdrs, &(output[done+cnt].i)))
1620
0
        error(_("XDR read failed"));
1621
971
      }
1622
358
      xdr_destroy(&xdrs);
1623
358
  }
1624
413
  break;
1625
0
    }
1626
23
    case R_pstream_binary_format:
1627
23
    {
1628
23
  R_xlen_t done, this;
1629
45
  for (done = 0; done < length; done += this) {
1630
22
      this = min2(CHUNK_SIZE, length - done);
1631
22
      stream->InBytes(stream, COMPLEX(obj) + done,
1632
22
          (int)(sizeof(Rcomplex) * this));
1633
22
  }
1634
23
  break;
1635
0
    }
1636
706
    default:
1637
4.06k
  for (R_xlen_t cnt = 0; cnt < length; cnt++)
1638
3.35k
      COMPLEX(obj)[cnt] = InComplex(stream);
1639
1.14k
    }
1640
1.14k
}
1641
1642
static int TryConvertString(void *obj, const char *inp, size_t inplen,
1643
                            char *buf, size_t *bufleft)
1644
5.65k
{
1645
5.65k
    if (Riconv(obj, NULL, NULL, &buf, bufleft) == -1)
1646
0
  return -1;
1647
5.65k
    return (int) Riconv(obj, &inp, &inplen, &buf, bufleft);
1648
5.65k
}
1649
1650
static SEXP
1651
ConvertChar(void *obj, char *inp, size_t inplen, cetype_t enc)
1652
5.46k
{
1653
5.46k
    size_t buflen = inplen;
1654
1655
5.65k
    for(;;) {
1656
5.65k
  size_t bufleft = buflen;
1657
5.65k
  if (buflen < 1000) {
1658
5.60k
      char buf[buflen + 1];
1659
5.60k
      if (TryConvertString(obj, inp, inplen, buf, &bufleft) == -1) {
1660
2.17k
    if (errno == E2BIG) {
1661
192
        buflen *= 2;
1662
192
        continue;
1663
192
    } else
1664
1.98k
        return R_NilValue;
1665
2.17k
      }
1666
3.43k
      return mkCharLenCE(buf, (int)(buflen - bufleft), enc);
1667
5.60k
  } else {
1668
      /* R_alloc, not Calloc: mkCharLenCE can signal errors (e.g. for
1669
         an embedded nul), which would leak a Calloc'd buffer */
1670
50
      const void *vmax = vmaxget();
1671
50
      char *buf = R_alloc(buflen + 1, sizeof(char));
1672
50
      if (TryConvertString(obj, inp, inplen, buf, &bufleft) == -1) {
1673
28
    vmaxset(vmax);
1674
28
    if (errno == E2BIG) {
1675
7
        buflen *= 2;
1676
7
        continue;
1677
7
    } else
1678
21
        return R_NilValue;
1679
28
      }
1680
22
      SEXP ans = mkCharLenCE(buf, (int)(buflen - bufleft), enc);
1681
22
      vmaxset(vmax);
1682
22
      return ans;
1683
50
  }
1684
5.65k
    }
1685
5.46k
}
1686
1687
static char *native_fromcode(R_inpstream_t stream)
1688
2.78k
{
1689
2.78k
    char *from = stream->native_encoding;
1690
2.78k
#ifdef HAVE_ICONV_CP1252
1691
2.78k
    if (!strcmp(from, "ISO-8859-1"))
1692
18
  from = "CP1252";
1693
2.78k
#endif
1694
2.78k
    return from;
1695
2.78k
}
1696
1697
static void invalid_utf8_warning(const char *buf, int length, const char *from)
1698
945
{
1699
945
    const void *vmax = vmaxget();
1700
945
    const char *native_buf;
1701
1702
945
    if (utf8Valid(buf)) {
1703
691
  native_buf = reEnc4(buf, length, "UTF-8", "", 1);
1704
691
  warning(_("input string '%s' cannot be translated from '%s' to UTF-8, but is valid UTF-8"),
1705
691
    native_buf, from);
1706
691
    } else {
1707
254
  native_buf = reEnc(reEnc4(buf, length, from, "UTF-8", 1),
1708
254
         CE_UTF8, CE_NATIVE, 2);
1709
254
  warning(_("input string '%s' cannot be translated to UTF-8, is it valid in '%s'?"),
1710
254
    native_buf, from);
1711
254
    }
1712
945
    vmaxset(vmax);
1713
945
}
1714
1715
/* Read string into pre-allocated buffer, convert encoding if necessary, and
1716
   return a CHARSXP */
1717
static SEXP
1718
ReadChar(R_inpstream_t stream, char *buf, int length, int levs)
1719
2.61M
{
1720
2.61M
    InString(stream, buf, length);
1721
2.61M
    buf[length] = '\0';
1722
2.61M
    if (levs & UTF8_MASK)
1723
989
  return mkCharLenCE(buf, length, CE_UTF8);
1724
2.61M
    if (levs & LATIN1_MASK)
1725
325
  return mkCharLenCE(buf, length, CE_LATIN1);
1726
2.61M
    if (levs & BYTES_MASK)
1727
155
  return mkCharLenCE(buf, length, CE_BYTES);
1728
2.61M
    if (levs & ASCII_MASK)
1729
2.59M
  return mkCharLenCE(buf, length, CE_NATIVE);
1730
1731
    /* native encoding, not ascii */
1732
21.5k
    if (!stream->native_encoding[0] || /* original native encoding unknown */
1733
18.1k
        (stream->nat2nat_obj == (void *)-1 && /* translation impossible or disabled */
1734
12.9k
         stream->nat2utf8_obj == (void *)-1))
1735
15.9k
  return mkCharLenCE(buf, length, CE_NATIVE);
1736
    /* try converting to native encoding */
1737
5.67k
    if (!stream->nat2nat_obj &&
1738
1.30k
        !strcmp(stream->native_encoding, R_nativeEncoding())) {
1739
  /* No translation needed. Performance optimization but also leaves
1740
     invalid strings in their encoding undetected. */
1741
329
  stream->nat2nat_obj = (void *)-1;
1742
329
  stream->nat2utf8_obj = (void *)-1;
1743
#ifdef WARN_DESERIALIZE_INVALID_UTF8
1744
  if (known_to_be_utf8 && !utf8Valid(buf))
1745
      warning(_("deserializing invalid UTF-8 string '%s'"), buf);
1746
#endif
1747
329
    }
1748
5.67k
    if (!stream->nat2nat_obj) {
1749
978
  char *from = native_fromcode(stream);
1750
978
  stream->nat2nat_obj = Riconv_open("", from);
1751
978
  if (stream->nat2nat_obj == (void *)-1)
1752
506
      warning(_("unsupported conversion from '%s' to '%s'"), from, "");
1753
978
    }
1754
5.67k
    if (stream->nat2nat_obj != (void *)-1) {
1755
4.40k
  cetype_t enc = CE_NATIVE;
1756
4.40k
  if (known_to_be_utf8) enc = CE_UTF8;
1757
4.40k
  else if (known_to_be_latin1) enc = CE_LATIN1;
1758
4.40k
  SEXP ans = ConvertChar(stream->nat2nat_obj, buf, length, enc);
1759
4.40k
  if (ans != R_NilValue)
1760
3.34k
      return ans;
1761
1.06k
  if (known_to_be_utf8) {
1762
      /* nat2nat_obj is converting to UTF-8, no need to use nat2utf8_obj */
1763
0
      stream->nat2utf8_obj = (void *)-1;
1764
0
      invalid_utf8_warning(buf, length, native_fromcode(stream));
1765
0
  }
1766
1.06k
    }
1767
    /* try converting to UTF-8 */
1768
2.33k
    if (!stream->nat2utf8_obj) {
1769
866
  char *from = native_fromcode(stream);
1770
866
  stream->nat2utf8_obj = Riconv_open("UTF-8", from);
1771
866
  if (stream->nat2utf8_obj == (void *)-1) {
1772
      /* very unlikely */
1773
506
      warning(_("unsupported conversion from '%s' to '%s'"),
1774
506
              from, "UTF-8");
1775
506
      warning(_("strings not representable in native encoding will not be translated"));
1776
506
  } else
1777
360
      warning(_("strings not representable in native encoding will be translated to UTF-8"));
1778
866
    }
1779
2.33k
    if (stream->nat2utf8_obj != (void *)-1) {
1780
1.05k
  SEXP ans = ConvertChar(stream->nat2utf8_obj, buf, length, CE_UTF8);
1781
1.05k
  if (ans != R_NilValue)
1782
6
      return ans;
1783
1.05k
  invalid_utf8_warning(buf, length, native_fromcode(stream));
1784
1.05k
    }
1785
    /* no translation possible */
1786
2.32k
    return mkCharLenCE(buf, length, CE_NATIVE);
1787
2.33k
}
1788
1789
static R_xlen_t ReadLENGTH (R_inpstream_t stream)
1790
2.87M
{
1791
2.87M
    int len = InInteger(stream);
1792
2.87M
#ifdef LONG_VECTOR_SUPPORT
1793
2.87M
    if (len < -1)
1794
59
  error(_("negative serialized length for vector"));
1795
2.87M
    if (len == -1) {
1796
316
  unsigned int len1, len2;
1797
316
  len1 = InInteger(stream); /* upper part */
1798
316
  len2 = InInteger(stream); /* lower part */
1799
316
  R_xlen_t xlen = len1;
1800
  /* sanity check for now */
1801
316
  if (len1 > 65536)
1802
2
      error (_("invalid upper part of serialized vector length"));
1803
316
  return (xlen << 32) + len2;
1804
2.87M
    } else return len;
1805
#else
1806
    if (len < 0)
1807
  error(_("negative serialized vector length:\nperhaps long vector from 64-bit version of R?"));
1808
    return len;
1809
#endif
1810
2.87M
}
1811
1812
/* differs when it fails from version in envir.c */
1813
static SEXP R_FindNamespace1(SEXP info)
1814
11.9k
{
1815
11.9k
    SEXP expr, val, where;
1816
11.9k
    PROTECT(info);
1817
11.9k
    where = PROTECT(ScalarString(mkChar(lastname)));
1818
11.9k
    SEXP s_getNamespace = install("..getNamespace");
1819
11.9k
    PROTECT(expr = LCONS(s_getNamespace,
1820
11.9k
       LCONS(info, LCONS(where, R_NilValue))));
1821
11.9k
    val = eval(expr, R_BaseEnv);
1822
11.9k
    UNPROTECT(3);
1823
11.9k
    return val;
1824
11.9k
}
1825
1826
static SEXP ReadItem_Recursive(int flags, SEXP ref_table, R_inpstream_t stream);
1827
1828
static SEXP ReadItem_Iterative(int flags, SEXP ref_table, R_inpstream_t stream)
1829
2.62M
{
1830
    /* Building dotted pair objects with recursion on the CDR will
1831
       overflow the PROTECT stack for long lists. Instead we build
1832
       pairlists in an iterative loop */
1833
    
1834
2.62M
    SEXPTYPE type = DECODE_TYPE(flags);
1835
2.62M
    SEXP s, sfirst = NULL, slast = NULL;
1836
    
1837
    /* An assertion here guarantees that we go through the loop at
1838
       least once. This make for cleaner exit code and avoids a
1839
       potential infinite loop: ReadItem_Recursive <->
1840
       ReadIterm_iterative */
1841
2.62M
    R_assert(type == LISTSXP || type == LANGSXP || type == CLOSXP ||
1842
2.62M
       type == PROMSXP || type == DOTSXP);
1843
    
1844
6.50M
    while (type == LISTSXP || type == LANGSXP || type == CLOSXP ||
1845
3.88M
     type == PROMSXP || type == DOTSXP) {
1846
3.88M
  int levs, objf, hasattr, hastag;
1847
3.88M
  UnpackFlags(flags, &type, &levs, &objf, &hasattr, &hastag);
1848
1849
3.88M
  PROTECT(s = allocSExp(type));
1850
3.88M
  SETLEVELS(s, levs);
1851
3.88M
  SET_OBJECT(s, objf);
1852
3.88M
  R_ReadItemDepth++;
1853
3.88M
  bool set_lastname = false;
1854
3.88M
  SET_ATTRIB(s, hasattr ? ReadItem(ref_table, stream) : R_NilValue);
1855
3.88M
  SET_TAG(s, hastag ? ReadItem(ref_table, stream) : R_NilValue);
1856
3.88M
  if (hastag && R_ReadItemDepth == R_InitReadItemDepth + 1 &&
1857
16.0k
      isSymbol(TAG(s))) {
1858
197
      snprintf(lastname, 8192, "%s", CHAR(PRINTNAME(TAG(s))));
1859
197
      set_lastname = TRUE;
1860
197
  }
1861
3.88M
  if (hastag && R_ReadItemDepth <= 0) {
1862
0
      Rprintf("%*s", 2*(R_ReadItemDepth - R_InitReadItemDepth), "");
1863
0
      PrintValue(TAG(s));
1864
0
  }
1865
3.88M
  SETCAR(s, ReadItem(ref_table, stream));
1866
3.88M
  R_ReadItemDepth--;
1867
3.88M
  R_CheckStack();
1868
1869
3.88M
  if (sfirst == NULL) {
1870
1.49M
      sfirst = s; /* First iteration: start list */
1871
1.49M
  }
1872
2.38M
  else {
1873
2.38M
      SETCDR(slast, s); /* Subsequent iterations: extend list */
1874
2.38M
      UNPROTECT(1); /* s, which is now protected as part of sfirst */
1875
2.38M
  }
1876
3.88M
  slast = s;
1877
1878
  /* For reading closures and promises stored in earlier
1879
     versions, convert NULL env to baseenv() */
1880
3.88M
  if (type == CLOSXP && CLOENV(s) == R_NilValue)
1881
124k
      SET_CLOENV(s, R_BaseEnv);
1882
3.75M
  else if (type == PROMSXP && PRENV(s) == R_NilValue)
1883
138k
      SET_PRENV(s, R_BaseEnv);
1884
3.88M
  if (set_lastname) strcpy(lastname, "<unknown>");
1885
1886
3.88M
  flags = InInteger(stream);
1887
3.88M
  type = DECODE_TYPE(flags);
1888
3.88M
    }
1889
1890
2.62M
    R_ReadItemDepth++;
1891
2.62M
    PROTECT(s = ReadItem_Recursive(flags, ref_table, stream));
1892
2.62M
    R_ReadItemDepth--;
1893
2.62M
    SETCDR(slast, s);
1894
2.62M
    UNPROTECT(2); /* s, sfirst */
1895
2.62M
    return sfirst;
1896
2.62M
}
1897
1898
1899
static SEXP ReadItem_Recursive (int flags, SEXP ref_table, R_inpstream_t stream)
1900
19.8M
{
1901
19.8M
    SEXPTYPE type;
1902
19.8M
    SEXP s;
1903
19.8M
    R_xlen_t len, count;
1904
19.8M
    int levs, objf, hasattr, hastag, length;
1905
1906
19.8M
    R_assert(TYPEOF(ref_table) == LISTSXP && TYPEOF(CAR(ref_table)) == VECSXP);
1907
1908
    /* Each level of nesting in the stream recurses here, so a stream
1909
       nesting lists tens of thousands deep would otherwise overflow the
1910
       C stack before anything else notices. */
1911
19.8M
    R_CheckStack();
1912
1913
19.8M
    UnpackFlags(flags, &type, &levs, &objf, &hasattr, &hastag);
1914
1915
19.8M
    switch(type) {
1916
5.82M
    case NILVALUE_SXP:      return R_NilValue;
1917
1.68k
    case EMPTYENV_SXP:     return R_EmptyEnv;
1918
1.60k
    case BASEENV_SXP:     return R_BaseEnv;
1919
9.22k
    case GLOBALENV_SXP:     return R_GlobalEnv;
1920
9.20k
    case UNBOUNDVALUE_SXP:  return R_UnboundValue;
1921
215k
    case MISSINGARG_SXP:    return R_MissingArg;
1922
18.8k
    case BASENAMESPACE_SXP:
1923
18.8k
  return R_BaseNamespace;
1924
4.45M
    case REFSXP:
1925
4.45M
  return GetReadRef(ref_table, InRefIndex(stream, flags));
1926
25.2k
    case PERSISTSXP:
1927
25.2k
  PROTECT(s = InStringVec(stream, ref_table));
1928
25.2k
  s = PersistentRestore(stream, s);
1929
25.2k
  UNPROTECT(1);
1930
25.2k
  AddReadRef(ref_table, s);
1931
25.2k
  return s;
1932
35.1k
    case ALTREP_SXP:
1933
35.1k
  {
1934
35.1k
      R_ReadItemDepth++;
1935
35.1k
      SEXP info = PROTECT(ReadItem(ref_table, stream));
1936
35.1k
      SEXP state = PROTECT(ReadItem(ref_table, stream));
1937
35.1k
      SEXP attr = PROTECT(ReadItem(ref_table, stream));
1938
35.1k
      s = ALTREP_UNSERIALIZE_EX(info, state, attr, objf, levs);
1939
35.1k
      UNPROTECT(3); /* info, state, attr */
1940
35.1k
      R_ReadItemDepth--;
1941
35.1k
      return s;
1942
0
  }
1943
490k
    case SYMSXP:
1944
490k
  R_ReadItemDepth++;
1945
490k
  PROTECT(s = ReadItem(ref_table, stream)); /* print name */
1946
490k
  R_ReadItemDepth--;
1947
490k
  s = installTrChar(s);
1948
490k
  AddReadRef(ref_table, s);
1949
490k
  UNPROTECT(1);
1950
490k
  return s;
1951
493
    case PACKAGESXP:
1952
493
  PROTECT(s = InStringVec(stream, ref_table));
1953
493
  s = R_FindPackageEnv(s);
1954
493
  UNPROTECT(1);
1955
493
  AddReadRef(ref_table, s);
1956
493
  return s;
1957
11.9k
    case NAMESPACESXP:
1958
11.9k
  PROTECT(s = InStringVec(stream, ref_table));
1959
11.9k
  s = R_FindNamespace1(s);
1960
11.9k
  AddReadRef(ref_table, s);
1961
11.9k
  UNPROTECT(1);
1962
11.9k
  return s;
1963
13.5k
    case ENVSXP:
1964
13.5k
  {
1965
13.5k
      int locked = InInteger(stream);
1966
1967
13.5k
      PROTECT(s = allocSExp(ENVSXP));
1968
1969
      /* MUST register before filling in */
1970
13.5k
      AddReadRef(ref_table, s);
1971
1972
      /* Now fill it in  */
1973
13.5k
      R_ReadItemDepth++;
1974
13.5k
      SET_ENCLOS(s, ReadItem(ref_table, stream));
1975
13.5k
      SET_FRAME(s, ReadItem(ref_table, stream));
1976
13.5k
      SET_HASHTAB(s, ReadItem(ref_table, stream));
1977
13.5k
      SET_ATTRIB(s, ReadItem(ref_table, stream));
1978
13.5k
      R_ReadItemDepth--;
1979
13.5k
      if (ATTRIB(s) != R_NilValue &&
1980
1.21k
    getAttrib(s, R_ClassSymbol) != R_NilValue)
1981
    /* We don't write out the object bit for environments,
1982
       so reconstruct it here if needed. */
1983
0
    SET_OBJECT(s, 1);
1984
13.5k
      R_RestoreHashCount(s);
1985
13.5k
      if (locked) R_LockEnvironment(s, FALSE);
1986
      /* Convert a NULL enclosure to baseenv() */
1987
13.5k
      if (ENCLOS(s) == R_NilValue) SET_ENCLOS(s, R_BaseEnv);
1988
13.5k
      UNPROTECT(1);
1989
13.5k
      return s;
1990
0
  }
1991
959k
    case LISTSXP:
1992
1.46M
    case LANGSXP:
1993
2.19M
    case CLOSXP:
1994
2.61M
    case PROMSXP:
1995
2.62M
    case DOTSXP:
1996
2.62M
  return ReadItem_Iterative(flags, ref_table, stream);
1997
6.09M
    default:
1998
  /* These break out of the switch to have their ATTR,
1999
     LEVELS, and OBJECT fields filled in.  Each leaves the
2000
     newly allocated value PROTECTed */
2001
6.09M
  switch (type) {
2002
17.9k
  case EXTPTRSXP:
2003
17.9k
      PROTECT(s = allocSExp(type));
2004
17.9k
      AddReadRef(ref_table, s);
2005
17.9k
      R_SetExternalPtrAddr(s, NULL);
2006
17.9k
      R_ReadItemDepth++;
2007
17.9k
      R_SetExternalPtrProtected(s, ReadItem(ref_table, stream));
2008
17.9k
      R_SetExternalPtrTag(s, ReadItem(ref_table, stream));
2009
17.9k
      R_ReadItemDepth--;
2010
17.9k
      break;
2011
37.6k
  case WEAKREFSXP:
2012
37.6k
      PROTECT(s = R_MakeWeakRef(R_NilValue, R_NilValue, R_NilValue,
2013
37.6k
              FALSE));
2014
37.6k
      AddReadRef(ref_table, s);
2015
37.6k
      break;
2016
325k
  case SPECIALSXP:
2017
341k
  case BUILTINSXP:
2018
341k
      {
2019
    /* These are all short strings */
2020
341k
    length = InInteger(stream);
2021
    /* Primitive names come from R_FunTab.  An implausible
2022
       length from the stream must not become a large
2023
       stack allocation. */
2024
341k
    if (length < 0 || length > 255)
2025
99
        error(_("invalid length"));
2026
341k
    R_CheckStack2(length+1);
2027
341k
    char cbuf[length+1];
2028
341k
    InString(stream, cbuf, length);
2029
341k
    cbuf[length] = '\0';
2030
341k
    int index = StrToInternal(cbuf);
2031
341k
    if (index == NA_INTEGER) {
2032
325k
        warning(_("unrecognized internal function name \"%s\""), cbuf);
2033
325k
        PROTECT(s = R_NilValue);
2034
325k
    } else
2035
15.3k
        PROTECT(s = mkPRIMSXP(index, type == BUILTINSXP));
2036
341k
      }
2037
341k
      break;
2038
2.66M
  case CHARSXP:
2039
      /* these are currently limited to 2^31 -1 bytes */
2040
2.66M
      length = InInteger(stream);
2041
2.66M
      if (length < -1)
2042
59
    error(_("invalid length"));
2043
2.66M
      else if (length == -1)
2044
50.4k
    PROTECT(s = NA_STRING);
2045
2.61M
      else if (length < 1000) {
2046
2.61M
    char cbuf[length+1];
2047
2.61M
    PROTECT(s = ReadChar(stream, cbuf, length, levs));
2048
2.61M
      } else {
2049
    /* R_alloc, not Calloc: ReadChar can signal errors (truncated
2050
       stream, embedded nul), which would leak a Calloc'd buffer */
2051
533
    const void *vmax = vmaxget();
2052
533
    char *cbuf = R_alloc((size_t)length + 1, sizeof(char));
2053
533
    PROTECT(s = ReadChar(stream, cbuf, length, levs));
2054
533
    vmaxset(vmax);
2055
533
      }
2056
2.66M
      break;
2057
238k
  case LGLSXP:
2058
1.23M
  case INTSXP:
2059
1.23M
      len = ReadLENGTH(stream);
2060
1.23M
      PROTECT(s = allocVector(type, len));
2061
1.23M
      InIntegerVec(stream, s, len);
2062
1.23M
      break;
2063
85.1k
  case REALSXP:
2064
85.1k
      len = ReadLENGTH(stream);
2065
85.1k
      PROTECT(s = allocVector(type, len));
2066
85.1k
      InRealVec(stream, s, len);
2067
85.1k
      break;
2068
1.23k
  case CPLXSXP:
2069
1.23k
      len = ReadLENGTH(stream);
2070
1.23k
      PROTECT(s = allocVector(type, len));
2071
1.23k
      InComplexVec(stream, s, len);
2072
1.23k
      break;
2073
1.42M
  case STRSXP:
2074
1.42M
      len = ReadLENGTH(stream);
2075
1.42M
      PROTECT(s = allocVector(type, len));
2076
1.42M
      R_ReadItemDepth++;
2077
3.53M
      for (count = 0; count < len; ++count)
2078
2.10M
    SET_STRING_ELT(s, count, ReadItem(ref_table, stream));
2079
1.42M
      R_ReadItemDepth--;
2080
1.42M
      break;
2081
85.7k
  case VECSXP:
2082
87.1k
  case EXPRSXP:
2083
87.1k
      len = ReadLENGTH(stream);
2084
87.1k
      PROTECT(s = allocVector(type, len));
2085
87.1k
      R_ReadItemDepth++;
2086
504k
      for (count = 0; count < len; ++count) {
2087
417k
    if (R_ReadItemDepth <= 0)
2088
0
        Rprintf("%*s[%lld]\n", 2*(R_ReadItemDepth - R_InitReadItemDepth),
2089
0
                "", (long long)count+1);
2090
417k
    SET_VECTOR_ELT(s, count, ReadItem(ref_table, stream));
2091
417k
      }
2092
87.1k
      R_ReadItemDepth--;
2093
87.1k
      break;
2094
107k
  case BCODESXP:
2095
107k
      PROTECT(s = ReadBC(ref_table, stream));
2096
107k
      break;
2097
3
  case CLASSREFSXP:
2098
3
      error(_("this version of R cannot read class references"));
2099
9
  case GENERICREFSXP:
2100
9
      error(_("this version of R cannot read generic function references"));
2101
36.6k
  case RAWSXP:
2102
36.6k
      len = ReadLENGTH(stream);
2103
36.6k
      PROTECT(s = allocVector(type, len));
2104
36.6k
      switch (stream->type) {
2105
36.3k
      case R_pstream_ascii_format:
2106
237k
    for (R_xlen_t ix = 0; ix < len; ix++) {
2107
201k
        char word[128];
2108
201k
        unsigned int i; // unsigned to avoid compiler warnings
2109
201k
        InWord(stream, word, sizeof(word));
2110
201k
        if(sscanf(word, "%2x", &i) != 1) error(_("read error"));
2111
201k
        RAW(s)[ix] = (Rbyte) i;
2112
201k
    }
2113
36.3k
    break;
2114
179
      default:
2115
179
      {
2116
179
    R_xlen_t done, this;
2117
365
    for (done = 0; done < len; done += this) {
2118
186
        this = min2(CHUNK_SIZE, len - done);
2119
186
        stream->InBytes(stream, RAW(s) + done, (int) this);
2120
186
    }
2121
179
      }
2122
36.6k
      }
2123
36.3k
      break;
2124
56.5k
  case OBJSXP:
2125
56.5k
      PROTECT(s = R_allocObject());
2126
56.5k
      break;
2127
738
  default:
2128
738
      s = R_NilValue; /* keep compiler happy */
2129
738
      error(_("ReadItem: unknown type %i, perhaps written by later version of R"), type);
2130
6.09M
  }
2131
6.07M
  if (type != CHARSXP) SETLEVELS(s, levs);
2132
6.07M
  SET_OBJECT(s, objf);
2133
6.07M
  if (TYPEOF(s) == CHARSXP) {
2134
      /* With the CHARSXP cache maintained through the ATTRIB
2135
         field that field has already been filled in by the
2136
         mkChar/mkCharCE call above, so we need to leave it
2137
         alone.  If there is an attribute (as there might be if
2138
         the serialized data was created by an older version) we
2139
         read and ignore the value. */
2140
2.66M
      R_ReadItemDepth++;
2141
2.66M
      if (hasattr) ReadItem(ref_table, stream);
2142
2.66M
      R_ReadItemDepth--;
2143
2.66M
  }
2144
3.40M
  else {
2145
3.40M
      R_ReadItemDepth++;
2146
3.40M
      SET_ATTRIB(s, hasattr ? ReadItem(ref_table, stream) : R_NilValue);
2147
3.40M
      R_ReadItemDepth--;
2148
3.40M
  }
2149
6.07M
  UNPROTECT(1); /* s */
2150
6.07M
  if (TYPEOF(s) == BCODESXP && !R_BCVersionOK(s))
2151
601
      return R_BytecodeExpr(s);
2152
6.07M
  return s;
2153
19.8M
    }
2154
19.8M
}
2155
2156
static R_INLINE SEXP ReadItem (SEXP ref_table, R_inpstream_t stream)
2157
18.6M
{
2158
18.6M
    int flags = InInteger(stream);
2159
18.6M
    return ReadItem_Recursive(flags, ref_table, stream);
2160
18.6M
}
2161
2162
/* Slots for the repeated language objects of one bytecode stream.  The
2163
   count written ahead of them is only a hint: it comes from the stream,
2164
   and a malformed one can claim billions, so the vector starts small
2165
   and grows as definitions are read. */
2166
typedef struct {
2167
    SEXP slots;   /* VECSXP, reprotected when it grows */
2168
    PROTECT_INDEX ipx;
2169
    int ndefs;    /* definitions read so far */
2170
} BCReps;
2171
2172
#define BC_REPS_INIT 1024
2173
2174
static void GrowBCReps(BCReps *reps)
2175
2
{
2176
2
    R_xlen_t n = XLENGTH(reps->slots), newn = n < 16 ? 16 : 2 * n;
2177
2
    SEXP s = allocVector(VECSXP, newn);
2178
2
    for (R_xlen_t i = 0; i < n; i++)
2179
0
  SET_VECTOR_ELT(s, i, VECTOR_ELT(reps->slots, i));
2180
2
    reps->slots = s;
2181
2
    REPROTECT(s, reps->ipx);
2182
2
}
2183
2184
static SEXP ReadBC1(SEXP ref_table, BCReps *reps, R_inpstream_t stream);
2185
2186
static SEXP ReadBCLang(int type, SEXP ref_table, BCReps *reps,
2187
           R_inpstream_t stream)
2188
8.47M
{
2189
8.47M
    switch (type) {
2190
1.03M
    case BCREPREF:
2191
1.03M
  {
2192
1.03M
      int pos = InInteger(stream);
2193
1.03M
      if (pos < 0 || pos >= reps->ndefs)
2194
77
    error(_("bytecode reference index out of range"));
2195
1.03M
      return VECTOR_ELT(reps->slots, pos);
2196
0
  }
2197
779k
    case BCREPDEF:
2198
1.18M
    case LANGSXP:
2199
3.67M
    case LISTSXP:
2200
3.67M
    case ATTRLANGSXP:
2201
3.67M
    case ATTRLISTSXP:
2202
3.67M
  {
2203
3.67M
      SEXP ans;
2204
3.67M
      int pos = -1;
2205
3.67M
      int hasattr = false;
2206
3.67M
      if (type == BCREPDEF) {
2207
779k
    pos = InInteger(stream);
2208
    /* definitions are numbered in writing order, so a
2209
       well-formed stream never skips ahead */
2210
779k
    if (pos < 0 || pos > reps->ndefs)
2211
79
        error(_("bytecode reference index out of range"));
2212
779k
    type = InInteger(stream);
2213
779k
      }
2214
3.67M
      switch (type) {
2215
39
      case ATTRLANGSXP: type = LANGSXP; hasattr = TRUE; break;
2216
4
      case ATTRLISTSXP: type = LISTSXP; hasattr = TRUE; break;
2217
3.67M
      }
2218
      /* the BCREPDEF type comes from the stream; allocSExp would
2219
         happily build a cons cell labelled as any other type */
2220
3.67M
      if (type != LANGSXP && type != LISTSXP)
2221
47
    error(_("ReadBCLang: invalid type %i"), type);
2222
3.67M
      PROTECT(ans = allocSExp(type));
2223
3.67M
      if (pos >= 0) {
2224
779k
    if (pos >= XLENGTH(reps->slots))
2225
2
        GrowBCReps(reps);
2226
779k
    SET_VECTOR_ELT(reps->slots, pos, ans);
2227
779k
    if (pos == reps->ndefs)
2228
779k
        reps->ndefs++;
2229
779k
      }
2230
3.67M
      R_ReadItemDepth++;
2231
3.67M
      if (hasattr)
2232
43
    SET_ATTRIB(ans, ReadItem(ref_table, stream));
2233
3.67M
      SET_TAG(ans, ReadItem(ref_table, stream));
2234
3.67M
      R_ReadItemDepth--;
2235
3.67M
      SETCAR(ans, ReadBCLang(InInteger(stream), ref_table, reps,
2236
3.67M
           stream));
2237
3.67M
      SETCDR(ans, ReadBCLang(InInteger(stream), ref_table, reps,
2238
3.67M
           stream));
2239
3.67M
      UNPROTECT(1);
2240
3.67M
      return ans;
2241
3.67M
  }
2242
3.77M
    default:
2243
3.77M
  {
2244
3.77M
      R_ReadItemDepth++;
2245
3.77M
      SEXP res = ReadItem(ref_table, stream);
2246
3.77M
      R_ReadItemDepth--;
2247
3.77M
      return res;
2248
3.67M
  }
2249
8.47M
    }
2250
8.47M
}
2251
2252
static SEXP ReadBCConsts(SEXP ref_table, BCReps *reps, R_inpstream_t stream)
2253
373k
{
2254
373k
    SEXP ans, c;
2255
373k
    int i, n;
2256
373k
    n = InInteger(stream);
2257
373k
    PROTECT(ans = allocVector(VECSXP, n));
2258
3.12M
    for (i = 0; i < n; i++) {
2259
2.75M
  int type = InInteger(stream);
2260
2.75M
  switch (type) {
2261
267k
  case BCODESXP:
2262
267k
      c = ReadBC1(ref_table, reps, stream);
2263
267k
      SET_VECTOR_ELT(ans, i, c);
2264
267k
      break;
2265
163k
  case LANGSXP:
2266
164k
  case LISTSXP:
2267
210k
  case BCREPDEF:
2268
1.13M
  case BCREPREF:
2269
1.13M
  case ATTRLANGSXP:
2270
1.13M
  case ATTRLISTSXP:
2271
1.13M
      c = ReadBCLang(type, ref_table, reps, stream);
2272
1.13M
      SET_VECTOR_ELT(ans, i, c);
2273
1.13M
      break;
2274
1.34M
  default:
2275
1.34M
      R_ReadItemDepth++;
2276
1.34M
      SET_VECTOR_ELT(ans, i, ReadItem(ref_table, stream));
2277
1.34M
      R_ReadItemDepth--;
2278
2.75M
  }
2279
2.75M
    }
2280
370k
    UNPROTECT(1);
2281
370k
    return ans;
2282
373k
}
2283
2284
static SEXP ReadBC1(SEXP ref_table, BCReps *reps, R_inpstream_t stream)
2285
374k
{
2286
374k
    SEXP s;
2287
374k
    PROTECT(s = allocSExp(BCODESXP));
2288
374k
    R_ReadItemDepth++;
2289
374k
    SETCAR(s, ReadItem(ref_table, stream)); /* code */
2290
374k
    R_ReadItemDepth--;
2291
374k
    SEXP bytes = PROTECT(CAR(s));
2292
374k
    SETCAR(s, R_bcEncode(bytes));
2293
374k
    SETCDR(s, ReadBCConsts(ref_table, reps, stream)); /* consts */
2294
374k
    SET_TAG(s, R_NilValue); /* expr */
2295
374k
    R_registerBC(bytes, s);
2296
374k
    UNPROTECT(2);
2297
374k
    return s;
2298
374k
}
2299
2300
static SEXP ReadBC(SEXP ref_table, R_inpstream_t stream)
2301
107k
{
2302
107k
    BCReps reps;
2303
107k
    SEXP ans;
2304
107k
    int n = InInteger(stream);
2305
2306
107k
    if (n < 0)
2307
39
  error(_("bytecode reference index out of range"));
2308
107k
    reps.ndefs = 0;
2309
107k
    PROTECT_WITH_INDEX(reps.slots = allocVector(VECSXP, n < BC_REPS_INIT ?
2310
107k
            n : BC_REPS_INIT),
2311
107k
           &reps.ipx);
2312
107k
    ans = ReadBC1(ref_table, &reps, stream);
2313
107k
    UNPROTECT(1);
2314
107k
    return ans;
2315
107k
}
2316
2317
static void DecodeVersion(int packed, int *v, int *p, int *s)
2318
96
{
2319
96
    *v = packed / 65536; packed = packed % 65536;
2320
96
    *p = packed / 256; packed = packed % 256;
2321
96
    *s = packed;
2322
96
}
2323
2324
/* ReadChar opens iconv descriptors lazily and caches them in the stream;
2325
   close them both when unserialization finishes and when an error unwinds
2326
   out of it */
2327
static void CloseInStreamConverters(void *data)
2328
36.8k
{
2329
36.8k
    R_inpstream_t stream = data;
2330
36.8k
    if (stream->nat2nat_obj && stream->nat2nat_obj != (void *)-1) {
2331
472
  Riconv_close(stream->nat2nat_obj);
2332
472
  stream->nat2nat_obj = NULL;
2333
472
    }
2334
36.8k
    if (stream->nat2utf8_obj && stream->nat2utf8_obj != (void *)-1) {
2335
360
  Riconv_close(stream->nat2utf8_obj);
2336
360
  stream->nat2utf8_obj = NULL;
2337
360
    }
2338
36.8k
}
2339
2340
SEXP R_Unserialize(R_inpstream_t stream)
2341
37.7k
{
2342
37.7k
    int version;
2343
37.7k
    int writer_version, min_reader_version;
2344
37.7k
    SEXP obj, ref_table;
2345
2346
37.7k
    InFormat(stream);
2347
2348
    /* Read the version numbers */
2349
37.7k
    version = InInteger(stream);
2350
37.7k
    writer_version = InInteger(stream);
2351
37.7k
    min_reader_version = InInteger(stream);
2352
37.7k
    switch (version) {
2353
4.59k
    case 2: break;
2354
32.5k
    case 3:
2355
32.5k
    {
2356
32.5k
  int nelen = InInteger(stream);
2357
32.5k
  if (nelen > R_CODESET_MAX || nelen < 0)
2358
82
      error(_("invalid length of encoding name"));
2359
32.5k
  InString(stream, stream->native_encoding, nelen);
2360
32.5k
  stream->native_encoding[nelen] = '\0';
2361
32.5k
  if (memchr(stream->native_encoding, '\0', nelen))
2362
13
      error(_("embedded nul in encoding name"));
2363
32.5k
  break;
2364
0
    }
2365
73
    default:
2366
73
  {
2367
73
      int vw, pw, sw;
2368
73
      DecodeVersion(writer_version, &vw, &pw, &sw);
2369
73
      if (min_reader_version < 0)
2370
50
    error(_("cannot read unreleased workspace version %d written by experimental R %d.%d.%d"), version, vw, pw, sw);
2371
23
      else {
2372
23
    int vm, pm, sm;
2373
23
    DecodeVersion(min_reader_version, &vm, &pm, &sm);
2374
23
    error(_("cannot read workspace version %d written by R %d.%d.%d; need R %d.%d.%d or newer"),
2375
23
          version, vw, pw, sw, vm, pm, sm);
2376
23
      }
2377
73
  }
2378
37.7k
    }
2379
2380
    /* Read the actual object back */
2381
36.8k
    PROTECT(ref_table = MakeReadRefTable());
2382
2383
36.8k
    RCNTXT cntxt;
2384
36.8k
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
2385
36.8k
     R_NilValue, R_NilValue);
2386
36.8k
    cntxt.cend = &CloseInStreamConverters;
2387
36.8k
    cntxt.cenddata = stream;
2388
2389
36.8k
    obj = ReadItem(ref_table, stream);
2390
2391
36.8k
    endcontext(&cntxt);
2392
36.8k
    CloseInStreamConverters(stream);
2393
36.8k
    UNPROTECT(1);
2394
2395
36.8k
    return obj;
2396
37.7k
}
2397
2398
attribute_hidden SEXP R_SerializeInfo(R_inpstream_t stream)
2399
0
{
2400
0
    int version;
2401
0
    int writer_version, min_reader_version, vv, vp, vs;
2402
0
    int anslen = 4;
2403
0
    SEXP ans, names;
2404
0
    char buf[128];
2405
2406
0
    InFormat(stream);
2407
2408
    /* Read the version numbers */
2409
0
    version = InInteger(stream);
2410
0
    if (version == 3)
2411
0
  anslen++;
2412
0
    writer_version = InInteger(stream);
2413
0
    min_reader_version = InInteger(stream);
2414
2415
0
    PROTECT(ans = allocVector(VECSXP, anslen));
2416
0
    PROTECT(names = allocVector(STRSXP, anslen));
2417
0
    SET_STRING_ELT(names, 0, mkChar("version"));
2418
0
    SET_VECTOR_ELT(ans, 0, ScalarInteger(version));
2419
0
    SET_STRING_ELT(names, 1, mkChar("writer_version"));
2420
0
    DecodeVersion(writer_version, &vv, &vp, &vs);
2421
0
    snprintf(buf, 128, "%d.%d.%d", vv, vp, vs);
2422
0
    SET_VECTOR_ELT(ans, 1, mkString(buf));
2423
0
    SET_STRING_ELT(names, 2, mkChar("min_reader_version"));
2424
0
    if (min_reader_version < 0)
2425
  /* unreleased version of R */
2426
0
  SET_VECTOR_ELT(ans, 2, ScalarString(NA_STRING));
2427
0
    else {
2428
0
  DecodeVersion(min_reader_version, &vv, &vp, &vs);
2429
0
  snprintf(buf, 128, "%d.%d.%d", vv, vp, vs);
2430
0
  SET_VECTOR_ELT(ans, 2, mkString(buf));
2431
0
    }
2432
0
    SET_STRING_ELT(names, 3, mkChar("format"));
2433
0
    switch(stream->type) {
2434
0
    case R_pstream_ascii_format:
2435
0
  SET_VECTOR_ELT(ans, 3, mkString("ascii"));
2436
0
  break;
2437
0
    case R_pstream_binary_format:
2438
0
  SET_VECTOR_ELT(ans, 3, mkString("binary"));
2439
0
  break;
2440
0
    case R_pstream_xdr_format:
2441
0
  SET_VECTOR_ELT(ans, 3, mkString("xdr"));
2442
0
  break;
2443
0
    default:
2444
0
  error(_("unknown input format"));
2445
0
    }
2446
0
    if (version == 3) {
2447
0
  SET_STRING_ELT(names, 4, mkChar("native_encoding"));
2448
0
  int nelen = InInteger(stream);
2449
0
  if (nelen > R_CODESET_MAX || nelen < 0)
2450
0
      error(_("invalid length of encoding name"));
2451
0
  char nbuf[nelen + 1];
2452
0
  InString(stream, nbuf, nelen);
2453
0
  nbuf[nelen] = '\0';
2454
0
  if (memchr(nbuf, '\0', nelen))
2455
0
      error(_("embedded nul in encoding name"));
2456
0
  SET_VECTOR_ELT(ans, 4, mkString(nbuf));
2457
0
    }
2458
0
    setAttrib(ans, R_NamesSymbol, names);
2459
0
    UNPROTECT(2); /* ans, names */
2460
2461
0
    return ans;
2462
0
}
2463
2464
/*
2465
 * Generic Persistent Stream Initializers
2466
 */
2467
2468
void
2469
R_InitInPStream(R_inpstream_t stream, R_pstream_data_t data,
2470
    R_pstream_format_t type,
2471
    int (*inchar)(R_inpstream_t),
2472
    void (*inbytes)(R_inpstream_t, void *, int),
2473
    SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2474
37.7k
{
2475
37.7k
    stream->data = data;
2476
37.7k
    stream->type = type;
2477
37.7k
    stream->InChar = inchar;
2478
37.7k
    stream->InBytes = inbytes;
2479
37.7k
    stream->InPersistHookFunc = phook;
2480
37.7k
    stream->InPersistHookData = pdata;
2481
37.7k
    stream->native_encoding[0] = 0;
2482
37.7k
    stream->nat2nat_obj = NULL;
2483
37.7k
    stream->nat2utf8_obj = NULL;
2484
37.7k
}
2485
2486
void
2487
R_InitOutPStream(R_outpstream_t stream, R_pstream_data_t data,
2488
     R_pstream_format_t type, int version,
2489
     void (*outchar)(R_outpstream_t, int),
2490
     void (*outbytes)(R_outpstream_t, void *, int),
2491
     SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2492
2
{
2493
2
    stream->data = data;
2494
2
    stream->type = type;
2495
2
    stream->version = version != 0 ? version : defaultSerializeVersion();
2496
2
    stream->OutChar = outchar;
2497
2
    stream->OutBytes = outbytes;
2498
2
    stream->OutPersistHookFunc = phook;
2499
2
    stream->OutPersistHookData = pdata;
2500
2
}
2501
2502
2503
/*
2504
 * Persistent File Streams
2505
 */
2506
2507
static void OutCharFile(R_outpstream_t stream, int c)
2508
0
{
2509
0
    FILE *fp = stream->data;
2510
0
    fputc(c, fp);
2511
0
}
2512
2513
2514
static int InCharFile(R_inpstream_t stream)
2515
3.64M
{
2516
3.64M
    FILE *fp = stream->data;
2517
3.64M
    return fgetc(fp);
2518
3.64M
}
2519
2520
static void OutBytesFile(R_outpstream_t stream, void *buf, int length)
2521
0
{
2522
0
    FILE *fp = stream->data;
2523
0
    size_t out = fwrite(buf, 1, length, fp);
2524
0
    if (out != length) error(_("write failed"));
2525
0
}
2526
2527
static void InBytesFile(R_inpstream_t stream, void *buf, int length)
2528
45.1k
{
2529
45.1k
    FILE *fp = stream->data;
2530
45.1k
    size_t in = fread(buf, 1, length, fp);
2531
45.1k
    if (in != length) error(_("read failed"));
2532
45.1k
}
2533
2534
void
2535
R_InitFileOutPStream(R_outpstream_t stream, FILE *fp,
2536
        R_pstream_format_t type, int version,
2537
        SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2538
0
{
2539
0
    R_InitOutPStream(stream, (R_pstream_data_t) fp, type, version,
2540
0
         OutCharFile, OutBytesFile, phook, pdata);
2541
0
}
2542
2543
void
2544
R_InitFileInPStream(R_inpstream_t stream, FILE *fp,
2545
       R_pstream_format_t type,
2546
       SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2547
1.87k
{
2548
1.87k
    R_InitInPStream(stream, (R_pstream_data_t) fp, type,
2549
1.87k
        InCharFile, InBytesFile, phook, pdata);
2550
1.87k
}
2551
2552
2553
/*
2554
 * Persistent Connection Streams
2555
 */
2556
2557
#include <Rconnections.h>
2558
2559
static void CheckInConn(Rconnection con)
2560
9.50M
{
2561
9.50M
    if (! con->isopen)
2562
0
  error(_("connection is not open"));
2563
9.50M
    if (! con->canread || con->read == NULL)
2564
0
  error(_("cannot read from this connection"));
2565
9.50M
}
2566
2567
static void CheckOutConn(Rconnection con)
2568
0
{
2569
0
    if (! con->isopen)
2570
0
  error(_("connection is not open"));
2571
0
    if (! con->canwrite || con->write == NULL)
2572
0
  error(_("cannot write to this connection"));
2573
0
}
2574
2575
static void InBytesConn(R_inpstream_t stream, void *buf, int length)
2576
1.73M
{
2577
1.73M
    Rconnection con = (Rconnection) stream->data;
2578
1.73M
    CheckInConn(con);
2579
1.73M
    if (con->text) {
2580
0
  int i;
2581
0
  char *p = buf;
2582
0
  for (i = 0; i < length; i++)
2583
0
      p[i] = (char) Rconn_fgetc(con);
2584
0
    }
2585
1.73M
    else {
2586
1.73M
  if (stream->type == R_pstream_ascii_format) {
2587
0
      char linebuf[4];
2588
0
      unsigned char *p = buf;
2589
0
      int i;
2590
0
      unsigned int res;
2591
0
      for (i = 0; i < length; i++) {
2592
0
    size_t ncread = Rconn_getline(con, linebuf, 3);
2593
0
    if (ncread != 2)
2594
0
        error(_("error reading from ascii connection"));
2595
0
    if (!sscanf(linebuf, "%02x", &res))
2596
0
        error(_("unexpected format in ascii connection '%s'"),
2597
0
      con->description);
2598
0
    *p++ = (unsigned char)res;
2599
0
      }
2600
1.73M
  } else {
2601
1.73M
      if (length != con->read(buf, 1, length, con))
2602
898
    error(_("error reading from connection '%s'"),
2603
898
          con->description);
2604
1.73M
  }
2605
1.73M
    }
2606
1.73M
}
2607
2608
static int InCharConn(R_inpstream_t stream)
2609
7.75M
{
2610
7.75M
    char buf[1];
2611
7.75M
    Rconnection con = (Rconnection) stream->data;
2612
7.75M
    CheckInConn(con);
2613
7.75M
    if (con->text)
2614
0
  return Rconn_fgetc(con);
2615
7.75M
    else {
2616
7.75M
  if (1 != con->read(buf, 1, 1, con))
2617
797
      error(_("error reading from connection '%s'"),
2618
797
        con->description);
2619
7.75M
  return buf[0];
2620
7.75M
    }
2621
7.75M
}
2622
2623
static void OutBytesConn(R_outpstream_t stream, void *buf, int length)
2624
0
{
2625
0
    Rconnection con = (Rconnection) stream->data;
2626
0
    CheckOutConn(con);
2627
0
    if (con->text) {
2628
0
  int i;
2629
0
  char *p = buf;
2630
0
  for (i = 0; i < length; i++)
2631
0
      Rconn_printf(con, "%c", p[i]);
2632
0
    }
2633
0
    else {
2634
0
  if (length != con->write(buf, 1, length, con))
2635
0
      error(_("error writing to connection"));
2636
0
    }
2637
0
}
2638
2639
static void OutCharConn(R_outpstream_t stream, int c)
2640
0
{
2641
0
    Rconnection con = (Rconnection) stream->data;
2642
0
    CheckOutConn(con);
2643
0
    if (con->text)
2644
0
  Rconn_printf(con, "%c", c);
2645
0
    else {
2646
0
  char buf[1];
2647
0
  buf[0] = (char) c;
2648
0
  if (1 != con->write(buf, 1, 1, con))
2649
0
      error(_("error writing to connection"));
2650
0
    }
2651
0
}
2652
2653
attribute_hidden
2654
void R_InitConnOutPStream(R_outpstream_t stream, Rconnection con,
2655
        R_pstream_format_t type, int version,
2656
        SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2657
0
{
2658
0
    CheckOutConn(con);
2659
0
    if (con->text &&
2660
0
  !(type == R_pstream_ascii_format || type == R_pstream_asciihex_format) )
2661
0
  error(_("only ascii format can be written to text mode connections"));
2662
0
    R_InitOutPStream(stream, (R_pstream_data_t) con, type, version,
2663
0
         OutCharConn, OutBytesConn, phook, pdata);
2664
0
}
2665
2666
attribute_hidden
2667
void R_InitConnInPStream(R_inpstream_t stream,  Rconnection con,
2668
       R_pstream_format_t type,
2669
       SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2670
6.13k
{
2671
6.13k
    CheckInConn(con);
2672
6.13k
    if (con->text) {
2673
0
  if (type == R_pstream_any_format)
2674
0
      type = R_pstream_ascii_format;
2675
0
  else if (type != R_pstream_ascii_format)
2676
0
      error(_("only ascii format can be read from text mode connections"));
2677
0
    }
2678
6.13k
    R_InitInPStream(stream, (R_pstream_data_t) con, type,
2679
6.13k
        InCharConn, InBytesConn, phook, pdata);
2680
6.13k
}
2681
2682
/* ought to quote the argument, but it should only be an ENVSXP or STRSXP */
2683
static SEXP CallHook(SEXP x, SEXP fun)
2684
25.2k
{
2685
25.2k
    SEXP val, call;
2686
25.2k
    PROTECT(call = LCONS(fun, LCONS(x, R_NilValue)));
2687
25.2k
    val = eval(call, R_GlobalEnv);
2688
25.2k
    UNPROTECT(1);
2689
25.2k
    return val;
2690
25.2k
}
2691
2692
static void con_cleanup(void *data)
2693
0
{
2694
0
    Rconnection con = data;
2695
0
    if(con->isopen) con->close(con);
2696
0
}
2697
2698
/* Used from saveRDS().
2699
   This became public in R 2.13.0, and that version added support for
2700
   connections internally */
2701
attribute_hidden SEXP
2702
do_serializeToConn(SEXP call, SEXP op, SEXP args, SEXP env)
2703
0
{
2704
    /* serializeToConn(object, conn, ascii, version, hook) */
2705
2706
0
    SEXP object, fun;
2707
0
    bool ascii, wasopen;
2708
0
    int version;
2709
0
    Rconnection con;
2710
0
    struct R_outpstream_st out;
2711
0
    R_pstream_format_t type;
2712
0
    SEXP (*hook)(SEXP, SEXP);
2713
0
    RCNTXT cntxt;
2714
2715
0
    checkArity(op, args);
2716
2717
0
    object = CAR(args);
2718
0
    con = getConnection(asInteger(CADR(args)));
2719
2720
/*    if (TYPEOF(CADDR(args)) != LGLSXP)
2721
  error(_("'ascii' must be logical"));
2722
  ascii = INTEGER(CADDR(args))[0]; */
2723
0
    ascii = asRbool(CADDR(args), call);
2724
0
    if (ascii == NA_LOGICAL) type = R_pstream_asciihex_format;
2725
0
    else if (ascii) type = R_pstream_ascii_format;
2726
0
    else type = R_pstream_xdr_format;
2727
2728
0
    if (CADDDR(args) == R_NilValue)
2729
0
  version = defaultSerializeVersion();
2730
0
    else
2731
0
  version = asInteger(CADDDR(args));
2732
0
    if (version == NA_INTEGER || version <= 0)
2733
0
  error(_("bad version value"));
2734
0
    if (version < 2)
2735
0
  error(_("cannot save to connections in version %d format"), version);
2736
2737
0
    fun = CAD4R(args);
2738
0
    hook = fun != R_NilValue ? CallHook : NULL;
2739
2740
    /* Now we need to do some sanity checking of the arguments.
2741
       A filename will already have been opened, so anything
2742
       not open was specified as a connection directly.
2743
     */
2744
0
    wasopen = con->isopen;
2745
0
    if(!wasopen) {
2746
0
  char mode[5];
2747
0
  strcpy(mode, con->mode);
2748
0
  strcpy(con->mode, ascii ? "w" : "wb");
2749
0
  if(!con->open(con)) error(_("cannot open the connection"));
2750
0
  strcpy(con->mode, mode);
2751
  /* Set up a context which will close the connection on error */
2752
0
  begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
2753
0
         R_NilValue, R_NilValue);
2754
0
  cntxt.cend = &con_cleanup;
2755
0
  cntxt.cenddata = con;
2756
0
    }
2757
0
    if (!ascii && con->text)
2758
0
  error(_("binary-mode connection required for ascii=false"));
2759
0
    if(!con->canwrite)
2760
0
  error(_("connection not open for writing"));
2761
2762
0
    R_InitConnOutPStream(&out, con, type, version, hook, fun);
2763
0
    R_Serialize(object, &out);
2764
0
    if(!wasopen) {endcontext(&cntxt); con->close(con);}
2765
2766
0
    return R_NilValue;
2767
0
}
2768
2769
static SEXP checkNotPromise(SEXP val)
2770
1.81k
{
2771
1.81k
    if (TYPEOF(val) == PROMSXP)
2772
7
  error(_("cannot return a promise (PROMSXP) object"));
2773
1.81k
    return val;
2774
1.81k
}
2775
2776
/* unserializeFromConn(conn, hook) used from readRDS().
2777
   It became public in R 2.13.0, and that version added support for
2778
   connections internally */
2779
attribute_hidden SEXP
2780
do_unserializeFromConn(SEXP call, SEXP op, SEXP args, SEXP env)
2781
4.24k
{
2782
    /* 0 .. unserializeFromConn(conn, hook) */
2783
    /* 1 .. serializeInfoFromConn(conn) */
2784
2785
4.24k
    struct R_inpstream_st in;
2786
4.24k
    Rconnection con;
2787
4.24k
    SEXP fun, ans;
2788
4.24k
    SEXP (*hook)(SEXP, SEXP);
2789
4.24k
    bool wasopen;
2790
4.24k
    RCNTXT cntxt;
2791
2792
4.24k
    checkArity(op, args);
2793
2794
4.24k
    con = getConnection(asInteger(CAR(args)));
2795
2796
    /* Now we need to do some sanity checking of the arguments.
2797
       A filename will already have been opened, so anything
2798
       not open was specified as a connection directly.
2799
     */
2800
4.24k
    wasopen = con->isopen;
2801
4.24k
    if(!wasopen) {
2802
0
  char mode[5];
2803
0
  strcpy(mode, con->mode);
2804
0
  strcpy(con->mode, "rb");
2805
0
  if(!con->open(con)) error(_("cannot open the connection"));
2806
0
  strcpy(con->mode, mode);
2807
  /* Set up a context which will close the connection on error */
2808
0
  begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
2809
0
         R_NilValue, R_NilValue);
2810
0
  cntxt.cend = &con_cleanup;
2811
0
  cntxt.cenddata = con;
2812
0
    }
2813
4.24k
    if(!con->canread) error(_("connection not open for reading"));
2814
2815
4.24k
    fun = PRIMVAL(op) == 0 ? CADR(args) : R_NilValue;
2816
4.24k
    hook = fun != R_NilValue ? CallHook : NULL;
2817
4.24k
    R_InitConnInPStream(&in, con, R_pstream_any_format, hook, fun);
2818
4.24k
    ans = PRIMVAL(op) == 0 ? R_Unserialize(&in) : R_SerializeInfo(&in);
2819
4.24k
    if(!wasopen) {
2820
0
  PROTECT(ans); /* paranoia about next line */
2821
0
  endcontext(&cntxt);
2822
0
  con->close(con);
2823
0
  UNPROTECT(1);
2824
0
    }
2825
4.24k
    return checkNotPromise(ans);
2826
4.24k
}
2827
2828
/*
2829
 * Persistent Buffered Binary Connection Streams
2830
 */
2831
2832
/**** should eventually come from a public header file */
2833
size_t R_WriteConnection(Rconnection con, void *buf, size_t n);
2834
2835
0
#define BCONBUFSIZ 4096
2836
2837
typedef struct bconbuf_st {
2838
    Rconnection con;
2839
    int count;
2840
    unsigned char buf[BCONBUFSIZ];
2841
} *bconbuf_t;
2842
2843
static void flush_bcon_buffer(bconbuf_t bb)
2844
0
{
2845
0
    if (R_WriteConnection(bb->con, bb->buf, bb->count) != bb->count)
2846
0
  error(_("error writing to connection"));
2847
0
    bb->count = 0;
2848
0
}
2849
2850
static void OutCharBB(R_outpstream_t stream, int c)
2851
0
{
2852
0
    bconbuf_t bb = stream->data;
2853
0
    if (bb->count >= BCONBUFSIZ)
2854
0
  flush_bcon_buffer(bb);
2855
0
    bb->buf[bb->count++] = (char) c;
2856
0
}
2857
2858
static void OutBytesBB(R_outpstream_t stream, void *buf, int length)
2859
0
{
2860
0
    bconbuf_t bb = stream->data;
2861
0
    if (bb->count + length > BCONBUFSIZ)
2862
0
  flush_bcon_buffer(bb);
2863
0
    if (length <= BCONBUFSIZ) {
2864
0
  if (length)
2865
0
      memcpy(bb->buf + bb->count, buf, length);
2866
0
  bb->count += length;
2867
0
    }
2868
0
    else if (R_WriteConnection(bb->con, buf, length) != length)
2869
0
  error(_("error writing to connection"));
2870
0
}
2871
2872
static void InitBConOutPStream(R_outpstream_t stream, bconbuf_t bb,
2873
             Rconnection con,
2874
             R_pstream_format_t type, int version,
2875
             SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2876
0
{
2877
0
    bb->count = 0;
2878
0
    bb->con = con;
2879
0
    R_InitOutPStream(stream, (R_pstream_data_t) bb, type, version,
2880
0
         OutCharBB, OutBytesBB, phook, pdata);
2881
0
}
2882
2883
/* only for use by serialize(), with binary write to a socket connection */
2884
static SEXP
2885
R_serializeb(SEXP object, SEXP icon, SEXP xdr, SEXP Sversion, SEXP fun)
2886
0
{
2887
0
    struct R_outpstream_st out;
2888
0
    SEXP (*hook)(SEXP, SEXP);
2889
0
    struct bconbuf_st bbs;
2890
0
    Rconnection con = getConnection(asInteger(icon));
2891
0
    int version;
2892
2893
0
    if (Sversion == R_NilValue)
2894
0
  version = defaultSerializeVersion();
2895
0
    else version = asInteger(Sversion);
2896
0
    if (version == NA_INTEGER || version <= 0)
2897
0
  error(_("bad version value"));
2898
2899
0
    hook = fun != R_NilValue ? CallHook : NULL;
2900
2901
0
    InitBConOutPStream(&out, &bbs, con,
2902
0
           asLogical(xdr) ? R_pstream_xdr_format : R_pstream_binary_format,
2903
0
           version, hook, fun);
2904
0
    R_Serialize(object, &out);
2905
0
    flush_bcon_buffer(&bbs);
2906
0
    return R_NilValue;
2907
0
}
2908
2909
2910
/*
2911
 * Persistent Memory Streams
2912
 */
2913
2914
typedef struct membuf_st {
2915
    R_size_t size;
2916
    R_size_t count;
2917
    unsigned char *buf;
2918
} *membuf_t;
2919
2920
2921
4
#define INCR MAXELTSIZE
2922
static void resize_buffer(membuf_t mb, R_size_t needed)
2923
2
{
2924
2
    if(needed > R_XLEN_T_MAX)
2925
0
  error(_("serialization is too large to store in a raw vector"));
2926
2
#ifdef LONG_VECTOR_SUPPORT
2927
2
    if(needed < 10000000) /* ca 10MB */
2928
2
  needed = (1+2*needed/INCR) * INCR;
2929
0
    else
2930
0
  needed = (R_size_t)((1+1.2*(double)needed/INCR) * INCR);
2931
#else
2932
    if(needed < 10000000) /* ca 10MB */
2933
  needed = (1+2*needed/INCR) * INCR;
2934
    else if(needed < 1700000000) /* close to 2GB/1.2 */
2935
  needed = (R_size_t)((1+1.2*(double)needed/INCR) * INCR);
2936
    else if(needed < INT_MAX - INCR)
2937
  needed = (1+needed/INCR) * INCR;
2938
#endif
2939
2
    unsigned char *tmp = realloc(mb->buf, needed);
2940
2
    if (tmp == NULL) {
2941
0
  free(mb->buf); mb->buf = NULL;
2942
0
  error(_("cannot allocate buffer"));
2943
2
    } else mb->buf = tmp;
2944
2
    mb->size = needed;
2945
2
}
2946
2947
static void OutCharMem(R_outpstream_t stream, int c)
2948
0
{
2949
0
    membuf_t mb = stream->data;
2950
0
    if (mb->count >= mb->size)
2951
0
  resize_buffer(mb, mb->count + 1);
2952
0
    mb->buf[mb->count++] = (char) c;
2953
0
}
2954
2955
static void OutBytesMem(R_outpstream_t stream, void *buf, int length)
2956
14
{
2957
14
    membuf_t mb = stream->data;
2958
14
    R_size_t needed = mb->count + (R_size_t) length;
2959
#ifndef LONG_VECTOR_SUPPORT
2960
    /* There is a potential overflow here on 32-bit systems */
2961
    if((double) mb->count + length > (double) INT_MAX)
2962
  error(_("serialization is too large to store in a raw vector"));
2963
#endif
2964
14
    if (needed > mb->size) resize_buffer(mb, needed);
2965
14
    if (length)
2966
14
  memcpy(mb->buf + mb->count, buf, length);
2967
14
    mb->count = needed;
2968
14
}
2969
2970
static int InCharMem(R_inpstream_t stream)
2971
2.76M
{
2972
2.76M
    membuf_t mb = stream->data;
2973
2.76M
    if (mb->count >= mb->size)
2974
236
  error(_("read error"));
2975
2.76M
    return mb->buf[mb->count++];
2976
2.76M
}
2977
2978
static void InBytesMem(R_inpstream_t stream, void *buf, int length)
2979
39.0M
{
2980
39.0M
    membuf_t mb = stream->data;
2981
39.0M
    if (mb->count + (R_size_t) length > mb->size)
2982
374
  error(_("read error"));
2983
39.0M
    if (length)
2984
39.0M
  memcpy(buf, mb->buf + mb->count, length);
2985
39.0M
    mb->count += length;
2986
39.0M
}
2987
2988
static void InitMemInPStream(R_inpstream_t stream, membuf_t mb,
2989
           void *buf, R_size_t length,
2990
           SEXP (*phook)(SEXP, SEXP), SEXP pdata)
2991
29.7k
{
2992
29.7k
    mb->count = 0;
2993
29.7k
    mb->size = length;
2994
29.7k
    mb->buf = buf;
2995
29.7k
    R_InitInPStream(stream, (R_pstream_data_t) mb, R_pstream_any_format,
2996
29.7k
        InCharMem, InBytesMem, phook, pdata);
2997
29.7k
}
2998
2999
static void InitMemOutPStream(R_outpstream_t stream, membuf_t mb,
3000
            R_pstream_format_t type, int version,
3001
            SEXP (*phook)(SEXP, SEXP), SEXP pdata)
3002
2
{
3003
2
    mb->count = 0;
3004
2
    mb->size = 0;
3005
2
    mb->buf = NULL;
3006
2
    R_InitOutPStream(stream, (R_pstream_data_t) mb, type, version,
3007
2
         OutCharMem, OutBytesMem, phook, pdata);
3008
2
}
3009
3010
static void free_mem_buffer(void *data)
3011
2
{
3012
2
    membuf_t mb = data;
3013
2
    if (mb->buf != NULL) {
3014
2
  unsigned char *buf = mb->buf;
3015
2
  mb->buf = NULL;
3016
2
  free(buf);
3017
2
    }
3018
2
}
3019
3020
static SEXP CloseMemOutPStream(R_outpstream_t stream)
3021
2
{
3022
2
    SEXP val;
3023
2
    membuf_t mb = stream->data;
3024
    /* duplicate check, for future proofing */
3025
#ifndef LONG_VECTOR_SUPPORT
3026
    if(mb->count > INT_MAX)
3027
  error(_("serialization is too large to store in a raw vector"));
3028
#endif
3029
2
    PROTECT(val = allocVector(RAWSXP, mb->count));
3030
2
    if (mb->count)
3031
2
  memcpy(RAW(val), mb->buf, mb->count);
3032
2
    free_mem_buffer(mb);
3033
2
    UNPROTECT(1);
3034
2
    return val;
3035
2
}
3036
3037
static SEXP
3038
R_serialize(SEXP object, SEXP icon, SEXP ascii, SEXP Sversion, SEXP fun)
3039
2
{
3040
2
    struct R_outpstream_st out;
3041
2
    R_pstream_format_t type;
3042
2
    SEXP (*hook)(SEXP, SEXP);
3043
2
    int version;
3044
3045
2
    if (Sversion == R_NilValue)
3046
2
  version = defaultSerializeVersion();
3047
0
    else version = asInteger(Sversion);
3048
2
    if (version == NA_INTEGER || version <= 0)
3049
0
  error(_("bad version value"));
3050
3051
2
    hook = fun != R_NilValue ? CallHook : NULL;
3052
3053
    // Prior to 3.2.0 this was logical, values 0/1/NA for binary.
3054
2
    int asc = asInteger(ascii);
3055
2
    switch(asc) {
3056
0
    case 1: type = R_pstream_ascii_format; break;
3057
0
    case 2: type = R_pstream_asciihex_format; break;
3058
0
    case 3: type = R_pstream_binary_format; break;
3059
2
    default: type = R_pstream_xdr_format; break;
3060
2
    }
3061
3062
2
    if (icon == R_NilValue) {
3063
2
  RCNTXT cntxt;
3064
2
  struct membuf_st mbs;
3065
2
  SEXP val;
3066
3067
  /* set up a context which will free the buffer if there is an error */
3068
2
  begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
3069
2
         R_NilValue, R_NilValue);
3070
2
  cntxt.cend = &free_mem_buffer;
3071
2
  cntxt.cenddata = &mbs;
3072
3073
2
  InitMemOutPStream(&out, &mbs, type, version, hook, fun);
3074
2
  R_Serialize(object, &out);
3075
3076
2
  PROTECT(val = CloseMemOutPStream(&out));
3077
3078
  /* end the context after anything that could raise an error but before
3079
     calling OutTerm so it doesn't get called twice */
3080
2
  endcontext(&cntxt);
3081
3082
2
  UNPROTECT(1); /* val */
3083
2
  return val;
3084
2
    }
3085
0
    else {
3086
0
  Rconnection con = getConnection(asInteger(icon));
3087
0
  R_InitConnOutPStream(&out, con, type, version, hook, fun);
3088
0
  R_Serialize(object, &out);
3089
0
  return R_NilValue;
3090
0
    }
3091
2
}
3092
3093
3094
static SEXP R_unserialize(SEXP icon, SEXP fun)
3095
30.3k
{
3096
30.3k
    struct R_inpstream_st in;
3097
30.3k
    SEXP (*hook)(SEXP, SEXP);
3098
3099
30.3k
    hook = fun != R_NilValue ? CallHook : NULL;
3100
3101
30.3k
    if (TYPEOF(icon) == STRSXP && LENGTH(icon) > 0) {
3102
  /* was the format in R < 2.4.0, removed in R 2.8.0 */
3103
0
  error("character vectors are no longer accepted by unserialize()");
3104
0
  return R_NilValue; /* -Wall */
3105
30.3k
    } else if (TYPEOF(icon) == RAWSXP) {
3106
  /* We might want to read from a long raw vector */
3107
29.7k
  struct membuf_st mbs;
3108
29.7k
  void *data = RAW(icon);
3109
29.7k
  R_size_t length = XLENGTH(icon);
3110
29.7k
  InitMemInPStream(&in, &mbs, data,  length, hook, fun);
3111
29.7k
  return R_Unserialize(&in);
3112
29.7k
    } else {
3113
666
  Rconnection con = getConnection(asInteger(icon));
3114
666
  R_InitConnInPStream(&in, con, R_pstream_any_format, hook, fun);
3115
666
  return R_Unserialize(&in);
3116
666
    }
3117
30.3k
}
3118
3119
3120
/*
3121
 * Support Code for Lazy Loading of Packages
3122
 */
3123
3124
3125
27.6k
#define IS_PROPER_STRING(s) (TYPEOF(s) == STRSXP && LENGTH(s) > 0)
3126
3127
/* Appends a raw vector to the end of a file using binary mode.
3128
   Returns an integer vector of the initial offset of the string in
3129
   the file and the length of the vector. */
3130
3131
static SEXP appendRawToFile(SEXP file, SEXP bytes)
3132
0
{
3133
0
    FILE *fp;
3134
0
    size_t len, out;
3135
0
    long pos;  // what ftell gives: won't work for > 2GB files
3136
0
    SEXP val;
3137
0
    const void *vmax;
3138
0
    const char *cfile;
3139
3140
0
    if (! IS_PROPER_STRING(file))
3141
0
  error(_("not a proper file name"));
3142
0
    vmax = vmaxget();
3143
0
    cfile = translateCharFP(STRING_ELT(file, 0));
3144
0
    if (TYPEOF(bytes) != RAWSXP)
3145
0
  error(_("not a proper raw vector"));
3146
0
#ifdef HAVE_WORKING_FTELL
3147
    /* Windows' ftell returns position 0 with "ab" */
3148
0
    if ((fp = R_fopen(cfile, "ab")) == NULL) {
3149
0
  error( _("cannot open file '%s': %s"), cfile,
3150
0
         strerror(errno));
3151
0
    }
3152
#else
3153
    if ((fp = R_fopen(cfile, "r+b")) == NULL) {
3154
  error( _("cannot open file '%s': %s"), cfile,
3155
         strerror(errno));
3156
    }
3157
    if (fseek(fp, 0, SEEK_END) != 0) {
3158
  fclose(fp);
3159
  error(_("seek failed on %s"), cfile);
3160
    }
3161
#endif
3162
3163
0
    len = LENGTH(bytes);
3164
0
    pos = ftell(fp);
3165
0
    out = fwrite(RAW(bytes), 1, len, fp);
3166
0
    fclose(fp);
3167
3168
0
    if (out != len) error(_("write failed"));
3169
0
    if (pos == -1) error(_("could not determine file position"));
3170
3171
0
    val = allocVector(INTSXP, 2);
3172
0
    INTEGER(val)[0] = (int) pos;
3173
0
    INTEGER(val)[1] = (int) len;
3174
0
    vmaxset(vmax);
3175
3176
0
    return val;
3177
0
}
3178
3179
/* Interface to cache the pkg.rdb files */
3180
3181
#ifdef HAVE_SYS_TYPES_H
3182
# include <sys/types.h>
3183
#endif
3184
#ifdef HAVE_SYS_STAT_H
3185
# include <sys/stat.h>
3186
#endif
3187
3188
struct fileid {
3189
    double size, mtime, ino, dev;
3190
};
3191
3192
263
#define NC 256
3193
static int used = 0;
3194
static char *names[NC];
3195
static char *ptr[NC];
3196
static size_t lens[NC];   /* length of the cached copy */
3197
static struct fileid ids[NC]; /* identity of the file when cached */
3198
3199
/* Capture the identity of a database file, used to detect that a
3200
   cached copy has become stale because the file was replaced, e.g. by
3201
   re-installing the package (possibly from another process).  Returns
3202
   false if the file cannot be stat-ed, in which case its contents
3203
   should not be cached. */
3204
static bool getFileID(const char *cfile, struct fileid *id)
3205
27.6k
{
3206
#ifdef Win32
3207
    struct _stati64 sb;
3208
    if (_stati64(cfile, &sb) != 0)
3209
  return false;
3210
#else
3211
27.6k
    struct stat sb;
3212
27.6k
    if (stat(cfile, &sb) != 0)
3213
0
  return false;
3214
27.6k
#endif
3215
27.6k
    id->size = (double) sb.st_size;
3216
    /* use sub-second modification times where available, as in
3217
       do_fileinfo (platform.c) */
3218
27.6k
#if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC \
3219
27.6k
    && defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
3220
27.6k
    id->mtime = (double) sb.st_mtim.tv_sec
3221
27.6k
  + 1e-9 * (double) sb.st_mtim.tv_nsec;
3222
#elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
3223
    id->mtime = (double) sb.st_mtimespec.tv_sec
3224
  + 1e-9 * (double) sb.st_mtimespec.tv_nsec;
3225
#else
3226
    id->mtime = (double) sb.st_mtime;
3227
#endif
3228
27.6k
    id->ino = (double) sb.st_ino;
3229
27.6k
    id->dev = (double) sb.st_dev;
3230
27.6k
    return true;
3231
27.6k
}
3232
3233
attribute_hidden SEXP
3234
do_lazyLoadDBflush(SEXP call, SEXP op, SEXP args, SEXP env)
3235
0
{
3236
0
    checkArity(op, args);
3237
3238
0
    int i;
3239
0
    const char *cfile = translateCharFP(STRING_ELT(CAR(args), 0));
3240
3241
    /* fprintf(stderr, "flushing file %s", cfile); */
3242
0
    for (i = 0; i < used; i++)
3243
0
  if(names[i] != NULL && strcmp(cfile, names[i]) == 0) {
3244
0
      free(names[i]);
3245
0
      names[i] = NULL;
3246
0
      free(ptr[i]);
3247
      /* fprintf(stderr, " found at pos %d in cache", i); */
3248
0
      break;
3249
0
  }
3250
    /* fprintf(stderr, "\n"); */
3251
0
    return R_NilValue;
3252
0
}
3253
3254
3255
/* Reads, in binary mode, the bytes in the range specified by a
3256
   position/length vector and returns them as raw vector. */
3257
3258
/* There are some large lazy-data examples, e.g. 80Mb for SNPMaP.cdm */
3259
263
#define LEN_LIMIT 10*1048576
3260
static SEXP readRawFromFile(SEXP file, SEXP key)
3261
27.6k
{
3262
27.6k
    FILE *fp;
3263
27.6k
    int offset, len, in, i, icache = -1;
3264
27.6k
    long filelen;
3265
27.6k
    SEXP val;
3266
27.6k
    const void *vmax;
3267
27.6k
    const char *cfile;
3268
3269
27.6k
    if (! IS_PROPER_STRING(file))
3270
0
  error(_("not a proper file name"));
3271
27.6k
    vmax = vmaxget();
3272
27.6k
    cfile = translateCharFP(STRING_ELT(file, 0));
3273
27.6k
    if (TYPEOF(key) != INTSXP || LENGTH(key) != 2)
3274
0
  error(_("bad offset/length argument"));
3275
3276
27.6k
    offset = INTEGER(key)[0];
3277
27.6k
    len = INTEGER(key)[1];
3278
3279
27.6k
    val = allocVector(RAWSXP, len);
3280
    /* Do we have this database cached? */
3281
72.3k
    for (i = 0; i < used; i++)
3282
72.1k
  if(names[i] != NULL && strcmp(cfile, names[i]) == 0) {icache = i; break;}
3283
27.6k
    if (icache >= 0) {
3284
  /* The file may have been replaced since it was cached, e.g. by
3285
     re-installing the package, possibly from another process.  If
3286
     so, drop the cached copy: the offsets in 'key' come from the
3287
     current index file and need not be valid in a stale copy of
3288
     the database. */
3289
27.3k
  struct fileid id;
3290
27.3k
  if (! getFileID(cfile, &id) ||
3291
27.3k
      id.size != ids[icache].size || id.mtime != ids[icache].mtime ||
3292
27.3k
      id.ino != ids[icache].ino || id.dev != ids[icache].dev) {
3293
0
      free(names[icache]);
3294
0
      names[icache] = NULL;
3295
0
      free(ptr[icache]);
3296
0
      icache = -1;
3297
0
  }
3298
27.3k
    }
3299
27.6k
    if (icache >= 0) {
3300
27.3k
  if (offset < 0 || (size_t) offset + (size_t) len > lens[icache])
3301
0
      error(_("bad offset/length argument"));
3302
27.3k
  if (len)
3303
27.3k
      memcpy(RAW(val), ptr[icache]+offset, len);
3304
27.3k
  vmaxset(vmax);
3305
27.3k
  return val;
3306
27.3k
    }
3307
3308
    /* find a vacant slot? */
3309
1.04k
    for (i = 0; i < used; i++)
3310
779
  if(names[i] == NULL) {icache = i; break;}
3311
263
    if(icache < 0 && used < NC) {
3312
263
  icache = used++;
3313
263
  names[icache] = NULL;
3314
263
    }
3315
3316
263
    if(icache >= 0) {
3317
  /* Capture the file's identity before reading it: if the file
3318
     is replaced while it is being read, the next fetch will see
3319
     a mismatch and drop the cached copy. */
3320
263
  struct fileid id;
3321
263
  bool cacheable = getFileID(cfile, &id);
3322
263
  if ((fp = R_fopen(cfile, "rb")) == NULL)
3323
0
      error(_("cannot open file '%s': %s"), cfile, strerror(errno));
3324
263
  if (fseek(fp, 0, SEEK_END) != 0) {
3325
0
      fclose(fp);
3326
0
      error(_("seek failed on %s"), cfile);
3327
0
  }
3328
263
  filelen = ftell(fp);
3329
263
  if (cacheable && filelen < LEN_LIMIT) {
3330
263
      char *p, *n;
3331
      /* fprintf(stderr, "adding file '%s' at pos %d in cache, length %d\n",
3332
         cfile, icache, filelen); */
3333
263
      p = (char *) malloc(filelen);
3334
263
      n = (char *) malloc(strlen(cfile) + 1);
3335
263
      if (p && n) {
3336
263
    names[icache] = n;
3337
263
    strcpy(names[icache], cfile);
3338
263
    ptr[icache] = p;
3339
263
    lens[icache] = (size_t) filelen;
3340
263
    ids[icache] = id;
3341
263
    if (fseek(fp, 0, SEEK_SET) != 0) {
3342
0
        fclose(fp);
3343
0
        error(_("seek failed on %s"), cfile);
3344
0
    }
3345
263
    in = (int) fread(p, 1, filelen, fp);
3346
263
    fclose(fp);
3347
263
    if (filelen != in) error(_("read failed on %s"), cfile);
3348
263
    if (offset < 0 || (size_t) offset + (size_t) len > lens[icache])
3349
0
        error(_("bad offset/length argument"));
3350
263
    if (len)
3351
263
        memcpy(RAW(val), p+offset, len);
3352
263
      } else {
3353
0
    if (p)
3354
0
        free(p);
3355
0
    if (n)
3356
0
        free(n);
3357
0
    if (fseek(fp, offset, SEEK_SET) != 0) {
3358
0
        fclose(fp);
3359
0
        error(_("seek failed on %s"), cfile);
3360
0
    }
3361
0
    in = (int) fread(RAW(val), 1, len, fp);
3362
0
    fclose(fp);
3363
0
    if (len != in) error(_("read failed on %s"), cfile);
3364
0
      }
3365
263
      vmaxset(vmax);
3366
263
      return val;
3367
263
  } else {
3368
0
      if (fseek(fp, offset, SEEK_SET) != 0) {
3369
0
    fclose(fp);
3370
0
    error(_("seek failed on %s"), cfile);
3371
0
      }
3372
0
      in = (int) fread(RAW(val), 1, len, fp);
3373
0
      fclose(fp);
3374
0
      if (len != in) error(_("read failed on %s"), cfile);
3375
0
      vmaxset(vmax);
3376
0
      return val;
3377
0
  }
3378
263
    }
3379
3380
0
    if ((fp = R_fopen(cfile, "rb")) == NULL)
3381
0
  error(_("cannot open file '%s': %s"), cfile, strerror(errno));
3382
0
    if (fseek(fp, offset, SEEK_SET) != 0) {
3383
0
  fclose(fp);
3384
0
  error(_("seek failed on %s"), cfile);
3385
0
    }
3386
0
    in = (int) fread(RAW(val), 1, len, fp);
3387
0
    fclose(fp);
3388
0
    if (len != in) error(_("read failed on %s"), cfile);
3389
0
    vmaxset(vmax);
3390
0
    return val;
3391
263
}
3392
3393
/* Gets the binding values of variables from a frame and returns them
3394
   as a list.  If the force argument is true, promises are forced;
3395
   otherwise they are not. */
3396
3397
static SEXP R_getVarsFromFrame(SEXP vars, SEXP env, SEXP forcesxp)
3398
0
{
3399
0
    SEXP val, tmp, sym;
3400
0
    bool force;
3401
0
    int i, len;
3402
3403
0
    if (TYPEOF(env) == NILSXP) {
3404
0
  error(_("use of NULL environment is defunct"));
3405
0
  env = R_BaseEnv;
3406
0
    } else
3407
0
    if (TYPEOF(env) != ENVSXP)
3408
0
  error(_("bad environment"));
3409
0
    if (TYPEOF(vars) != STRSXP)
3410
0
  error(_("bad variable names"));
3411
0
    force = asRbool(forcesxp, R_NilValue);
3412
3413
0
    len = LENGTH(vars);
3414
0
    PROTECT(val = allocVector(VECSXP, len));
3415
0
    for (i = 0; i < len; i++) {
3416
0
  sym = installTrChar(STRING_ELT(vars, i));
3417
3418
0
  tmp = R_findVarInFrame(env, sym);
3419
0
  if (tmp == R_UnboundValue) {
3420
/*    PrintValue(env);
3421
    PrintValue(R_GetTraceback(0)); */  /* DJM debugging */
3422
0
      R_ObjectNotFoundError(sym, R_CurrentExpression, NULL);
3423
0
      }
3424
0
  if (force && TYPEOF(tmp) == PROMSXP) {
3425
0
      PROTECT(tmp);
3426
0
      tmp = eval(tmp, R_GlobalEnv);
3427
0
      ENSURE_NAMEDMAX(tmp);
3428
0
      UNPROTECT(1);
3429
0
  }
3430
0
  else ENSURE_NAMED(tmp); /* should not really be needed - LT */
3431
0
  SET_VECTOR_ELT(val, i, tmp);
3432
0
    }
3433
0
    setAttrib(val, R_NamesSymbol, vars);
3434
0
    UNPROTECT(1);
3435
3436
0
    return val;
3437
0
}
3438
3439
/* from connections.c */
3440
SEXP R_compress1(SEXP in);
3441
SEXP R_decompress1(SEXP in, Rboolean *err);
3442
SEXP R_compress2(SEXP in);
3443
SEXP R_decompress2(SEXP in, Rboolean *err);
3444
SEXP R_compress3(SEXP in);
3445
SEXP R_decompress3(SEXP in, Rboolean *err);
3446
3447
/* Serializes and, optionally, compresses a value and appends the
3448
   result to a file.  Returns the key position/length key for
3449
   retrieving the value */
3450
3451
static SEXP
3452
R_lazyLoadDBinsertValue(SEXP value, SEXP file, SEXP ascii,
3453
      SEXP compsxp, SEXP hook)
3454
0
{
3455
0
    PROTECT_INDEX vpi;
3456
0
    int compress = asInteger(compsxp);
3457
0
    SEXP key;
3458
3459
0
    value = R_serialize(value, R_NilValue, ascii, R_NilValue, hook);
3460
0
    PROTECT_WITH_INDEX(value, &vpi);
3461
0
    if (compress == 3)
3462
0
  REPROTECT(value = R_compress3(value), vpi);
3463
0
    else if (compress == 2)
3464
0
  REPROTECT(value = R_compress2(value), vpi);
3465
0
    else if (compress)
3466
0
  REPROTECT(value = R_compress1(value), vpi);
3467
0
    key = appendRawToFile(file, value);
3468
0
    UNPROTECT(1);
3469
0
    return key;
3470
0
}
3471
3472
/* Retrieves a sequence of bytes as specified by a position/length key
3473
   from a file, optionally decompresses, and unserializes the bytes.
3474
   If the result is a promise, then the promise is forced. */
3475
3476
attribute_hidden SEXP
3477
do_lazyLoadDBfetch(SEXP call, SEXP op, SEXP args, SEXP env)
3478
27.6k
{
3479
27.6k
    SEXP key, file, compsxp, hook;
3480
27.6k
    PROTECT_INDEX vpi;
3481
27.6k
    int compressed;
3482
27.6k
    Rboolean err = FALSE;
3483
27.6k
    SEXP val;
3484
3485
27.6k
    checkArity(op, args);
3486
27.6k
    key = CAR(args); args = CDR(args);
3487
27.6k
    file = CAR(args); args = CDR(args);
3488
27.6k
    compsxp = CAR(args); args = CDR(args);
3489
27.6k
    hook = CAR(args);
3490
27.6k
    compressed = asInteger(compsxp);
3491
3492
27.6k
    PROTECT_WITH_INDEX(val = readRawFromFile(file, key), &vpi);
3493
27.6k
    if (compressed == 3)
3494
0
  REPROTECT(val = R_decompress3(val, &err), vpi);
3495
27.6k
    else if (compressed == 2)
3496
0
  REPROTECT(val = R_decompress2(val, &err), vpi);
3497
27.6k
    else if (compressed)
3498
27.6k
  REPROTECT(val = R_decompress1(val, &err), vpi);
3499
27.6k
    if (err) error("lazy-load database '%s' is corrupt",
3500
0
       translateChar(STRING_ELT(file, 0)));
3501
27.6k
    val = R_unserialize(val, hook);
3502
27.6k
    if (TYPEOF(val) == PROMSXP) {
3503
0
  REPROTECT(val, vpi);
3504
0
  val = eval(val, R_GlobalEnv);
3505
0
  ENSURE_NAMEDMAX(val);
3506
0
    }
3507
27.6k
    UNPROTECT(1);
3508
27.6k
    return val;
3509
27.6k
}
3510
3511
attribute_hidden SEXP
3512
do_getVarsFromFrame(SEXP call, SEXP op, SEXP args, SEXP env)
3513
0
{
3514
0
    checkArity(op, args);
3515
0
    return R_getVarsFromFrame(CAR(args), CADR(args), CADDR(args));
3516
0
}
3517
3518
3519
attribute_hidden SEXP
3520
do_lazyLoadDBinsertValue(SEXP call, SEXP op, SEXP args, SEXP env)
3521
0
{
3522
0
    checkArity(op, args);
3523
0
    SEXP value, file, ascii, compsxp, hook;
3524
0
    value = CAR(args); args = CDR(args);
3525
0
    file = CAR(args); args = CDR(args);
3526
0
    ascii = CAR(args); args = CDR(args);
3527
0
    compsxp = CAR(args); args = CDR(args);
3528
0
    hook = CAR(args); args = CDR(args);
3529
0
    return R_lazyLoadDBinsertValue(value, file, ascii, compsxp, hook);
3530
0
}
3531
3532
attribute_hidden SEXP
3533
do_serialize(SEXP call, SEXP op, SEXP args, SEXP env)
3534
2.71k
{
3535
2.71k
    checkArity(op, args);
3536
2.71k
    if (PRIMVAL(op) == 2) //return R_unserialize(CAR(args), CADR(args));
3537
2.71k
  return checkNotPromise(R_unserialize(CAR(args), CADR(args)));
3538
2
    SEXP object, icon, type, ver, fun;
3539
2
    object = CAR(args); args = CDR(args);
3540
2
    icon = CAR(args); args = CDR(args);
3541
2
    type = CAR(args); args = CDR(args);
3542
2
    ver = CAR(args); args = CDR(args);
3543
2
    fun = CAR(args);
3544
3545
2
    if(PRIMVAL(op) == 1)
3546
0
  return R_serializeb(object, icon, type, ver, fun);
3547
2
    else
3548
2
  return R_serialize(object, icon, type, ver, fun);
3549
2
}