/src/gmp-6.2.1/mpz/export.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpz_export -- create word data from mpz.  | 
2  |  |  | 
3  |  | Copyright 2002, 2003, 2012 Free Software Foundation, Inc.  | 
4  |  |  | 
5  |  | This file is part of the GNU MP Library.  | 
6  |  |  | 
7  |  | The GNU MP Library is free software; you can redistribute it and/or modify  | 
8  |  | it under the terms of either:  | 
9  |  |  | 
10  |  |   * the GNU Lesser General Public License as published by the Free  | 
11  |  |     Software Foundation; either version 3 of the License, or (at your  | 
12  |  |     option) any later version.  | 
13  |  |  | 
14  |  | or  | 
15  |  |  | 
16  |  |   * the GNU General Public License as published by the Free Software  | 
17  |  |     Foundation; either version 2 of the License, or (at your option) any  | 
18  |  |     later version.  | 
19  |  |  | 
20  |  | or both in parallel, as here.  | 
21  |  |  | 
22  |  | The GNU MP Library is distributed in the hope that it will be useful, but  | 
23  |  | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  | 
24  |  | or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  | 
25  |  | for more details.  | 
26  |  |  | 
27  |  | You should have received copies of the GNU General Public License and the  | 
28  |  | GNU Lesser General Public License along with the GNU MP Library.  If not,  | 
29  |  | see https://www.gnu.org/licenses/.  */  | 
30  |  |  | 
31  |  | #include <stdio.h>  /* for NULL */  | 
32  |  | #include "gmp-impl.h"  | 
33  |  | #include "longlong.h"  | 
34  |  |  | 
35  |  |  | 
36  |  | #if HAVE_LIMB_BIG_ENDIAN  | 
37  |  | #define HOST_ENDIAN     1  | 
38  |  | #endif  | 
39  |  | #if HAVE_LIMB_LITTLE_ENDIAN  | 
40  | 2  | #define HOST_ENDIAN     (-1)  | 
41  |  | #endif  | 
42  |  | #ifndef HOST_ENDIAN  | 
43  |  | static const mp_limb_t  endian_test = (CNST_LIMB(1) << (GMP_LIMB_BITS-7)) - 1;  | 
44  |  | #define HOST_ENDIAN     (* (signed char *) &endian_test)  | 
45  |  | #endif  | 
46  |  |  | 
47  |  | void *  | 
48  |  | mpz_export (void *data, size_t *countp, int order,  | 
49  |  |       size_t size, int endian, size_t nail, mpz_srcptr z)  | 
50  | 2  | { | 
51  | 2  |   mp_size_t      zsize;  | 
52  | 2  |   mp_srcptr      zp;  | 
53  | 2  |   size_t         count, dummy;  | 
54  | 2  |   unsigned long  numb;  | 
55  | 2  |   unsigned       align;  | 
56  |  |  | 
57  | 2  |   ASSERT (order == 1 || order == -1);  | 
58  | 2  |   ASSERT (endian == 1 || endian == 0 || endian == -1);  | 
59  | 2  |   ASSERT (nail <= 8*size);  | 
60  | 2  |   ASSERT (nail <  8*size || SIZ(z) == 0); /* nail < 8*size+(SIZ(z)==0) */  | 
61  |  |  | 
62  | 2  |   if (countp == NULL)  | 
63  | 0  |     countp = &dummy;  | 
64  |  |  | 
65  | 2  |   zsize = SIZ(z);  | 
66  | 2  |   if (zsize == 0)  | 
67  | 0  |     { | 
68  | 0  |       *countp = 0;  | 
69  | 0  |       return data;  | 
70  | 0  |     }  | 
71  |  |  | 
72  | 2  |   zsize = ABS (zsize);  | 
73  | 2  |   zp = PTR(z);  | 
74  | 2  |   numb = 8*size - nail;  | 
75  | 2  |   MPN_SIZEINBASE_2EXP (count, zp, zsize, numb);  | 
76  | 2  |   *countp = count;  | 
77  |  |  | 
78  | 2  |   if (data == NULL)  | 
79  | 0  |     data = (*__gmp_allocate_func) (count*size);  | 
80  |  |  | 
81  | 2  |   if (endian == 0)  | 
82  | 2  |     endian = HOST_ENDIAN;  | 
83  |  |  | 
84  | 2  |   align = ((char *) data - (char *) NULL) % sizeof (mp_limb_t);  | 
85  |  |  | 
86  | 2  |   if (nail == GMP_NAIL_BITS)  | 
87  | 2  |     { | 
88  | 2  |       if (size == sizeof (mp_limb_t) && align == 0)  | 
89  | 0  |   { | 
90  | 0  |     if (order == -1 && endian == HOST_ENDIAN)  | 
91  | 0  |       { | 
92  | 0  |         MPN_COPY ((mp_ptr) data, zp, (mp_size_t) count);  | 
93  | 0  |         return data;  | 
94  | 0  |       }  | 
95  | 0  |     if (order == 1 && endian == HOST_ENDIAN)  | 
96  | 0  |       { | 
97  | 0  |         MPN_REVERSE ((mp_ptr) data, zp, (mp_size_t) count);  | 
98  | 0  |         return data;  | 
99  | 0  |       }  | 
100  |  |  | 
101  | 0  |     if (order == -1 && endian == -HOST_ENDIAN)  | 
102  | 0  |       { | 
103  | 0  |         MPN_BSWAP ((mp_ptr) data, zp, (mp_size_t) count);  | 
104  | 0  |         return data;  | 
105  | 0  |       }  | 
106  | 0  |     if (order == 1 && endian == -HOST_ENDIAN)  | 
107  | 0  |       { | 
108  | 0  |         MPN_BSWAP_REVERSE ((mp_ptr) data, zp, (mp_size_t) count);  | 
109  | 0  |         return data;  | 
110  | 0  |       }  | 
111  | 0  |   }  | 
112  | 2  |     }  | 
113  |  |  | 
114  | 2  |   { | 
115  | 2  |     mp_limb_t      limb, wbitsmask;  | 
116  | 2  |     size_t         i, numb;  | 
117  | 2  |     mp_size_t      j, wbytes, woffset;  | 
118  | 2  |     unsigned char  *dp;  | 
119  | 2  |     int            lbits, wbits;  | 
120  | 2  |     mp_srcptr      zend;  | 
121  |  |  | 
122  | 2  |     numb = size * 8 - nail;  | 
123  |  |  | 
124  |  |     /* whole bytes per word */  | 
125  | 2  |     wbytes = numb / 8;  | 
126  |  |  | 
127  |  |     /* possible partial byte */  | 
128  | 2  |     wbits = numb % 8;  | 
129  | 2  |     wbitsmask = (CNST_LIMB(1) << wbits) - 1;  | 
130  |  |  | 
131  |  |     /* offset to get to the next word */  | 
132  | 2  |     woffset = (endian >= 0 ? size : - (mp_size_t) size)  | 
133  | 2  |       + (order < 0 ? size : - (mp_size_t) size);  | 
134  |  |  | 
135  |  |     /* least significant byte */  | 
136  | 2  |     dp = (unsigned char *) data  | 
137  | 2  |       + (order >= 0 ? (count-1)*size : 0) + (endian >= 0 ? size-1 : 0);  | 
138  |  |  | 
139  | 2  | #define EXTRACT(N, MASK)                                \  | 
140  | 4.98k  |     do {                                                \ | 
141  | 4.98k  |       if (lbits >= (N))                                 \  | 
142  | 4.98k  |         {                                               \ | 
143  | 4.36k  |           *dp = limb MASK;                              \  | 
144  | 4.36k  |           limb >>= (N);                                 \  | 
145  | 4.36k  |           lbits -= (N);                                 \  | 
146  | 4.36k  |         }                                               \  | 
147  | 4.98k  |       else                                              \  | 
148  | 4.98k  |         {                                               \ | 
149  | 624  |           mp_limb_t  newlimb;                           \  | 
150  | 624  |           newlimb = (zp == zend ? 0 : *zp++);           \  | 
151  | 624  |           *dp = (limb | (newlimb << lbits)) MASK;       \  | 
152  | 624  |           limb = newlimb >> ((N)-lbits);                \  | 
153  | 624  |           lbits += GMP_NUMB_BITS - (N);                 \  | 
154  | 624  |         }                                               \  | 
155  | 4.98k  |     } while (0)  | 
156  |  |  | 
157  | 2  |     zend = zp + zsize;  | 
158  | 2  |     lbits = 0;  | 
159  | 2  |     limb = 0;  | 
160  | 1.24k  |     for (i = 0; i < count; i++)  | 
161  | 1.24k  |       { | 
162  | 6.23k  |   for (j = 0; j < wbytes; j++)  | 
163  | 4.98k  |     { | 
164  | 4.98k  |       EXTRACT (8, + 0);  | 
165  | 4.98k  |       dp -= endian;  | 
166  | 4.98k  |     }  | 
167  | 1.24k  |   if (wbits != 0)  | 
168  | 0  |     { | 
169  | 0  |       EXTRACT (wbits, & wbitsmask);  | 
170  | 0  |       dp -= endian;  | 
171  | 0  |       j++;  | 
172  | 0  |     }  | 
173  | 1.24k  |   for ( ; j < size; j++)  | 
174  | 0  |     { | 
175  | 0  |       *dp = '\0';  | 
176  | 0  |       dp -= endian;  | 
177  | 0  |     }  | 
178  | 1.24k  |   dp += woffset;  | 
179  | 1.24k  |       }  | 
180  |  |  | 
181  | 2  |     ASSERT (zp == PTR(z) + ABSIZ(z));  | 
182  |  |  | 
183  |  |     /* low byte of word after most significant */  | 
184  | 2  |     ASSERT (dp == (unsigned char *) data  | 
185  | 2  |       + (order < 0 ? count*size : - (mp_size_t) size)  | 
186  | 2  |       + (endian >= 0 ? (mp_size_t) size - 1 : 0));  | 
187  | 2  |   }  | 
188  | 2  |   return data;  | 
189  | 2  | }  |