summaryrefslogtreecommitdiff
path: root/ecos/packages/net/snmp/lib/current/src/int64.c
blob: 5bbe5080f601a18ea011e7ff1ce7e9f900beb971 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//==========================================================================
//
//      ./lib/current/src/int64.c
//
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos 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.                                                                 
//
// eCos 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 eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// As a special exception, if other files instantiate templates or use      
// macros or inline functions from this file, or you compile this file      
// and link it with other works to produce a work based on this file,       
// this file does not by itself cause the resulting work to be covered by   
// the GNU General Public License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//####UCDSNMPCOPYRIGHTBEGIN####
//
// -------------------------------------------
//
// Portions of this software may have been derived from the UCD-SNMP
// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
// California at Davis, which was originally based on the Carnegie Mellon
// University SNMP implementation.  Portions of this software are therefore
// covered by the appropriate copyright disclaimers included herein.
//
// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
// -------------------------------------------
//
//####UCDSNMPCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    hmt
// Contributors: hmt
// Date:         2000-05-30
// Purpose:      Port of UCD-SNMP distribution to eCos.
// Description:  
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================
/********************************************************************
       Copyright 1989, 1991, 1992 by Carnegie Mellon University

			  Derivative Work -
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California

			 All Rights Reserved

Permission to use, copy, modify and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appears in all copies and
that both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU and The Regents of
the University of California not be used in advertising or publicity
pertaining to distribution of the software without specific written
permission.

CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*********************************************************************/
/** file: test.c - test of 64-bit integer stuff
*
*
* 21-jan-1998: David Perkins <dperkins@dsperkins.com>
*
*/

#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#include "asn1.h"
#include "int64.h"

#define TRUE 1
#define FALSE 0

/** divBy10 - divide an unsigned 64-bit integer by 10
*
* call with:
*   u64 - number to be divided
*   pu64Q - location to store quotient
*   puR - location to store remainder
*
*/
void
divBy10(U64 u64,
	U64 *pu64Q,
	unsigned int *puR)  
{
    unsigned long ulT;
    unsigned long ulQ;
    unsigned long ulR;


    /* top 16 bits */
    ulT = (u64.high>>16) & 0x0ffff;
    ulQ = ulT/10;
    ulR = ulT%10;
    pu64Q->high = ulQ<<16;

    /* next 16 */
    ulT = (u64.high & 0x0ffff);
	ulT += (ulR<<16);
    ulQ = ulT/10;
    ulR = ulT%10;
    pu64Q->high = pu64Q->high | ulQ;

    /* next 16 */
    ulT = ((u64.low>>16) & 0x0ffff) + (ulR<<16);
    ulQ = ulT/10;
    ulR = ulT%10;
    pu64Q->low = ulQ<<16;

    /* final 16 */
    ulT = (u64.low & 0x0ffff);
	ulT += (ulR<<16);
    ulQ = ulT/10;
    ulR = ulT%10;
    pu64Q->low = pu64Q->low | ulQ;

    *puR = (unsigned int)(ulR);


} /* divBy10 */


/** multBy10 - multiply an unsigned 64-bit integer by 10
*
* call with:
*   u64 - number to be multiplied
*   pu64P - location to store product
*
*/
void
multBy10(U64 u64,
	 U64 *pu64P)
{
    unsigned long ulT;
    unsigned long ulP;
    unsigned long ulK;


    /* lower 16 bits */
    ulT = u64.low & 0x0ffff;
    ulP = ulT * 10;
    ulK = ulP>>16;
    pu64P->low = ulP & 0x0ffff;

    /* next 16 */
    ulT = (u64.low>>16) & 0x0ffff;
    ulP = (ulT * 10) + ulK;
    ulK = ulP>>16;
    pu64P->low = (ulP & 0x0ffff)<<16 | pu64P->low;

    /* next 16 bits */
    ulT = u64.high & 0x0ffff;
    ulP = (ulT * 10) + ulK;
    ulK = ulP>>16;
    pu64P->high = ulP & 0x0ffff;

    /* final 16 */
    ulT = (u64.high>>16) & 0x0ffff;
    ulP = (ulT * 10) + ulK;
    ulK = ulP>>16;
    pu64P->high = (ulP & 0x0ffff)<<16 | pu64P->high;


} /* multBy10 */


/** incrByU16 - add an unsigned 16-bit int to an unsigned 64-bit integer
*
* call with:
*   pu64 - number to be incremented
*   u16 - amount to add
*
*/
void
incrByU16(U64 *pu64,
	  unsigned int u16)
{
    unsigned long ulT1;
    unsigned long ulT2;
    unsigned long ulR;
    unsigned long ulK;


    /* lower 16 bits */
    ulT1 = pu64->low;
    ulT2 = ulT1 & 0x0ffff;
    ulR = ulT2 + u16;
    ulK = ulR>>16;
    if (ulK == 0) {
        pu64->low = ulT1 + u16;
        return;
    }

    /* next 16 bits */
    ulT2 = (ulT1>>16) & 0x0ffff;
    ulR = ulT2+1;
    ulK = ulR>>16;
    if (ulK == 0) {
        pu64->low = ulT1 + u16;
        return;
    }
   
    /* next 32 - ignore any overflow */
    pu64->low = (ulT1 + u16) & 0x0FFFFFFFFL;
    pu64->high++;

} /* incrByV16 */

void
incrByU32(U64 *pu64,
	  unsigned int u32)
{
  unsigned int tmp;
  tmp = pu64->low;
  pu64->low += u32;
  if (pu64->low < tmp)
    pu64->high++;
}

/* pu64out = pu64one - pu64two */
void
u64Subtract(U64 *pu64one,
            U64 *pu64two,
            U64 *pu64out)
{
  if (pu64one->low > pu64two->low) {
    pu64out->low = 0xffffffff - pu64two->low + pu64one->low + 1;
    pu64out->high = pu64one->high - pu64two->high - 1;
  } else {
    pu64out->low = pu64one->low - pu64two->low;
    pu64out->high = pu64one->high - pu64two->high;
  }
}

/** zeroU64 - set an unsigned 64-bit number to zero
*
* call with:
*   pu64 - number to be zero'ed
*
*/
void
zeroU64(U64 *pu64)
{

    pu64->low = 0;
    pu64->high = 0;
} /* zeroU64 */


/** isZeroU64 - check if an unsigned 64-bit number is
*
* call with:
*   pu64 - number to be zero'ed
*
*/
int
isZeroU64(U64 *pu64)
{

    if ((pu64->low == 0) && (pu64->high == 0))
        return(TRUE);
    else
        return(FALSE);

} /* isZeroU64 */

void
printU64(char * buf, /* char [I64CHARSZ+1]; */
	 U64 *pu64)
{
  U64 u64a;
  U64 u64b;

  char aRes [I64CHARSZ+1];
  unsigned int u;
  int j;

  u64a.high = pu64->high;
  u64a.low = pu64->low;
  aRes[I64CHARSZ] = 0;
  for (j = 0; j < I64CHARSZ; j++) {
    divBy10(u64a, &u64b, &u);
    aRes[(I64CHARSZ-1)-j] = (char)('0' + u);
    u64a.high = u64b.high;
    u64a.low = u64b.low;
    if (isZeroU64(&u64a))
      break;
  }
  strcpy(buf, &aRes[(I64CHARSZ-1)-j]);
}

void
printI64(char * buf, /* char [I64CHARSZ+1]; */
	 U64 *pu64)
{
  U64 u64a;
  U64 u64b;

  char aRes [I64CHARSZ+1];
  unsigned int u;
  int j, sign=0;

  if (pu64->high & 0x80000000) {
    u64a.high = ~pu64->high;
    u64a.low = ~pu64->low;
    sign = 1;
    incrByU32(&u64a, 1);  /* bit invert and incr by 1 to print 2s complement */
  } else {
    u64a.high = pu64->high;
    u64a.low = pu64->low;
  }
  
  aRes[I64CHARSZ] = 0;
  for (j = 0; j < I64CHARSZ; j++) {
    divBy10(u64a, &u64b, &u);
    aRes[(I64CHARSZ-1)-j] = (char)('0' + u);
    u64a.high = u64b.high;
    u64a.low = u64b.low;
    if (isZeroU64(&u64a))
      break;
  }
  if (sign == 1) {
    aRes[(I64CHARSZ-1)-j-1] = '-';
    strcpy(buf, &aRes[(I64CHARSZ-1)-j-1]);
    return;
  }
  strcpy(buf, &aRes[(I64CHARSZ-1)-j]);
}

int
read64(U64 *i64,
       const char *string)
{
  U64 i64p;
  unsigned int u;
  int sign = 0;
  int ok = 0;
  
  zeroU64(i64);
  if (*string == '-') {
    sign = 1;
    string++;
  }
  
  while (*string && isdigit(*string)) {
    ok = 1;
    u = *string - '0';
    multBy10(*i64, &i64p);
    memcpy(i64, &i64p, sizeof(i64p));
    incrByU16(i64, u);
    string++;
  }
  if (sign) {
    i64->high = ~i64->high;
    i64->low = ~i64->low;
    incrByU16(i64,1);
  }
  return ok;
}


  

#ifdef TESTING
void
main(int argc, char *argv[])
{
    int i;
    int j;
    int l;
    unsigned int u;
    U64 u64a;
    U64 u64b;
#define MXSZ 20
    char aRes[MXSZ+1];


    if (argc < 2) {
        printf("This program takes numbers from the command line\n"
               "and prints them out.\n"
               "Usage: test <unsignedInt>...\n");
        exit(1);
    }

    aRes[MXSZ] = 0;

    for (i = 1; i < argc; i++) {
        l = strlen(argv[i]);
        zeroU64(&u64a);
        for (j = 0; j < l; j++) {
            if (!isdigit(argv[i][j])) {
                printf("Argument is not a number \"%s\"\n", argv[i]);
                exit(1);
            }
            u = argv[i][j] - '0';
            multBy10(u64a, &u64b);
            u64a = u64b;
            incrByU16(&u64a, u);
        }

        printf("number \"%s\" in hex is '%08x%08x'h\n",
                argv[i], u64a.high, u64a.low);

        printf("number is \"%s\"\n", printU64(&u64a));
        for (j = 0; j < MXSZ; j++) {
            divBy10(u64a, &u64b, &u);
            aRes[(MXSZ-1)-j] = (char)('0' + u);
            u64a = u64b;
            if (isZeroU64(&u64a))
                break;
        }

        printf("number is \"%s\"\n", &aRes[(MXSZ-1)-j]);
    }
	exit(0);
} /* main */
#endif /* TESTING */

/* file: test.c */