aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2023-01-10 13:34:31 -0900
committerDavid M. Syzdek <david@syzdek.net>2023-01-10 13:34:31 -0900
commit3a894e610d041f0236b1199b57e602d634c025de (patch)
tree687a27df9b514307b23bfd05685f467ab19132b7
parent1709d84dae4b68d832023b1972060b8c2967e9bf (diff)
downloadotputils-3a894e610d041f0236b1199b57e602d634c025de.tar.gz
otputils-3a894e610d041f0236b1199b57e602d634c025de.tar.bz2
otputils-3a894e610d041f0236b1199b57e602d634c025de.tar.xz
otputils-3a894e610d041f0236b1199b57e602d634c025de.zip
libotputil: refactoring otputil_otp_encode_word()
-rw-r--r--lib/libotputil/lrfc2289-otp.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/lib/libotputil/lrfc2289-otp.c b/lib/libotputil/lrfc2289-otp.c
index d65a348..0469ae5 100644
--- a/lib/libotputil/lrfc2289-otp.c
+++ b/lib/libotputil/lrfc2289-otp.c
@@ -62,7 +62,7 @@
62static int 62static int
63otputil_otp_encode_word( 63otputil_otp_encode_word(
64 char * dst, 64 char * dst,
65 const char * src, 65 int value,
66 size_t dstsize, 66 size_t dstsize,
67 const char * terminator); 67 const char * terminator);
68 68
@@ -239,7 +239,7 @@ otputil_otp_encode(
239 // byte 1: ( 00000000111 11111------ ) 239 // byte 1: ( 00000000111 11111------ )
240 case 8: 240 case 8:
241 val |= (bv_val[pos] >> 5) & 0x0007; 241 val |= (bv_val[pos] >> 5) & 0x0007;
242 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 242 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
243 return(NULL); 243 return(NULL);
244 val = (bv_val[pos] << 6) & 0x7C0; 244 val = (bv_val[pos] << 6) & 0x7C0;
245 break; 245 break;
@@ -247,7 +247,7 @@ otputil_otp_encode(
247 // byte 2: ( 11111222222 22--------- ) 247 // byte 2: ( 11111222222 22--------- )
248 case 5: 248 case 5:
249 val |= (bv_val[pos] >> 2) & 0x003f; 249 val |= (bv_val[pos] >> 2) & 0x003f;
250 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 250 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
251 return(NULL); 251 return(NULL);
252 val = (bv_val[pos] << 9) & 0x07C0; 252 val = (bv_val[pos] << 9) & 0x07C0;
253 break; 253 break;
@@ -260,7 +260,7 @@ otputil_otp_encode(
260 // byte 4: ( 22333333334 4444444---- ) 260 // byte 4: ( 22333333334 4444444---- )
261 case 10: 261 case 10:
262 val |= (bv_val[pos] >> 7) & 0x0001; 262 val |= (bv_val[pos] >> 7) & 0x0001;
263 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 263 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
264 return(NULL); 264 return(NULL);
265 val = (bv_val[pos] << 4) & 0x07F0; 265 val = (bv_val[pos] << 4) & 0x07F0;
266 break; 266 break;
@@ -268,7 +268,7 @@ otputil_otp_encode(
268 // byte 5: ( 44444445555 5555------- ) 268 // byte 5: ( 44444445555 5555------- )
269 case 7: 269 case 7:
270 val |= (bv_val[pos] >> 4) & 0x000f; 270 val |= (bv_val[pos] >> 4) & 0x000f;
271 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 271 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
272 return(NULL); 272 return(NULL);
273 val = (bv_val[pos] << 7) & 0x0780; 273 val = (bv_val[pos] << 7) & 0x0780;
274 break; 274 break;
@@ -276,7 +276,7 @@ otputil_otp_encode(
276 // byte 6: ( 55556666666 6---------- ) 276 // byte 6: ( 55556666666 6---------- )
277 case 4: 277 case 4:
278 val |= (bv_val[pos] >> 1) & 0x007f; 278 val |= (bv_val[pos] >> 1) & 0x007f;
279 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 279 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
280 return(NULL); 280 return(NULL);
281 val = (bv_val[pos] << 10) & 0x0400; 281 val = (bv_val[pos] << 10) & 0x0400;
282 break; 282 break;
@@ -289,7 +289,7 @@ otputil_otp_encode(
289 // byte 8: ( 67777777788 888888----- ) 289 // byte 8: ( 67777777788 888888----- )
290 case 9: 290 case 9:
291 val |= (bv_val[pos] >> 6) & 0x0003; 291 val |= (bv_val[pos] >> 6) & 0x0003;
292 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 292 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
293 return(NULL); 293 return(NULL);
294 val = (bv_val[pos] << 5) & 0x07e0; 294 val = (bv_val[pos] << 5) & 0x07e0;
295 break; 295 break;
@@ -297,7 +297,7 @@ otputil_otp_encode(
297 // byte 9: ( 88888899999 999-------- ) 297 // byte 9: ( 88888899999 999-------- )
298 case 6: 298 case 6:
299 val |= (bv_val[pos] >> 3) & 0x001f; 299 val |= (bv_val[pos] >> 3) & 0x001f;
300 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 300 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
301 return(NULL); 301 return(NULL);
302 val = (bv_val[pos] << 8) & 0x0700; 302 val = (bv_val[pos] << 8) & 0x0700;
303 break; 303 break;
@@ -305,7 +305,7 @@ otputil_otp_encode(
305 // byte 10: ( 999aaaaaaaa ----------- ) 305 // byte 10: ( 999aaaaaaaa ----------- )
306 case 3: 306 case 3:
307 val |= bv_val[pos] & 0x00ff; 307 val |= bv_val[pos] & 0x00ff;
308 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 308 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
309 return(NULL); 309 return(NULL);
310 break; 310 break;
311 311
@@ -340,7 +340,7 @@ otputil_otp_encode(
340 // byte 4: ( 2233333333x x---------- ) 340 // byte 4: ( 2233333333x x---------- )
341 case 10: 341 case 10:
342 val |= (checksum >> 1) & 0x0001; 342 val |= (checksum >> 1) & 0x0001;
343 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, " ") == -1) 343 if (otputil_otp_encode_word(dst, val, dstlen, " ") == -1)
344 return(NULL); 344 return(NULL);
345 val = (checksum << 10) & 0x0400; 345 val = (checksum << 10) & 0x0400;
346 break; 346 break;
@@ -378,7 +378,7 @@ otputil_otp_encode(
378 default: 378 default:
379 break; 379 break;
380 }; 380 };
381 if (otputil_otp_encode_word(dst, otputil_skey_rfc1760_dict[val], dstlen, NULL) == -1) 381 if (otputil_otp_encode_word(dst, val, dstlen, NULL) == -1)
382 return(NULL); 382 return(NULL);
383 383
384 return(dst); 384 return(dst);
@@ -388,11 +388,14 @@ otputil_otp_encode(
388int 388int
389otputil_otp_encode_word( 389otputil_otp_encode_word(
390 char * dst, 390 char * dst,
391 const char * src, 391 int value,
392 size_t dstsize, 392 size_t dstsize,
393 const char * terminator) 393 const char * terminator)
394{ 394{
395 if (bindle_strlcat(dst, src, dstsize) >= (dstsize-1)) 395 const char * word;
396 if ((word = otputil_skey_dict_word(value)) == NULL)
397 return(-1);
398 if (bindle_strlcat(dst, word, dstsize) >= (dstsize-1))
396 return(-1); 399 return(-1);
397 if ((terminator)) 400 if ((terminator))
398 if (bindle_strlcat(dst, terminator, dstsize) >= (dstsize-1)) 401 if (bindle_strlcat(dst, terminator, dstsize) >= (dstsize-1))