monotone

monotone Mtn Source Tree

Root/botan/big_code.cpp

1/*************************************************
2* BigInt Encoding/Decoding Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/bigint.h>
7#include <botan/numthry.h>
8#include <botan/charset.h>
9#include <botan/hex.h>
10
11namespace Botan {
12
13/*************************************************
14* Encode a BigInt *
15*************************************************/
16void BigInt::encode(byte output[], const BigInt& n, Base base)
17 {
18 if(base == Binary)
19 n.binary_encode(output);
20 else if(base == Hexadecimal)
21 {
22 SecureVector<byte> binary(n.encoded_size(Binary));
23 n.binary_encode(binary);
24 for(u32bit j = 0; j != binary.size(); j++)
25 Hex_Encoder::encode(binary[j], output + 2*j);
26 }
27 else if(base == Octal)
28 {
29 BigInt copy = n;
30 const u32bit output_size = n.encoded_size(Octal);
31 for(u32bit j = 0; j != output_size; j++)
32 {
33 output[output_size - 1 - j] = digit2char(copy % 8);
34 copy /= 8;
35 }
36 }
37 else if(base == Decimal)
38 {
39 BigInt copy = n;
40 BigInt remainder;
41 copy.set_sign(Positive);
42 const u32bit output_size = n.encoded_size(Decimal);
43 for(u32bit j = 0; j != output_size; j++)
44 {
45 divide(copy, 10, copy, remainder);
46 output[output_size - 1 - j] = digit2char(remainder.word_at(0));
47 if(copy.is_zero())
48 break;
49 }
50 }
51 else
52 throw Invalid_Argument("Unknown BigInt encoding method");
53 }
54
55/*************************************************
56* Encode a BigInt *
57*************************************************/
58SecureVector<byte> BigInt::encode(const BigInt& n, Base base)
59 {
60 SecureVector<byte> output(n.encoded_size(base));
61 encode(output, n, base);
62 if(base != Binary)
63 for(u32bit j = 0; j != output.size(); j++)
64 if(output[j] == 0)
65 output[j] = '0';
66 return output;
67 }
68
69/*************************************************
70* Encode a BigInt, with leading 0s if needed *
71*************************************************/
72SecureVector<byte> BigInt::encode_1363(const BigInt& n, u32bit bytes)
73 {
74 const u32bit n_bytes = n.bytes();
75 if(n_bytes > bytes)
76 throw Encoding_Error("encode_1363: n is too large to encode properly");
77
78 const u32bit leading_0s = bytes - n_bytes;
79
80 SecureVector<byte> output(bytes);
81 encode(output + leading_0s, n, Binary);
82 return output;
83 }
84
85/*************************************************
86* Decode a BigInt *
87*************************************************/
88BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base)
89 {
90 return BigInt::decode(buf, buf.size(), base);
91 }
92
93/*************************************************
94* Decode a BigInt *
95*************************************************/
96BigInt BigInt::decode(const byte buf[], u32bit length, Base base)
97 {
98 BigInt r;
99 if(base == Binary)
100 r.binary_decode(buf, length);
101 else if(base == Hexadecimal)
102 {
103 SecureVector<byte> hex;
104 for(u32bit j = 0; j != length; j++)
105 if(Hex_Decoder::is_valid(buf[j]))
106 hex.append(buf[j]);
107
108 u32bit offset = (hex.size() % 2);
109 SecureVector<byte> binary(hex.size() / 2 + offset);
110
111 if(offset)
112 {
113 byte temp[2] = { '0', hex[0] };
114 binary[0] = Hex_Decoder::decode(temp);
115 }
116
117 for(u32bit j = offset; j != binary.size(); j++)
118 binary[j] = Hex_Decoder::decode(hex+2*j-offset);
119 r.binary_decode(binary, binary.size());
120 }
121 else if(base == Decimal || base == Octal)
122 {
123 const u32bit RADIX = ((base == Decimal) ? 10 : 8);
124 for(u32bit j = 0; j != length; j++)
125 {
126 byte x = char2digit(buf[j]);
127 if(x >= RADIX)
128 {
129 if(RADIX == 10)
130 throw Invalid_Argument("BigInt: Invalid decimal string");
131 else
132 throw Invalid_Argument("BigInt: Invalid octal string");
133 }
134 r = RADIX * r + x;
135 }
136 }
137 else
138 throw Invalid_Argument("Unknown BigInt decoding method");
139 return r;
140 }
141
142}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status