Skip to content

Commit

Permalink
a few C api changes, mainly comment updates
Browse files Browse the repository at this point in the history
  • Loading branch information
danpape committed Jun 5, 2021
1 parent f524b30 commit bbb589e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 8 additions & 8 deletions include/libbech32/libbech32.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace bech32 {

// Encoding: denotes which encoding was used for a bech32 string
// Represents which encoding was used for a bech32 string
enum Encoding {
Invalid, // no or invalid encoding was detected
Bech32, // encoding used original checksum constant (1)
Expand Down Expand Up @@ -90,11 +90,11 @@ typedef struct bech32_bstring_s {
} bech32_bstring;

/**
* Encoding: denotes which encoding was used for a bech32 string
* Represents which encoding was used for a bech32 string
*/
typedef enum bech32_encoding_e {
ENCODING_INVALID, // no or invalid encoding was detected
ENCODING_BECH32, // encoding uses original checksum constant (1)
ENCODING_BECH32, // encoding used original checksum constant (1)
ENCODING_BECH32M // encoding used default checksum constant (M = 0x2bc830a3)
} bech32_encoding;

Expand All @@ -116,7 +116,7 @@ typedef struct bech32_DecodedResult_s {
} bech32_DecodedResult;

/**
* Possible error codes
* libbech32 error codes
*/
typedef enum bech32_error_e
{
Expand Down Expand Up @@ -145,7 +145,7 @@ extern const char * bech32_strerror(bech32_error error_code);
/**
* Allocates memory for a bech32_DecodedResult struct based on the size of the bech32 string passed in.
*
* This memory must be freed using the bech32_free_DecodedResult function.
* This memory must be freed using bech32_free_DecodedResult().
*
* @param str the bech32 string to be decoded by bech32_decode()
*
Expand Down Expand Up @@ -173,7 +173,7 @@ extern size_t bech32_compute_encoded_string_length(size_t hrplen, size_t dplen);
/**
* Allocates memory for a to-be-encoded bech32 string
*
* This memory must be freed using the bech32_free_bstring function.
* This memory must be freed using bech32_free_bstring().
*
* @param hrplen the length of the "human-readable part" string. must be > 0
* @param dplen the length of the "data part" array
Expand All @@ -185,7 +185,7 @@ extern bech32_bstring * bech32_create_bstring(size_t hrplen, size_t dplen);
/**
* Allocates memory for a to-be-encoded bech32 string based on the size of the bech32_DecodedResult struct
*
* This memory must be freed using the bech32_free_bstring function.
* This memory must be freed using bech32_free_bstring().
*
* @param decodedResult a pointer to a bech32_DecodedResult struct
*
Expand All @@ -196,7 +196,7 @@ extern bech32_bstring * bech32_create_bstring_from_DecodedResult(bech32_DecodedR
/**
* Frees memory for a bech32 string.
*
* @param bstring pointer to a bech32 string
* @param bstring pointer to a bech32_bstring struct
*/
extern void bech32_free_bstring(bech32_bstring *bstring);

Expand Down
14 changes: 10 additions & 4 deletions libbech32/bech32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ const char * bech32_strerror(bech32_error error_code) {
/**
* Allocates memory for a bech32_DecodedResult struct based on the size of the bech32 string passed in.
*
* This memory must be freed using the bech32_free_DecodedResult function.
* This memory must be freed using bech32_free_DecodedResult().
*
* @param str the bech32 string to be decoded by bech32_decode()
*
Expand Down Expand Up @@ -431,7 +431,7 @@ size_t bech32_compute_encoded_string_length(size_t hrplen, size_t dplen) {
/**
* Allocates memory for a to-be-encoded bech32 string
*
* This memory must be freed using the bech32_free_bstring function.
* This memory must be freed using bech32_free_bstring().
*
* @param hrplen the length of the "human-readable part" string. must be > 0
* @param dplen the length of the "data part" array
Expand All @@ -443,15 +443,21 @@ bech32_bstring * bech32_create_bstring(size_t hrplen, size_t dplen) {
if(hrplen < 1)
return nullptr;
auto *bstring = static_cast<bech32_bstring *>(malloc(sizeof(bech32_bstring)));
if(bstring == nullptr)
return nullptr;
bstring->length = bech32_compute_encoded_string_length(hrplen, dplen);
bstring->string = static_cast<char *>(calloc(bstring->length + 1, 1)); // +1 for '\0'
if(bstring->string == nullptr) {
bech32_free_bstring(bstring);
return nullptr;
}
return bstring;
}

/**
* Allocates memory for a to-be-encoded bech32 string based on the size of the bech32_DecodedResult struct
*
* This memory must be freed using the bech32_free_bstring function.
* This memory must be freed using bech32_free_bstring().
*
* @param decodedResult a pointer to a bech32_DecodedResult struct
*
Expand All @@ -469,7 +475,7 @@ bech32_bstring * bech32_create_bstring_from_DecodedResult(bech32_DecodedResult *
/**
* Frees memory for a bech32 string.
*
* @param bstring pointer to a bech32 string
* @param bstring pointer to a bech32_bstring struct
*/
extern "C"
void bech32_free_bstring(bech32_bstring *bstring) {
Expand Down

0 comments on commit bbb589e

Please sign in to comment.