Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / binutils-2.14 / gas / bignum-copy.c
1 /* bignum_copy.c - copy a bignum
2    Copyright 1987, 1990, 1991, 1992, 1993, 2000
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "as.h"
22
23 /*
24  *                      bignum_copy ()
25  *
26  * Copy a bignum from in to out.
27  * If the output is shorter than the input, copy lower-order littlenums.
28  * Return 0 or the number of significant littlenums dropped.
29  * Assumes littlenum arrays are densely packed: no unused chars between
30  * the littlenums. Uses memcpy() to move littlenums, and wants to
31  * know length (in chars) of the input bignum.
32  */
33
34 /* void */
35 int
36 bignum_copy (in, in_length, out, out_length)
37      register LITTLENUM_TYPE *in;
38      register int in_length;    /* in sizeof(littlenum)s */
39      register LITTLENUM_TYPE *out;
40      register int out_length;   /* in sizeof(littlenum)s */
41 {
42   int significant_littlenums_dropped;
43
44   if (out_length < in_length)
45     {
46       LITTLENUM_TYPE *p;        /* -> most significant (non-zero) input
47                                       littlenum.  */
48
49       memcpy ((void *) out, (void *) in,
50               (unsigned int) out_length << LITTLENUM_SHIFT);
51       for (p = in + in_length - 1; p >= in; --p)
52         {
53           if (*p)
54             break;
55         }
56       significant_littlenums_dropped = p - in - in_length + 1;
57
58       if (significant_littlenums_dropped < 0)
59         {
60           significant_littlenums_dropped = 0;
61         }
62     }
63   else
64     {
65       memcpy ((char *) out, (char *) in,
66               (unsigned int) in_length << LITTLENUM_SHIFT);
67
68       if (out_length > in_length)
69         {
70           memset ((char *) (out + in_length),
71                   '\0',
72                   (unsigned int) (out_length - in_length) << LITTLENUM_SHIFT);
73         }
74
75       significant_littlenums_dropped = 0;
76     }
77
78   return (significant_littlenums_dropped);
79 }                               /* bignum_copy() */
80
81 /* end of bignum-copy.c */