mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Enhance application name conversion in convert.c to support UTF-16LE encoding
This commit is contained in:
@@ -23,6 +23,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <aaruformat.h>
|
#include <aaruformat.h>
|
||||||
|
#include <unicode/ucnv.h>
|
||||||
|
#include <unicode/ustring.h>
|
||||||
|
|
||||||
#include "aaruformattool.h"
|
#include "aaruformattool.h"
|
||||||
|
|
||||||
@@ -49,19 +51,58 @@ int convert(const char *input_path, const char *output_path)
|
|||||||
total_sectors = input_ctx->imageInfo.Sectors;
|
total_sectors = input_ctx->imageInfo.Sectors;
|
||||||
sector_size = input_ctx->imageInfo.SectorSize;
|
sector_size = input_ctx->imageInfo.SectorSize;
|
||||||
|
|
||||||
printf("Input image has %llu sectors of %u bytes each.\n", (unsigned long long)total_sectors, sector_size);
|
printf("Input image has %llu sectors of %u bytes each.\n", total_sectors, sector_size);
|
||||||
|
|
||||||
|
// Convert application name from UTF-8 to UTF-16LE using libicu
|
||||||
|
const char *app_name_utf8 = "aaruformattool";
|
||||||
|
size_t app_name_utf8_len = strlen(app_name_utf8);
|
||||||
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
|
int32_t app_name_utf16_len = 0;
|
||||||
|
// Get required length for UTF-16
|
||||||
|
u_strFromUTF8(NULL, 0, &app_name_utf16_len, app_name_utf8, (int32_t)app_name_utf8_len, &status);
|
||||||
|
status = U_ZERO_ERROR;
|
||||||
|
UChar *app_name_utf16 = (UChar *)malloc((app_name_utf16_len + 1) * sizeof(UChar));
|
||||||
|
if(app_name_utf16 == NULL)
|
||||||
|
{
|
||||||
|
printf("Error allocating memory for UTF-16 application name.\n");
|
||||||
|
aaruf_close(input_ctx);
|
||||||
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||||
|
}
|
||||||
|
u_strFromUTF8(app_name_utf16, app_name_utf16_len + 1, NULL, app_name_utf8, (int32_t)app_name_utf8_len, &status);
|
||||||
|
if(U_FAILURE(status))
|
||||||
|
{
|
||||||
|
printf("Error converting application name to UTF-16LE: %d\n", status);
|
||||||
|
free(app_name_utf16);
|
||||||
|
aaruf_close(input_ctx);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
// Convert UChar (UTF-16, host endian) to raw UTF-16LE bytes
|
||||||
|
uint8_t *app_name_utf16le = (uint8_t *)malloc(app_name_utf16_len * 2);
|
||||||
|
if(app_name_utf16le == NULL)
|
||||||
|
{
|
||||||
|
printf("Error allocating memory for UTF-16LE application name.\n");
|
||||||
|
free(app_name_utf16);
|
||||||
|
aaruf_close(input_ctx);
|
||||||
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||||
|
}
|
||||||
|
for(int32_t i = 0; i < app_name_utf16_len; i++)
|
||||||
|
{
|
||||||
|
app_name_utf16le[i * 2] = (uint8_t)(app_name_utf16[i] & 0xFF);
|
||||||
|
app_name_utf16le[i * 2 + 1] = (uint8_t)((app_name_utf16[i] >> 8) & 0xFF);
|
||||||
|
}
|
||||||
|
free(app_name_utf16);
|
||||||
|
|
||||||
// Create output image
|
// Create output image
|
||||||
output_ctx = aaruf_create(output_path, input_ctx->imageInfo.MediaType, sector_size, total_sectors,
|
output_ctx = aaruf_create(output_path, input_ctx->imageInfo.MediaType, sector_size, total_sectors,
|
||||||
0, // negative sectors
|
0, // negative sectors
|
||||||
0, // overflow sectors
|
0, // overflow sectors
|
||||||
NULL, // options
|
NULL, // options
|
||||||
(const uint8_t *)"aaruformattool",
|
app_name_utf16le,
|
||||||
14, // application name length
|
app_name_utf16_len * 2, // application name length in bytes
|
||||||
1, // major version
|
1, // major version
|
||||||
0, // minor version
|
0, // minor version
|
||||||
false
|
false);
|
||||||
);
|
free(app_name_utf16le);
|
||||||
|
|
||||||
if(output_ctx == NULL)
|
if(output_ctx == NULL)
|
||||||
{
|
{
|
||||||
@@ -88,8 +129,8 @@ int convert(const char *input_path, const char *output_path)
|
|||||||
// Show progress every 1000 sectors
|
// Show progress every 1000 sectors
|
||||||
if(sector % 1000 == 0 || sector == total_sectors - 1)
|
if(sector % 1000 == 0 || sector == total_sectors - 1)
|
||||||
{
|
{
|
||||||
printf("\rProgress: %llu/%llu sectors (%.1f%%)", (unsigned long long)sector + 1,
|
printf("\rProgress: %llu/%llu sectors (%.1f%%)", sector + 1, total_sectors,
|
||||||
(unsigned long long)total_sectors, (double)(sector + 1) / total_sectors * 100.0);
|
(double)(sector + 1) / total_sectors * 100.0);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user