aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2023-01-10 16:57:55 -0900
committerDavid M. Syzdek <david@syzdek.net>2023-01-10 17:16:14 -0900
commit63138267227c09a35cf591b2fe2bebb2710dd6f7 (patch)
tree30a653c70a5033d79c3674c8467c373ab6f51b71 /lib
parent26900f5f3223f88075533e9a1fbba83fb2c77678 (diff)
downloadotputils-63138267227c09a35cf591b2fe2bebb2710dd6f7.tar.gz
otputils-63138267227c09a35cf591b2fe2bebb2710dd6f7.tar.bz2
otputils-63138267227c09a35cf591b2fe2bebb2710dd6f7.tar.xz
otputils-63138267227c09a35cf591b2fe2bebb2710dd6f7.zip
libotputil: refactoring otputil_otp_decode_hex()
Diffstat (limited to 'lib')
-rw-r--r--lib/libotputil/lrfc2289-otp.c101
1 files changed, 58 insertions, 43 deletions
diff --git a/lib/libotputil/lrfc2289-otp.c b/lib/libotputil/lrfc2289-otp.c
index f18eadd..627ed12 100644
--- a/lib/libotputil/lrfc2289-otp.c
+++ b/lib/libotputil/lrfc2289-otp.c
@@ -60,6 +60,12 @@
60#pragma mark - Prototypes 60#pragma mark - Prototypes
61 61
62static int 62static int
63otputil_otp_decode_hex(
64 const char * src,
65 otputil_bv_t * dst );
66
67
68static int
63otputil_otp_encode_word( 69otputil_otp_encode_word(
64 char * dst, 70 char * dst,
65 int value, 71 int value,
@@ -86,70 +92,79 @@ otputil_otp_decode(
86{ 92{
87 static otputil_bv_t bv; 93 static otputil_bv_t bv;
88 static uint8_t buff[OTPUTIL_MAX_DECODE_SIZE]; 94 static uint8_t buff[OTPUTIL_MAX_DECODE_SIZE];
89 uint8_t * bv_val; 95 int rc;
90 int method;
91 int i;
92 size_t pos;
93 size_t off;
94 size_t len;
95 char word[OTPUTIL_MAX_WORD_SIZE];
96 char * endptr;
97 96
98 assert(src != NULL); 97 assert(src != NULL);
99 98
99 // set default buffer
100 if (!(dst)) 100 if (!(dst))
101 { 101 {
102 dst = &bv; 102 dst = &bv;
103 dst->bv_len = sizeof(buff); 103 dst->bv_len = sizeof(buff);
104 dst->bv_val = buff; 104 dst->bv_val = buff;
105 }; 105 };
106 bv_val = dst->bv_val;
107 106
108 // checks for hexadecimal notation 107 // checks for hexadecimal notation
109 method = OTPUTIL_ENC_HEX; 108 if ((rc = otputil_otp_decode_hex(src, dst)) == 0)
110 word[1] = '\0'; 109 return(dst);
111 for(pos = 0, off = 0; ( ((src[pos])) && (method == OTPUTIL_ENC_HEX) ); pos++) 110 if (rc == -1)
111 return(NULL);
112
113 // checks for six-word format with S/KEY dictionary
114
115 return(NULL);
116}
117
118
119int
120otputil_otp_decode_hex(
121 const char * src,
122 otputil_bv_t * dst )
123{
124 uint8_t * bv_val;
125 int i;
126 size_t count;
127 size_t pos;
128 size_t off;
129 size_t len;
130 char digit[2];
131 char * endptr;
132
133 bv_val = dst->bv_val;
134 digit[1] = '\0';
135
136 // checks for hexadecimal notation
137 for(pos = 0, count = 0; ((src[pos])); pos++)
112 { 138 {
113 if ((isspace(src[pos]))) 139 if ((isspace(src[pos])))
114 continue; 140 continue;
115 if ((isxdigit(src[pos]))) 141 if (!(isxdigit(src[pos])))
116 { 142 return(1);
117 off++; 143 count++;
118 continue;
119 };
120 method = 0;
121 }; 144 };
122 if (method == OTPUTIL_ENC_HEX)
123 {
124 // generate length of binary output in bytes
125 len = (off/2) + (off%2);
126 if ( (len = (off/2) + (off%2)) > dst->bv_len )
127 return(NULL);
128 dst->bv_len = len;
129 145
130 // convert to binary output 146 // generate length of binary output in bytes
131 for(pos = 0, off = 0; ((src[pos])); pos++) 147 if ( (len = (count/2) + (count%2)) > dst->bv_len )
132 { 148 return(-1);
133 word[0] = src[pos]; 149 dst->bv_len = len;
134 i = (int)strtol(word, &endptr, 16);
135 if (endptr == word)
136 continue;
137 if ((off & 0x01) == 0)
138 bv_val[off/2] = (i << 4) & 0xf0;
139 if ((off & 0x01) == 1)
140 bv_val[off/2] |= (i << 0) & 0x0f;
141 off++;
142 };
143 150
144 return(dst); 151 // convert to binary output
152 for(pos = 0, off = 0; ((src[pos])); pos++)
153 {
154 digit[0] = src[pos];
155 i = (int)strtol(digit, &endptr, 16);
156 if (endptr == digit)
157 continue;
158 if ((off & 0x01) == 0)
159 bv_val[off/2] = (i << 4) & 0xf0;
160 if ((off & 0x01) == 1)
161 bv_val[off/2] |= (i << 0) & 0x0f;
162 off++;
145 }; 163 };
146 164
147 // checks for six-word format with S/KEY dictionary 165 return(0);
148
149 return(NULL);
150} 166}
151 167
152
153size_t 168size_t
154otputil_otp_decode_len( 169otputil_otp_decode_len(
155 const char * str ) 170 const char * str )