From eeae193f277b1faf7fdc763a4d4b288cddc03df7 Mon Sep 17 00:00:00 2001 From: korenkonder Date: Thu, 1 Feb 2024 00:55:28 +0300 Subject: [PATCH] `strncpy_s` has different behaviour than I expected --- src/CRE/Glitter/file_reader.cpp | 14 ++++++++------ src/CRE/Glitter/file_writer.cpp | 11 ++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/CRE/Glitter/file_reader.cpp b/src/CRE/Glitter/file_reader.cpp index 10b1a231..4c671fec 100644 --- a/src/CRE/Glitter/file_reader.cpp +++ b/src/CRE/Glitter/file_reader.cpp @@ -1042,7 +1042,7 @@ namespace Glitter { ext_anim_x->instance_id = 0; ext_anim_x->file_name_hash = hash_murmurhash_empty; if (*(char*)(d + 12)) { - strncpy_s(ext_anim_x->mesh_name, 0x80, (char*)(d + 12), 0x80); + strncpy_s(ext_anim_x->mesh_name, 0x80, (char*)(d + 12), 0x7F); ext_anim_x->mesh_name[0x7F] = 0; } else @@ -1067,7 +1067,7 @@ namespace Glitter { ext_anim_x->instance_id = 0; ext_anim_x->file_name_hash = hash_murmurhash_empty; if (*(char*)(d + 16)) { - strncpy_s(ext_anim_x->mesh_name, 0x80, (char*)(d + 16), 0x80); + strncpy_s(ext_anim_x->mesh_name, 0x80, (char*)(d + 16), 0x7F); ext_anim_x->mesh_name[0x7F] = 0; } else @@ -1095,7 +1095,7 @@ namespace Glitter { } if (*(char*)(d + 32)) { - strncpy_s(ext_anim_x->mesh_name, 0x80, (char*)(d + 32), 0x80); + strncpy_s(ext_anim_x->mesh_name, 0x80, (char*)(d + 32), 0x7F); ext_anim_x->mesh_name[0x7F] = 0; } else @@ -1218,7 +1218,7 @@ namespace Glitter { ext_anim->object = obj_db->get_object_info_by_fnv1a64m_hash_upper(ext_anim->object_hash); ext_anim->node_index = EFFECT_EXT_ANIM_CHARA_MAX; if (*(char*)(d + 12)) { - strncpy_s(ext_anim->mesh_name, 0x80, (char*)(d + 12), 0x80); + strncpy_s(ext_anim->mesh_name, 0x80, (char*)(d + 12), 0x7F); ext_anim->mesh_name[0x7F] = 0; } else @@ -1687,8 +1687,10 @@ namespace Glitter { ptcl->data.mesh.object_name_hash = *(uint64_t*)d; ptcl->data.mesh.object_set_name_hash = *(uint64_t*)(d + 8); } - //if (*(char*)(d + 10)) - // strncpy_s(ptcl->data.mesh.mesh_name, (char*)(d + 16), 0x40); + //if (*(char*)(d + 10)) { + // strncpy_s(ptcl->data.mesh.mesh_name, 0x40, (char*)(d + 16), 0x3F); + // ptcl->data.mesh.mesh_name[0x3F] = 0; + //} //else // ptcl->data.mesh.mesh_name[0] = 0; //if (big_endian) diff --git a/src/CRE/Glitter/file_writer.cpp b/src/CRE/Glitter/file_writer.cpp index 23659ee2..4c77179f 100644 --- a/src/CRE/Glitter/file_writer.cpp +++ b/src/CRE/Glitter/file_writer.cpp @@ -824,7 +824,7 @@ namespace Glitter { } if (ext_anim_x->mesh_name[0]) { - strncpy_s((char*)(d + 12), 0x80, ext_anim_x->mesh_name, 0x80); + strncpy_s((char*)(d + 12), 0x80, ext_anim_x->mesh_name, 0x7F); ((char*)(d + 12))[0x7F] = 0; } else @@ -841,7 +841,7 @@ namespace Glitter { } if (ext_anim_x->mesh_name[0]) { - strncpy_s((char*)(d + 16), 0x80, ext_anim_x->mesh_name, 0x80); + strncpy_s((char*)(d + 16), 0x80, ext_anim_x->mesh_name, 0x7F); ((char*)(d + 16))[0x7F] = 0; } else @@ -862,7 +862,7 @@ namespace Glitter { } if (ext_anim_x->mesh_name[0]) { - strncpy_s((char*)(d + 32), 0x80, ext_anim_x->mesh_name, 0x80); + strncpy_s((char*)(d + 32), 0x80, ext_anim_x->mesh_name, 0x7F); ((char*)(d + 32))[0x7F] = 0; } else @@ -947,7 +947,7 @@ namespace Glitter { } if (ext_anim->mesh_name[0]) { - strncpy_s((char*)(d + 12), 0x80, ext_anim->mesh_name, 0x80); + strncpy_s((char*)(d + 12), 0x80, ext_anim->mesh_name, 0x7F); ((char*)(d + 12))[0x7F] = 0; } else @@ -1585,7 +1585,8 @@ namespace Glitter { *(uint64_t*)d = ptcl->data.mesh.object_name_hash; *(uint64_t*)(d + 8) = ptcl->data.mesh.object_set_name_hash; } - //strncpy_s((char*)(d + 16), ptcl->data.mesh.mesh_name, 0x40); + //strncpy_s((char*)(d + 16), 0x40, ptcl->data.mesh.mesh_name, 0x3F); + //((char*)(d + 16))[0x3F] = 0; //if (big_endian) // store_reverse_endianness_uint64_t((void*)(d + 80), ptcl->data.mesh.sub_mesh_hash); //else