~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/botantools/botan/big_code.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 1999-2007 The Botan Project. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, for any use, with or without
 
5
modification, is permitted provided that the following conditions are met:
 
6
 
 
7
1. Redistributions of source code must retain the above copyright notice, this
 
8
list of conditions, and the following disclaimer.
 
9
 
 
10
2. Redistributions in binary form must reproduce the above copyright notice,
 
11
this list of conditions, and the following disclaimer in the documentation
 
12
and/or other materials provided with the distribution.
 
13
 
 
14
THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
 
15
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
16
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
 
17
 
 
18
IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
 
19
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
20
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
21
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
22
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 
23
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
24
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
25
*/
 
26
// LICENSEHEADER_END
 
27
namespace QCA { // WRAPNS_LINE
 
28
/*************************************************
 
29
* BigInt Encoding/Decoding Source File           *
 
30
* (C) 1999-2007 The Botan Project                *
 
31
*************************************************/
 
32
 
 
33
} // WRAPNS_LINE
 
34
#include <botan/bigint.h>
 
35
namespace QCA { // WRAPNS_LINE
 
36
} // WRAPNS_LINE
 
37
#include <botan/numthry.h>
 
38
namespace QCA { // WRAPNS_LINE
 
39
} // WRAPNS_LINE
 
40
#include <botan/charset.h>
 
41
namespace QCA { // WRAPNS_LINE
 
42
#ifndef BOTAN_MINIMAL_BIGINT
 
43
} // WRAPNS_LINE
 
44
#include <botan/hex.h>
 
45
namespace QCA { // WRAPNS_LINE
 
46
#endif
 
47
 
 
48
namespace Botan {
 
49
 
 
50
/*************************************************
 
51
* Encode a BigInt                                *
 
52
*************************************************/
 
53
void BigInt::encode(byte output[], const BigInt& n, Base base)
 
54
   {
 
55
   if(base == Binary)
 
56
      n.binary_encode(output);
 
57
#ifndef BOTAN_MINIMAL_BIGINT
 
58
   else if(base == Hexadecimal)
 
59
      {
 
60
      SecureVector<byte> binary(n.encoded_size(Binary));
 
61
      n.binary_encode(binary);
 
62
      for(u32bit j = 0; j != binary.size(); ++j)
 
63
         Hex_Encoder::encode(binary[j], output + 2*j);
 
64
      }
 
65
#endif
 
66
   else if(base == Octal)
 
67
      {
 
68
      BigInt copy = n;
 
69
      const u32bit output_size = n.encoded_size(Octal);
 
70
      for(u32bit j = 0; j != output_size; ++j)
 
71
         {
 
72
         output[output_size - 1 - j] = Charset::digit2char(copy % 8);
 
73
         copy /= 8;
 
74
         }
 
75
      }
 
76
   else if(base == Decimal)
 
77
      {
 
78
      BigInt copy = n;
 
79
      BigInt remainder;
 
80
      copy.set_sign(Positive);
 
81
      const u32bit output_size = n.encoded_size(Decimal);
 
82
      for(u32bit j = 0; j != output_size; ++j)
 
83
         {
 
84
         divide(copy, 10, copy, remainder);
 
85
         output[output_size - 1 - j] =
 
86
            Charset::digit2char(remainder.word_at(0));
 
87
         if(copy.is_zero())
 
88
            {
 
89
            if(j < output_size - 1)
 
90
               {
 
91
               int extra = output_size - 1 - j;
 
92
               memmove(output, output + extra, output_size - extra);
 
93
               memset(output + output_size - extra, 0, extra);
 
94
               }
 
95
            break;
 
96
            }
 
97
         }
 
98
      }
 
99
   else
 
100
      throw Invalid_Argument("Unknown BigInt encoding method");
 
101
   }
 
102
 
 
103
/*************************************************
 
104
* Encode a BigInt                                *
 
105
*************************************************/
 
106
SecureVector<byte> BigInt::encode(const BigInt& n, Base base)
 
107
   {
 
108
   SecureVector<byte> output(n.encoded_size(base));
 
109
   encode(output, n, base);
 
110
   if(base != Binary)
 
111
      for(u32bit j = 0; j != output.size(); ++j)
 
112
         if(output[j] == 0)
 
113
            output[j] = '0';
 
114
   return output;
 
115
   }
 
116
 
 
117
/*************************************************
 
118
* Encode a BigInt, with leading 0s if needed     *
 
119
*************************************************/
 
120
SecureVector<byte> BigInt::encode_1363(const BigInt& n, u32bit bytes)
 
121
   {
 
122
   const u32bit n_bytes = n.bytes();
 
123
   if(n_bytes > bytes)
 
124
      throw Encoding_Error("encode_1363: n is too large to encode properly");
 
125
 
 
126
   const u32bit leading_0s = bytes - n_bytes;
 
127
 
 
128
   SecureVector<byte> output(bytes);
 
129
   encode(output + leading_0s, n, Binary);
 
130
   return output;
 
131
   }
 
132
 
 
133
/*************************************************
 
134
* Decode a BigInt                                *
 
135
*************************************************/
 
136
BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base)
 
137
   {
 
138
   return BigInt::decode(buf, buf.size(), base);
 
139
   }
 
140
 
 
141
/*************************************************
 
142
* Decode a BigInt                                *
 
143
*************************************************/
 
144
BigInt BigInt::decode(const byte buf[], u32bit length, Base base)
 
145
   {
 
146
   BigInt r;
 
147
   if(base == Binary)
 
148
      r.binary_decode(buf, length);
 
149
#ifndef BOTAN_MINIMAL_BIGINT
 
150
   else if(base == Hexadecimal)
 
151
      {
 
152
      SecureVector<byte> hex;
 
153
      for(u32bit j = 0; j != length; ++j)
 
154
         if(Hex_Decoder::is_valid(buf[j]))
 
155
            hex.append(buf[j]);
 
156
 
 
157
      u32bit offset = (hex.size() % 2);
 
158
      SecureVector<byte> binary(hex.size() / 2 + offset);
 
159
 
 
160
      if(offset)
 
161
         {
 
162
         byte temp[2] = { '0', hex[0] };
 
163
         binary[0] = Hex_Decoder::decode(temp);
 
164
         }
 
165
 
 
166
      for(u32bit j = offset; j != binary.size(); ++j)
 
167
         binary[j] = Hex_Decoder::decode(hex+2*j-offset);
 
168
      r.binary_decode(binary, binary.size());
 
169
      }
 
170
#endif
 
171
   else if(base == Decimal || base == Octal)
 
172
      {
 
173
      const u32bit RADIX = ((base == Decimal) ? 10 : 8);
 
174
      for(u32bit j = 0; j != length; ++j)
 
175
         {
 
176
         byte x = Charset::char2digit(buf[j]);
 
177
         if(x >= RADIX)
 
178
            {
 
179
            if(RADIX == 10)
 
180
               throw Invalid_Argument("BigInt: Invalid decimal string");
 
181
            else
 
182
               throw Invalid_Argument("BigInt: Invalid octal string");
 
183
            }
 
184
 
 
185
         r *= RADIX;
 
186
         r += x;
 
187
         }
 
188
      }
 
189
   else
 
190
      throw Invalid_Argument("Unknown BigInt decoding method");
 
191
   return r;
 
192
   }
 
193
 
 
194
}
 
195
} // WRAPNS_LINE