Skip to content

Commit

Permalink
Fixed base58_decode
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed May 15, 2018
1 parent 8ac2d22 commit 8a7729c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,26 @@ PHP_FUNCTION(base58_encode)

PHP_FUNCTION(base58_decode)
{
char *data;
size_t data_len;

const char *b58;
size_t b58_len;

char *data;
size_t data_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &b58, &b58_len) == FAILURE) {
RETURN_FALSE;
}

data_len = b58_len;
data = emalloc(data_len);

if (!b58tobin(data, &data_len, b58, b58_len)) {
if (!b58tobin(data, &data_len, b58, b58_len - 1)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to base58 decode string");
RETURN_FALSE;
}

RETURN_STRINGL(data, data_len);
// libbase58 starts at the end of the buffer, so skip preceding '\0' chars.
RETURN_STRINGL(data + (b58_len - data_len), data_len);
}

#endif
Expand Down
6 changes: 4 additions & 2 deletions test.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

$base58 = base58_encode("Hello world");
$base58 = base58_encode($argv[1] ?? 'Hello world');

echo $base58, " - ", base58_decode($base58), "\n";
$value = base58_decode($base58);

echo $base58, " <=> ", $value, "\n";

0 comments on commit 8a7729c

Please sign in to comment.