-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4048 from sipa/nobigb58
Remove dependency of base58 on OpenSSL
- Loading branch information
Showing
3 changed files
with
101 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2014 The Bitcoin developers | ||
// Distributed under the MIT/X11 software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <assert.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <vector> | ||
#include <string> | ||
|
||
/* All alphanumeric characters except for "0", "I", "O", and "l" */ | ||
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | ||
|
||
bool DecodeBase58(const char *psz, std::vector<unsigned char>& vch) { | ||
// Skip leading spaces. | ||
while (*psz && isspace(*psz)) | ||
psz++; | ||
// Skip and count leading '1's. | ||
int zeroes = 0; | ||
while (*psz == '1') { | ||
zeroes++; | ||
psz++; | ||
} | ||
// Allocate enough space in big-endian base256 representation. | ||
std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up. | ||
// Process the characters. | ||
while (*psz && !isspace(*psz)) { | ||
// Decode base58 character | ||
const char *ch = strchr(pszBase58, *psz); | ||
if (ch == NULL) | ||
return false; | ||
// Apply "b256 = b256 * 58 + ch". | ||
int carry = ch - pszBase58; | ||
for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) { | ||
carry += 58 * (*it); | ||
*it = carry % 256; | ||
carry /= 256; | ||
} | ||
assert(carry == 0); | ||
psz++; | ||
} | ||
// Skip trailing spaces. | ||
while (isspace(*psz)) | ||
psz++; | ||
if (*psz != 0) | ||
return false; | ||
// Skip leading zeroes in b256. | ||
std::vector<unsigned char>::iterator it = b256.begin(); | ||
while (it != b256.end() && *it == 0) | ||
it++; | ||
// Copy result into output vector. | ||
vch.reserve(zeroes + (b256.end() - it)); | ||
vch.assign(zeroes, 0x00); | ||
while (it != b256.end()) | ||
vch.push_back(*(it++)); | ||
return true; | ||
} | ||
|
||
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) { | ||
// Skip & count leading zeroes. | ||
int zeroes = 0; | ||
while (pbegin != pend && *pbegin == 0) { | ||
pbegin++; | ||
zeroes++; | ||
} | ||
// Allocate enough space in big-endian base58 representation. | ||
std::vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up. | ||
// Process the bytes. | ||
while (pbegin != pend) { | ||
int carry = *pbegin; | ||
// Apply "b58 = b58 * 256 + ch". | ||
for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) { | ||
carry += 256 * (*it); | ||
*it = carry % 58; | ||
carry /= 58; | ||
} | ||
assert(carry == 0); | ||
pbegin++; | ||
} | ||
// Skip leading zeroes in base58 result. | ||
std::vector<unsigned char>::iterator it = b58.begin(); | ||
while (it != b58.end() && *it == 0) | ||
it++; | ||
// Translate the result into a string. | ||
std::string str; | ||
str.reserve(zeroes + (b58.end() - it)); | ||
str.assign(zeroes, '1'); | ||
while (it != b58.end()) | ||
str += pszBase58[*(it++)]; | ||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters