summaryrefslogtreecommitdiff
path: root/lib/dns/sec/dnssafe/intbits.c
blob: 6a60e22ad82624d300b4fffb6891fdf5eacfa206 (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
/* Copyright (C) RSA Data Security, Inc. created 1990, 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 "global.h"
#include "algae.h"

/* Return the number of bits in the canonical, positive integer.
   IntgerBits (0) = 0.
 */
unsigned int A_IntegerBits (integer, integerLen)
unsigned char *integer;
unsigned int integerLen;
{
  unsigned char mask, byte;
  unsigned int bytes, bits;
  
  for (bytes = 0; bytes < integerLen && integer[bytes] == 0; bytes++);
  if (bytes == integerLen)
    return (0);
  
  /* Get byte to test and increment byte count for final calculation */
  byte = integer[bytes++];
  
  /* Get number of bits in most significant byte */
  for (bits = 8, mask = 0x80; (byte & mask) == 0; bits--, mask >>= 1);
  return (8 * (integerLen - bytes) + bits);
}