File optimization method, file optimization system and electronic equipment

By optimizing and encapsulating the binary files of smart card applications, the problems of large size and slow loading speed on smart cards are solved, and more efficient file loading and execution are achieved.

CN119127816APending Publication Date: 2024-12-13GUANGDONG CHUTIAN DRAGON SMART CARD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411246858.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-06
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

The binary files running on smart cards are large in size and complex in structure, resulting in high storage space occupation and slow loading speed.

Method used

By obtaining the binary file of the target smart card application, it determines whether it complies with the preset format specification, divide it into initial binary data segments, and optimizes and encapsulates the data based on the preset optimization strategy, generates the optimized binary file and stores it in the smart card In nonvolatile memory.

Benefits of technology

Reduces binary file size, improves the loading speed and execution efficiency of smart card applications, and reduces the computing resource requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127816A_ABST
    Figure CN119127816A_ABST
Patent Text Reader

Abstract

The invention provides a file optimization method, a file optimization system and electronic equipment, and the method comprises the steps: obtaining a binary file corresponding to a target smart card application program, and judging whether the binary file accords with a preset format specification or not; if the binary file conforms to the preset format specification, dividing the binary file into at least one initial binary data segment based on the preset format specification; based on a preset optimization strategy, to-be-optimized data corresponding to the initial binary data segment is determined, the to-be-optimized data is optimized, an optimized target binary data segment is obtained, and the data size of the target binary data segment is smaller than that of the initial binary data segment; and packaging the obtained at least one target binary data segment based on a preset packaging strategy to obtain an optimized binary file. According to the method, the binary file which is large in size and complex in structure can be optimized, so that the size of the binary file is reduced, and the loading speed and the execution efficiency of the application program of the intelligent card are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a file optimization method, a file optimization system and an electronic device. Background Art

[0002] With the popularity of the Internet of Things and mobile payments, smart cards have been widely used in secure payment, identity authentication, access control and other fields. In order to improve the execution efficiency of applications on smart cards, Web-oriented binary formats, as a lightweight binary format, are gradually being used in smart cards. However, binary files are large in size and complex in structure, resulting in problems such as high storage space usage and slow loading speed when running on resource-constrained smart cards. Summary of the invention

[0003] In view of this, the purpose of the present invention is to provide a file optimization method, a file optimization system and an electronic device, which can optimize binary files with large volume and complex structure, thereby reducing the size of the binary file and further improving the loading speed and execution efficiency of the smart card application.

[0004] In a first aspect, an embodiment of the present invention provides a file optimization method, which is applied to an electronic device, and the method includes: obtaining a binary file corresponding to a target smart card application, and determining whether the binary file conforms to a preset format specification; if the binary file conforms to the preset format specification, dividing the binary file into at least one initial binary data segment based on the preset format specification; based on a preset optimization strategy, determining the data to be optimized corresponding to the initial binary data segment, and optimizing the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is less than the data volume of the initial binary data segment; based on a preset encapsulation strategy, encapsulating the obtained at least one target binary data segment to obtain an optimized binary file.

[0005] Furthermore, the electronic device is communicatively connected to the smart card; the method also includes: storing the optimized binary file in the non-volatile memory of the smart card, so that the smart card executes the target smart card application by calling the optimized binary file.

[0006] Furthermore, the preset format specification includes a preset file data structure; the step of dividing the binary file into at least one initial binary data segment based on the preset format specification includes: determining the data segment identifier and data segment length corresponding to the initial binary data segment to be divided based on the file data structure; dividing the binary file into at least one initial binary data segment based on the data segment identifier and the data segment length; wherein the data segment identifier includes a type segment identifier, an import segment identifier, a function segment identifier, a table segment identifier, an element segment identifier, a memory segment identifier, a global segment identifier, an export segment identifier, a data segment identifier and a custom segment identifier.

[0007] Furthermore, the preset optimization strategy includes at least one of the following strategies: optimization strategy for type segment: converting multi-byte codes in type segment into single-byte codes; optimization strategy for import segment: obtaining the configuration file corresponding to the smart card, and establishing a mapping relationship between the function index in the import segment and the actual index in the configuration file; optimization strategy for function segment: dividing the function segment into two parts: function data and function instance; function data is used to store static information corresponding to the function segment; function instance is used to store runtime information corresponding to the function segment; wherein static information is data that does not change when the function is called; optimization strategy for table segment and element segment: converting multi-byte codes in table segment into single-byte codes or double-byte codes, and converting multi-byte codes in element segment into Single-byte encoding or double-byte encoding; integrate the converted table segments and element segments for storage; memory segment optimization strategy: obtain the current number of pages, maximum number of pages and number of bytes per page in the memory segment; convert the current number of pages and maximum number of pages into single-byte encoding, and convert the number of bytes per page into double-byte encoding; global segment optimization strategy: divide the global segment into two parts: global data and global instance; global data is used to store the initial value corresponding to the global segment; global instance is used to store the running information corresponding to the global segment; export segment optimization strategy: delete the content in the export segment except the export function information offset and export function name information; data segment optimization strategy: convert the multi-byte encoding in the data segment into double-byte encoding; custom segment optimization strategy: delete the custom segment.

[0008] Furthermore, before the step of encapsulating at least one target binary data segment obtained based on a preset encapsulation strategy to obtain an optimized binary file, the method includes: obtaining constant instructions, address offsets and element number information in each initial binary data segment; the constant instructions include multiple constant values; judging whether the constant value is a repeated constant value; if not, determining that the constant value is a target constant value; the repeated constant value is a constant value that appears more than once in multiple initial binary data segments; saving the target constant value as a constant segment, and saving the address offset and element number information as a module instance segment.

[0009] Furthermore, the step of encapsulating at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file includes: encapsulating a constant segment, a module instance segment and a target binary data segment to obtain an optimized binary file.

[0010] In a second aspect, an embodiment of the present invention provides a file optimization system, which is applied to an electronic device, and the system includes: a format specification judgment module, which is used to obtain a binary file corresponding to a target smart card application and determine whether the binary file conforms to a preset format specification; a division module, which is used to divide the binary file into at least one initial binary data segment based on the preset format specification if the binary file conforms to the preset format specification; an optimization module, which is used to determine the data to be optimized corresponding to the initial binary data segment based on a preset optimization strategy, and optimize the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is less than the data volume of the initial binary data segment; an encapsulation module, which is used to encapsulate the obtained at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file.

[0011] In a third aspect, an embodiment of the present invention provides an electronic device, including an electronic device body and an optimization unit disposed in the electronic device body; and also including the above-mentioned file optimization system; the file optimization system is disposed in the optimization unit.

[0012] Furthermore, an intelligent management unit is also provided in the electronic device body; the intelligent management unit is connected to the smart card for communication; the intelligent management unit is used to obtain the configuration file of the smart card; and is also used to write the smart card application into the smart card.

[0013] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored, and the program code enables the processor to execute the method as described above.

[0014] The embodiment of the present invention provides a file optimization method, a file optimization system and an electronic device, the method comprising: obtaining a binary file corresponding to a target smart card application, determining whether the binary file conforms to a preset format specification; if the binary file conforms to the preset format specification, dividing the binary file into at least one initial binary data segment based on the preset format specification; determining the data to be optimized corresponding to the initial binary data segment based on a preset optimization strategy, and optimizing the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is less than the data volume of the initial binary data segment; encapsulating the obtained at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file. In this manner, a binary file with a large volume and a complex structure can be optimized, thereby reducing the volume of the binary file, thereby improving the loading speed and execution efficiency of the smart card application.

[0015] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention are realized and obtained by the structures particularly pointed out in the description, claims and drawings.

[0016] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0018] Figure 1 A flowchart of a file optimization method provided in Embodiment 1 of the present invention;

[0019] Figure 2 A flowchart of a method for dividing a binary file into at least one initial binary data segment based on a preset format specification provided in Embodiment 1 of the present invention;

[0020] Figure 3 A flowchart of a method for setting a constant segment and a module instance segment provided in Embodiment 1 of the present invention;

[0021] Figure 4 A schematic diagram of a file optimization system provided in Embodiment 2 of the present invention;

[0022] Figure 5This is a schematic diagram of an electronic device provided in Embodiment 3 of the present invention.

[0023] Icons: 1- format specification judgment module; 2- division module; 3- optimization module; 4- packaging module; 5- electronic device body; 6- optimization unit; 7- file optimization system; 8- intelligent management unit. DETAILED DESCRIPTION

[0024] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0025] To facilitate understanding of this embodiment, the embodiment of the present invention is described in detail below.

[0026] Embodiment 1:

[0027] Figure 1 This is a flow chart of the file optimization method provided in Example 1 of the present invention.

[0028] The file optimization method is applied to an electronic device, which is an external device that can communicate with a smart card. The electronic device can be a PC, a server, or other device that can process and optimize binary files corresponding to smart card applications. In this electronic device, the binary file will be analyzed, optimized, and reorganized to compress data, remove redundant parts in the binary file, and reorganize data segments, thereby reducing the size of the binary file. After the optimization and reorganization are completed, the optimized binary file is obtained and saved and downloaded to the smart card. The optimized binary file is downloaded to the smart card as a smart card application, and the smart card can run the smart card application. In this way, the smart card application can be executed more efficiently, while the storage space occupied by the smart card application in the smart card is smaller, so that the computing resource requirements of the smart card application when running are lower.

[0029] Among them, the smart card application is a binary file that runs on the smart card, also called a smart card application file, which is the core component of the smart card. The binary file contains the code and related data of the smart card application and can run in a variety of environments, including but not limited to embedded devices.

[0030] Smart card applications need to go through two operations to run in a smart card: downloading and instantiation. Downloading is loading the executable file (i.e. binary file) of the card application into the non-volatile memory of the smart card. Instantiation is to build the execution environment for the card application to run, including the allocation of memory space required for operation, initialization data and instance data structure.

[0031] Reference Figure 1 , a file optimization method applied to an electronic device, comprising:

[0032] Step S101, obtaining a binary file corresponding to a target smart card application, and determining whether the binary file complies with a preset format specification.

[0033] Here, the preset format specification is preset according to the actual situation. The preset format specification in this application can be a WebAssembly (Wasm, a binary format for the Web) specification.

[0034] Specifically, the binary file includes a file data structure, which includes the type of binary data segments in the binary file, a data segment identifier corresponding to each type of binary data segment, a data segment length, and a data segment format and specification.

[0035] The types of binary data segments include: type segment (type segment), import segment (import segment), function segment (function segment), table segment (table segment), element segment (elem segment), memory segment (memory segment), global segment (global segment), export segment (export segment), data segment (data segment) and user segment (user segment).

[0036] Among them, the type segment is used to define the function type, including the function signature (parameter type and return value type). The import segment is used to import functions, tables, memory, global variables, etc. from other modules. The function segment contains a list of function indexes, specifying which functions are defined in the module. The table segment is used to define the type and size of the table, which is used to store function pointers. The element segment is used to define the initialization elements of the table. The memory segment is used to define the page size and maximum number of pages of the memory. The global segment is used to define global variables and their initial values ​​and variability. The export segment is used to specify the functions, global variables, memory, tables, etc. exported by the module. The data segment is used to initialize the data in the memory. The custom segment contains custom data.

[0037] Step S102: if the binary file conforms to a preset format specification, the binary file is divided into at least one initial binary data segment based on the preset format specification.

[0038] In one embodiment, referring to Figure 2In step S102, the step of dividing the binary file into at least one initial binary data segment based on a preset format specification includes:

[0039] Step S201, determining the data segment identifier and data segment length corresponding to the initial binary data segment to be divided based on the file data structure.

[0040] Step S202, dividing the binary file into at least one initial binary data segment based on the data segment identifier and the data segment length; wherein the data segment identifier includes a type segment identifier, an import segment identifier, a function segment identifier, a table segment identifier, an element segment identifier, a memory segment identifier, a global segment identifier, an export segment identifier, a data segment identifier, and a custom segment identifier.

[0041] Specifically, an identifier in a section header of a binary file is identified based on a data segment identifier. For example, a data segment with an identifier of '0x01' is determined to be a type segment; a data segment with an identifier of '0x02' is determined to be an import segment; a data segment with an identifier of '0x03' is determined to be a function segment; a data segment with an identifier of '0x04' is determined to be a table segment; a data segment with an identifier of '0x05' is determined to be an element segment; a data segment with an identifier of '0x06' is determined to be a memory segment; a data segment with an identifier of '0x07' is determined to be a global segment; a data segment with an identifier of '0x08' is determined to be an export segment; a data segment with an identifier of '0x09' is determined to be a data segment; and a data segment with an identifier of '0x0A' or a higher value is determined to be a custom segment.

[0042] After dividing the binary file into at least one initial binary data segment based on the data segment identifier and data segment length, check whether each initial binary data segment complies with the data segment format and specification requirements. For example: check whether the function signature is defined in the type segment; check whether the external function or global variable to be imported is correctly defined in the import segment; check whether the parameter and return value information in the function segment is consistent with the type definition in the type segment; check whether all required tables and their initial sizes are defined in the table segment; check whether the table initialization data defined in the element segment is correct; check whether the appropriate number and size of memory pages are defined in the memory segment; check whether the global variables defined in the global segment contain appropriate initialization values; check whether the required functions or data items are correctly exported in the export segment; check whether the data defined in the data segment is initialized correctly and at the specified address location; check whether the user-defined data is contained in the custom segment, and confirm whether its format is as expected.

[0043] Step S103, based on the preset optimization strategy, determine the data to be optimized corresponding to the initial binary data segment, and optimize the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is smaller than the data volume of the initial binary data segment.

[0044] In one embodiment, the preset optimization strategy includes at least one of the following strategies.

[0045] 1. Optimization strategy for type segments: Convert multi-byte encoding in type segments to single-byte encoding.

[0046] Here, the type segment contains a function type with two parameters and a return value. The LEB128 (Little-Endian Base 128) encoding format (a 128-bit, variable-length encoding format based on little-endian representation) in the initial binary data segment may use more bytes to represent this information, especially when it comes to the number of parameters and return values ​​and the number of units they occupy.

[0047] Specifically, the type information of the parameters and return values ​​in the type segment is extracted, for example: parameter type: 'i32,i64' (two parameters, occupying 4 bytes and 8 bytes respectively), return value type: 'f32' (one return value, occupying 4 bytes).

[0048] In the 'LEB128' encoding, these information will be represented by multiple bytes. For optimization, a single-byte encoding is used here to represent the number of parameters and return values ​​and the number of units they occupy. Specifically, the possible formats are as follows:

[0049] The first byte: the number of parameters and the number of return values ​​(each represented by 4 bits), the upper 4 bits: represent the number of parameters (such as 2), the lower 4 bits: represent the number of return values ​​(such as 1). For example, if there are 2 parameters and 1 return value, then this byte may be encoded as '0x21'.

[0050] The second byte: the total number of units occupied by the parameter. For example, 'i32' occupies 4 bytes, 'i64' occupies 8 bytes, so here it may be represented by one byte as '0x0C' (12 bytes).

[0051] The third byte: the number of units occupied by the return value. For example, if 'f32' occupies 4 bytes, it can be directly represented by one byte as '0x04'.

[0052] With the above encoding, the data in the type segment is compressed into a more compact format. Compared with the 'LEB128' encoding, this method greatly reduces the space occupied by the type segment.

[0053] All of this re-encoded type segment information will be reorganized according to the structure of the type instance based on the requirements of the target device to obtain the target binary data segment corresponding to the type segment.

[0054] Assume that the type segment in the initial binary data segment requires 6 bytes to represent the function type, and after optimization, only 3 bytes are needed to represent the same information. The initial type segment is [0x60, 0x02, 0x7F, 0x7E, 0x01, 0x7D], 6 bytes, and the optimized target type segment is [0x21, 0x0C, 0x04, 3 bytes.

[0055] Through the above type segment optimization strategy, the size of the type segment can be effectively reduced, thereby optimizing the overall space occupation of the binary file corresponding to the target smart card application.

[0056] 2. Import segment optimization strategy: obtain the configuration file corresponding to the smart card and establish a mapping relationship between the function index in the import segment and the actual index in the configuration file.

[0057] Here, the import section defines several external functions that need to be imported into the smart card for execution. There is a local native configuration file on the smart card, which defines the specific indexes and implementation methods of these external functions on the smart card.

[0058] Specifically, the import section in the initial binary data segment defines two functions: 'foo' and 'bar'.

[0059] The target smart card's 'native' profile defines the implementation of these functions and assigns them specific indices, e.g. 'foo' has index '0x10' and 'bar' has index '0x11'.

[0060] Parse the import segment and extract the function information that needs to be imported. Assume that the parsed results are as follows: 'foo' function, original index is '0x01'; 'bar' function, original index is '0x02'.

[0061] The indexes of these functions in the binary file are logical and need to be mapped to actual indexes on the target smart card.

[0062] Find the 'native' configuration file on the target device, which contains the actual implementation of the external function in the target device and the corresponding index. For example, the configuration file content may be as follows:

[0063]

[0064] In this configuration file, 'foo' has device index '0x10' and 'bar' has device index '0x11'.

[0065] By looking up the 'native' configuration file, the original indexes of 'foo' and 'bar' are mapped to the actual indexes of the target smart card. The specific mapping is as follows:

[0066] The original index '0x01' of the 'foo' function maps to index '0x10' of the target smartcard.

[0067] The original index '0x02' of the 'bar' function maps to the index '0x11' of the target smart card.

[0068] Update the mapped index value to the import segment to form a new import segment corresponding to the optimized target binary data segment. The data structure of this new import segment may be as follows:

[0069] Actual index of the 'foo' function on the target smart card: '0x10'.

[0070] Actual index of the 'bar' function on the target smart card: '0x11'.

[0071] Assume that the initial import segment data is as follows:

[0072] "'plain text

[0073] (import "env" "foo" (func$0))

[0074] (import "env" "bar" (func$1))

[0075] ”'

[0076] After optimization and mapping, the target import segment is updated to:

[0077] "'plain text

[0078] (import "env" "foo" (func$0x10))

[0079] (import "env" "bar" (func$0x11))

[0080] ”'

[0081] Through the optimization strategy of the import segment, the imported external functions are correctly mapped to the actual implementation on the device according to the configuration file of the smart card. Stepping ensures the correct execution of the target smart card application on the smart card, reduces the search cost at runtime, and improves the execution efficiency.

[0082] 3. Function segment optimization strategy: Divide the function segment into two parts: function data and function instance; function data is used to store static information corresponding to the function segment; function instance is used to store runtime information corresponding to the function segment; among them, static information is data that will not change when the function is called.

[0083] Here, according to the function instance structure required when the function is running, the function segment is divided into two parts: function section (function data) and function instance (function instance), which store static data and runtime information respectively. The function section contains the local variables, stack size, code size and code address of the application-defined function; the function instance contains the address of the function type, the function index of the imported function or the address of the custom function in the function section; it is consistent with the function instance structure when the function is running.

[0084] Read the function segment in the initial binary data segment and extract the basic information of each function, such as function type, local variables, code segment, etc.

[0085] Create a function section:

[0086] The function section stores static information related to each function, which does not change when the function is called. Specifically, it includes:

[0087] Local variables: A list of the function's local variables and their types.

[0088] Occupied stack size: The size of the stack space required by the function when running.

[0089] Code size: The length of the function's code segment, in bytes.

[0090] Code address: The offset address of the function's code in the binary file.

[0091] For example, suppose a function is defined as follows:

[0092]

[0093] Create a function instance:

[0094] Function instance is a structure used at runtime, which contains runtime information, such as function type address, index of imported function, and actual running address of custom function. Specifically, it includes:

[0095] Address of a function type: points to the type information associated with the function.

[0096] Function index for imported functions: If the function is imported, contains the index in the import table.

[0097] The address of the custom function in the function section: points to the static data of the function in the function section.

[0098] The function instance will contain:

[0099]

[0100] The function segment optimization strategy is used to split the function segment into function section and function instance, which optimizes the storage and access of function data. The function section only stores static information related to the function, which can reduce redundant data, while the function instance performs dynamic operations as needed at runtime.

[0101] Assume the following function segment definition:

[0102]

[0103] The optimization strategy based on function segments can be obtained:

[0104]

[0105]

[0106]

[0107] Through the optimization strategy of function segments, the smart card can load and call functions more efficiently and reduce the storage of redundant data when running the target smart card application.

[0108] 4. Optimization strategy for table segments and element segments: convert multi-byte codes in table segments into single-byte codes or double-byte codes, and convert multi-byte codes in element segments into single-byte codes or double-byte codes; integrate and store the converted table segments and element segments.

[0109] Specifically, the information contained in the table segment in the initial binary data segment is:

[0110] Current table size: 100.

[0111] Maximum table size: 200.

[0112] The index of each element: Assume the index values ​​are '0,1,2,...'.

[0113] The data structure of the table segment corresponding to the optimized target binary data segment is:

[0114] 1-byte element type: Assume the value is '01', indicating that this is a table of function type.

[0115] 2-byte current table size: Use 2 bytes to represent the current table size, 100.

[0116] 2-byte maximum table size: Use 2 bytes to represent the maximum table size, 200.

[0117] Element instance: Contains the data of the element, which can be a function index.

[0118] After optimization, the entire table segment may be represented as: '01 00 64 00C8', where '01' indicates the element type, '00 64' indicates the current table size is 100, and '00C8' indicates the maximum table size is 200.

[0119] The element segment in the initial binary data segment is used to initialize the table segment, which contains the address offset and function index of the element.

[0120] The data structure of the element segment corresponding to the optimized target binary data segment is:

[0121] 1-byte identifier: Indicates the storage format used for the data. Assume it is '01', which means that 1 byte is used to represent the element data.

[0122] 1-byte element type: indicates the type of the element. Assuming it is '01', it means it is a function type.

[0123] 2-byte current table size: consistent with the table segment, which is 100.

[0124] 2-byte maximum table size: consistent with the table segment, which is 200.

[0125] Element instance: Each instance is represented by 1 byte.

[0126] Assuming that the element instance is '0x01, 0x02', indicating two function indexes, the optimized target elem' may be represented as: '01 01 00 64 00C8 01 02'.

[0127] The optimized target table segment and the target element segment are integrated and stored.

[0128] Through the optimization strategy of table segments and element segments, the space occupied by table segments and element segments can be reduced, while improving the execution efficiency of the target smart card application. Through 1-byte or 2-byte data representation, data can be effectively compressed while still maintaining its functionality and usability.

[0129] 5. Memory segment optimization strategy: Get the current number of pages, maximum number of pages, and number of bytes per page in the memory segment; convert the current number of pages and maximum number of pages into single-byte encoding, and convert the number of bytes per page into double-byte encoding.

[0130] Here, the memory segment is used to define the initial configuration of the memory in the module, such as the current number of pages, the maximum number of pages, and the number of bytes per page. The memory segment usually uses the variable-length LEB128 encoding format to represent these values. In order to optimize the memory segment, these values ​​can be compressed into a fixed-length representation.

[0131] Specifically, assume that the memory segment in an initial binary data segment contains the following information:

[0132] Current number of pages: 64 pages (usually expressed in LEB128 encoding format).

[0133] Maximum number of pages: 128 pages (usually expressed in LEB128 encoding format).

[0134] Bytes per page: 65536 bytes (64KB, the standard page size).

[0135] In binary files, these values ​​may occupy different numbers of bytes. For example:

[0136] Current page number: 64 may be encoded as '0x40' (1 byte).

[0137] Maximum number of pages: 128 may be encoded as '0x80 0x01' (2 bytes).

[0138] Bytes per page: 65536 may be encoded as '0x80 0x80 0x01' (3 bytes).

[0139] To optimize storage, re-encode these values ​​to fixed length:

[0140] Current page number: Use 1 byte to represent it, and directly write '0x40'.

[0141] Maximum number of pages: Use 1 byte to represent it, and directly write '0x80'.

[0142] Number of bytes per page: Use 2 bytes to represent it, directly write '0x10000', that is, '0x00 0x10'.

[0143] The data structure of the memory segment corresponding to the optimized target binary data segment is as follows:

[0144] Current page number: '0x40' (1 byte, indicating 64 pages).

[0145] Maximum number of pages: '0x80' (1 byte, indicating 128 pages).

[0146] Number of bytes per page: '0x00 0x10' (2 bytes, indicating 65536 bytes).

[0147] When loading the memory segment in the smart card, the system will directly read these 4 bytes of optimized data:

[0148] Current page number: '0x40' is directly interpreted as 64 pages.

[0149] Maximum number of pages: '0x80' is directly interpreted as 128 pages.

[0150] Bytes per page: '0x00 0x10' is directly parsed as '65536' bytes.

[0151] The memory segment optimization strategy not only reduces the space occupied by the memory segment, but also simplifies the parsing process, because there is no need to process variable-length LEB128 encoding, making the parsing more efficient.

[0152] The current page number and the maximum page number are compressed into 1 byte and the bytes per page are compressed into 2 bytes through the memory segment optimization strategy. The structure of the memory segment is optimized to occupy less space and speed up the parsing speed, so that the smart card can read and interpret the data faster when processing the memory configuration, thereby improving the overall performance.

[0153] 6. Optimization strategy for global segments: Divide the global segment into global data and global instances; global data is used to store the initial value corresponding to the global segment; global instances are used to store the operating information corresponding to the global segment.

[0154] Here, the global segment is used to define global variables. Global variables can be constant or mutable and are used at runtime. The optimization strategy based on the global segment divides the global segment into two parts: global data and global instance. Global data only has initialization values, and global instance is the global instance structure used at runtime.

[0155] Specifically, the global segment in a binary file usually contains the definitions of several global variables. The definition of each variable includes the type of the variable (such as integer or floating point number), the variability flag (constant or variable), and the initial value expression.

[0156] For each global variable corresponding to the global segment in the initial binary data segment, extract its initial value and save these initial values ​​as the global data part. This part of the data only contains initial values ​​because these values ​​need to be initialized when the file is loaded.

[0157] For example, a global variable is defined as:

[0158] "'wasm

[0159] (global$g0(mut i32)(i32.const 10))

[0160] ”'

[0161] The initial value '10' is the global data.

[0162] The global instance is a structure used at runtime that contains all the necessary metadata information for accessing and modifying global variables. This instance structure typically contains the following fields:

[0163] The type of the variable (e.g. 'i32', 'f32', etc.).

[0164] Whether it is mutable (constant / mutable).

[0165] The current value of the variable (which may differ from the initial value at runtime).

[0166] The address or offset of the variable in memory

[0167] The global instance will include the following:

[0168]

[0169] A global instance is created for each global variable so that it can be managed at runtime.

[0170] In the optimized target global segment, the global data part only contains initial values, and all runtime information is encapsulated into the global instance. This reduces the amount of redundant data in the global segment and makes runtime access more efficient.

[0171] When the file is loaded into the smart card, the smart card will initialize the global instance according to the global data.

[0172] Assume the following initial global segment definition:

[0173] "'wasm

[0174] (global$g1 i32(i32.const 20))

[0175] (global$g2(mut f64)(f64.const 3.14))

[0176] ”'

[0177] The optimized target global segments include:

[0178] -global data:

[0179] '20' (corresponds to the initial value of '$g1')

[0180] '3.14' (corresponds to the initial value of '$g2')

[0181] -global instance (created when the device loads):

[0182] "'c

[0183] struct GlobalInstance g1 = {20, false}; / / $g1: type i32, value 20, immutable structGlobalInstance g2 = {3.14, true}; / / $g2: type f64, value 3.14, mutable

[0184] ”'

[0185] The global segment optimization strategy not only reduces the file size, but also speeds up the initialization and access to global variables at runtime.

[0186] 7. Export segment optimization strategy: Delete the contents in the export segment except the export function information offset and export function name information.

[0187] Here, the export segment usually contains complete export item information, including the type, name, index, and other related data of the export item. In order to optimize the export segment, necessary information such as the offset and name of the function information can be retained, and other parts can be simplified or deleted.

[0188] Specifically, assume that the export segment in an initial binary data segment contains the following information:

[0189] Export item type: Indicates whether the exported item is a function, global variable, memory, or table.

[0190] Export item name: such as '"myFunc"', which indicates the name of the function.

[0191] Export item index: such as '0x01', which indicates the index of this function in the function segment.

[0192] Additional metadata for the exported item: may include other unnecessary redundant information.

[0193] The complete information of an exported function may be as follows:

[0194] "'plain text

[0195] (export "myFunc" (func 1))

[0196] ”'

[0197] Where '"myFunc"' is the name of the function and '1' is the index of the function in the function section.

[0198] Preserve export function information offset: Record the actual offset of each export item in the file so that it can be quickly located when loading.

[0199] Preserve exported function name information: Save the exported function name such as '"myFunc"' so that the external environment can identify and call it.

[0200] Optimize or remove other parts: remove unnecessary metadata, such as redundant export type information or possible comments, etc.

[0201] The export segment structure in the optimized target binary data segment may be as follows:

[0202] Export item name: '"myFunc"', occupies a certain byte length.

[0203] Export item information offset: such as '0x100', which indicates the offset position of the function definition in the file.

[0204] The specific optimized storage format may be as follows:

[0205] "'plain text

[0206] 0x100 "myFunc"

[0207] ”'

[0208] Where '0x100' represents the offset of the function definition in the file, and '"myFunc"' is the function name.

[0209] When the smart card loads the target smart card application, the optimized export segment is read first:

[0210] Based on the function name '"myFunc"', find the offset '0x100' corresponding to the name.

[0211] This offset is used to quickly locate the location where the function is defined, thereby executing the function.

[0212] The redundant information in the export segment is deleted through the optimization strategy of the export segment, making the file structure more compact and locating the export function faster during execution, effectively reducing the file size and improving the efficiency of finding and calling export functions at runtime. It is suitable for embedded systems with limited resources such as smart cards.

[0213] 8. Data segment optimization strategy: Convert multi-byte encoding in the data segment to double-byte encoding.

[0214] Here, only the base address of the data represented by 2 bytes, the data length represented by 2 bytes, and the initialization data part are retained in the data segment, and the redundant data in the rest of the part is cleared.

[0215] 9. Optimization strategy for custom segments: delete custom segments.

[0216] Here, the custom data in the custom segment is deleted because the data is not needed in the runtime environment of the smart card.

[0217] Step S104: encapsulate the obtained at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file.

[0218] In one embodiment, referring to Figure 3 , before step S104, the method further includes:

[0219] Step S301, obtaining constant instructions, address offsets and element number information in each initial binary data segment; the constant instructions include multiple constant values.

[0220] Step S302, determining whether the constant value is a repeated constant value; if not, determining the constant value to be a target constant value; a repeated constant value is a constant value that appears more than once in multiple initial binary data segments.

[0221] Step S303, save the target constant value as a constant segment, and save the address offset and element number information as a module instance segment.

[0222] Here, in the initial binary data segment, constant values ​​may be reused in multiple functions. In order to optimize storage space, all constant values ​​can be stored in a constant segment. Then, a module instance segment is added to save the address offset and element number information of each segment.

[0223] Specifically, suppose that multiple functions in the initial binary data segment use the same constant values ​​'42' and '3.14'. In the initial binary data segment, these constant values ​​may be stored repeatedly in each place where they are used. For example:

[0224]

[0225] In this case, the constant values ​​'42' and '3.14' are saved multiple times in different functions, resulting in redundancy.

[0226] By adding a constant segment, these repeated constant values ​​are stored centrally to eliminate redundancy. The constant segment structure may be as follows:

[0227] "'plain text

[0228] const section:

[0229] 0x01->42 (store the constant '42' in the first location of the constant segment)

[0230] 0x02->3.14 (store the constant '3.14' in the second location of the constant segment)

[0231] ”'

[0232] When a constant is referenced in a function, the instruction is modified to access the corresponding location in the constant segment:

[0233] "'wasm

[0234] (func$func1

[0235] const.load 0x01 / / load constant 42 from the 0x01 position of the constant segment

[0236] / / Other instructions )

[0238] (func$func3

[0239] const.load 0x02 / / load constant from 0x02 in constant segment 3.14

[0240] / / Other instructions )

[0242] ”'

[0243] Add module instance segments to save address offset and element count information of each segment:

[0244] In the optimized target binary data segments, the location of each segment in the file and the number of elements it contains need to be quickly found and accessed at runtime.

[0245] A new module instance segment is added to store the address offset and element count information of each data segment. The structure of the module instance segment may be as follows:

[0246] "'plain text

[0247] module instance section:

[0248] Type segment: offset = 0x100, number of elements = 3

[0249] Function segment: offset = 0x200, number of elements = 5

[0250] Const segment: offset = 0x300, number of elements = 2

[0251] ”'

[0252] This structure allows the runtime to quickly find and access data for a specific segment.

[0253] By adding constant segments, repeated constant values ​​can be stored centrally to reduce redundancy.

[0254] By adding module instance segments, the address offset and element number information of each segment are saved for quick positioning and access.

[0255] In one embodiment, step S104 includes:

[0256] The constant segment, module instance segment and target binary data segment are encapsulated to obtain an optimized binary file.

[0257] Here, the constant segment, module instance segment, and target binary data segment are encapsulated together to obtain an optimized binary file.

[0258] The optimized binary file has a smaller size and higher execution efficiency because constant values ​​are no longer stored repeatedly and the location and content of each segment can be quickly located and accessed.

[0259] In one embodiment, the electronic device is in communication with the smart card. The method further comprises:

[0260] The optimized binary file is stored in the non-volatile memory of the smart card, so that the smart card executes the target smart card application by calling the optimized binary file.

[0261] Here, the optimized binary file packages the constant segment, module instance segment and target binary data segment into an integral card application executable file, which can be executed efficiently at runtime, and can significantly improve the execution efficiency and storage utilization of the target smart card application in resource-constrained devices (such as smart cards).

[0262] Non-volatile memory (NVM) is a type of memory that can store data persistently. Unlike volatile memory (such as RAM), non-volatile memory can retain stored data after power failure or restart.

[0263] In a resource-constrained device (such as a smart card), since the capacity of the volatile memory is limited, the optimized binary file can be stored in the non-volatile memory of the smart card, and the target smart card application is executed by calling the optimized binary file.

[0264] The embodiment of the present invention provides a file optimization method, which includes: obtaining a binary file corresponding to a target smart card application, determining whether the binary file conforms to a preset format specification; if the binary file conforms to the preset format specification, dividing the binary file into at least one initial binary data segment based on the preset format specification; determining the data to be optimized corresponding to the initial binary data segment based on a preset optimization strategy, and optimizing the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is less than the data volume of the initial binary data segment; and encapsulating the obtained at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file. In this method, by preprocessing the binary file corresponding to the smart card application downloaded to the smart card and adjusting the data structure in the binary file, a binary file with a large volume and a complex structure can be optimized, thereby reducing the volume of the binary file, thereby improving the loading speed and execution efficiency of the smart card application.

[0265] Embodiment 2:

[0266] Figure 4 A schematic diagram of a file optimization system provided in Embodiment 2 of the present invention.

[0267] Reference Figure 4 , a file optimization system for electronic devices, comprising:

[0268] The format specification judgment module 1 is used to obtain the binary file corresponding to the target smart card application and judge whether the binary file conforms to the preset format specification.

[0269] The division module 2 is used for dividing the binary file into at least one initial binary data segment based on the preset format specification if the binary file conforms to the preset format specification.

[0270] The optimization module 3 is used to determine the data to be optimized corresponding to the initial binary data segment based on a preset optimization strategy, and optimize the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is smaller than the data volume of the initial binary data segment.

[0271] The encapsulation module 4 is used to encapsulate the obtained at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file.

[0272] An embodiment of the present invention provides a file optimization system, in which a binary file with a large volume and complex structure can be optimized by preprocessing the binary file corresponding to the smart card application downloaded to the smart card and adjusting the data structure in the binary file, thereby reducing the binary file size and further improving the loading speed and execution efficiency of the smart card application.

[0273] Embodiment three:

[0274] Figure 5 This is a schematic diagram of an electronic device provided in Embodiment 3 of the present invention.

[0275] Reference Figure 5 The electronic device includes: an electronic device body 5 and an optimization unit 6 arranged in the electronic device body 5; and also includes the above-mentioned file optimization system 7; the file optimization system 7 is arranged in the optimization unit 6.

[0276] Furthermore, the electronic device body 5 is also provided with an intelligent management unit 8; the intelligent management unit 8 is connected to the smart card for communication; the intelligent management unit 8 is used to obtain the configuration file of the smart card; and is also used to write the smart card application into the smart card.

[0277] An embodiment of the present invention provides an electronic device, in which the efficiency of the smart card running the smart card application can be improved by preprocessing the binary file corresponding to the smart card application downloaded to the smart card, and at the same time, the memory usage of the smart card can be saved.

[0278] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the file optimization method of the above embodiment are executed.

[0279] The computer program product provided in the embodiment of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the method described in the previous method embodiment. The specific implementation can be found in the method embodiment and will not be repeated here.

[0280] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0281] In addition, in the description of the embodiments of the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.

[0282] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc., which can store program codes.

[0283] In the description of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.

[0284] Finally, it should be noted that the above-described embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.

Claims

1. A file optimization method, characterized in that: Applied to electronic equipment, the method comprises: Obtaining a binary file corresponding to a target smart card application, and determining whether the binary file complies with a preset format specification; If the binary file conforms to the preset format specification, dividing the binary file into at least one initial binary data segment based on the preset format specification; Based on a preset optimization strategy, determine the data to be optimized corresponding to the initial binary data segment, and optimize the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is smaller than the data volume of the initial binary data segment; The at least one target binary data segment is encapsulated based on a preset encapsulation strategy to obtain an optimized binary file.

2. The file optimization method according to claim 1, characterized in that: The electronic device is communicatively connected with the smart card; the method further comprises: The optimized binary file is stored in the non-volatile memory of the smart card, so that the smart card executes the target smart card application by calling the optimized binary file.

3. The file optimization method according to claim 1, characterized in that: The preset format specification includes a preset file data structure; the step of dividing the binary file into at least one initial binary data segment based on the preset format specification includes: Determine the data segment identifier and data segment length corresponding to the initial binary data segment to be divided based on the file data structure; dividing the binary file into at least one of the initial binary data segments based on the data segment identifier and the data segment length; The data segment identifier includes a type segment identifier, an import segment identifier, a function segment identifier, a table segment identifier, an element segment identifier, a memory segment identifier, a global segment identifier, an export segment identifier, a data segment identifier and a custom segment identifier.

4. The file optimization method according to claim 2, characterized in that: The preset optimization strategy includes at least one of the following strategies: Optimization strategy for type segment: converting multi-byte encoding in the type segment into single-byte encoding; Optimization strategy for the import segment: obtaining a configuration file corresponding to the smart card, and establishing a mapping relationship between a function index in the import segment and an actual index in the configuration file; Optimization strategy for function segments: dividing the function segments into two parts: function data and function instances; the function data is used to store static information corresponding to the function segments; The function instance is used to store runtime information corresponding to the function segment; wherein the static information is data that does not change when the function is called; Optimization strategy for table segments and element segments: converting the multi-byte code in the table segment into a single-byte code or a double-byte code, and converting the multi-byte code in the element segment into a single-byte code or a double-byte code; integrating and storing the converted table segment and element segment; Optimization strategy for memory segments: obtaining the current number of pages, the maximum number of pages, and the number of bytes per page in the memory segment; converting the current number of pages and the maximum number of pages into single-byte encoding, and converting the number of bytes per page into double-byte encoding; Optimization strategy for global segment: divide the global segment into two parts: global data and global instance; the global data is used to store the initial value corresponding to the global segment; the global instance is used to store the operation information corresponding to the global segment; Optimization strategy for the export segment: deleting the contents of the export segment except for the export function information offset and the export function name information; Optimization strategy for data segments: converting multi-byte codes in the data segments into double-byte codes; Optimization strategy for custom segments: delete the custom segments.

5. The file optimization method according to claim 1, characterized in that: Before the step of encapsulating the obtained at least one target binary data segment based on a preset encapsulation strategy to obtain an optimized binary file, the method includes: Obtaining constant instructions, address offsets and element number information in each of the initial binary data segments; the constant instructions include multiple constant values; Determine whether the constant value is a repeated constant value; if not, determine the constant value to be a target constant value; the repeated constant value is a constant value that appears more than once in a plurality of the initial binary data segments; The target constant value is saved as a constant segment, and the address offset and element number information is saved as a module instance segment.

6. The file optimization method according to claim 5, characterized in that: The step of encapsulating at least one of the target binary data segments based on a preset encapsulation strategy to obtain an optimized binary file includes: The constant segment, the module instance segment and the target binary data segment are encapsulated to obtain an optimized binary file.

7. A file optimization system, characterized in that: Applied to electronic equipment, the system comprises: A format specification determination module, used to obtain a binary file corresponding to a target smart card application and determine whether the binary file complies with a preset format specification; a division module, configured to divide the binary file into at least one initial binary data segment based on the preset format specification if the binary file conforms to the preset format specification; an optimization module, configured to determine the data to be optimized corresponding to the initial binary data segment based on a preset optimization strategy, and optimize the data to be optimized to obtain an optimized target binary data segment, wherein the data volume of the target binary data segment is smaller than the data volume of the initial binary data segment; The encapsulation module is used to encapsulate at least one of the target binary data segments obtained based on a preset encapsulation strategy to obtain an optimized binary file.

8. An electronic device, characterized in that: It comprises an electronic device body and an optimization unit arranged in the electronic device body; it also comprises the file optimization system as described in claim 7; the file optimization system is arranged in the optimization unit.

9. The electronic device according to claim 8, characterized in that: The electronic device body is also provided with an intelligent management unit; the intelligent management unit is connected to the smart card for communication; the intelligent management unit is used to obtain the configuration file of the smart card; and is also used to write the smart card application into the smart card.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the file optimization method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Program data generation method and device

    CN116069304A