Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/uint256.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2011 The Bitcoin developers | // Copyright (c) 2011 The Bitcoin developers | ||||
// Distributed under the MIT/X11 software license, see the accompanying | // Distributed under the MIT/X11 software license, see the accompanying | ||||
// file license.txt or http://www.opensource.org/licenses/mit-license.php. | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_UINT256_H | #ifndef BITCOIN_SEEDER_UINT256_H | ||||
#define BITCOIN_UINT256_H | #define BITCOIN_SEEDER_UINT256_H | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include <limits.h> | #include <climits> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#if defined(_MSC_VER) || defined(__BORLANDC__) | #if defined(_MSC_VER) || defined(__BORLANDC__) | ||||
typedef __int64 int64; | typedef __int64 int64; | ||||
typedef unsigned __int64 uint64; | typedef unsigned __int64 uint64; | ||||
#else | #else | ||||
typedef long long int64; | typedef long long int64; | ||||
▲ Show 20 Lines • Show All 225 Lines • ▼ Show 20 Lines | public: | ||||
friend inline bool operator!=(const base_uint &a, uint64 b) { | friend inline bool operator!=(const base_uint &a, uint64 b) { | ||||
return (!(a == b)); | return (!(a == b)); | ||||
} | } | ||||
std::string GetHex() const { | std::string GetHex() const { | ||||
char psz[sizeof(pn) * 2 + 1]; | char psz[sizeof(pn) * 2 + 1]; | ||||
for (int i = 0; i < sizeof(pn); i++) | for (int i = 0; i < sizeof(pn); i++) | ||||
sprintf(psz + i * 2, "%02x", | sprintf(psz + i * 2, "%02x", ((uint8_t *)pn)[sizeof(pn) - i - 1]); | ||||
((unsigned char *)pn)[sizeof(pn) - i - 1]); | |||||
return std::string(psz, psz + sizeof(pn) * 2); | return std::string(psz, psz + sizeof(pn) * 2); | ||||
} | } | ||||
void SetHex(const char *psz) { | void SetHex(const char *psz) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
// skip leading spaces | // skip leading spaces | ||||
while (isspace(*psz)) | while (isspace(*psz)) | ||||
psz++; | psz++; | ||||
// skip 0x | // skip 0x | ||||
if (psz[0] == '0' && tolower(psz[1]) == 'x') psz += 2; | if (psz[0] == '0' && tolower(psz[1]) == 'x') psz += 2; | ||||
// hex string to uint | // hex string to uint | ||||
static char phexdigit[256] = { | static char phexdigit[256] = { | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, | ||||
0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||||
const char *pbegin = psz; | const char *pbegin = psz; | ||||
while (phexdigit[(unsigned char)*psz] || *psz == '0') | while (phexdigit[(uint8_t)*psz] || *psz == '0') | ||||
psz++; | psz++; | ||||
psz--; | psz--; | ||||
unsigned char *p1 = (unsigned char *)pn; | uint8_t *p1 = (uint8_t *)pn; | ||||
unsigned char *pend = p1 + WIDTH * 4; | uint8_t *pend = p1 + WIDTH * 4; | ||||
while (psz >= pbegin && p1 < pend) { | while (psz >= pbegin && p1 < pend) { | ||||
*p1 = phexdigit[(unsigned char)*psz--]; | *p1 = phexdigit[(uint8_t)*psz--]; | ||||
if (psz >= pbegin) { | if (psz >= pbegin) { | ||||
*p1 |= (phexdigit[(unsigned char)*psz--] << 4); | *p1 |= (phexdigit[(uint8_t)*psz--] << 4); | ||||
p1++; | p1++; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void SetHex(const std::string &str) { SetHex(str.c_str()); } | void SetHex(const std::string &str) { SetHex(str.c_str()); } | ||||
std::string ToString() const { return (GetHex()); } | std::string ToString() const { return (GetHex()); } | ||||
unsigned char *begin() { return (unsigned char *)&pn[0]; } | uint8_t *begin() { return (uint8_t *)&pn[0]; } | ||||
unsigned char *end() { return (unsigned char *)&pn[WIDTH]; } | uint8_t *end() { return (uint8_t *)&pn[WIDTH]; } | ||||
unsigned int size() { return sizeof(pn); } | unsigned int size() { return sizeof(pn); } | ||||
unsigned int GetSerializeSize(int nType = 0, | unsigned int GetSerializeSize(int nType = 0, | ||||
int nVersion = PROTOCOL_VERSION) const { | int nVersion = PROTOCOL_VERSION) const { | ||||
return sizeof(pn); | return sizeof(pn); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | uint160 &operator=(uint64 b) { | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
return *this; | return *this; | ||||
} | } | ||||
explicit uint160(const std::string &str) { SetHex(str); } | explicit uint160(const std::string &str) { SetHex(str); } | ||||
explicit uint160(const std::vector<unsigned char> &vch) { | explicit uint160(const std::vector<uint8_t> &vch) { | ||||
if (vch.size() == sizeof(pn)) | if (vch.size() == sizeof(pn)) | ||||
memcpy(pn, &vch[0], sizeof(pn)); | memcpy(pn, &vch[0], sizeof(pn)); | ||||
else | else | ||||
*this = 0; | *this = 0; | ||||
} | } | ||||
}; | }; | ||||
inline bool operator==(const uint160 &a, uint64 b) { | inline bool operator==(const uint160 &a, uint64 b) { | ||||
▲ Show 20 Lines • Show All 170 Lines • ▼ Show 20 Lines | uint256 &operator=(uint64 b) { | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
return *this; | return *this; | ||||
} | } | ||||
explicit uint256(const std::string &str) { SetHex(str); } | explicit uint256(const std::string &str) { SetHex(str); } | ||||
explicit uint256(const std::vector<unsigned char> &vch) { | explicit uint256(const std::vector<uint8_t> &vch) { | ||||
if (vch.size() == sizeof(pn)) | if (vch.size() == sizeof(pn)) | ||||
memcpy(pn, &vch[0], sizeof(pn)); | memcpy(pn, &vch[0], sizeof(pn)); | ||||
else | else | ||||
*this = 0; | *this = 0; | ||||
} | } | ||||
}; | }; | ||||
inline bool operator==(const uint256 &a, uint64 b) { | inline bool operator==(const uint256 &a, uint64 b) { | ||||
▲ Show 20 Lines • Show All 263 Lines • Show Last 20 Lines |