@@ -426,6 +426,37 @@ static int32_t __foreach_handler_getModuleNum(Arg* argEach,
426426 return 0 ;
427427}
428428
429+ #include <stdint.h>
430+ #include <stdio.h>
431+
432+ /* Times33 checksum calculation from a specific offset in the file */
433+ uint32_t file_calculate_times33 (FILE * fp , uint32_t offset ) {
434+ uint32_t checksum = 5381 ; // Times33 uses 5381 as the initial value
435+ uint8_t buffer [64 ]; // 64-byte buffer for reading
436+
437+ // Seek to the specified offset in the file
438+ pika_platform_fseek (fp , offset , SEEK_SET );
439+
440+ size_t bytes_read ;
441+ // Read the file chunk by chunk and update the checksum
442+ while ((bytes_read = pika_platform_fread (buffer , 1 , sizeof (buffer ), fp )) >
443+ 0 ) {
444+ for (size_t i = 0 ; i < bytes_read ; i ++ ) {
445+ checksum = (checksum * 33 ) + buffer [i ];
446+ }
447+ }
448+
449+ return checksum ;
450+ }
451+
452+ uint32_t bytes_caclulate_times33 (uint8_t * bytes , uint32_t size ) {
453+ uint32_t checksum = 5381 ; // Times33 uses 5381 as the initial value
454+ for (uint32_t i = 0 ; i < size ; i ++ ) {
455+ checksum = (checksum * 33 ) + bytes [i ];
456+ }
457+ return checksum ;
458+ }
459+
429460int LibObj_linkFile (LibObj * self , char * output_file_name ) {
430461 FILE * out_file = pika_platform_fopen (output_file_name , "wb+" );
431462
@@ -451,22 +482,23 @@ int LibObj_linkFile(LibObj* self, char* output_file_name) {
451482 uint32_t module_num = linker .module_num ;
452483 uint32_t modules_size = linker .sum_size ;
453484 uint32_t block_size = linker .block_size ;
454- uint32_t totle_size = block_size * (module_num + 1 ) + modules_size ;
485+ uint32_t total_size = block_size * (module_num + 1 ) + modules_size ;
455486 uint8_t * meta_block_buff = pikaMalloc (block_size );
456487 pika_platform_memset (meta_block_buff , 0 , block_size );
457488
458489 /* write meta info */
459490 const uint32_t magic_code_offset =
460491 sizeof (uint32_t ) * PIKA_APP_MAGIC_CODE_OFFSET ;
461- const uint32_t totle_size_offset =
492+ const uint32_t total_size_offset =
462493 sizeof (uint32_t ) * PIKA_APP_MODULE_SIZE_OFFSET ;
463494 const uint32_t version_offset = sizeof (uint32_t ) * PIKA_APP_VERSION_OFFSET ;
464495 const uint32_t module_num_offset =
465496 sizeof (uint32_t ) * PIKA_APP_MODULE_NUM_OFFSET ;
466497 const uint32_t info_block_size_offset =
467498 sizeof (uint32_t ) * PIKA_APP_INFO_BLOCK_SIZE_OFFSET ;
499+ const uint32_t times33_offset = sizeof (uint32_t ) * PIKA_APP_TIMES33_OFFSET ;
468500
469- // the meta info is the first block
501+ /* the meta info is the first block */
470502 pika_platform_memcpy (meta_block_buff + magic_code_offset , & magic_code ,
471503 sizeof (uint32_t ));
472504 pika_platform_memcpy (meta_block_buff + version_offset , & version_num ,
@@ -475,23 +507,33 @@ int LibObj_linkFile(LibObj* self, char* output_file_name) {
475507 pika_platform_memcpy (meta_block_buff + module_num_offset , & module_num ,
476508 sizeof (uint32_t ));
477509 /* write modules_size to the file */
478- pika_platform_memcpy (meta_block_buff + totle_size_offset , & totle_size ,
510+ pika_platform_memcpy (meta_block_buff + total_size_offset , & total_size ,
479511 sizeof (uint32_t ));
480512 /* write block_size to the file */
481513 pika_platform_memcpy (meta_block_buff + info_block_size_offset , & block_size ,
482514 sizeof (uint32_t ));
483515 linker_fwrite (& linker , meta_block_buff , block_size );
516+
484517 /* write module index to file */
485518 args_foreach (self -> list , (fn_args_foreach )__foreach_handler_libWriteIndex ,
486519 & linker );
487520 /* write module bytecode to file */
488521 args_foreach (self -> list ,
489522 (fn_args_foreach )__foreach_handler_libWriteBytecode , & linker );
523+
524+ /* Calculate CRC16 from block_size to the end of the file using the new
525+ * function */
526+ uint32_t ftimes33 = file_calculate_times33 (out_file , block_size );
527+
528+ /* Move back to times33_offset position and write the CRC16 value */
529+ pika_platform_fseek (out_file , times33_offset , SEEK_SET );
530+ pika_platform_fwrite (& ftimes33 , sizeof (uint32_t ), 1 , out_file );
531+
490532 /* main process */
491533 /* deinit */
492534 pika_platform_fclose (out_file );
493535 pikaFree (meta_block_buff , block_size );
494- pika_assert (totle_size == linker .written_size );
536+ pika_assert (total_size == linker .written_size );
495537 return 0 ;
496538}
497539
@@ -505,7 +547,9 @@ static int _getModuleNum(uint8_t* library_bytes) {
505547 uint32_t * library_info = (uint32_t * )library_bytes ;
506548 uint32_t version_num = library_info [PIKA_APP_VERSION_OFFSET ];
507549 uint32_t module_num = library_info [PIKA_APP_MODULE_NUM_OFFSET ];
508-
550+ uint32_t library_times33_saved = library_info [PIKA_APP_TIMES33_OFFSET ];
551+ uint32_t total_size = library_info [PIKA_APP_MODULE_SIZE_OFFSET ];
552+ uint32_t info_block_size = library_info [PIKA_APP_INFO_BLOCK_SIZE_OFFSET ];
509553 /* check magic_code */
510554 if (!((magic_code [0 ] == 0x0f ) && (magic_code [1 ] == 'p' ) &&
511555 (magic_code [2 ] == 'y' ) && (magic_code [3 ] == 'a' ))) {
@@ -522,6 +566,19 @@ static int _getModuleNum(uint8_t* library_bytes) {
522566 "version of compiled library.\r\n" );
523567 return PIKA_RES_ERR_INVALID_VERSION_NUMBER ;
524568 }
569+
570+ /* check times33 */
571+ if (0 != library_times33_saved ) {
572+ // support the old version without times33
573+ uint32_t library_times34_calculated = bytes_caclulate_times33 (
574+ library_bytes + info_block_size , total_size - info_block_size );
575+ if (library_times33_saved != library_times34_calculated ) {
576+ pika_platform_printf (
577+ "Error: invalid checksum, the package is broken.\r\n" ,
578+ library_times34_calculated , library_times33_saved );
579+ return PIKA_RES_ERR_INVALID_PARAM ;
580+ }
581+ }
525582 return module_num ;
526583}
527584
0 commit comments