summaryrefslogtreecommitdiff
path: root/usr/src/lib/libresolv2/common/dnssafe/bigarith.c
blob: adc1b0b19474e7f0beba17343328a520cf05572c (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/* Copyright (C) RSA Data Security, Inc. created 1987, 1996.  This is an
   unpublished work protected as such under copyright law.  This work
   contains proprietary, confidential, and trade secret information of
   RSA Data Security, Inc.  Use, disclosure or reproduction without the
   express written authorization of RSA Data Security, Inc. is
   prohibited.
 */

#include "port_before.h"
#include "global.h"
#include "bigmath.h"
#include "port_after.h"

void BigZero (a, n)
UINT2 *a;
unsigned int n;
{
  register unsigned int i;
  
  for (i = 0; i < n; i++)
    a[i] = 0;
}

void BigAdd (a, b, c, n)
UINT2 *a, *b, *c;
unsigned int n;
{
  UINT4 result = (UINT4)0;
  register unsigned int i;

  for (i = 0; i < n; i++) {
    result += (UINT4) b[i];
    result += (UINT4) c[i];
    a[i] = (UINT2) result;
    result >>= 16;
  }
}

void BigSub (a, b, c, n)
UINT2 *a, *b, *c;
unsigned int n;
{
  UINT4 result = (UINT4)1;                   /* carry bit for negation of c */
  register unsigned int i;

  for (i = 0; i < n; i++) {
    result += (UINT4) b[i];
    result += (((UINT4) ~c[i]) & 0x0000FFFFL);
    a[i] = (UINT2)result;
    result >>= 16;
  }
}

void BigNeg (a, b, n)
UINT2 *a, *b;
unsigned int n;
{
  register unsigned int i;
  unsigned int carry = 1;

  for (i = 0; i < n-1; i++) {
    a[i] = ~b[i] + carry;
    if (a[i])
      carry = 0;
  }
  
  a[i] = ~b[i] + carry;
}

void BigInc (a, n)
UINT2 *a;
unsigned int n;
{
  register unsigned int i;
  unsigned int carry = 1;                                 /* carry to start */

  for (i = 0; i < n-1 && carry; i++) {
    a[i]++;
    if (a[i])
      carry = 0;
  }
  
  if (carry)
    a[i]++;
}

void BigDec (a, n)
UINT2 *a;
unsigned int n;
{
  register unsigned int i;
  unsigned int borrow = 1;                               /* borrow to start */

  for (i = 0; i < n-1 && borrow; i++) {
    a[i]--;
    if (a[i] != 0xFFFF)
      borrow = 0;
  }
  
  if (borrow)
    a[i]--;
}

int BigSign (a, n)
UINT2 *a;
unsigned int n;
{
  register int i;
  
  if (a[n-1] & 0x8000)
    return (-1);
  for (i = n-1; i >= 0; i--)
    if (a[i])
      return (1);
  return (0);
}

void BigCopy (a, b, n)
UINT2 *a, *b;
unsigned int n;
{
  register unsigned int i;
  
  for (i = 0; i < n; i++)
    a[i] = b[i];
}

/* Assumes a is nonnegative.
 */
unsigned int BigLenw (a, n)
UINT2 *a;
unsigned int n;
{
  register int i;
  
  for (i = n-1; i >= 0; i--)
    if (a[i])
      return (i+1);
  return (0);
}