1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
|
/*
* Copyright 2017 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#define G_LOG_DOMAIN "FuCommon"
#include "config.h"
#include "fu-byte-array.h"
#include "fu-chunk-array.h"
#include "fu-mem.h"
#include "fu-partial-input-stream.h"
#include "fu-string.h"
/**
* fu_strtoull:
* @str: a string, e.g. `0x1234`
* @value: (out) (nullable): parsed value
* @min: minimum acceptable value, typically 0
* @max: maximum acceptable value, typically G_MAXUINT64
* @base: default log base, usually %FU_INTEGER_BASE_AUTO
* @error: (nullable): optional return location for an error
*
* Converts a string value to an integer. If the @value is prefixed with `0x` then the base is
* set to 16 automatically.
*
* Returns: %TRUE if the value was parsed correctly, or %FALSE for error
*
* Since: 2.0.0
**/
gboolean
fu_strtoull(const gchar *str,
guint64 *value,
guint64 min,
guint64 max,
FuIntegerBase base,
GError **error)
{
gchar *endptr = NULL;
guint64 value_tmp;
/* sanity check */
if (str == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse NULL");
return FALSE;
}
/* detect hex */
if (base == FU_INTEGER_BASE_AUTO) {
if (g_str_has_prefix(str, "0x")) {
str += 2;
base = FU_INTEGER_BASE_16;
} else {
base = FU_INTEGER_BASE_10;
}
} else if (base == FU_INTEGER_BASE_16 && g_str_has_prefix(str, "0x")) {
str += 2;
} else if (base == FU_INTEGER_BASE_10 && g_str_has_prefix(str, "0x")) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse 0x-prefixed base-10 string");
return FALSE;
}
/* convert */
value_tmp = g_ascii_strtoull(str, &endptr, base); /* nocheck:blocked */
if ((gsize)(endptr - str) != strlen(str) && *endptr != '\n') {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse datastream");
return FALSE;
}
/* overflow check */
if (value_tmp == G_MAXUINT64) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"parsing datastream caused overflow");
return FALSE;
}
/* range check */
if (value_tmp < min) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"value %" G_GUINT64_FORMAT " was below minimum %" G_GUINT64_FORMAT,
value_tmp,
min);
return FALSE;
}
if (value_tmp > max) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"value %" G_GUINT64_FORMAT " was above maximum %" G_GUINT64_FORMAT,
value_tmp,
max);
return FALSE;
}
/* success */
if (value != NULL)
*value = value_tmp;
return TRUE;
}
/**
* fu_strtoll:
* @str: a string, e.g. `0x1234`, `-12345`
* @value: (out) (nullable): parsed value
* @min: minimum acceptable value, typically 0
* @max: maximum acceptable value, typically G_MAXINT64
* @base: default log base, usually %FU_INTEGER_BASE_AUTO
* @error: (nullable): optional return location for an error
*
* Converts a string value to an integer. Values are assumed base 10, unless
* prefixed with "0x" where they are parsed as base 16.
*
* Returns: %TRUE if the value was parsed correctly, or %FALSE for error
*
* Since: 2.0.0
**/
gboolean
fu_strtoll(const gchar *str,
gint64 *value,
gint64 min,
gint64 max,
FuIntegerBase base,
GError **error)
{
gchar *endptr = NULL;
gint64 value_tmp;
/* sanity check */
if (str == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse NULL");
return FALSE;
}
/* detect hex */
if (base == FU_INTEGER_BASE_AUTO) {
if (g_str_has_prefix(str, "0x")) {
str += 2;
base = FU_INTEGER_BASE_16;
} else {
base = FU_INTEGER_BASE_10;
}
} else if (base == FU_INTEGER_BASE_16 && g_str_has_prefix(str, "0x")) {
str += 2;
} else if (base == FU_INTEGER_BASE_10 && g_str_has_prefix(str, "0x")) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse 0x-prefixed base-10 string");
return FALSE;
}
/* convert */
value_tmp = g_ascii_strtoll(str, &endptr, base); /* nocheck:blocked */
if ((gsize)(endptr - str) != strlen(str) && *endptr != '\n') {
g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA, "cannot parse %s", str);
return FALSE;
}
/* overflow check */
if (value_tmp == G_MAXINT64) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse %s as caused overflow",
str);
return FALSE;
}
/* range check */
if (value_tmp < min) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"value %" G_GINT64_FORMAT " was below minimum %" G_GINT64_FORMAT,
value_tmp,
min);
return FALSE;
}
if (value_tmp > max) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"value %" G_GINT64_FORMAT " was above maximum %" G_GINT64_FORMAT,
value_tmp,
max);
return FALSE;
}
/* success */
if (value != NULL)
*value = value_tmp;
return TRUE;
}
/**
* fu_strtobool:
* @str: a string, e.g. `true`
* @value: (out) (nullable): parsed value
* @error: (nullable): optional return location for an error
*
* Converts a string value to a boolean. Only `true` and `false` are accepted values.
*
* Returns: %TRUE if the value was parsed correctly, or %FALSE for error
*
* Since: 1.8.2
**/
gboolean
fu_strtobool(const gchar *str, gboolean *value, GError **error)
{
/* sanity check */
if (str == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse NULL");
return FALSE;
}
/* be super strict */
if (g_strcmp0(str, "true") == 0) {
if (value != NULL)
*value = TRUE;
return TRUE;
}
if (g_strcmp0(str, "false") == 0) {
if (value != NULL)
*value = FALSE;
return TRUE;
}
/* invalid */
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse %s as boolean, expected true|false",
str);
return FALSE;
}
/**
* fu_strstrip:
* @str: a string, e.g. ` test `
*
* Removes leading and trailing whitespace from a constant string.
*
* Returns: newly allocated string
*
* Since: 1.8.2
**/
gchar *
fu_strstrip(const gchar *str)
{
guint head = G_MAXUINT;
guint tail = 0;
g_return_val_if_fail(str != NULL, NULL);
/* find first non-space char */
for (guint i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ') {
head = i;
break;
}
}
if (head == G_MAXUINT)
return g_strdup("");
/* find last non-space char */
for (guint i = head; str[i] != '\0'; i++) {
if (!g_ascii_isspace(str[i]))
tail = i;
}
return g_strndup(str + head, tail - head + 1);
}
/**
* fu_string_strip:
* @str: a #GString, e.g. ` test `
*
* Removes leading and trailing whitespace from a mutable string.
*
* Since: 2.0.17
**/
void
fu_string_strip(GString *str)
{
guint i;
/* leading whitespace */
for (i = 0; i < str->len; i++) {
if (!g_ascii_isspace(str->str[i]))
break;
}
if (i > 0)
g_string_erase(str, 0, i);
/* trailing whitespace */
for (i = 0; i < str->len; i++) {
if (!g_ascii_isspace(str->str[str->len - (i + 1)]))
break;
}
if (i < str->len)
g_string_truncate(str, str->len - i);
}
/**
* fu_strdup:
* @str: a string, e.g. ` test `
* @bufsz: the maximum size of @str
* @offset: the offset to start copying from
*
* Copies a string from a buffer of a specified size up to (but not including) `NUL`.
*
* Returns: (transfer full): a #GString, possibly of zero size.
*
* Since: 1.8.11
**/
GString *
fu_strdup(const gchar *str, gsize bufsz, gsize offset)
{
GString *substr;
g_return_val_if_fail(str != NULL, NULL);
g_return_val_if_fail(offset < bufsz, NULL);
substr = g_string_new(NULL);
while (offset < bufsz) {
if (str[offset] == '\0')
break;
g_string_append_c(substr, str[offset++]);
}
return substr;
}
/**
* fu_strwidth:
* @text: the string to operate on
*
* Returns the width of the string in displayed characters on the console.
*
* Returns: width of text
*
* Since: 1.8.2
**/
gsize
fu_strwidth(const gchar *text)
{
const gchar *p = text;
gsize width = 0;
g_return_val_if_fail(text != NULL, 0);
while (*p) {
gunichar c = g_utf8_get_char(p);
if (g_unichar_iswide(c))
width += 2;
else if (!g_unichar_iszerowidth(c))
width += 1;
p = g_utf8_next_char(p);
}
return width;
}
/**
* fu_strsplit:
* @str: (not nullable): a string to split
* @sz: size of @str, which must be more than 0
* @delimiter: a string which specifies the places at which to split the string
* @max_tokens: the maximum number of pieces to split @str into
*
* Splits a string into a maximum of @max_tokens pieces, using the given
* delimiter. If @max_tokens is reached, the remainder of string is appended
* to the last token.
*
* Returns: (transfer full): a newly-allocated NULL-terminated array of strings
*
* Since: 1.8.2
**/
gchar **
fu_strsplit(const gchar *str, gsize sz, const gchar *delimiter, gint max_tokens)
{
g_return_val_if_fail(str != NULL, NULL);
g_return_val_if_fail(sz > 0, NULL);
if (str[sz - 1] != '\0') {
g_autofree gchar *str2 = g_strndup(str, sz);
return g_strsplit(str2, delimiter, max_tokens);
}
return g_strsplit(str, delimiter, max_tokens);
}
/**
* fu_strsplit_bytes:
* @blob: (not nullable): a #GBytes
* @delimiter: a string which specifies the places at which to split the string
* @max_tokens: the maximum number of pieces to split @str into
*
* Splits a string into a maximum of @max_tokens pieces, using the given
* delimiter. If @max_tokens is reached, the remainder of string is appended
* to the last token.
*
* Returns: (transfer full): a newly-allocated NULL-terminated array of strings
*
* Since: 2.0.7
**/
gchar **
fu_strsplit_bytes(GBytes *blob, const gchar *delimiter, gint max_tokens)
{
g_return_val_if_fail(blob != NULL, NULL);
return fu_strsplit(g_bytes_get_data(blob, NULL),
g_bytes_get_size(blob),
delimiter,
max_tokens);
}
typedef struct {
FuStrsplitFunc callback;
gpointer user_data;
guint token_idx;
const gchar *delimiter;
gsize delimiter_sz;
gboolean detected_nul;
gboolean more_chunks;
} FuStrsplitHelper;
static gboolean
fu_strsplit_buffer_drain(GByteArray *buf, FuStrsplitHelper *helper, GError **error)
{
gsize buf_offset = 0;
while (buf_offset <= buf->len) {
gsize offset;
g_autoptr(GString) token = g_string_new(NULL);
/* find first match in buffer, starting at the buffer offset */
for (offset = buf_offset; offset < buf->len; offset++) {
if (buf->data[offset] == 0x0) {
helper->detected_nul = TRUE;
break;
}
if (strncmp((const gchar *)buf->data + offset,
helper->delimiter,
helper->delimiter_sz) == 0)
break;
}
/* no token found, keep going */
if (helper->more_chunks && offset == buf->len)
break;
/* sanity check is valid UTF-8 */
g_string_append_len(token,
(const gchar *)buf->data + buf_offset,
offset - buf_offset);
if (!g_utf8_validate_len(token->str, token->len, NULL)) {
g_debug("ignoring invalid UTF-8, got: %s", token->str);
} else {
if (!helper->callback(token, helper->token_idx++, helper->user_data, error))
return FALSE;
}
if (helper->detected_nul) {
buf_offset = buf->len;
break;
}
buf_offset = offset + helper->delimiter_sz;
}
g_byte_array_remove_range(buf, 0, MIN(buf_offset, buf->len));
return TRUE;
}
/**
* fu_strsplit_stream:
* @stream: a #GInputStream to split
* @offset: offset into @stream
* @delimiter: a string which specifies the places at which to split the string
* @callback: (scope call) (closure user_data): a #FuStrsplitFunc.
* @user_data: user data
* @error: (nullable): optional return location for an error
*
* Splits the string, calling the given function for each
* of the tokens found. If any @callback returns %FALSE scanning is aborted.
*
* Use this function in preference to fu_strsplit() when the input file is untrusted,
* and you don't want to allocate a GStrv with billions of one byte items.
*
* Returns: %TRUE if no @callback returned FALSE
*
* Since: 2.0.0
*/
gboolean
fu_strsplit_stream(GInputStream *stream,
gsize offset,
const gchar *delimiter,
FuStrsplitFunc callback,
gpointer user_data,
GError **error)
{
g_autoptr(FuChunkArray) chunks = NULL;
g_autoptr(GByteArray) buf = g_byte_array_new();
g_autoptr(GInputStream) stream_partial = NULL;
FuStrsplitHelper helper = {
.callback = callback,
.user_data = user_data,
.delimiter = delimiter,
.token_idx = 0,
};
g_return_val_if_fail(G_IS_INPUT_STREAM(stream), FALSE);
g_return_val_if_fail(delimiter != NULL && delimiter[0] != '\0', FALSE);
g_return_val_if_fail(callback != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
helper.delimiter_sz = strlen(delimiter);
if (offset > 0) {
stream_partial = fu_partial_input_stream_new(stream, offset, G_MAXSIZE, error);
if (stream_partial == NULL) {
g_prefix_error_literal(error, "failed to cut string: ");
return FALSE;
}
} else {
stream_partial = g_object_ref(stream);
}
chunks = fu_chunk_array_new_from_stream(stream_partial,
FU_CHUNK_ADDR_OFFSET_NONE,
FU_CHUNK_PAGESZ_NONE,
0x8000,
error);
if (chunks == NULL)
return FALSE;
for (gsize i = 0; i < fu_chunk_array_length(chunks); i++) {
g_autoptr(FuChunk) chk = fu_chunk_array_index(chunks, i, error);
if (chk == NULL)
return FALSE;
g_byte_array_append(buf, fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk));
helper.more_chunks = i != fu_chunk_array_length(chunks) - 1;
if (!fu_strsplit_buffer_drain(buf, &helper, error))
return FALSE;
if (helper.detected_nul)
break;
}
return TRUE;
}
/**
* fu_strsplit_full:
* @str: a string to split
* @sz: size of @str, or -1 for unknown
* @delimiter: a string which specifies the places at which to split the string
* @callback: (scope call) (closure user_data): a #FuStrsplitFunc.
* @user_data: user data
* @error: (nullable): optional return location for an error
*
* Splits the string, calling the given function for each
* of the tokens found. If any @callback returns %FALSE scanning is aborted.
*
* Use this function in preference to fu_strsplit() when the input file is untrusted,
* and you don't want to allocate a GStrv with billions of one byte items.
*
* Returns: %TRUE if no @callback returned FALSE
*
* Since: 1.8.2
*/
gboolean
fu_strsplit_full(const gchar *str,
gssize sz,
const gchar *delimiter,
FuStrsplitFunc callback,
gpointer user_data,
GError **error)
{
gsize delimiter_sz;
gsize offset_old = 0;
gsize str_sz;
guint token_idx = 0;
g_return_val_if_fail(str != NULL, FALSE);
g_return_val_if_fail(delimiter != NULL && delimiter[0] != '\0', FALSE);
g_return_val_if_fail(callback != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* make known */
str_sz = sz != -1 ? (gsize)sz : strlen(str);
delimiter_sz = strlen(delimiter);
/* cannot split */
if (delimiter_sz > str_sz) {
g_autoptr(GString) token = g_string_new(str);
return callback(token, token_idx, user_data, error);
}
/* start splittin' */
while (offset_old <= str_sz) {
gsize offset;
g_autoptr(GString) token = g_string_new(NULL);
for (offset = offset_old; offset < str_sz; offset++) {
if (strncmp(str + offset, delimiter, delimiter_sz) == 0)
break;
}
g_string_append_len(token, str + offset_old, offset - offset_old);
if (!callback(token, token_idx++, user_data, error))
return FALSE;
offset_old = offset + delimiter_sz;
}
/* success */
return TRUE;
}
/**
* fu_strsafe:
* @str: (nullable): a string to make safe for printing
* @maxsz: maximum size of returned string, or %G_MAXSIZE for no limit
*
* Converts a string into something that can be safely printed.
*
* Returns: (transfer full): safe string, or %NULL if there was nothing valid
*
* Since: 1.8.2
**/
gchar *
fu_strsafe(const gchar *str, gsize maxsz)
{
gboolean valid = FALSE;
g_autoptr(GString) tmp = g_string_new(NULL);
/* sanity check */
if (str == NULL || maxsz == 0)
return NULL;
/* replace non-printable chars with '.' */
for (gsize i = 0; i < maxsz && str[i] != '\0'; i++) {
if (!g_ascii_isgraph(str[i]) && !g_ascii_isspace(str[i])) {
g_string_append_c(tmp, '.');
continue;
}
g_string_append_c(tmp, str[i]);
if (!g_ascii_isspace(str[i]))
valid = TRUE;
}
/* if just junk, don't return 'all dots' */
if (tmp->len == 0 || !valid)
return NULL;
return g_string_free(g_steal_pointer(&tmp), FALSE);
}
/**
* fu_strsafe_bytes:
* @blob: (not nullable): a #GBytes
* @maxsz: maximum size of returned string, or %G_MAXSIZE for no limit
*
* Converts a #GBytes into something that can be safely printed.
*
* Returns: (transfer full): safe string, or %NULL if there was nothing valid
*
* Since: 2.0.2
**/
gchar *
fu_strsafe_bytes(GBytes *blob, gsize maxsz)
{
g_return_val_if_fail(blob != NULL, NULL);
return fu_strsafe((const gchar *)g_bytes_get_data(blob, NULL),
MIN(g_bytes_get_size(blob), maxsz));
}
/**
* fu_strjoin:
* @separator: (nullable): string to insert between each of the strings
* @array: (element-type utf8): a #GPtrArray
*
* Joins an array of strings together to form one long string, with the optional
* separator inserted between each of them.
*
* If @array has no items, the return value will be an empty string.
* If @array contains a single item, separator will not appear in the resulting
* string.
*
* Returns: a string
*
* Since: 1.8.2
**/
gchar *
fu_strjoin(const gchar *separator, GPtrArray *array)
{
g_autofree const gchar **strv = NULL;
g_return_val_if_fail(array != NULL, NULL);
strv = g_new0(const gchar *, array->len + 1);
for (guint i = 0; i < array->len; i++)
strv[i] = g_ptr_array_index(array, i);
return g_strjoinv(separator, (gchar **)strv);
}
/**
* fu_strpassmask:
* @str: (nullable): a string to make safe for printing
*
* Hides password strings encoded in HTTP requests.
*
* Returns: a string
*
* Since: 1.9.10
**/
gchar *
fu_strpassmask(const gchar *str)
{
g_autoptr(GString) tmp = g_string_new(str);
if (tmp->str != NULL && g_strstr_len(tmp->str, -1, "@") != NULL &&
g_strstr_len(tmp->str, -1, ":") != NULL) {
gboolean is_password = FALSE;
gboolean is_url = FALSE;
for (guint i = 0; i < tmp->len; i++) {
const gchar *url_prefixes[] = {"http://", "https://", NULL};
for (guint j = 0; url_prefixes[j] != NULL; j++) {
if (g_str_has_prefix(tmp->str + i, url_prefixes[j])) {
is_url = TRUE;
i += strlen(url_prefixes[j]);
break;
}
}
if (tmp->str[i] == ' ' || tmp->str[i] == '@' || tmp->str[i] == '/') {
is_url = FALSE;
is_password = FALSE;
continue;
}
if (is_url && tmp->str[i] == ':') {
is_password = TRUE;
continue;
}
if (is_url && is_password) {
if (tmp->str[i] == '@') {
is_password = FALSE;
continue;
}
tmp->str[i] = 'X';
}
}
}
return g_string_free(g_steal_pointer(&tmp), FALSE);
}
/**
* fu_utf16_to_utf8_byte_array:
* @array: a #GByteArray
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Converts a UTF-16 buffer to a UTF-8 string.
*
* Returns: (transfer full): a string, or %NULL on error
*
* Since: 1.9.3
**/
gchar *
fu_utf16_to_utf8_byte_array(GByteArray *array, FuEndianType endian, GError **error)
{
g_autofree guint16 *buf16 = NULL;
g_return_val_if_fail(array != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
if (array->len % 2 != 0) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"invalid UTF-16 buffer length");
return NULL;
}
buf16 = g_new0(guint16, (array->len / sizeof(guint16)) + 1);
for (guint i = 0; i < array->len / 2; i++) {
guint16 data = fu_memread_uint16(array->data + (i * 2), endian);
fu_memwrite_uint16((guint8 *)(buf16 + i), data, G_BYTE_ORDER);
}
return g_utf16_to_utf8(buf16, array->len / sizeof(guint16), NULL, NULL, error);
}
/**
* fu_utf8_to_utf16_byte_array:
* @str: a UTF-8 string
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @flags: a FuUtfConvertFlags, e.g. %FU_UTF_CONVERT_FLAG_APPEND_NUL
* @error: (nullable): optional return location for an error
*
* Converts UTF-8 string to a buffer of UTF-16, optionially including the trailing NULw.
*
* Returns: (transfer full): a #GByteArray, or %NULL on error
*
* Since: 1.9.3
**/
GByteArray *
fu_utf8_to_utf16_byte_array(const gchar *str,
FuEndianType endian,
FuUtfConvertFlags flags,
GError **error)
{
glong buf_utf16sz = 0;
g_autoptr(GByteArray) array = g_byte_array_new();
g_autofree gunichar2 *buf_utf16 = NULL;
g_return_val_if_fail(str != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
buf_utf16 = g_utf8_to_utf16(str, (glong)-1, NULL, &buf_utf16sz, error);
if (buf_utf16 == NULL)
return NULL;
if (flags & FU_UTF_CONVERT_FLAG_APPEND_NUL)
buf_utf16sz += 1;
for (glong i = 0; i < buf_utf16sz; i++) {
guint16 data = fu_memread_uint16((guint8 *)(buf_utf16 + i), G_BYTE_ORDER);
fu_byte_array_append_uint16(array, data, endian);
}
return g_steal_pointer(&array);
}
/**
* fu_utf16_to_utf8_bytes:
* @bytes: a #GBytes
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Converts a UTF-16 buffer to a UTF-8 string.
*
* Returns: (transfer full): a string, or %NULL on error
*
* Since: 1.9.3
**/
gchar *
fu_utf16_to_utf8_bytes(GBytes *bytes, FuEndianType endian, GError **error)
{
GByteArray array = {0x0};
g_return_val_if_fail(bytes != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
array.data = (guint8 *)g_bytes_get_data(bytes, NULL);
array.len = g_bytes_get_size(bytes);
return fu_utf16_to_utf8_byte_array(&array, endian, error);
}
/**
* fu_utf8_to_utf16_bytes:
* @str: a UTF-8 string
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Converts UTF-8 string to a buffer of UTF-16, optionally including the trailing NULw.
*
* Returns: (transfer full): a #GBytes, or %NULL on error
*
* Since: 1.9.3
**/
GBytes *
fu_utf8_to_utf16_bytes(const gchar *str,
FuEndianType endian,
FuUtfConvertFlags flags,
GError **error)
{
g_autoptr(GByteArray) buf = NULL;
g_return_val_if_fail(str != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
buf = fu_utf8_to_utf16_byte_array(str, endian, flags, error);
if (buf == NULL)
return NULL;
return g_bytes_new(buf->data, buf->len);
}
|