// for reading ini
int mm_util_ini_get_int(const char *category, const char *item, int default_value);
+// for testsuites
+gboolean mm_util_safe_str_to_uint(const char *str, unsigned int *number);
+gboolean mm_util_safe_str_to_valid_uint(const char *str, unsigned int min, unsigned int max, unsigned int *value);
+
#ifdef __cplusplus
}
#endif
return val;
}
+
+gboolean mm_util_safe_str_to_uint(const char *str, unsigned int *number)
+{
+ gint64 temp = 0;
+
+ if (!str || strlen(str) == 0)
+ return FALSE;
+
+ temp = g_ascii_strtoll(str, NULL, 10);
+ if (errno != 0) {
+ mm_util_stderror("g_ascii_strtoll");
+ return FALSE;
+ }
+
+ if (temp < 0 || temp > UINT_MAX)
+ return FALSE;
+
+ *number = (unsigned int)temp;
+
+ return TRUE;
+}
+
+gboolean mm_util_safe_str_to_valid_uint(const char *str, unsigned int min, unsigned int max, unsigned int *value)
+{
+ unsigned int converted = 0;
+ gboolean is_num = mm_util_safe_str_to_uint(str, &converted);
+
+ if (!is_num || converted < min || converted > max)
+ return FALSE;
+
+ *value = converted;
+ return TRUE;
+}
};
/* for arguments */
-static int g_test_mode = 0;
+static unsigned int g_test_mode = 0;
static char *g_path = NULL;
static unsigned int g_width = 0;
static unsigned int g_height = 0;
static mm_util_image_h g_decoded_data = NULL;
-gboolean _get_input_data(const char *argv, const long min, const long max, int *data)
-{
- if (argv == NULL || strlen(argv) == 0)
- return FALSE;
-
- long temp = g_ascii_strtoll(argv, NULL, 10);
- if (temp < min || temp > max)
- return FALSE;
-
- *data = (int)temp;
-
- return TRUE;
-}
-
void _print_help(const char *argv0)
{
fprintf(stderr, "\t[usage]\n");
gboolean _get_arguments(int argc, char *argv[])
{
- int width = 0, height = 0;
-
- if (FALSE == _get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
fprintf(stderr, "\t[GIF_testsuite] wrong mode(%s) for test\n", argv[1]);
_print_help(argv[0]);
return FALSE;
_print_help(argv[0]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[3], 0, INT_MAX, &width)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[3], &g_width)) {
fprintf(stderr, "\t[GIF_testsuite] wrong width %s\n", argv[3]);
return FALSE;
}
- g_width = (unsigned int)width;
- if (FALSE == _get_input_data(argv[4], 0, INT_MAX, &height)) {
+
+ if (FALSE == mm_util_safe_str_to_uint(argv[4], &g_height)) {
fprintf(stderr, "\t[GIF_testsuite] wrong height %s\n", argv[4]);
return FALSE;
}
- g_height = (unsigned int)height;
} else {
fprintf(stderr, "\t[GIF_testsuite] wrong mode for test %s\n", argv[1]);
return FALSE;
};
/* for arguments */
-static int g_test_mode = 0;
+static unsigned int g_test_mode = 0;
static char *g_path = NULL;
-static int g_color = MM_UTIL_COLOR_RGB24;
+static unsigned int g_color = MM_UTIL_COLOR_RGB24;
/* for reading file */
static void *g_read_data = NULL;
static mm_util_image_h g_decoded_data = NULL;
-static gboolean __get_input_data(const char *argv, const long min, const long max, int *data)
-{
- if (!argv || strlen(argv) == 0)
- return FALSE;
-
- if (!data)
- return FALSE;
-
- long temp = g_ascii_strtoll(argv, NULL, 10);
- if (temp < min || temp > max)
- return FALSE;
-
- *data = (int)temp;
-
- return TRUE;
-}
-
static void __print_help(const char *argv0)
{
g_print("\t[usage]\n");
static gboolean __get_arguments(int argc, char *argv[])
{
- if (!__get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
+ if (!mm_util_safe_str_to_valid_uint(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
g_print("\t[HEIF_testsuite] wrong mode(%s) for test\n", argv[1]);
__print_help(argv[0]);
return FALSE;
if (g_test_mode == TEST_AUTO) {
// do nothing
} else if (g_test_mode == TEST_DECODE_FILE) {
- if (!__get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
+ if (!mm_util_safe_str_to_valid_uint(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
g_print("\t[HEIF_testsuite] color is default(%d)\n", g_color);
} else if (g_test_mode == TEST_DECODE_BUFFER) {
- if (!__get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
+ if (!mm_util_safe_str_to_valid_uint(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
g_print("\t[HEIF_testsuite] color is default(%d)\n", g_color);
} else {
g_print("\t[HEIF_testsuite] wrong mode for test %s\n", argv[1]);
static input_params g_args;
static output_values g_transformed;
-gboolean _get_input_data(const char *argv, const unsigned long min, const unsigned long max, unsigned int *data)
-{
- if (argv == NULL || strlen(argv) == 0)
- return FALSE;
-
- unsigned long temp = g_ascii_strtoll(argv, NULL, 10);
-
- if (temp < min || temp > max)
- return FALSE;
-
- *data = (unsigned int)temp;
-
- return TRUE;
-}
-
void _print_help(const char *argv0)
{
fprintf(stderr, "\t[usage]\n");
g_args.path = g_strdup(argv[index++]);
g_args.cmd = g_strdup(argv[index++]);
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.width)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.width)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong src_width %s\n", argv[index-1]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.height)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.height)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong src_height %s\n", argv[index-1]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.colorspace)) {
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.colorspace)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong src_format %s\n", argv[index-1]);
return FALSE;
}
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.cs)) {
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.cs)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong dst_format %s\n", argv[index-1]);
return FALSE;
}
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.w)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.h)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
return FALSE;
}
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &g_args.rot)) {
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[index++], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &g_args.rot)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong rotation %s\n", argv[index-1]);
return FALSE;
}
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.x)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.x)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong start_x %s\n", argv[index-1]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.y)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.y)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong start_y %s\n", argv[index-1]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.w)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[index++], &g_args.h)) {
fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
return FALSE;
}
};
/* for arguments */
-static int g_test_mode = 0;
+static unsigned int g_test_mode = 0;
static char *g_path = NULL;
static unsigned int g_width = 0;
static unsigned int g_height = 0;
-static int g_color = MM_UTIL_COLOR_RGB24;
-static int g_quality = 75;
-static int g_downscale = MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1;
+static unsigned int g_color = MM_UTIL_COLOR_RGB24;
+static unsigned int g_quality = 75;
+static unsigned int g_downscale = MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1;
/* for reading file */
static void *g_read_data = NULL;
static mm_util_image_h g_decoded_data = NULL;
-gboolean _get_input_data(const char *argv, const long min, const long max, int *data)
-{
- if (argv == NULL || strlen(argv) == 0)
- return FALSE;
-
- long temp = g_ascii_strtoll(argv, NULL, 10);
- if (temp < min || temp > max)
- return FALSE;
-
- *data = (int)temp;
-
- return TRUE;
-}
-
void _print_help(const char *argv0)
{
fprintf(stderr, "\t[usage]\n");
gboolean _get_arguments(int argc, char *argv[])
{
- int width = 0, height = 0;
-
- if (FALSE == _get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
fprintf(stderr, "\t[JPEG_testsuite] wrong mode(%s) for test\n", argv[1]);
_print_help(argv[0]);
return FALSE;
g_path = g_strdup(argv[2]);
if (g_test_mode == TEST_AUTO) {
- if (FALSE == _get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
fprintf(stderr, "\t[JPEG_testsuite] color is default(%d)\n", g_color);
- if (FALSE == _get_input_data(argv[4], 1, 100, &g_quality))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[4], 1, 100, &g_quality))
fprintf(stderr, "\t[JPEG_testsuite] quality is default(%d)\n", g_quality);
/* min: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1, max: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8 */
- if (FALSE == _get_input_data(argv[5], 1, 8, &g_downscale))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[5], 1, 8, &g_downscale))
fprintf(stderr, "\t[JPEG_testsuite] downscale is default(%d)\n", g_downscale);
} else if (g_test_mode == TEST_DECODE_FILE) {
- if (FALSE == _get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
fprintf(stderr, "\t[JPEG_testsuite] color is default(%d)\n", g_color);
} else if (g_test_mode == TEST_DECODE_MEMORY) {
- if (FALSE == _get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
fprintf(stderr, "\t[JPEG_testsuite] color is default(%d)\n", g_color);
} else if (g_test_mode == TEST_ENCODE_FILE || g_test_mode == TEST_ENCODE_MEMORY) {
if (argc < 5) {
_print_help(argv[0]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[3], 0, INT_MAX, &width)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[3], &g_width)) {
fprintf(stderr, "\t[JPEG_testsuite] wrong width %s\n", argv[3]);
return FALSE;
}
- g_width = (unsigned int)width;
- if (FALSE == _get_input_data(argv[4], 0, INT_MAX, &height)) {
+ if (FALSE == mm_util_safe_str_to_uint(argv[4], &g_height)) {
fprintf(stderr, "\t[JPEG_testsuite] wrong height %s\n", argv[4]);
return FALSE;
}
- g_height = (unsigned int)height;
- if (FALSE == _get_input_data(argv[5], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color)) {
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[5], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color)) {
fprintf(stderr, "\t[JPEG_testsuite] wrong color %s\n", argv[5]);
return FALSE;
}
- if (FALSE == _get_input_data(argv[6], 1, 100, &g_quality))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[6], 1, 100, &g_quality))
fprintf(stderr, "\t[JPEG_testsuite] quality is default(%d)\n", g_quality);
/* min: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1, max: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8 */
- if (FALSE == _get_input_data(argv[7], 1, 8, &g_downscale))
+ if (FALSE == mm_util_safe_str_to_valid_uint(argv[7], 1, 8, &g_downscale))
fprintf(stderr, "\t[JPEG_testsuite] downscale is default(%d)\n", g_downscale);
} else {
fprintf(stderr, "\t[JPEG_testsuite] wrong mode for test %s\n", argv[1]);
/* test encoding jpeg */
if (mode == TEST_ENCODE_FILE) {
- ret = mm_util_jpeg_encode_to_file(g_decoded_data, g_quality, ENCODE_FILE_PATH);
+ ret = mm_util_jpeg_encode_to_file(g_decoded_data, (int)g_quality, ENCODE_FILE_PATH);
if (ret != MM_UTIL_ERROR_NONE) {
fprintf(stderr, "\t[JPEG_testsuite] mm_util_jpeg_encode_to_file failed : %d\n", ret);
return FALSE;
}
} else if (mode == TEST_ENCODE_MEMORY) {
- ret = mm_util_encode_to_jpeg_memory(g_decoded_data, g_quality, &encoded_data, &encoded_size);
+ ret = mm_util_encode_to_jpeg_memory(g_decoded_data, (int)g_quality, &encoded_data, &encoded_size);
if (ret != MM_UTIL_ERROR_NONE) {
fprintf(stderr, "\t[JPEG_testsuite] mm_util_jpeg_encode_to_file failed : %d\n", ret);
SAFE_FREE(encoded_data);
};
/* for arguments */
-static int g_test_mode = 0;
+static unsigned int g_test_mode = 0;
static char *g_path = NULL;
static unsigned int g_width = 0;
static unsigned int g_height = 0;
-static int g_color = MM_UTIL_COLOR_RGB24;
+static unsigned int g_color = MM_UTIL_COLOR_RGB24;
static bool g_lossless = false;
/* for reading file */
static mm_util_image_h g_decoded_data = NULL;
static mm_util_enc_opt_h g_enc_opt = NULL;
-static gboolean __get_input_data(const char *argv, const long min, const long max, int *data)
-{
- if (!argv || strlen(argv) == 0)
- return FALSE;
-
- if (!data)
- return FALSE;
-
- long temp = g_ascii_strtoll(argv, NULL, 10);
- if (temp < min || temp > max)
- return FALSE;
-
- *data = (int)temp;
-
- return TRUE;
-}
-
static void __print_help(const char *argv0)
{
g_print("\t[usage]\n");
static gboolean __get_arguments(int argc, char *argv[])
{
- int width = 0, height = 0;
- int lossless = 0;
+ unsigned int lossless = 0;
- if (!__get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
+ if (!mm_util_safe_str_to_valid_uint(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
g_print("\t[JXL_testsuite] wrong mode(%s) for test\n", argv[1]);
__print_help(argv[0]);
return FALSE;
if (g_test_mode == TEST_AUTO) {
// optional : lossless
- if (!__get_input_data(argv[3], 0, 1, &lossless))
+ if (!mm_util_safe_str_to_valid_uint(argv[3], 0, 1, &lossless))
g_print("\t[JXL_testsuite] lossless is default(%d)\n", g_lossless);
else
g_lossless = (lossless == 1) ? true : false;
} else if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_BUFFER)) {
- if (!__get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
+ if (!mm_util_safe_str_to_valid_uint(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
g_print("\t[JXL_testsuite] color is default(%d)\n", g_color);
} else if (g_test_mode == TEST_ENCODE_FILE || g_test_mode == TEST_ENCODE_BUFFER) {
if (argc < 5) {
return FALSE;
}
- if (!__get_input_data(argv[3], 0, INT_MAX, &width)) {
+ if (!mm_util_safe_str_to_uint(argv[3], &g_width)) {
g_print("\t[JXL_testsuite] wrong width %s\n", argv[3]);
return FALSE;
}
- g_width = (unsigned int)width;
- if (!__get_input_data(argv[4], 0, INT_MAX, &height)) {
+ if (!mm_util_safe_str_to_uint(argv[4], &g_height)) {
g_print("\t[JXL_testsuite] wrong height %s\n", argv[4]);
return FALSE;
}
- g_height = (unsigned int)height;
- if (!__get_input_data(argv[5], 0, MM_UTIL_COLOR_NUM - 1, &g_color)) {
+ if (!mm_util_safe_str_to_valid_uint(argv[5], 0, MM_UTIL_COLOR_NUM - 1, &g_color)) {
g_print("\t[JXL_testsuite] wrong color %s\n", argv[5]);
return FALSE;
}
// optional : lossless
- if (!__get_input_data(argv[6], 0, 1, &lossless))
+ if (!mm_util_safe_str_to_valid_uint(argv[6], 0, 1, &lossless))
g_print("\t[JXL_testsuite] lossless is default(%d)\n", g_lossless);
else
g_lossless = (lossless == 1) ? true : false;