monotone

monotone Mtn Source Tree

Root/botan/big_code.cpp

1/*************************************************
2* BigInt Encoding/Decoding Source File *
3* (C) 1999-2006 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] = Charset::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] =
47 Charset::digit2char(remainder.word_at(0));
48 if(copy.is_zero())
49 break;
50 }
51 }
52 else
53 throw Invalid_Argument("Unknown BigInt encoding method");
54 }
55
56/*************************************************
57* Encode a BigInt *
58*************************************************/
59SecureVector<byte> BigInt::encode(const BigInt& n, Base base)
60 {
61 SecureVector<byte> output(n.encoded_size(base));
62 encode(output, n, base);
63 if(base != Binary)
64 for(u32bit j = 0; j != output.size(); ++j)
65 if(output[j] == 0)
66 output[j] = '0';
67 return output;
68 }
69
70/*************************************************
71* Encode a BigInt, with leading 0s if needed *
72*************************************************/
73SecureVector<byte> BigInt::encode_1363(const BigInt& n, u32bit bytes)
74 {
75 const u32bit n_bytes = n.bytes();
76 if(n_bytes > bytes)
77 throw Encoding_Error("encode_1363: n is too large to encode properly");
78
79 const u32bit leading_0s = bytes - n_bytes;
80
81 SecureVector<byte> output(bytes);
82 encode(output + leading_0s, n, Binary);
83 return output;
84 }
85
86/*************************************************
87* Decode a BigInt *
88*************************************************/
89BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base)
90 {
91 return BigInt::decode(buf, buf.size(), base);
92 }
93
94/*************************************************
95* Decode a BigInt *
96*************************************************/
97BigInt BigInt::decode(const byte buf[], u32bit length, Base base)
98 {
99 BigInt r;
100 if(base == Binary)
101 r.binary_decode(buf, length);
102 else if(base == Hexadecimal)
103 {
104 SecureVector<byte> hex;
105 for(u32bit j = 0; j != length; ++j)
106 if(Hex_Decoder::is_valid(buf[j]))
107 hex.append(buf[j]);
108
109 u32bit offset = (hex.size() % 2);
110 SecureVector<byte> binary(hex.size() / 2 + offset);
111
112 if(offset)
113 {
114 byte temp[2] = { '0', hex[0] };
115 binary[0] = Hex_Decoder::decode(temp);
116 }
117
118 for(u32bit j = offset; j != binary.size(); ++j)
119 binary[j] = Hex_Decoder::decode(hex+2*j-offset);
120 r.binary_decode(binary, binary.size());
121 }
122 else if(base == Decimal || base == Octal)
123 {
124 const u32bit RADIX = ((base == Decimal) ? 10 : 8);
125 for(u32bit j = 0; j != length; ++j)
126 {
127 byte x = Charset::char2digit(buf[j]);
128 if(x >= RADIX)
129 {
130 if(RADIX == 10)
131 throw Invalid_Argument("BigInt: Invalid decimal string");
132 else
133 throw Invalid_Argument("BigInt: Invalid octal string");
134 }
135
136 r *= RADIX;
137 r += x;
138 }
139 }
140 else
141 throw Invalid_Argument("Unknown BigInt decoding method");
142 return r;
143 }
144
145}

Archive Download this file

Branches

Tags

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