Skip to content

Commit

Permalink
Move hex_decode() utility into common file
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddrysdale committed Oct 1, 2014
1 parent 5d1a882 commit 65e6e83
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
26 changes: 0 additions & 26 deletions object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,6 @@ namespace {

CK_BYTE deadbeef[] = { 0xDE, 0xAD, 0xBE, 0xEF};

string hex_decode(string hex_value) {
bool high_nibble = true;
stringstream ss;
int value = 0;
for (char c : hex_value) {
int nibble;
if (c >= '0' && c <= '9') {
nibble = c - '0';
} else if (c >= 'a' && c <= 'f') {
nibble = 10 + (c - 'a');
} else if (c >= 'A' && c <= 'F') {
nibble = 10 + (c - 'A');
} else {
exit(1);
}
if (high_nibble) {
value = (nibble << 4);
} else {
value |= nibble;
ss << static_cast<char>(value);
}
high_nibble = !high_nibble;
}
return ss.str();
}

TEST(BERDecode, DERDecode) {
string hex_value = "3077" // universal,constructed,SEQUENCE_OF,len=0x77
"3119" // universal,constructed,SET_OF,len=0x19
Expand Down
26 changes: 26 additions & 0 deletions pkcs11-describe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ string hex_data(CK_BYTE_PTR p, int len) {
return ss.str();
}

string hex_decode(string hex_value) {
bool high_nibble = true;
stringstream ss;
int value = 0;
for (char c : hex_value) {
int nibble;
if (c >= '0' && c <= '9') {
nibble = c - '0';
} else if (c >= 'a' && c <= 'f') {
nibble = 10 + (c - 'a');
} else if (c >= 'A' && c <= 'F') {
nibble = 10 + (c - 'A');
} else {
exit(1);
}
if (high_nibble) {
value = (nibble << 4);
} else {
value |= nibble;
ss << static_cast<char>(value);
}
high_nibble = !high_nibble;
}
return ss.str();
}

string rv_name(CK_RV val) {
// PKCS#11 s11.1: Function return values
switch (val) {
Expand Down
1 change: 1 addition & 0 deletions pkcs11-describe.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ std::string hex_data(const CK_BYTE_PTR p, int len);
inline std::string hex_data(std::string s) {
return hex_data((CK_BYTE_PTR)(s.data()), s.length());
}
std::string hex_decode(std::string hex_value);
std::string rv_name(CK_RV val);
std::string user_type_name(CK_USER_TYPE val);
std::string key_type_name(CK_KEY_TYPE val);
Expand Down

0 comments on commit 65e6e83

Please sign in to comment.