@@ -1997,6 +1997,30 @@ __ml_validate_model_file (const char *const *model,
19971997 return ML_ERROR_NONE ;
19981998}
19991999
2000+ /**
2001+ * @brief Internal helper to check if the file has one of the valid extensions.
2002+ * @return TRUE if valid, FALSE otherwise.
2003+ */
2004+ static gboolean
2005+ _is_valid_extension (const char * filename , const char * const * valid_exts )
2006+ {
2007+ const char * dot ;
2008+
2009+ if (!filename || !valid_exts )
2010+ return FALSE;
2011+
2012+ dot = strrchr (filename , '.' );
2013+ if (!dot )
2014+ return FALSE;
2015+
2016+ for (; * valid_exts ; valid_exts ++ ) {
2017+ if (g_ascii_strcasecmp (dot , * valid_exts ) == 0 )
2018+ return TRUE;
2019+ }
2020+
2021+ return FALSE;
2022+ }
2023+
20002024/**
20012025 * @brief Validates the nnfw model file.
20022026 * @since_tizen 5.5
@@ -2014,9 +2038,8 @@ _ml_validate_model_file (const char *const *model,
20142038 int status = ML_ERROR_NONE ;
20152039 ml_nnfw_type_e detected = ML_NNFW_TYPE_ANY ;
20162040 gboolean is_dir = FALSE;
2017- gchar * pos , * fw_name ;
2018- gchar * * file_ext = NULL ;
2019- guint i ;
2041+ gchar * fw_name ;
2042+ // gchar **file_ext = NULL;
20202043
20212044 if (!nnfw )
20222045 _ml_error_report_return (ML_ERROR_INVALID_PARAMETER ,
@@ -2060,19 +2083,6 @@ _ml_validate_model_file (const char *const *model,
20602083 goto done ;
20612084 }
20622085
2063- /* Handle mismatched case, check file extension. */
2064- file_ext = g_malloc0 (sizeof (char * ) * (num_models + 1 ));
2065- for (i = 0 ; i < num_models ; i ++ ) {
2066- if ((pos = strrchr (model [i ], '.' )) == NULL ) {
2067- _ml_error_report ("The given model [%d]=\"%s\" has invalid extension." , i ,
2068- model [i ]);
2069- status = ML_ERROR_INVALID_PARAMETER ;
2070- goto done ;
2071- }
2072-
2073- file_ext [i ] = g_ascii_strdown (pos , -1 );
2074- }
2075-
20762086 /** @todo Make sure num_models is correct for each nnfw type */
20772087 switch (* nnfw ) {
20782088 case ML_NNFW_TYPE_NNFW :
@@ -2101,11 +2111,10 @@ _ml_validate_model_file (const char *const *model,
21012111 status = ML_ERROR_NOT_SUPPORTED ;
21022112 break ;
21032113 case ML_NNFW_TYPE_VD_AIFW :
2104- if (!g_str_equal (file_ext [0 ], ".nb" ) &&
2105- !g_str_equal (file_ext [0 ], ".ncp" ) &&
2106- !g_str_equal (file_ext [0 ], ".tvn" ) &&
2107- !g_str_equal (file_ext [0 ], ".bin" )) {
2108- status = ML_ERROR_INVALID_PARAMETER ;
2114+ {
2115+ const char * exts [] = { ".nb" , ".ncp" , ".tvn" , ".bin" , NULL };
2116+ if (!_is_valid_extension (model [0 ], exts ))
2117+ status = ML_ERROR_INVALID_PARAMETER ;
21092118 }
21102119 break ;
21112120 case ML_NNFW_TYPE_SNAP :
@@ -2116,20 +2125,20 @@ _ml_validate_model_file (const char *const *model,
21162125 /* SNAP requires multiple files, set supported if model file exists. */
21172126 break ;
21182127 case ML_NNFW_TYPE_ARMNN :
2119- if (!g_str_equal (file_ext [0 ], ".caffemodel" ) &&
2120- !g_str_equal (file_ext [0 ], ".tflite" ) &&
2121- !g_str_equal (file_ext [0 ], ".pb" ) &&
2122- !g_str_equal (file_ext [0 ], ".prototxt" )) {
2123- _ml_error_report
2124- ("ARMNN accepts .caffemodel, .tflite, .pb, and .prototxt files only. Please support correct file extension. You have specified: \"%s\"" ,
2125- file_ext [0 ]);
2126- status = ML_ERROR_INVALID_PARAMETER ;
2128+ {
2129+ const char * exts [] =
2130+ { ".caffemodel" , ".tflite" , ".pb" , ".prototxt" , NULL };
2131+ if (!_is_valid_extension (model [0 ], exts )) {
2132+ _ml_error_report ("Invalid extension for ARMNN: %s" , model [0 ]);
2133+ status = ML_ERROR_INVALID_PARAMETER ;
2134+ }
21272135 }
21282136 break ;
21292137 case ML_NNFW_TYPE_MXNET :
2130- if (!g_str_equal (file_ext [0 ], ".params" ) &&
2131- !g_str_equal (file_ext [0 ], ".json" )) {
2132- status = ML_ERROR_INVALID_PARAMETER ;
2138+ {
2139+ const char * exts [] = { ".params" , ".json" , NULL };
2140+ if (!_is_valid_extension (model [0 ], exts ))
2141+ status = ML_ERROR_INVALID_PARAMETER ;
21332142 }
21342143 break ;
21352144 default :
@@ -2153,6 +2162,5 @@ _ml_validate_model_file (const char *const *model,
21532162 model [0 ], num_models );
21542163 }
21552164
2156- g_strfreev (file_ext );
21572165 return status ;
21582166}
0 commit comments