1 | /* mpz_out_raw -- Output a mpz_t in binary. Use an endianess and word size |
---|
2 | independent format. |
---|
3 | |
---|
4 | Copyright (C) 1995 Free Software Foundation, Inc. |
---|
5 | |
---|
6 | This file is part of the GNU MP Library. |
---|
7 | |
---|
8 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU Lesser General Public License as published by |
---|
10 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
11 | option) any later version. |
---|
12 | |
---|
13 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
15 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
16 | License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU Lesser General Public License |
---|
19 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
21 | MA 02111-1307, USA. */ |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | |
---|
25 | #include "gmp.h" |
---|
26 | #include "gmp-impl.h" |
---|
27 | |
---|
28 | size_t |
---|
29 | #if __STDC__ |
---|
30 | mpz_out_raw (FILE *stream, mpz_srcptr x) |
---|
31 | #else |
---|
32 | mpz_out_raw (stream, x) |
---|
33 | FILE *stream; |
---|
34 | mpz_srcptr x; |
---|
35 | #endif |
---|
36 | { |
---|
37 | int i; |
---|
38 | mp_size_t s; |
---|
39 | mp_size_t xsize = ABS (x->_mp_size); |
---|
40 | mp_srcptr xp = x->_mp_d; |
---|
41 | mp_size_t out_bytesize; |
---|
42 | mp_limb_t hi_limb; |
---|
43 | int n_bytes_in_hi_limb; |
---|
44 | |
---|
45 | if (stream == 0) |
---|
46 | stream = stdout; |
---|
47 | |
---|
48 | if (xsize == 0) |
---|
49 | { |
---|
50 | for (i = 4 - 1; i >= 0; i--) |
---|
51 | fputc (0, stream); |
---|
52 | return ferror (stream) ? 0 : 4; |
---|
53 | } |
---|
54 | |
---|
55 | hi_limb = xp[xsize - 1]; |
---|
56 | for (i = BYTES_PER_MP_LIMB - 1; i > 0; i--) |
---|
57 | { |
---|
58 | if ((hi_limb >> i * BITS_PER_CHAR) != 0) |
---|
59 | break; |
---|
60 | } |
---|
61 | n_bytes_in_hi_limb = i + 1; |
---|
62 | out_bytesize = BYTES_PER_MP_LIMB * (xsize - 1) + n_bytes_in_hi_limb; |
---|
63 | if (x->_mp_size < 0) |
---|
64 | out_bytesize = -out_bytesize; |
---|
65 | |
---|
66 | /* Make the size 4 bytes on all machines, to make the format portable. */ |
---|
67 | for (i = 4 - 1; i >= 0; i--) |
---|
68 | fputc ((out_bytesize >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR), |
---|
69 | stream); |
---|
70 | |
---|
71 | /* Output from the most significant limb to the least significant limb, |
---|
72 | with each limb also output in decreasing significance order. */ |
---|
73 | |
---|
74 | /* Output the most significant limb separately, since we will only |
---|
75 | output some of its bytes. */ |
---|
76 | for (i = n_bytes_in_hi_limb - 1; i >= 0; i--) |
---|
77 | fputc ((hi_limb >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR), stream); |
---|
78 | |
---|
79 | /* Output the remaining limbs. */ |
---|
80 | for (s = xsize - 2; s >= 0; s--) |
---|
81 | { |
---|
82 | mp_limb_t x_limb; |
---|
83 | |
---|
84 | x_limb = xp[s]; |
---|
85 | for (i = BYTES_PER_MP_LIMB - 1; i >= 0; i--) |
---|
86 | fputc ((x_limb >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR), stream); |
---|
87 | } |
---|
88 | return ferror (stream) ? 0 : ABS (out_bytesize) + 4; |
---|
89 | } |
---|