1#!/usr/bin/python3 2 3# This script converts the testvectors form a JSON format to C source and header file. 4 5import re 6import json 7import binascii 8 9in_out_path = "../test_vectors/" 10in_file = "edhoc_test_vectors_p256_v16.json" 11out_file = "edhoc_test_vectors_p256_v16.h" 12 13 14HEADER = "/*This is an automatically generated file!*/\n\n" 15INCLUDES = '#include "edhoc.h"\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n\n' 16STRUCT_DEF = "\n\ 17struct test_vector {\n\ 18 const uint8_t *method;\n\ 19 uint32_t method_len;\n\ 20 \n\ 21 const uint8_t *SUITES_I;\n\ 22 uint32_t SUITES_I_len;\n\ 23 \n\ 24 const uint8_t *SUITES_R;\n\ 25 uint32_t SUITES_R_len;\n\ 26 \n\ 27 const uint8_t *x_raw;\n\ 28 uint32_t x_raw_len;\n\ 29 \n\ 30 const uint8_t *g_x_raw;\n\ 31 uint32_t g_x_raw_len;\n\ 32 \n\ 33 const uint8_t *g_x;\n\ 34 uint32_t g_x_len;\n\ 35 \n\ 36 const uint8_t *c_i;\n\ 37 uint32_t c_i_len;\n\ 38 \n\ 39 const uint8_t *ead_1;\n\ 40 uint32_t ead_1_len;\n\ 41 \n\ 42 const uint8_t *message_1;\n\ 43 uint32_t message_1_len;\n\ 44 \n\ 45 const uint8_t *y_raw;\n\ 46 uint32_t y_raw_len;\n\ 47 \n\ 48 const uint8_t *g_y_raw;\n\ 49 uint32_t g_y_raw_len;\n\ 50 \n\ 51 const uint8_t *g_y;\n\ 52 uint32_t g_y_len;\n\ 53 \n\ 54 const uint8_t *g_xy_raw;\n\ 55 uint32_t g_xy_raw_len;\n\ 56 \n\ 57 const uint8_t *salt_raw;\n\ 58 uint32_t salt_raw_len;\n\ 59 \n\ 60 const uint8_t *prk_2e_raw;\n\ 61 uint32_t prk_2e_raw_len;\n\ 62 \n\ 63 const uint8_t *sk_r_raw;\n\ 64 uint32_t sk_r_raw_len;\n\ 65 \n\ 66 const uint8_t *pk_r_raw;\n\ 67 uint32_t pk_r_raw_len;\n\ 68 \n\ 69 const uint8_t *prk_3e2m_raw;\n\ 70 uint32_t prk_3e2m_raw_len;\n\ 71 \n\ 72 const uint8_t *c_r;\n\ 73 uint32_t c_r_len;\n\ 74 \n\ 75 const uint8_t *h_message_1_raw;\n\ 76 uint32_t h_message_1_raw_len;\n\ 77 \n\ 78 const uint8_t *h_message_1;\n\ 79 uint32_t h_message_1_len;\n\ 80 \n\ 81 const uint8_t *input_th_2;\n\ 82 uint32_t input_th_2_len;\n\ 83 \n\ 84 const uint8_t *th_2_raw;\n\ 85 uint32_t th_2_raw_len;\n\ 86 \n\ 87 const uint8_t *th_2;\n\ 88 uint32_t th_2_len;\n\ 89 \n\ 90 const uint8_t *id_cred_r;\n\ 91 uint32_t id_cred_r_len;\n\ 92 \n\ 93 const uint8_t *cred_r;\n\ 94 uint32_t cred_r_len;\n\ 95 \n\ 96 const uint8_t *ead_2;\n\ 97 uint32_t ead_2_len;\n\ 98 \n\ 99 const uint8_t *info_mac_2;\n\ 100 uint32_t info_mac_2_len;\n\ 101 \n\ 102 const uint8_t *mac_2_raw;\n\ 103 uint32_t mac_2_raw_len;\n\ 104 \n\ 105 const uint8_t *mac_2;\n\ 106 uint32_t mac_2_len;\n\ 107 \n\ 108 const uint8_t *m_2;\n\ 109 uint32_t m_2_len;\n\ 110 \n\ 111 const uint8_t *sig_or_mac_2_raw;\n\ 112 uint32_t sig_or_mac_2_raw_len;\n\ 113 \n\ 114 const uint8_t *sig_or_mac_2;\n\ 115 uint32_t sig_or_mac_2_len;\n\ 116 \n\ 117 const uint8_t *plaintext_2;\n\ 118 uint32_t plaintext_2_len;\n\ 119 \n\ 120 const uint8_t *info_keystream_2;\n\ 121 uint32_t info_keystream_2_len;\n\ 122 \n\ 123 const uint8_t *keystream_2_raw;\n\ 124 uint32_t keystream_2_raw_len;\n\ 125 \n\ 126 const uint8_t *ciphertext_2_raw;\n\ 127 uint32_t ciphertext_2_raw_len;\n\ 128 \n\ 129 const uint8_t *ciphertext_2;\n\ 130 uint32_t ciphertext_2_len;\n\ 131 \n\ 132 const uint8_t *message_2;\n\ 133 uint32_t message_2_len;\n\ 134 \n\ 135 const uint8_t *sk_i_raw;\n\ 136 uint32_t sk_i_raw_len;\n\ 137 \n\ 138 const uint8_t *pk_i_raw;\n\ 139 uint32_t pk_i_raw_len;\n\ 140 \n\ 141 const uint8_t *prk_4x3m_raw;\n\ 142 uint32_t prk_4x3m_raw_len;\n\ 143 \n\ 144 const uint8_t *input_TH_3;\n\ 145 uint32_t input_TH_3_len;\n\ 146 \n\ 147 const uint8_t *th_3_raw;\n\ 148 uint32_t th_3_raw_len;\n\ 149 \n\ 150 const uint8_t *th_3;\n\ 151 uint32_t th_3_len;\n\ 152 \n\ 153 const uint8_t *id_cred_i;\n\ 154 uint32_t id_cred_i_len;\n\ 155 \n\ 156 const uint8_t *cred_i;\n\ 157 uint32_t cred_i_len;\n\ 158 \n\ 159 const uint8_t *ead_3;\n\ 160 uint32_t ead_3_len;\n\ 161 \n\ 162 const uint8_t *info_mac_3;\n\ 163 uint32_t info_mac_3_len;\n\ 164 \n\ 165 const uint8_t *mac_3_raw;\n\ 166 uint32_t mac_3_raw_len;\n\ 167 \n\ 168 const uint8_t *mac_3;\n\ 169 uint32_t mac_3_len;\n\ 170 \n\ 171 const uint8_t *m_3;\n\ 172 uint32_t m_3_len;\n\ 173 \n\ 174 const uint8_t *sig_or_mac_3_raw;\n\ 175 uint32_t sig_or_mac_3_raw_len;\n\ 176 \n\ 177 const uint8_t *sig_or_mac_3;\n\ 178 uint32_t sig_or_mac_3_len;\n\ 179 \n\ 180 const uint8_t *p_3;\n\ 181 uint32_t p_3_len;\n\ 182 \n\ 183 const uint8_t *a_3;\n\ 184 uint32_t a_3_len;\n\ 185 \n\ 186 const uint8_t *info_k_3;\n\ 187 uint32_t info_k_3_len;\n\ 188 \n\ 189 const uint8_t *k_3_raw;\n\ 190 uint32_t k_3_raw_len;\n\ 191 \n\ 192 const uint8_t *info_iv_3;\n\ 193 uint32_t info_iv_3_len;\n\ 194 \n\ 195 const uint8_t *iv_3_raw;\n\ 196 uint32_t iv_3_raw_len;\n\ 197 \n\ 198 const uint8_t *ciphertext_3_raw;\n\ 199 uint32_t ciphertext_3_raw_len;\n\ 200 \n\ 201 const uint8_t *ciphertext_3;\n\ 202 uint32_t ciphertext_3_len;\n\ 203 \n\ 204 const uint8_t *message_3;\n\ 205 uint32_t message_3_len;\n\ 206 \n\ 207 const uint8_t *input_th_4;\n\ 208 uint32_t input_th_4_len;\n\ 209 \n\ 210 const uint8_t *th_4_raw;\n\ 211 uint32_t th_4_raw_len;\n\ 212 \n\ 213 const uint8_t *th_4;\n\ 214 uint32_t th_4_len;\n\ 215 \n\ 216 const uint8_t *ead_4;\n\ 217 uint32_t ead_4_len;\n\ 218 \n\ 219 const uint8_t *p_4;\n\ 220 uint32_t p_4_len;\n\ 221 \n\ 222 const uint8_t *a_4;\n\ 223 uint32_t a_4_len;\n\ 224 \n\ 225 const uint8_t *info_k_4;\n\ 226 uint32_t info_k_4_len;\n\ 227 \n\ 228 const uint8_t *k_4_raw;\n\ 229 uint32_t k_4_raw_len;\n\ 230 \n\ 231 const uint8_t *info_iv_4;\n\ 232 uint32_t info_iv_4_len;\n\ 233 \n\ 234 const uint8_t *iv_4_raw;\n\ 235 uint32_t iv_4_raw_len;\n\ 236 \n\ 237 const uint8_t *ciphertext_4_raw;\n\ 238 uint32_t ciphertext_4_raw_len;\n\ 239 \n\ 240 const uint8_t *ciphertext_4;\n\ 241 uint32_t ciphertext_4_len;\n\ 242 \n\ 243 const uint8_t *message_4;\n\ 244 uint32_t message_4_len;\n\ 245 \n\ 246 int32_t *oscore_aead_alg;\n\ 247 \n\ 248 int32_t *oscore_hash_alg;\n\ 249 \n\ 250 const uint8_t *client_sender_id_raw;\n\ 251 uint32_t client_sender_id_raw_len;\n\ 252 \n\ 253 const uint8_t *server_sender_id_raw;\n\ 254 uint32_t server_sender_id_raw_len;\n\ 255 \n\ 256 const uint8_t *info_oscore_secret;\n\ 257 uint32_t info_oscore_secret_len;\n\ 258 \n\ 259 const uint8_t *oscore_secret_raw;\n\ 260 uint32_t oscore_secret_raw_len;\n\ 261 \n\ 262 const uint8_t *info_oscore_salt;\n\ 263 uint32_t info_oscore_salt_len;\n\ 264 \n\ 265 const uint8_t *oscore_salt_raw;\n\ 266 uint32_t oscore_salt_raw_len;\n\ 267 \n\ 268 const uint8_t *key_update_nonce_raw;\n\ 269 uint32_t key_update_nonce_raw_len;\n\ 270 \n\ 271 const uint8_t *prk_4x3m_key_update_raw;\n\ 272 uint32_t prk_4x3m_key_update_raw_len;\n\ 273 \n\ 274 const uint8_t *oscore_secret_key_update_raw;\n\ 275 uint32_t oscore_secret_key_update_raw_len;\n\ 276 \n\ 277 const uint8_t *oscore_salt_key_update_raw;\n\ 278 uint32_t oscore_salt_key_update_raw_len;\n\ 279 \n\ 280 const uint8_t *ca_r;\n\ 281 uint32_t ca_r_len;\n\ 282 \n\ 283 const uint8_t *ca_i;\n\ 284 uint32_t ca_i_len;\n\ 285 \n\ 286 const uint8_t *ca_r_pk;\n\ 287 uint32_t ca_r_pk_len;\n\ 288 \n\ 289 const uint8_t *ca_i_pk;\n\ 290 uint32_t ca_i_pk_len;\n\ 291 \n\ 292 const uint8_t *i_raw;\n\ 293 uint32_t i_raw_len;\n\ 294 \n\ 295 const uint8_t *g_i_raw;\n\ 296 uint32_t g_i_raw_len;\n\ 297 \n\ 298 const uint8_t *r_raw;\n\ 299 uint32_t r_raw_len;\n\ 300 \n\ 301 const uint8_t *g_r_raw;\n\ 302 uint32_t g_r_raw_len;\n\ 303 \n\ 304 const uint8_t *c_i_raw;\n\ 305 uint32_t c_i_raw_len;\n\ 306 int32_t *c_i_raw_int;\n\ 307 \n\ 308 const uint8_t *c_r_raw;\n\ 309 uint32_t c_r_raw_len;\n\ 310 int32_t *c_r_raw_int;\n\ 311};\n" 312 313STRUCT_POPULATE_START = "\n\nstruct test_vector test_vectors[] = {\n" 314STRUCT_POPULATE = "\n\ 315 {\n\ 316 .method = NULL,\n\ 317 .method_len = 0,\n\ 318 \n\ 319 .SUITES_I = NULL,\n\ 320 .SUITES_I_len = 0,\n\ 321 \n\ 322 .SUITES_R = NULL,\n\ 323 .SUITES_R_len = 0,\n\ 324 \n\ 325 .x_raw = NULL,\n\ 326 .x_raw_len = 0,\n\ 327 \n\ 328 .g_x_raw = NULL,\n\ 329 .g_x_raw_len = 0,\n\ 330 \n\ 331 .g_x = NULL,\n\ 332 .g_x_len = 0,\n\ 333 \n\ 334 .c_i = NULL,\n\ 335 .c_i_len = 0,\n\ 336 \n\ 337 .ead_1 = NULL,\n\ 338 .ead_1_len = 0,\n\ 339 \n\ 340 .message_1 = NULL,\n\ 341 .message_1_len = 0,\n\ 342 \n\ 343 .y_raw = NULL,\n\ 344 .y_raw_len = 0,\n\ 345 \n\ 346 .g_y_raw = NULL,\n\ 347 .g_y_raw_len = 0,\n\ 348 \n\ 349 .g_y = NULL,\n\ 350 .g_y_len = 0,\n\ 351 \n\ 352 .g_xy_raw = NULL,\n\ 353 .g_xy_raw_len = 0,\n\ 354 \n\ 355 .salt_raw = NULL,\n\ 356 .salt_raw_len = 0,\n\ 357 \n\ 358 .prk_2e_raw = NULL,\n\ 359 .prk_2e_raw_len = 0,\n\ 360 \n\ 361 .sk_r_raw = NULL,\n\ 362 .sk_r_raw_len = 0,\n\ 363 \n\ 364 .pk_r_raw = NULL,\n\ 365 .pk_r_raw_len = 0,\n\ 366 \n\ 367 .prk_3e2m_raw = NULL,\n\ 368 .prk_3e2m_raw_len = 0,\n\ 369 \n\ 370 .c_r = NULL,\n\ 371 .c_r_len = 0,\n\ 372 \n\ 373 .h_message_1_raw = NULL,\n\ 374 .h_message_1_raw_len = 0,\n\ 375 \n\ 376 .h_message_1 = NULL,\n\ 377 .h_message_1_len = 0,\n\ 378 \n\ 379 .input_th_2 = NULL,\n\ 380 .input_th_2_len = 0,\n\ 381 \n\ 382 .th_2_raw = NULL,\n\ 383 .th_2_raw_len = 0,\n\ 384 \n\ 385 .th_2 = NULL,\n\ 386 .th_2_len = 0,\n\ 387 \n\ 388 .id_cred_r = NULL,\n\ 389 .id_cred_r_len = 0,\n\ 390 \n\ 391 .cred_r = NULL,\n\ 392 .cred_r_len = 0,\n\ 393 \n\ 394 .ead_2 = NULL,\n\ 395 .ead_2_len = 0,\n\ 396 \n\ 397 .info_mac_2 = NULL,\n\ 398 .info_mac_2_len = 0,\n\ 399 \n\ 400 .mac_2_raw = NULL,\n\ 401 .mac_2_raw_len = 0,\n\ 402 \n\ 403 .mac_2 = NULL,\n\ 404 .mac_2_len = 0,\n\ 405 \n\ 406 .m_2 = NULL,\n\ 407 .m_2_len = 0,\n\ 408 \n\ 409 .sig_or_mac_2_raw = NULL,\n\ 410 .sig_or_mac_2_raw_len = 0,\n\ 411 \n\ 412 .sig_or_mac_2 = NULL,\n\ 413 .sig_or_mac_2_len = 0,\n\ 414 \n\ 415 .plaintext_2 = NULL,\n\ 416 .plaintext_2_len = 0,\n\ 417 \n\ 418 .info_keystream_2 = NULL,\n\ 419 .info_keystream_2_len = 0,\n\ 420 \n\ 421 .keystream_2_raw = NULL,\n\ 422 .keystream_2_raw_len = 0,\n\ 423 \n\ 424 .ciphertext_2_raw = NULL,\n\ 425 .ciphertext_2_raw_len = 0,\n\ 426 \n\ 427 .ciphertext_2 = NULL,\n\ 428 .ciphertext_2_len = 0,\n\ 429 \n\ 430 .message_2 = NULL,\n\ 431 .message_2_len = 0,\n\ 432 \n\ 433 .sk_i_raw = NULL,\n\ 434 .sk_i_raw_len = 0,\n\ 435 \n\ 436 .pk_i_raw = NULL,\n\ 437 .pk_i_raw_len = 0,\n\ 438 \n\ 439 .prk_4x3m_raw = NULL,\n\ 440 .prk_4x3m_raw_len = 0,\n\ 441 \n\ 442 .input_TH_3 = NULL,\n\ 443 .input_TH_3_len = 0,\n\ 444 \n\ 445 .th_3_raw = NULL,\n\ 446 .th_3_raw_len = 0,\n\ 447 \n\ 448 .th_3 = NULL,\n\ 449 .th_3_len = 0,\n\ 450 \n\ 451 .id_cred_i = NULL,\n\ 452 .id_cred_i_len = 0,\n\ 453 \n\ 454 .cred_i = NULL,\n\ 455 .cred_i_len = 0,\n\ 456 \n\ 457 .ead_3 = NULL,\n\ 458 .ead_3_len = 0,\n\ 459 \n\ 460 .info_mac_3 = NULL,\n\ 461 .info_mac_3_len = 0,\n\ 462 \n\ 463 .mac_3_raw = NULL,\n\ 464 .mac_3_raw_len = 0,\n\ 465 \n\ 466 .mac_3 = NULL,\n\ 467 .mac_3_len = 0,\n\ 468 \n\ 469 .m_3 = NULL,\n\ 470 .m_3_len = 0,\n\ 471 \n\ 472 .sig_or_mac_3_raw = NULL,\n\ 473 .sig_or_mac_3_raw_len = 0,\n\ 474 \n\ 475 .sig_or_mac_3 = NULL,\n\ 476 .sig_or_mac_3_len = 0,\n\ 477 \n\ 478 .p_3 = NULL,\n\ 479 .p_3_len = 0,\n\ 480 \n\ 481 .a_3 = NULL,\n\ 482 .a_3_len = 0,\n\ 483 \n\ 484 .info_k_3 = NULL,\n\ 485 .info_k_3_len = 0,\n\ 486 \n\ 487 .k_3_raw = NULL,\n\ 488 .k_3_raw_len = 0,\n\ 489 \n\ 490 .info_iv_3 = NULL,\n\ 491 .info_iv_3_len = 0,\n\ 492 \n\ 493 .iv_3_raw = NULL,\n\ 494 .iv_3_raw_len = 0,\n\ 495 \n\ 496 .ciphertext_3_raw = NULL,\n\ 497 .ciphertext_3_raw_len = 0,\n\ 498 \n\ 499 .ciphertext_3 = NULL,\n\ 500 .ciphertext_3_len = 0,\n\ 501 \n\ 502 .message_3 = NULL,\n\ 503 .message_3_len = 0,\n\ 504 \n\ 505 .input_th_4 = NULL,\n\ 506 .input_th_4_len = 0,\n\ 507 \n\ 508 .th_4_raw = NULL,\n\ 509 .th_4_raw_len = 0,\n\ 510 \n\ 511 .th_4 = NULL,\n\ 512 .th_4_len = 0,\n\ 513 \n\ 514 .ead_4 = NULL,\n\ 515 .ead_4_len = 0,\n\ 516 \n\ 517 .p_4 = NULL,\n\ 518 .p_4_len = 0,\n\ 519 \n\ 520 .a_4 = NULL,\n\ 521 .a_4_len = 0,\n\ 522 \n\ 523 .info_k_4 = NULL,\n\ 524 .info_k_4_len = 0,\n\ 525 \n\ 526 .k_4_raw = NULL,\n\ 527 .k_4_raw_len = 0,\n\ 528 \n\ 529 .info_iv_4 = NULL,\n\ 530 .info_iv_4_len = 0,\n\ 531 \n\ 532 .iv_4_raw = NULL,\n\ 533 .iv_4_raw_len = 0,\n\ 534 \n\ 535 .ciphertext_4_raw = NULL,\n\ 536 .ciphertext_4_raw_len = 0,\n\ 537 \n\ 538 .ciphertext_4 = NULL,\n\ 539 .ciphertext_4_len = 0,\n\ 540 \n\ 541 .message_4 = NULL,\n\ 542 .message_4_len = 0,\n\ 543 \n\ 544 .oscore_aead_alg = NULL,\n\ 545 \n\ 546 .oscore_hash_alg = NULL,\n\ 547 \n\ 548 .client_sender_id_raw = NULL,\n\ 549 .client_sender_id_raw_len = 0,\n\ 550 \n\ 551 .server_sender_id_raw = NULL,\n\ 552 .server_sender_id_raw_len = 0,\n\ 553 \n\ 554 .info_oscore_secret = NULL,\n\ 555 .info_oscore_secret_len = 0,\n\ 556 \n\ 557 .oscore_secret_raw = NULL,\n\ 558 .oscore_secret_raw_len = 0,\n\ 559 \n\ 560 .info_oscore_salt = NULL,\n\ 561 .info_oscore_salt_len = 0,\n\ 562 \n\ 563 .oscore_salt_raw = NULL,\n\ 564 .oscore_salt_raw_len = 0,\n\ 565 \n\ 566 .key_update_nonce_raw = NULL,\n\ 567 .key_update_nonce_raw_len = 0,\n\ 568 \n\ 569 .prk_4x3m_key_update_raw = NULL,\n\ 570 .prk_4x3m_key_update_raw_len = 0,\n\ 571 \n\ 572 .oscore_secret_key_update_raw = NULL,\n\ 573 .oscore_secret_key_update_raw_len = 0,\n\ 574 \n\ 575 .oscore_salt_key_update_raw = NULL,\n\ 576 .oscore_salt_key_update_raw_len = 0,\n\ 577 \n\ 578 .ca_r = NULL,\n\ 579 .ca_r_len = 0,\n\ 580 \n\ 581 .ca_i = NULL,\n\ 582 .ca_i_len = 0,\n\ 583 \n\ 584 .ca_r_pk = NULL,\n\ 585 .ca_r_pk_len = 0,\n\ 586 \n\ 587 .ca_i_pk = NULL,\n\ 588 .ca_i_pk_len = 0,\n\ 589 \n\ 590 .i_raw = NULL,\n\ 591 .i_raw_len = 0,\n\ 592 \n\ 593 .g_i_raw = NULL,\n\ 594 .g_i_raw_len = 0,\n\ 595 \n\ 596 .r_raw = NULL,\n\ 597 .r_raw_len = 0,\n\ 598 \n\ 599 .g_r_raw = NULL,\n\ 600 .g_r_raw_len = 0,\n\ 601 \n\ 602 .c_i_raw = NULL,\n\ 603 .c_i_raw_len = 0,\n\ 604 .c_i_raw_int = NULL,\n\ 605 \n\ 606 .c_r_raw = NULL,\n\ 607 .c_r_raw_len = 0,\n\ 608 .c_r_raw_int = NULL,\n\ 609 },\n\ 610" 611STRUCT_POPULATE_END = "};" 612 613 614def print_c_array(in_string): 615 616 cnt = 1 617 if type(in_string) is str: 618 out_string = "[] = {\n\t" 619 for i in range(0, len(in_string), 2): 620 out_string = out_string + "0x" + in_string[i] + in_string[i + 1] 621 622 if i != len(in_string) - 2: 623 out_string = out_string + ", " 624 if cnt == 8: 625 out_string = out_string + "\n\t" 626 cnt = 0 627 else: 628 out_string = out_string + "};\n" 629 cnt = cnt + 1 630 631 if len(in_string) == 0: 632 out_string = out_string + "};\n" 633 634 if type(in_string) is int: 635 out_string = " = " + str(in_string) + ";\n" 636 637 # print(out_string) 638 return out_string 639 640 641def get_len_str(i, data): 642 if type(data["test_vector_1"][i]) is str: 643 return str(len(data["test_vector_1"][i]) >> 1) 644 else: 645 return "1" 646 647 648def populate_struct_body(input, val="dummy", key="dummy", array_name="dummy"): 649 650 pattern_str_len = " ." + key + "_len = 0" 651 repl_str_len = " ." + key + "_len = sizeof(" + array_name + ")" 652 pattern_str_val = " ." + key + " = NULL" 653 repl_str_val = " ." + key + " = " + array_name 654 655 pattern_int_val = " ." + key + " = NULL" 656 repl_int_val = " ." + key + " = &" + array_name 657 658 pattern_c_x_int_val = " ." + key + "_int = NULL" 659 # repl_c_x_str_val = " ." + key + \ 660 # " = {BSTR, {sizeof(" + array_name + "), " + array_name + "}}" 661 repl_c_x_int_val = " ." + key + "_int" + " = &" + array_name 662 663 # if (key == "c_r_raw") or (key == "c_i_raw"): 664 # if isinstance(val, str): 665 # return re.sub(pattern_c_x_str_val, repl_c_x_str_val, input) 666 # if isinstance(val, int): 667 # return re.sub(pattern_c_x_str_val, repl_c_x_int_val, input) 668 669 if isinstance(val, str): 670 changed_len = re.sub(pattern_str_len, repl_str_len, input) 671 changed_val_len = re.sub(pattern_str_val, repl_str_val, changed_len) 672 return changed_val_len 673 674 if isinstance(val, int): 675 if (key == "c_r_raw") or (key == "c_i_raw"): 676 return re.sub(pattern_c_x_int_val, repl_c_x_int_val, input) 677 changed_val_len = re.sub(pattern_int_val, repl_int_val, input) 678 return changed_val_len 679 680 681def main(): 682 with open(in_out_path + in_file, "r") as in_fp: 683 data = json.load(in_fp) 684 685 data_arrays = "\n" 686 struct_body_out = "\n" 687 688 test_vector_labels = list(data.keys()) 689 # print(test_vector_labels[0]) 690 vec_num = test_vector_labels[0] 691 692 for vec_num in test_vector_labels: 693 data_arrays += "\n/***************** " + vec_num + " ************/\n\n " 694 test_vector_elements = data[vec_num] 695 # print(test_vector_elements.keys()) 696 # print(test_vector_elements.values()) 697 698 struct_body = populate_struct_body(STRUCT_POPULATE) 699 for key in test_vector_elements.keys(): 700 # print(key, test_vector_elements[key]) 701 array_name = vec_num + "_" + key 702 type = test_vector_elements[key] 703 704 # if the value of an element is a int save it as int_32 705 if isinstance(type, int): 706 data_arrays = ( 707 data_arrays 708 + "int32_t " 709 + array_name 710 + print_c_array(test_vector_elements[key]) 711 ) 712 713 # if the value of an element is a string save it in uint8_t array 714 if isinstance(type, str): 715 data_arrays = ( 716 data_arrays 717 + "static const uint8_t " 718 + array_name 719 + print_c_array(test_vector_elements[key]) 720 ) 721 722 struct_body = populate_struct_body(struct_body, type, key, array_name) 723 724 struct_body_out += "/*--------------- " + vec_num + " ------------*/\n " 725 struct_body_out += struct_body 726 727 output = ( 728 HEADER 729 + INCLUDES 730 + STRUCT_DEF 731 + data_arrays 732 + STRUCT_POPULATE_START 733 + struct_body_out 734 + STRUCT_POPULATE_END 735 ) 736 with open(in_out_path + out_file, "w") as out_fp: 737 out_fp.write(output) 738 739 740if __name__ == "__main__": 741 main() 742