summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libgmp/mpz_inp_raw.c
blob: 576f4b6cae54964f24f22eba92fc3d31646ec886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* mpz_inp_raw -- Input a MP_INT in raw, but endianess, and wordsize
   independent format (as output by mpz_out_raw).

Copyright (C) 1991, 1992 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU MP Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the GNU MP Library; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <stdio.h>

#include "gmp.h"
#include "gmp-impl.h"

void
#ifdef __STDC__
mpz_inp_raw (MP_INT *x, FILE *file)
#else
mpz_inp_raw (x, file)
     MP_INT *x;
     FILE *file;
#endif
{
  int i;
  mp_size s;
  mp_size xsize;
  mp_ptr xp;
  unsigned int c;
  mp_limb x_digit;
  mp_size x_index;

  xsize = 0;
  for (i = 4 - 1; i >= 0; i--)
    {
      c = fgetc (file);
      xsize = (xsize << BITS_PER_CHAR) | c;
    }

  /* ??? Sign extend xsize for non-32 bit machines?  */

  x_index = (ABS (xsize) + BYTES_PER_MP_LIMB - 1) / BYTES_PER_MP_LIMB - 1;

  if (x->alloc < x_index)
    _mpz_realloc (x, x_index);

  xp = x->d;
  x->size = xsize / BYTES_PER_MP_LIMB;
  x_digit = 0;
  for (s = ABS (xsize) - 1; s >= 0; s--)
    {
      i = s % BYTES_PER_MP_LIMB;
      c = fgetc (file);
      x_digit = (x_digit << BITS_PER_CHAR) | c;
      if (i == 0)
	{
	  xp[x_index--] = x_digit;
	  x_digit = 0;
	}
    }
}
OpenPOWER on IntegriCloud