From 8169cbf5372f60950afd77b6adbe3702c6fbcff1 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Sat, 9 Mar 2024 18:06:42 +0100 Subject: [PATCH] dwgadd: block adds the BLOCK_HEADER also and check for valid names before --- examples/dwgadd.c | 39 +++++++++++++++++++++++++++++------ examples/dwgadd.example_r10 | 6 +++--- examples/dwgadd.example_r11 | 10 ++++----- examples/dwgadd.example_r1_4 | 6 +++--- examples/dwgadd.example_r2_10 | 6 +++--- src/dwg_api.c | 23 ++++++++++++++++----- 6 files changed, 65 insertions(+), 25 deletions(-) diff --git a/examples/dwgadd.c b/examples/dwgadd.c index e9af04c8bc..639b4e55f4 100644 --- a/examples/dwgadd.c +++ b/examples/dwgadd.c @@ -1162,8 +1162,14 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) if (strlen (text) && text[strlen (text) - 1] == '"') text[strlen (text) - 1] = '\0'; // strip the \" CHK_MISSING_BLOCK_HEADER - ent = (lastent_t){ .u.block = dwg_add_BLOCK (hdr, text), - .type = DWG_TYPE_BLOCK }; + if (!dwg_is_valid_name (dwg, text)) + fn_error ("Invalid BLOCK name\n"); + else + { + dwg_add_BLOCK_HEADER (dwg, text); + ent = (lastent_t){ .u.block = dwg_add_BLOCK (hdr, text), + .type = DWG_TYPE_BLOCK }; + } } else // clang-format off @@ -1188,10 +1194,13 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) pt1.x, pt1.y, pt1.z, text, scale.x, scale.y, scale.z, deg2rad (rot)); CHK_MISSING_BLOCK_HEADER - insert = ent = (lastent_t){ .u.insert = dwg_add_INSERT ( - hdr, &pt1, text, scale.x, scale.y, - scale.z, deg2rad (rot)), - .type = DWG_TYPE_INSERT }; + if (!dwg_is_valid_name (dwg, text)) + fn_error ("Invalid BLOCK name\n"); + else + insert = ent = (lastent_t){ .u.insert = dwg_add_INSERT ( + hdr, &pt1, text, scale.x, scale.y, + scale.z, deg2rad (rot)), + .type = DWG_TYPE_INSERT }; } else // clang-format off @@ -1212,6 +1221,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) hdr_s, pt1.x, pt1.y, pt1.z, text, scale.x, scale.y, scale.z, deg2rad (rot), i1, i2, f1, f2); CHK_MISSING_BLOCK_HEADER + if (!dwg_is_valid_name (dwg, text)) + fn_error ("Invalid block name\n"); insert = ent = (lastent_t){ .u.minsert = dwg_add_MINSERT ( hdr, &pt1, text, scale.x, scale.y, scale.z, @@ -1648,6 +1659,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) text SZ, s1 SZ, &u)) { LOG_TRACE ("add_DICTIONARY \"%s\" \"%s\" %u\n", text, s1, u); + if (!dwg_is_valid_name (dwg, text)) + fn_error ("Invalid dictionary name\n"); dict = ent = (lastent_t){ .u.dictionary = dwg_add_DICTIONARY ( dwg, text, s1, (unsigned long)u), .type = DWG_TYPE_DICTIONARY }; @@ -1661,6 +1674,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) if (dict.type != DWG_TYPE_DICTIONARY) fn_error ("xrecord: missing dictionary\n"); LOG_TRACE ("add_XRECORD dictionary \"%s\"\n", text); + if (!dwg_is_valid_name (dwg, text)) + fn_error ("Invalid dictionary name\n"); ent = (lastent_t){ .u.xrecord = dwg_add_XRECORD (dict.u.dictionary, text), .type = DWG_TYPE_XRECORD }; @@ -1688,6 +1703,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) { LOG_TRACE ("add_VIEWPORT %s \"%s\"\n", hdr_s, text); CHK_MISSING_BLOCK_HEADER + if (!dwg_is_valid_name (dwg, text)) + fn_error ("Invalid table record name\n"); ent = (lastent_t){ .u.viewport = dwg_add_VIEWPORT (hdr, text), .type = DWG_TYPE_VIEWPORT }; } @@ -1796,6 +1813,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat) if (version <= R_11) fn_error ("Invalid entity MLINESTYLE R_11) { + const char *standard = dwg->header.version < R_13 ? "STANDARD" : "Standard"; // LAYER: (0.1.10) layer = dwg_add_LAYER (dwg, (const BITCODE_T) "0"); layer->color = (BITCODE_CMC){ 7, CMC_DEFAULTS }; @@ -22644,7 +22645,7 @@ dwg_add_Document (Dwg_Data *restrict dwg, const int imperial) // if (ctrl) // dwg->layer_control = ctrl->tio.object->tio.LAYER_CONTROL; // STYLE: (0.1.11) - style = dwg_add_STYLE (dwg, "Standard"); + style = dwg_add_STYLE (dwg, standard); style->font_file = dwg_add_u8_input (dwg, "txt"); style->last_height = 0.2; // TEXTSTYLE: (5.1.11) [H 7] @@ -23403,6 +23404,11 @@ dwg_add_INSERT (Dwg_Object_BLOCK_HEADER *restrict blkhdr, // TODO scale_flag _obj->rotation = rotation; ADD_CHECK_ANGLE (_obj->rotation); + if (!dwg_is_valid_name (dwg, name)) { + API_UNADD_ENTITY; + LOG_ERROR("Invalid blockname %s", name); + return NULL; + } hdrref = dwg_find_tablehandle (dwg, name, "BLOCK"); if (hdrref) { @@ -23422,6 +23428,12 @@ dwg_add_INSERT (Dwg_Object_BLOCK_HEADER *restrict blkhdr, blkhdr->inserts[blkhdr->num_inserts - 1] = dwg_add_handleref (dwg, 4, obj->handle.value, NULL); } + else + { + API_UNADD_ENTITY; + LOG_ERROR("block %s not found", name); + return NULL; + } if (dwg->header.version < R_2_0b) _obj->block_name = strdup (name); if (dwg->header.version <= R_12) @@ -24005,9 +24017,10 @@ dwg_add_LINE (Dwg_Object_BLOCK_HEADER *restrict blkhdr, static void dwg_require_DIMSTYLE_Standard (Dwg_Data *restrict dwg) { - if (!(dwg_find_tablehandle_silent (dwg, "Standard", "DIMSTYLE"))) + const char *standard = dwg->header.version < R_13 ? "STANDARD" : "Standard"; + if (!(dwg_find_tablehandle_silent (dwg, standard, "DIMSTYLE"))) { - Dwg_Object_DIMSTYLE *std = dwg_add_DIMSTYLE (dwg, "Standard"); + Dwg_Object_DIMSTYLE *std = dwg_add_DIMSTYLE (dwg, standard); if (std) dwg->header_vars.DIMSTYLE = dwg_add_handleref ( dwg, 5, dwg_obj_generic_handlevalue (std), NULL); @@ -25302,7 +25315,7 @@ dwg_add_APPID (Dwg_Data *restrict dwg, const char *restrict name) EXPORT Dwg_Object_DIMSTYLE * dwg_add_DIMSTYLE (Dwg_Data *restrict dwg, const char *restrict name) { - if (name && strNE (name, "Standard")) + if (name && strNE (name, dwg->header.version < R_13 ? "STANDARD" : "Standard")) dwg_require_DIMSTYLE_Standard (dwg); { API_ADD_TABLE (DIMSTYLE, DIMSTYLE_CONTROL, { @@ -25466,7 +25479,7 @@ dwg_add_MLINESTYLE (Dwg_Data *restrict dwg, const char *restrict name) } } - _obj->name = strEQc (name, "Standard") ? dwg_add_u8_input (dwg, "STANDARD") + _obj->name = strEQc (name, "Standard") ? dwg_add_u8_input (dwg, "Standard") : dwg_add_u8_input (dwg, name); _obj->fill_color = (BITCODE_CMC){ 256, CMC_DEFAULTS }; if (strEQc (name, "Standard") || strEQc (name, "STANDARD"))