Skip to content

Commit 73bc82a

Browse files
authored
Merge pull request #3487 from matt335672/fix_g_htoi
Rationalise g_htoi() / xrdp_wm_htoi()
2 parents 267443e + de0c704 commit 73bc82a

File tree

7 files changed

+83
-136
lines changed

7 files changed

+83
-136
lines changed

Diff for: common/string_calls.c

+22-72
Original file line numberDiff line numberDiff line change
@@ -502,82 +502,32 @@ size_t strlcpy(char *dst, const char *src, size_t dsize)
502502
#endif
503503

504504
/*****************************************************************************/
505-
int
506-
g_htoi(char *str)
505+
unsigned int
506+
g_htoi(const char *str)
507507
{
508-
int len;
509-
int index;
510-
int rv;
511-
int val;
512-
int shift;
513-
514-
rv = 0;
515-
len = strlen(str);
516-
index = len - 1;
517-
shift = 0;
518-
519-
while (index >= 0)
508+
unsigned int rv = 0;
509+
while (*str != '\0')
520510
{
521-
val = 0;
522-
523-
switch (str[index])
511+
char c = *str;
512+
unsigned int val;
513+
if (c >= '0' && c <= '9')
524514
{
525-
case '1':
526-
val = 1;
527-
break;
528-
case '2':
529-
val = 2;
530-
break;
531-
case '3':
532-
val = 3;
533-
break;
534-
case '4':
535-
val = 4;
536-
break;
537-
case '5':
538-
val = 5;
539-
break;
540-
case '6':
541-
val = 6;
542-
break;
543-
case '7':
544-
val = 7;
545-
break;
546-
case '8':
547-
val = 8;
548-
break;
549-
case '9':
550-
val = 9;
551-
break;
552-
case 'a':
553-
case 'A':
554-
val = 10;
555-
break;
556-
case 'b':
557-
case 'B':
558-
val = 11;
559-
break;
560-
case 'c':
561-
case 'C':
562-
val = 12;
563-
break;
564-
case 'd':
565-
case 'D':
566-
val = 13;
567-
break;
568-
case 'e':
569-
case 'E':
570-
val = 14;
571-
break;
572-
case 'f':
573-
case 'F':
574-
val = 15;
575-
break;
515+
val = c - '0';
576516
}
577-
578-
rv = rv | (val << shift);
579-
index--;
580-
shift += 4;
517+
else if (c >= 'A' && c <= 'F')
518+
{
519+
val = (c - 'A' + 10);
520+
}
521+
else if (c >= 'a' && c <= 'f')
522+
{
523+
val = (c - 'a' + 10);
524+
}
525+
else
526+
{
527+
break; // Unrecognised character
528+
}
529+
rv = (rv << 4) | val;
530+
++str;
581531
}
582532

583533
return rv;

Diff for: common/string_calls.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ size_t strlcpy(char *dst, const char *src, size_t dsize);
310310
* @return int Integer expression of a string
311311
*/
312312
int g_atoix(const char *str);
313-
int g_htoi(char *str);
313+
/**
314+
* Converts a hex string to an integer
315+
* @param str pointer to Hex string containing only hex digits
316+
* @return value, converted from hex
317+
*
318+
* This function is intended to be used as an analogue to the standard
319+
* function atoi(). It performs no error checking of its own
320+
*/
321+
unsigned int g_htoi(const char *str);
314322
int g_bytes_to_hexstr(const void *bytes, int num_bytes, char *out_str,
315323
int bytes_out_str);
316324
int g_pos(const char *str, const char *to_find);

Diff for: tests/common/test_string_calls.c

+27
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,28 @@ END_TEST
11411141

11421142
/******************************************************************************/
11431143

1144+
START_TEST(test_htoi__all)
1145+
{
1146+
// Invalid 1st character
1147+
ck_assert_int_eq(g_htoi(""), 0);
1148+
ck_assert_int_eq(g_htoi("z"), 0);
1149+
// Test border conditions (assumes ASCII) */
1150+
ck_assert_int_eq(g_htoi("99/"), 0x99); /* '/' = one-before '0' */
1151+
ck_assert_int_eq(g_htoi("99:"), 0x99); /* '/' = one-after '9' */
1152+
ck_assert_int_eq(g_htoi("99@"), 0x99); /* '#' = one-before 'A' */
1153+
ck_assert_int_eq(g_htoi("99G"), 0x99); /* 'G' = one-after 'F' */
1154+
ck_assert_int_eq(g_htoi("99`"), 0x99); /* '`' = one-before 'a' */
1155+
ck_assert_int_eq(g_htoi("99g"), 0x99); /* 'g' = one-after 'f' */
1156+
1157+
// Test all valid characters and shifting is OK */
1158+
ck_assert_int_eq(g_htoi("01234567"), 0x01234567);
1159+
ck_assert_int_eq(g_htoi("8ABCDEFa"), 0x8ABCDEFa);
1160+
ck_assert_int_eq(g_htoi("bcdef"), 0xbcdef);
1161+
}
1162+
END_TEST
1163+
1164+
/******************************************************************************/
1165+
11441166
Suite *
11451167
make_suite_test_string(void)
11461168
{
@@ -1152,6 +1174,7 @@ make_suite_test_string(void)
11521174
TCase *tc_char2bm;
11531175
TCase *tc_strtrim;
11541176
TCase *tc_sigs;
1177+
TCase *tc_htoi;
11551178

11561179
s = suite_create("String");
11571180

@@ -1230,5 +1253,9 @@ make_suite_test_string(void)
12301253
tcase_add_test(tc_sigs, test_sigs__common);
12311254
tcase_add_test(tc_sigs, test_sigs__bigint);
12321255

1256+
tc_htoi = tcase_create("g_htoi");
1257+
suite_add_tcase(s, tc_htoi);
1258+
tcase_add_test(tc_htoi, test_htoi__all);
1259+
12331260
return s;
12341261
}

Diff for: xrdp/lang.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ xrdp_init_xkb_layout(struct xrdp_client_info *client_info)
698698
int rdp_layout_id;
699699
item = (char *)list_get_item(items, index);
700700
value = (char *)list_get_item(values, index);
701-
rdp_layout_id = g_htoi(value);
701+
rdp_layout_id = g_atoix(value);
702702
if (rdp_layout_id == client_info->keylayout)
703703
{
704704
g_strncpy(rdp_layout, item, 255);
@@ -714,12 +714,12 @@ xrdp_init_xkb_layout(struct xrdp_client_info *client_info)
714714
{
715715
item = (char *)list_get_item(items, index);
716716
value = (char *)list_get_item(values, index);
717-
int rdp_layout_id = g_htoi(value);
717+
int rdp_layout_id = g_atoix(value);
718718
if (rdp_layout_id == alt_layout)
719719
{
720720
g_strncpy(rdp_layout, item, 255);
721721
LOG(LOG_LEVEL_INFO,
722-
"Failed to match layout %08X, but matched %04X to %s",
722+
"Failed to match layout 0x%08X, but matched 0x%04X to %s",
723723
client_info->keylayout, alt_layout, rdp_layout);
724724
break;
725725
}

Diff for: xrdp/xrdp.h

-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ xrdp_wm_send_bitmap(struct xrdp_wm *self, struct xrdp_bitmap *bitmap,
141141
int x, int y, int cx, int cy);
142142
int
143143
xrdp_wm_set_pointer(struct xrdp_wm *self, int cache_idx);
144-
unsigned int
145-
xrdp_wm_htoi (const char *ptr);
146144
int
147145
xrdp_wm_set_focused(struct xrdp_wm *self, struct xrdp_bitmap *wnd);
148146
int

Diff for: xrdp/xrdp_login_wnd.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,8 @@ load_xrdp_config(struct xrdp_config *config, const char *xrdp_ini, int bpp)
10741074
globals->ini_version = 1;
10751075
globals->default_dpi = 96;
10761076

1077-
globals->ls_top_window_bg_color = HCOLOR(bpp, xrdp_wm_htoi("009cb5"));
1078-
globals->ls_bg_color = HCOLOR(bpp, xrdp_wm_htoi("dedede"));
1077+
globals->ls_top_window_bg_color = HCOLOR(bpp, g_htoi("009cb5"));
1078+
globals->ls_bg_color = HCOLOR(bpp, g_htoi("dedede"));
10791079
globals->ls_unscaled.width = 350;
10801080
globals->ls_unscaled.height = 350;
10811081
globals->ls_background_transform = XBLT_NONE;
@@ -1214,47 +1214,47 @@ load_xrdp_config(struct xrdp_config *config, const char *xrdp_ini, int bpp)
12141214

12151215
else if (g_strncmp(n, "grey", 64) == 0)
12161216
{
1217-
globals->grey = xrdp_wm_htoi(v);
1217+
globals->grey = g_htoi(v);
12181218
}
12191219

12201220
else if (g_strncmp(n, "black", 64) == 0)
12211221
{
1222-
globals->black = xrdp_wm_htoi(v);
1222+
globals->black = g_htoi(v);
12231223
}
12241224

12251225
else if (g_strncmp(n, "dark_grey", 64) == 0)
12261226
{
1227-
globals->dark_grey = xrdp_wm_htoi(v);
1227+
globals->dark_grey = g_htoi(v);
12281228
}
12291229

12301230
else if (g_strncmp(n, "blue", 64) == 0)
12311231
{
1232-
globals->blue = xrdp_wm_htoi(v);
1232+
globals->blue = g_htoi(v);
12331233
}
12341234

12351235
else if (g_strncmp(n, "dark_blue", 64) == 0)
12361236
{
1237-
globals->dark_blue = xrdp_wm_htoi(v);
1237+
globals->dark_blue = g_htoi(v);
12381238
}
12391239

12401240
else if (g_strncmp(n, "white", 64) == 0)
12411241
{
1242-
globals->white = xrdp_wm_htoi(v);
1242+
globals->white = g_htoi(v);
12431243
}
12441244

12451245
else if (g_strncmp(n, "red", 64) == 0)
12461246
{
1247-
globals->red = xrdp_wm_htoi(v);
1247+
globals->red = g_htoi(v);
12481248
}
12491249

12501250
else if (g_strncmp(n, "green", 64) == 0)
12511251
{
1252-
globals->green = xrdp_wm_htoi(v);
1252+
globals->green = g_htoi(v);
12531253
}
12541254

12551255
else if (g_strncmp(n, "background", 64) == 0)
12561256
{
1257-
globals->background = xrdp_wm_htoi(v);
1257+
globals->background = g_htoi(v);
12581258
}
12591259

12601260
/* misc stuff */
@@ -1313,7 +1313,7 @@ load_xrdp_config(struct xrdp_config *config, const char *xrdp_ini, int bpp)
13131313

13141314
else if (g_strncmp(n, "ls_top_window_bg_color", 64) == 0)
13151315
{
1316-
globals->ls_top_window_bg_color = HCOLOR(bpp, xrdp_wm_htoi(v));
1316+
globals->ls_top_window_bg_color = HCOLOR(bpp, g_htoi(v));
13171317
}
13181318

13191319
else if (g_strncmp(n, "ls_width", 64) == 0)
@@ -1328,7 +1328,7 @@ load_xrdp_config(struct xrdp_config *config, const char *xrdp_ini, int bpp)
13281328

13291329
else if (g_strncmp(n, "ls_bg_color", 64) == 0)
13301330
{
1331-
globals->ls_bg_color = HCOLOR(bpp, xrdp_wm_htoi(v));
1331+
globals->ls_bg_color = HCOLOR(bpp, g_htoi(v));
13321332
}
13331333

13341334
else if (g_strncmp(n, "ls_title", 255) == 0)

Diff for: xrdp/xrdp_wm.c

+9-45
Original file line numberDiff line numberDiff line change
@@ -442,42 +442,6 @@ xrdp_wm_set_pointer(struct xrdp_wm *self, int cache_idx)
442442
return libxrdp_set_pointer(self->session, cache_idx);
443443
}
444444

445-
/*****************************************************************************/
446-
/* convert hex string to int */
447-
unsigned int
448-
xrdp_wm_htoi (const char *ptr)
449-
{
450-
unsigned int value = 0;
451-
char ch = *ptr;
452-
453-
while (ch == ' ' || ch == '\t')
454-
{
455-
ch = *(++ptr);
456-
}
457-
458-
for (;;)
459-
{
460-
if (ch >= '0' && ch <= '9')
461-
{
462-
value = (value << 4) + (ch - '0');
463-
}
464-
else if (ch >= 'A' && ch <= 'F')
465-
{
466-
value = (value << 4) + (ch - 'A' + 10);
467-
}
468-
else if (ch >= 'a' && ch <= 'f')
469-
{
470-
value = (value << 4) + (ch - 'a' + 10);
471-
}
472-
else
473-
{
474-
return value;
475-
}
476-
477-
ch = *(++ptr);
478-
}
479-
}
480-
481445
/*****************************************************************************/
482446
int
483447
xrdp_wm_load_static_colors_plus(struct xrdp_wm *self, char *autorun_name)
@@ -529,47 +493,47 @@ xrdp_wm_load_static_colors_plus(struct xrdp_wm *self, char *autorun_name)
529493
if (g_strcasecmp(val, "black") == 0)
530494
{
531495
val = (char *)list_get_item(values, index);
532-
self->black = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
496+
self->black = HCOLOR(self->screen->bpp, g_htoi(val));
533497
}
534498
else if (g_strcasecmp(val, "grey") == 0)
535499
{
536500
val = (char *)list_get_item(values, index);
537-
self->grey = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
501+
self->grey = HCOLOR(self->screen->bpp, g_htoi(val));
538502
}
539503
else if (g_strcasecmp(val, "dark_grey") == 0)
540504
{
541505
val = (char *)list_get_item(values, index);
542-
self->dark_grey = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
506+
self->dark_grey = HCOLOR(self->screen->bpp, g_htoi(val));
543507
}
544508
else if (g_strcasecmp(val, "blue") == 0)
545509
{
546510
val = (char *)list_get_item(values, index);
547-
self->blue = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
511+
self->blue = HCOLOR(self->screen->bpp, g_htoi(val));
548512
}
549513
else if (g_strcasecmp(val, "dark_blue") == 0)
550514
{
551515
val = (char *)list_get_item(values, index);
552-
self->dark_blue = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
516+
self->dark_blue = HCOLOR(self->screen->bpp, g_htoi(val));
553517
}
554518
else if (g_strcasecmp(val, "white") == 0)
555519
{
556520
val = (char *)list_get_item(values, index);
557-
self->white = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
521+
self->white = HCOLOR(self->screen->bpp, g_htoi(val));
558522
}
559523
else if (g_strcasecmp(val, "red") == 0)
560524
{
561525
val = (char *)list_get_item(values, index);
562-
self->red = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
526+
self->red = HCOLOR(self->screen->bpp, g_htoi(val));
563527
}
564528
else if (g_strcasecmp(val, "green") == 0)
565529
{
566530
val = (char *)list_get_item(values, index);
567-
self->green = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
531+
self->green = HCOLOR(self->screen->bpp, g_htoi(val));
568532
}
569533
else if (g_strcasecmp(val, "background") == 0)
570534
{
571535
val = (char *)list_get_item(values, index);
572-
self->background = HCOLOR(self->screen->bpp, xrdp_wm_htoi(val));
536+
self->background = HCOLOR(self->screen->bpp, g_htoi(val));
573537
}
574538
else if (g_strcasecmp(val, "autorun") == 0)
575539
{

0 commit comments

Comments
 (0)