diff --git a/contrib/migrate.py b/contrib/migrate.py new file mode 100755 index 000000000..cbcffdbda --- /dev/null +++ b/contrib/migrate.py @@ -0,0 +1,62 @@ +#!/usr/bin/python3 +# +# Copyright (C) 2022 Richard Hughes +# +# SPDX-License-Identifier: LGPL-2.1+ +# + +# import os +import sys +import glob + + +if __name__ == "__main__": + + fns = [] + + if len(sys.argv) > 1: + fns.extend(sys.argv[1:]) + else: + exts = ["c", "h", "map"] + for ext in exts: + for fn in glob.glob("**/*.{}".format(ext), recursive=True): + if fn.startswith("build"): + continue + if fn.startswith("subprojects"): + continue + if fn.startswith(".git"): + continue + fns.append(fn) + + for fn in fns: + modified: bool = False + with open(fn, "r") as f: + buf = f.read() + for old, new in { + "fu_common_sum8": "fu_sum8", + "fu_common_sum8_bytes": "fu_sum8_bytes", + "fu_common_sum16": "fu_sum16", + "fu_common_sum16_bytes": "fu_sum16_bytes", + "fu_common_sum16w": "fu_sum16w", + "fu_common_sum16w_bytes": "fu_sum16w_bytes", + "fu_common_sum32": "fu_sum32", + "fu_common_sum32_bytes": "fu_sum32_bytes", + "fu_common_sum32w": "fu_sum32w", + "fu_common_sum32w_bytes": "fu_sum32w_bytes", + "fu_common_crc8": "fu_crc8", + "fu_common_crc8_full": "fu_crc8_full", + "fu_common_crc16": "fu_crc16", + "fu_common_crc16_full": "fu_crc16_full", + "fu_common_crc32": "fu_crc32", + "fu_common_crc32_full": "fu_crc32_full", + }.items(): + if buf.find(old) == -1: + continue + buf = buf.replace(old, new) + modified = True + if modified: + print("MODIFIED: {}".format(fn)) + with open(fn, "w") as f: + f.write(buf) + + sys.exit(0) diff --git a/libfwupdplugin/README.md b/libfwupdplugin/README.md index f009b09e4..352f2374d 100644 --- a/libfwupdplugin/README.md +++ b/libfwupdplugin/README.md @@ -4,6 +4,8 @@ This library is only partially API and ABI stable. Keeping unused, unsafe and deprecated functions around forever is a maintenance burden and so symbols are removed when branching for new minor versions. +Use `./contrib/migrate.py` to migrate up out-of-tree plugins to the new API. + Remember: Plugins should be upstream! ## 1.5.5 @@ -58,3 +60,5 @@ Remember: Plugins should be upstream! * `fu_backend_coldplug`: Now requires a `FuProgress`, although it can be ignored. * `FuPluginVfuncs->setup`: Now requires a `FuProgress`, although it can be ignored. * `FuPluginVfuncs->coldplug`: Now requires a `FuProgress`, although it can be ignored. +* `fu_common_crc*`: Use `fu_crc` prefix, i.e. remove the `_common` +* `fu_common_sum*`: Use `fu_sum` prefix, i.e. remove the `_common` diff --git a/libfwupdplugin/fu-common.c b/libfwupdplugin/fu-common.c index 710a47b08..d5b1f5a6a 100644 --- a/libfwupdplugin/fu-common.c +++ b/libfwupdplugin/fu-common.c @@ -3631,343 +3631,6 @@ fu_common_get_esp_for_path(const gchar *esp_path, GError **error) return NULL; } -/** - * fu_common_crc8_full: - * @buf: memory buffer - * @bufsz: size of @buf - * @crc_init: initial CRC value, typically 0x00 - * @polynomial: CRC polynomial, e.g. 0x07 for CCITT - * - * Returns the cyclic redundancy check value for the given memory buffer. - * - * Returns: CRC value - * - * Since: 1.7.1 - **/ -guint8 -fu_common_crc8_full(const guint8 *buf, gsize bufsz, guint8 crc_init, guint8 polynomial) -{ - guint32 crc = crc_init; - for (gsize j = bufsz; j > 0; j--) { - crc ^= (*(buf++) << 8); - for (guint32 i = 8; i; i--) { - if (crc & 0x8000) - crc ^= ((polynomial | 0x100) << 7); - crc <<= 1; - } - } - return ~((guint8)(crc >> 8)); -} - -/** - * fu_common_crc8: - * @buf: memory buffer - * @bufsz: size of @buf - * - * Returns the cyclic redundancy check value for the given memory buffer. - * - * Returns: CRC value - * - * Since: 1.5.0 - **/ -guint8 -fu_common_crc8(const guint8 *buf, gsize bufsz) -{ - return fu_common_crc8_full(buf, bufsz, 0x00, 0x07); -} - -/** - * fu_common_crc16_full: - * @buf: memory buffer - * @bufsz: size of @buf - * @crc: initial CRC value, typically 0xFFFF - * @polynomial: CRC polynomial, typically 0xA001 for IBM or 0x1021 for CCITT - * - * Returns the cyclic redundancy check value for the given memory buffer. - * - * Returns: CRC value - * - * Since: 1.6.2 - **/ -guint16 -fu_common_crc16_full(const guint8 *buf, gsize bufsz, guint16 crc, guint16 polynomial) -{ - for (gsize len = bufsz; len > 0; len--) { - crc = (guint16)(crc ^ (*buf++)); - for (guint8 i = 0; i < 8; i++) { - if (crc & 0x1) { - crc = (crc >> 1) ^ polynomial; - } else { - crc >>= 1; - } - } - } - return ~crc; -} - -/** - * fu_common_crc16: - * @buf: memory buffer - * @bufsz: size of @buf - * - * Returns the CRC-16-IBM cyclic redundancy value for the given memory buffer. - * - * Returns: CRC value - * - * Since: 1.5.0 - **/ -guint16 -fu_common_crc16(const guint8 *buf, gsize bufsz) -{ - return fu_common_crc16_full(buf, bufsz, 0xFFFF, 0xA001); -} - -/** - * fu_common_crc32_full: - * @buf: memory buffer - * @bufsz: size of @buf - * @crc: initial CRC value, typically 0xFFFFFFFF - * @polynomial: CRC polynomial, typically 0xEDB88320 - * - * Returns the cyclic redundancy check value for the given memory buffer. - * - * Returns: CRC value - * - * Since: 1.5.0 - **/ -guint32 -fu_common_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial) -{ - for (guint32 idx = 0; idx < bufsz; idx++) { - guint8 data = *buf++; - crc = crc ^ data; - for (guint32 bit = 0; bit < 8; bit++) { - guint32 mask = -(crc & 1); - crc = (crc >> 1) ^ (polynomial & mask); - } - } - return ~crc; -} - -/** - * fu_common_crc32: - * @buf: memory buffer - * @bufsz: size of @buf - * - * Returns the cyclic redundancy check value for the given memory buffer. - * - * Returns: CRC value - * - * Since: 1.5.0 - **/ -guint32 -fu_common_crc32(const guint8 *buf, gsize bufsz) -{ - return fu_common_crc32_full(buf, bufsz, 0xFFFFFFFF, 0xEDB88320); -} - -/** - * fu_common_sum8: - * @buf: memory buffer - * @bufsz: size of @buf - * - * Returns the arithmetic sum of all bytes in @buf. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint8 -fu_common_sum8(const guint8 *buf, gsize bufsz) -{ - guint8 checksum = 0; - g_return_val_if_fail(buf != NULL, G_MAXUINT8); - for (gsize i = 0; i < bufsz; i++) - checksum += buf[i]; - return checksum; -} - -/** - * fu_common_sum8_bytes: - * @blob: a #GBytes - * - * Returns the arithmetic sum of all bytes in @blob. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint8 -fu_common_sum8_bytes(GBytes *blob) -{ - g_return_val_if_fail(blob != NULL, G_MAXUINT8); - if (g_bytes_get_size(blob) == 0) - return 0; - return fu_common_sum8(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); -} - -/** - * fu_common_sum16: - * @buf: memory buffer - * @bufsz: size of @buf - * - * Returns the arithmetic sum of all bytes in @buf, adding them one byte at a time. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint16 -fu_common_sum16(const guint8 *buf, gsize bufsz) -{ - guint16 checksum = 0; - g_return_val_if_fail(buf != NULL, G_MAXUINT16); - for (gsize i = 0; i < bufsz; i++) - checksum += buf[i]; - return checksum; -} - -/** - * fu_common_sum16_bytes: - * @blob: a #GBytes - * - * Returns the arithmetic sum of all bytes in @blob, adding them one byte at a time. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint16 -fu_common_sum16_bytes(GBytes *blob) -{ - g_return_val_if_fail(blob != NULL, G_MAXUINT16); - return fu_common_sum16(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); -} - -/** - * fu_common_sum16w: - * @buf: memory buffer - * @bufsz: size of @buf - * @endian: an endian type, e.g. %G_LITTLE_ENDIAN - * - * Returns the arithmetic sum of all bytes in @buf, adding them one word at a time. - * The caller must ensure that @bufsz is a multiple of 2. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint16 -fu_common_sum16w(const guint8 *buf, gsize bufsz, FuEndianType endian) -{ - guint16 checksum = 0; - g_return_val_if_fail(buf != NULL, G_MAXUINT16); - g_return_val_if_fail(bufsz % 2 == 0, G_MAXUINT16); - for (gsize i = 0; i < bufsz; i += 2) - checksum += fu_common_read_uint16(&buf[i], endian); - return checksum; -} - -/** - * fu_common_sum16w_bytes: - * @blob: a #GBytes - * @endian: an endian type, e.g. %G_LITTLE_ENDIAN - * - * Returns the arithmetic sum of all bytes in @blob, adding them one word at a time. - * The caller must ensure that the size of @blob is a multiple of 2. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint16 -fu_common_sum16w_bytes(GBytes *blob, FuEndianType endian) -{ - g_return_val_if_fail(blob != NULL, G_MAXUINT16); - return fu_common_sum16w(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob), endian); -} - -/** - * fu_common_sum32: - * @buf: memory buffer - * @bufsz: size of @buf - * - * Returns the arithmetic sum of all bytes in @buf, adding them one byte at a time. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint32 -fu_common_sum32(const guint8 *buf, gsize bufsz) -{ - guint32 checksum = 0; - g_return_val_if_fail(buf != NULL, G_MAXUINT32); - for (gsize i = 0; i < bufsz; i++) - checksum += buf[i]; - return checksum; -} - -/** - * fu_common_sum32_bytes: - * @blob: a #GBytes - * - * Returns the arithmetic sum of all bytes in @blob, adding them one byte at a time. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint32 -fu_common_sum32_bytes(GBytes *blob) -{ - g_return_val_if_fail(blob != NULL, G_MAXUINT32); - return fu_common_sum32(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); -} - -/** - * fu_common_sum32w: - * @buf: memory buffer - * @bufsz: size of @buf - * @endian: an endian type, e.g. %G_LITTLE_ENDIAN - * - * Returns the arithmetic sum of all bytes in @buf, adding them one dword at a time. - * The caller must ensure that @bufsz is a multiple of 4. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint32 -fu_common_sum32w(const guint8 *buf, gsize bufsz, FuEndianType endian) -{ - guint32 checksum = 0; - g_return_val_if_fail(buf != NULL, G_MAXUINT32); - g_return_val_if_fail(bufsz % 4 == 0, G_MAXUINT32); - for (gsize i = 0; i < bufsz; i += 4) - checksum += fu_common_read_uint32(&buf[i], endian); - return checksum; -} - -/** - * fu_common_sum32w_bytes: - * @blob: a #GBytes - * @endian: an endian type, e.g. %G_LITTLE_ENDIAN - * - * Returns the arithmetic sum of all bytes in @blob, adding them one dword at a time. - * The caller must ensure that the size of @blob is a multiple of 4. - * - * Returns: sum value - * - * Since: 1.7.3 - **/ -guint32 -fu_common_sum32w_bytes(GBytes *blob, FuEndianType endian) -{ - g_return_val_if_fail(blob != NULL, G_MAXUINT32); - return fu_common_sum32w(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob), endian); -} - /** * fu_common_reverse_uint8: * @value: integer diff --git a/libfwupdplugin/fu-common.h b/libfwupdplugin/fu-common.h index 4b6a6d7df..0da29f5bb 100644 --- a/libfwupdplugin/fu-common.h +++ b/libfwupdplugin/fu-common.h @@ -441,43 +441,9 @@ fu_common_get_esp_default(GError **error) G_GNUC_WARN_UNUSED_RESULT; gboolean fu_common_check_full_disk_encryption(GError **error); -guint8 -fu_common_crc8(const guint8 *buf, gsize bufsz); -guint8 -fu_common_crc8_full(const guint8 *buf, gsize bufsz, guint8 crc_init, guint8 polynomial); -guint16 -fu_common_crc16(const guint8 *buf, gsize bufsz); -guint16 -fu_common_crc16_full(const guint8 *buf, gsize bufsz, guint16 crc, guint16 polynomial); -guint32 -fu_common_crc32(const guint8 *buf, gsize bufsz); -guint32 -fu_common_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial); - guint8 fu_common_reverse_uint8(guint8 value); -guint8 -fu_common_sum8(const guint8 *buf, gsize bufsz); -guint8 -fu_common_sum8_bytes(GBytes *blob); -guint16 -fu_common_sum16(const guint8 *buf, gsize bufsz); -guint16 -fu_common_sum16_bytes(GBytes *blob); -guint16 -fu_common_sum16w(const guint8 *buf, gsize bufsz, FuEndianType endian); -guint16 -fu_common_sum16w_bytes(GBytes *blob, FuEndianType endian); -guint32 -fu_common_sum32(const guint8 *buf, gsize bufsz); -guint32 -fu_common_sum32_bytes(GBytes *blob); -guint32 -fu_common_sum32w(const guint8 *buf, gsize bufsz, FuEndianType endian); -guint32 -fu_common_sum32w_bytes(GBytes *blob, FuEndianType endian); - gchar * fu_common_uri_get_scheme(const gchar *uri); gsize diff --git a/libfwupdplugin/fu-crc.c b/libfwupdplugin/fu-crc.c new file mode 100644 index 000000000..9502b1d18 --- /dev/null +++ b/libfwupdplugin/fu-crc.c @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2017 Richard Hughes + * + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#define G_LOG_DOMAIN "FuCommon" + +#include "config.h" + +#include "fu-crc.h" + +/** + * fu_crc8_full: + * @buf: memory buffer + * @bufsz: size of @buf + * @crc_init: initial CRC value, typically 0x00 + * @polynomial: CRC polynomial, e.g. 0x07 for CCITT + * + * Returns the cyclic redundancy check value for the given memory buffer. + * + * Returns: CRC value + * + * Since: 1.8.2 + **/ +guint8 +fu_crc8_full(const guint8 *buf, gsize bufsz, guint8 crc_init, guint8 polynomial) +{ + guint32 crc = crc_init; + for (gsize j = bufsz; j > 0; j--) { + crc ^= (*(buf++) << 8); + for (guint32 i = 8; i; i--) { + if (crc & 0x8000) + crc ^= ((polynomial | 0x100) << 7); + crc <<= 1; + } + } + return ~((guint8)(crc >> 8)); +} + +/** + * fu_crc8: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the cyclic redundancy check value for the given memory buffer. + * + * Returns: CRC value + * + * Since: 1.8.2 + **/ +guint8 +fu_crc8(const guint8 *buf, gsize bufsz) +{ + return fu_crc8_full(buf, bufsz, 0x00, 0x07); +} + +/** + * fu_crc16_full: + * @buf: memory buffer + * @bufsz: size of @buf + * @crc: initial CRC value, typically 0xFFFF + * @polynomial: CRC polynomial, typically 0xA001 for IBM or 0x1021 for CCITT + * + * Returns the cyclic redundancy check value for the given memory buffer. + * + * Returns: CRC value + * + * Since: 1.8.2 + **/ +guint16 +fu_crc16_full(const guint8 *buf, gsize bufsz, guint16 crc, guint16 polynomial) +{ + for (gsize len = bufsz; len > 0; len--) { + crc = (guint16)(crc ^ (*buf++)); + for (guint8 i = 0; i < 8; i++) { + if (crc & 0x1) { + crc = (crc >> 1) ^ polynomial; + } else { + crc >>= 1; + } + } + } + return ~crc; +} + +/** + * fu_crc16: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the CRC-16-IBM cyclic redundancy value for the given memory buffer. + * + * Returns: CRC value + * + * Since: 1.8.2 + **/ +guint16 +fu_crc16(const guint8 *buf, gsize bufsz) +{ + return fu_crc16_full(buf, bufsz, 0xFFFF, 0xA001); +} + +/** + * fu_crc32_full: + * @buf: memory buffer + * @bufsz: size of @buf + * @crc: initial CRC value, typically 0xFFFFFFFF + * @polynomial: CRC polynomial, typically 0xEDB88320 + * + * Returns the cyclic redundancy check value for the given memory buffer. + * + * Returns: CRC value + * + * Since: 1.8.2 + **/ +guint32 +fu_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial) +{ + for (guint32 idx = 0; idx < bufsz; idx++) { + guint8 data = *buf++; + crc = crc ^ data; + for (guint32 bit = 0; bit < 8; bit++) { + guint32 mask = -(crc & 1); + crc = (crc >> 1) ^ (polynomial & mask); + } + } + return ~crc; +} + +/** + * fu_crc32: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the cyclic redundancy check value for the given memory buffer. + * + * Returns: CRC value + * + * Since: 1.8.2 + **/ +guint32 +fu_crc32(const guint8 *buf, gsize bufsz) +{ + return fu_crc32_full(buf, bufsz, 0xFFFFFFFF, 0xEDB88320); +} diff --git a/libfwupdplugin/fu-crc.h b/libfwupdplugin/fu-crc.h new file mode 100644 index 000000000..49cdb6e40 --- /dev/null +++ b/libfwupdplugin/fu-crc.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2017 Richard Hughes + * + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#pragma once + +#include "fu-common.h" + +guint8 +fu_crc8(const guint8 *buf, gsize bufsz); +guint8 +fu_crc8_full(const guint8 *buf, gsize bufsz, guint8 crc_init, guint8 polynomial); +guint16 +fu_crc16(const guint8 *buf, gsize bufsz); +guint16 +fu_crc16_full(const guint8 *buf, gsize bufsz, guint16 crc, guint16 polynomial); +guint32 +fu_crc32(const guint8 *buf, gsize bufsz); +guint32 +fu_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial); diff --git a/libfwupdplugin/fu-dfu-firmware.c b/libfwupdplugin/fu-dfu-firmware.c index 57a6c5110..85c3877e1 100644 --- a/libfwupdplugin/fu-dfu-firmware.c +++ b/libfwupdplugin/fu-dfu-firmware.c @@ -11,6 +11,7 @@ #include #include "fu-common.h" +#include "fu-crc.h" #include "fu-dfu-firmware-private.h" /** @@ -245,7 +246,7 @@ fu_dfu_firmware_parse_footer(FuDfuFirmware *self, return FALSE; crc = GUINT32_FROM_LE(ftr.crc); if ((flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { - crc_new = ~fu_common_crc32(data, len - 4); + crc_new = ~fu_crc32(data, len - 4); if (crc != crc_new) { g_set_error(error, FWUPD_ERROR, @@ -323,7 +324,7 @@ fu_dfu_firmware_append_footer(FuDfuFirmware *self, GBytes *contents, GError **er fu_byte_array_append_uint16(buf, priv->dfu_version, G_LITTLE_ENDIAN); g_byte_array_append(buf, (const guint8 *)"UFD", 3); fu_byte_array_append_uint8(buf, sizeof(FuDfuFirmwareFooter)); - fu_byte_array_append_uint32(buf, ~fu_common_crc32(buf->data, buf->len), G_LITTLE_ENDIAN); + fu_byte_array_append_uint32(buf, ~fu_crc32(buf->data, buf->len), G_LITTLE_ENDIAN); return g_byte_array_free_to_bytes(buf); } diff --git a/libfwupdplugin/fu-efi-firmware-file.c b/libfwupdplugin/fu-efi-firmware-file.c index 067236292..d510e9851 100644 --- a/libfwupdplugin/fu-efi-firmware-file.c +++ b/libfwupdplugin/fu-efi-firmware-file.c @@ -12,6 +12,7 @@ #include "fu-efi-firmware-common.h" #include "fu-efi-firmware-file.h" #include "fu-efi-firmware-section.h" +#include "fu-sum.h" typedef struct { guint8 type; @@ -254,7 +255,7 @@ fu_efi_firmware_file_parse(FuFirmware *firmware, /* verify data checksum */ if ((priv->attrib & FU_EFI_FIRMWARE_FILE_ATTRIB_CHECKSUM) > 0 && (flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { - guint8 data_checksum_verify = 0x100 - fu_common_sum8_bytes(blob); + guint8 data_checksum_verify = 0x100 - fu_sum8_bytes(blob); if (data_checksum_verify != data_checksum) { g_set_error(error, FWUPD_ERROR, @@ -333,7 +334,7 @@ fu_efi_firmware_file_write(FuFirmware *firmware, GError **error) return NULL; g_byte_array_append(buf, (guint8 *)&guid, sizeof(guid)); fu_byte_array_append_uint8(buf, 0x0); /* hdr_checksum */ - fu_byte_array_append_uint8(buf, 0x100 - fu_common_sum8_bytes(blob)); + fu_byte_array_append_uint8(buf, 0x100 - fu_sum8_bytes(blob)); fu_byte_array_append_uint8(buf, priv->type); /* data_checksum */ fu_byte_array_append_uint8(buf, priv->attrib); /* data_checksum */ fu_byte_array_append_uint32(buf, diff --git a/libfwupdplugin/fu-self-test.c b/libfwupdplugin/fu-self-test.c index 1a17c2ed1..fb362d855 100644 --- a/libfwupdplugin/fu-self-test.c +++ b/libfwupdplugin/fu-self-test.c @@ -180,9 +180,9 @@ static void fu_common_crc_func(void) { guint8 buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; - g_assert_cmpint(fu_common_crc8(buf, sizeof(buf)), ==, 0x7A); - g_assert_cmpint(fu_common_crc16(buf, sizeof(buf)), ==, 0x4DF1); - g_assert_cmpint(fu_common_crc32(buf, sizeof(buf)), ==, 0x40EFAB9E); + g_assert_cmpint(fu_crc8(buf, sizeof(buf)), ==, 0x7A); + g_assert_cmpint(fu_crc16(buf, sizeof(buf)), ==, 0x4DF1); + g_assert_cmpint(fu_crc32(buf, sizeof(buf)), ==, 0x40EFAB9E); } static void diff --git a/libfwupdplugin/fu-sum.c b/libfwupdplugin/fu-sum.c new file mode 100644 index 000000000..ae006bbd6 --- /dev/null +++ b/libfwupdplugin/fu-sum.c @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2017 Richard Hughes + * + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#define G_LOG_DOMAIN "FuCommon" + +#include "config.h" + +#include "fu-common.h" +#include "fu-sum.h" + +/** + * fu_sum8: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the arithmetic sum of all bytes in @buf. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint8 +fu_sum8(const guint8 *buf, gsize bufsz) +{ + guint8 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT8); + for (gsize i = 0; i < bufsz; i++) + checksum += buf[i]; + return checksum; +} + +/** + * fu_sum8_bytes: + * @blob: a #GBytes + * + * Returns the arithmetic sum of all bytes in @blob. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint8 +fu_sum8_bytes(GBytes *blob) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT8); + if (g_bytes_get_size(blob) == 0) + return 0; + return fu_sum8(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); +} + +/** + * fu_sum16: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the arithmetic sum of all bytes in @buf, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint16 +fu_sum16(const guint8 *buf, gsize bufsz) +{ + guint16 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT16); + for (gsize i = 0; i < bufsz; i++) + checksum += buf[i]; + return checksum; +} + +/** + * fu_sum16_bytes: + * @blob: a #GBytes + * + * Returns the arithmetic sum of all bytes in @blob, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint16 +fu_sum16_bytes(GBytes *blob) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT16); + return fu_sum16(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); +} + +/** + * fu_sum16w: + * @buf: memory buffer + * @bufsz: size of @buf + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @buf, adding them one word at a time. + * The caller must ensure that @bufsz is a multiple of 2. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint16 +fu_sum16w(const guint8 *buf, gsize bufsz, FuEndianType endian) +{ + guint16 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT16); + g_return_val_if_fail(bufsz % 2 == 0, G_MAXUINT16); + for (gsize i = 0; i < bufsz; i += 2) + checksum += fu_common_read_uint16(&buf[i], endian); + return checksum; +} + +/** + * fu_sum16w_bytes: + * @blob: a #GBytes + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @blob, adding them one word at a time. + * The caller must ensure that the size of @blob is a multiple of 2. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint16 +fu_sum16w_bytes(GBytes *blob, FuEndianType endian) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT16); + return fu_sum16w(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob), endian); +} + +/** + * fu_sum32: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the arithmetic sum of all bytes in @buf, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint32 +fu_sum32(const guint8 *buf, gsize bufsz) +{ + guint32 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT32); + for (gsize i = 0; i < bufsz; i++) + checksum += buf[i]; + return checksum; +} + +/** + * fu_sum32_bytes: + * @blob: a #GBytes + * + * Returns the arithmetic sum of all bytes in @blob, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint32 +fu_sum32_bytes(GBytes *blob) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT32); + return fu_sum32(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); +} + +/** + * fu_sum32w: + * @buf: memory buffer + * @bufsz: size of @buf + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @buf, adding them one dword at a time. + * The caller must ensure that @bufsz is a multiple of 4. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint32 +fu_sum32w(const guint8 *buf, gsize bufsz, FuEndianType endian) +{ + guint32 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT32); + g_return_val_if_fail(bufsz % 4 == 0, G_MAXUINT32); + for (gsize i = 0; i < bufsz; i += 4) + checksum += fu_common_read_uint32(&buf[i], endian); + return checksum; +} + +/** + * fu_sum32w_bytes: + * @blob: a #GBytes + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @blob, adding them one dword at a time. + * The caller must ensure that the size of @blob is a multiple of 4. + * + * Returns: sum value + * + * Since: 1.8.2 + **/ +guint32 +fu_sum32w_bytes(GBytes *blob, FuEndianType endian) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT32); + return fu_sum32w(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob), endian); +} diff --git a/libfwupdplugin/fu-sum.h b/libfwupdplugin/fu-sum.h new file mode 100644 index 000000000..3d174af45 --- /dev/null +++ b/libfwupdplugin/fu-sum.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017 Richard Hughes + * + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#pragma once + +#include "fu-common.h" + +guint8 +fu_sum8(const guint8 *buf, gsize bufsz); +guint8 +fu_sum8_bytes(GBytes *blob); +guint16 +fu_sum16(const guint8 *buf, gsize bufsz); +guint16 +fu_sum16_bytes(GBytes *blob); +guint16 +fu_sum16w(const guint8 *buf, gsize bufsz, FuEndianType endian); +guint16 +fu_sum16w_bytes(GBytes *blob, FuEndianType endian); +guint32 +fu_sum32(const guint8 *buf, gsize bufsz); +guint32 +fu_sum32_bytes(GBytes *blob); +guint32 +fu_sum32w(const guint8 *buf, gsize bufsz, FuEndianType endian); +guint32 +fu_sum32w_bytes(GBytes *blob, FuEndianType endian); diff --git a/libfwupdplugin/fwupdplugin.h b/libfwupdplugin/fwupdplugin.h index 55d48a5a8..da71cd077 100644 --- a/libfwupdplugin/fwupdplugin.h +++ b/libfwupdplugin/fwupdplugin.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,7 @@ #include #include #include +#include #include #include #include diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index ba46921e5..11c12a4f2 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -523,10 +523,6 @@ LIBFWUPDPLUGIN_1.5.0 { global: fu_byte_array_set_size; fu_common_cpuid; - fu_common_crc16; - fu_common_crc32; - fu_common_crc32_full; - fu_common_crc8; fu_common_filename_glob; fu_device_bind_driver; fu_device_dump_firmware; @@ -791,7 +787,6 @@ LIBFWUPDPLUGIN_1.6.1 { LIBFWUPDPLUGIN_1.6.2 { global: fu_common_check_kernel_version; - fu_common_crc16_full; fu_common_get_firmware_search_path; fu_common_reset_firmware_search_path; fu_common_set_firmware_search_path; @@ -919,7 +914,6 @@ LIBFWUPDPLUGIN_1.7.1 { fu_cfi_device_set_flash_id; fu_cfi_device_set_size; fu_common_check_full_disk_encryption; - fu_common_crc8_full; fu_common_mkdir; fu_device_add_string; fu_device_get_internal_flags; @@ -950,16 +944,6 @@ LIBFWUPDPLUGIN_1.7.3 { fu_cfi_device_set_page_size; fu_cfi_device_set_sector_size; fu_common_strtoull_full; - fu_common_sum16; - fu_common_sum16_bytes; - fu_common_sum16w; - fu_common_sum16w_bytes; - fu_common_sum32; - fu_common_sum32_bytes; - fu_common_sum32w; - fu_common_sum32w_bytes; - fu_common_sum8; - fu_common_sum8_bytes; local: *; } LIBFWUPDPLUGIN_1.7.2; @@ -1041,11 +1025,27 @@ LIBFWUPDPLUGIN_1.8.1 { LIBFWUPDPLUGIN_1.8.2 { global: + fu_crc16; + fu_crc16_full; + fu_crc32; + fu_crc32_full; + fu_crc8; + fu_crc8_full; fu_device_new; fu_efivar_secure_boot_enabled; fu_progress_add_step; fu_progress_get_name; fu_progress_set_name; + fu_sum16; + fu_sum16_bytes; + fu_sum16w; + fu_sum16w_bytes; + fu_sum32; + fu_sum32_bytes; + fu_sum32w; + fu_sum32w_bytes; + fu_sum8; + fu_sum8_bytes; fu_udev_device_ioctl; fu_udev_device_new; fu_udev_device_pread; diff --git a/libfwupdplugin/meson.build b/libfwupdplugin/meson.build index 1afa28e12..2a9ef6c26 100644 --- a/libfwupdplugin/meson.build +++ b/libfwupdplugin/meson.build @@ -15,6 +15,8 @@ fwupdplugin_src = [ 'fu-cabinet.c', 'fu-chunk.c', # fuzzing 'fu-common.c', # fuzzing + 'fu-sum.c', # fuzzing + 'fu-crc.c', # fuzzing 'fu-common-cab.c', 'fu-common-guid.c', 'fu-common-version.c', # fuzzing @@ -87,6 +89,8 @@ fwupdplugin_headers = [ 'fu-cabinet.h', 'fu-chunk.h', 'fu-common.h', + 'fu-sum.h', + 'fu-crc.h', 'fu-common-cab.h', 'fu-common-guid.h', 'fu-common-version.h', diff --git a/plugins/acpi-phat/fu-acpi-phat.c b/plugins/acpi-phat/fu-acpi-phat.c index 6eb76a564..71b07531d 100644 --- a/plugins/acpi-phat/fu-acpi-phat.c +++ b/plugins/acpi-phat/fu-acpi-phat.c @@ -163,7 +163,7 @@ fu_acpi_phat_parse(FuFirmware *firmware, /* verify checksum */ if ((flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { - guint8 checksum = fu_common_sum8(buf, length); + guint8 checksum = fu_sum8(buf, length); if (checksum != 0x00) { g_set_error(error, G_IO_ERROR, @@ -273,7 +273,7 @@ fu_acpi_phat_write(FuFirmware *firmware, GError **error) g_byte_array_append(buf, buf2->data, buf2->len); /* fixup checksum */ - buf->data[9] = 0xFF - fu_common_sum8(buf->data, buf->len); + buf->data[9] = 0xFF - fu_sum8(buf->data, buf->len); /* success */ return g_byte_array_free_to_bytes(g_steal_pointer(&buf)); diff --git a/plugins/bcm57xx/fu-bcm57xx-common.c b/plugins/bcm57xx/fu-bcm57xx-common.c index 36ca72bde..715c39f80 100644 --- a/plugins/bcm57xx/fu-bcm57xx-common.c +++ b/plugins/bcm57xx/fu-bcm57xx-common.c @@ -16,7 +16,7 @@ guint32 fu_bcm57xx_nvram_crc(const guint8 *buf, gsize bufsz) { - return fu_common_crc32(buf, bufsz); + return fu_crc32(buf, bufsz); } gboolean diff --git a/plugins/ccgx/fu-ccgx-firmware.c b/plugins/ccgx/fu-ccgx-firmware.c index 9ee43deae..421317286 100644 --- a/plugins/ccgx/fu-ccgx-firmware.c +++ b/plugins/ccgx/fu-ccgx-firmware.c @@ -227,7 +227,7 @@ fu_ccgx_firmware_parse_md_block(FuCcgxFirmware *self, FwupdInstallFlags flags, G } for (guint i = 0; i < self->records->len - 1; i++) { rcd = g_ptr_array_index(self->records, i); - checksum_calc += fu_common_sum8_bytes(rcd->data); + checksum_calc += fu_sum8_bytes(rcd->data); fw_size += g_bytes_get_size(rcd->data); } if (fw_size != metadata.fw_size) { diff --git a/plugins/elantp/fu-elantp-hid-device.c b/plugins/elantp/fu-elantp-hid-device.c index 72fdb8395..def283bb6 100644 --- a/plugins/elantp/fu-elantp-hid-device.c +++ b/plugins/elantp/fu-elantp-hid-device.c @@ -347,9 +347,8 @@ fu_elantp_hid_device_write_firmware(FuDevice *device, chunks = fu_chunk_array_new(buf + iap_addr, bufsz - iap_addr, 0x0, 0x0, self->fw_page_size); for (guint i = 0; i < chunks->len; i++) { FuChunk *chk = g_ptr_array_index(chunks, i); - guint16 csum_tmp = fu_common_sum16w(fu_chunk_get_data(chk), - fu_chunk_get_data_sz(chk), - G_LITTLE_ENDIAN); + guint16 csum_tmp = + fu_sum16w(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk), G_LITTLE_ENDIAN); gsize blksz = self->fw_page_size + 3; g_autofree guint8 *blk = g_malloc0(blksz); diff --git a/plugins/elantp/fu-elantp-i2c-device.c b/plugins/elantp/fu-elantp-i2c-device.c index 9bdf8d142..77870088d 100644 --- a/plugins/elantp/fu-elantp-i2c-device.c +++ b/plugins/elantp/fu-elantp-i2c-device.c @@ -409,9 +409,8 @@ fu_elantp_i2c_device_write_firmware(FuDevice *device, chunks = fu_chunk_array_new(buf + iap_addr, bufsz - iap_addr, 0x0, 0x0, self->fw_page_size); for (guint i = 0; i < chunks->len; i++) { FuChunk *chk = g_ptr_array_index(chunks, i); - guint16 csum_tmp = fu_common_sum16w(fu_chunk_get_data(chk), - fu_chunk_get_data_sz(chk), - G_LITTLE_ENDIAN); + guint16 csum_tmp = + fu_sum16w(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk), G_LITTLE_ENDIAN); gsize blksz = self->fw_page_size + 4; g_autofree guint8 *blk = g_malloc0(blksz); diff --git a/plugins/genesys/fu-genesys-usbhub-firmware.c b/plugins/genesys/fu-genesys-usbhub-firmware.c index 6dbddd7a5..b6aac7f42 100644 --- a/plugins/genesys/fu-genesys-usbhub-firmware.c +++ b/plugins/genesys/fu-genesys-usbhub-firmware.c @@ -94,7 +94,7 @@ fu_genesys_usbhub_firmware_verify(const guint8 *buf, gsize bufsz, guint16 code_s return FALSE; /* calculate checksum */ - checksum = fu_common_sum16(buf, code_size - sizeof(checksum)); + checksum = fu_sum16(buf, code_size - sizeof(checksum)); if (checksum != fw_checksum) { g_set_error(error, FWUPD_ERROR, @@ -266,7 +266,7 @@ fu_genesys_usbhub_firmware_write(FuFirmware *firmware, GError **error) return NULL; /* checksum */ - checksum = fu_common_sum16(buf->data, code_size - sizeof(checksum)); + checksum = fu_sum16(buf->data, code_size - sizeof(checksum)); if (!fu_common_write_uint16_safe(buf->data, buf->len, code_size - sizeof(guint16), diff --git a/plugins/goodix-moc/fu-goodixmoc-device.c b/plugins/goodix-moc/fu-goodixmoc-device.c index f5ae079e5..4e09bae02 100644 --- a/plugins/goodix-moc/fu-goodixmoc-device.c +++ b/plugins/goodix-moc/fu-goodixmoc-device.c @@ -47,11 +47,11 @@ goodixmoc_device_cmd_send(FuGoodixMocDevice *self, fu_byte_array_append_uint8(buf, type); /* pkg_flag */ fu_byte_array_append_uint8(buf, self->dummy_seq++); /* reserved */ fu_byte_array_append_uint16(buf, req->len + GX_SIZE_CRC32, G_LITTLE_ENDIAN); - crc_hdr = fu_common_crc8(buf->data, buf->len); + crc_hdr = fu_crc8(buf->data, buf->len); fu_byte_array_append_uint8(buf, crc_hdr); fu_byte_array_append_uint8(buf, ~crc_hdr); g_byte_array_append(buf, req->data, req->len); - crc_all = fu_common_crc32(buf->data, buf->len); + crc_all = fu_crc32(buf->data, buf->len); fu_byte_array_append_uint32(buf, crc_all, G_LITTLE_ENDIAN); /* send zero length package */ @@ -154,7 +154,7 @@ goodixmoc_device_cmd_recv(FuGoodixMocDevice *self, error)) return FALSE; offset = sizeof(GxfpPkgHeader) + header_len - GX_SIZE_CRC32; - crc_actual = fu_common_crc32(reply->data, offset); + crc_actual = fu_crc32(reply->data, offset); if (!fu_common_read_uint32_safe(reply->data, reply->len, offset, diff --git a/plugins/nordic-hid/fu-nordic-hid-firmware.c b/plugins/nordic-hid/fu-nordic-hid-firmware.c index 5e12499df..cf268554e 100644 --- a/plugins/nordic-hid/fu-nordic-hid-firmware.c +++ b/plugins/nordic-hid/fu-nordic-hid-firmware.c @@ -44,10 +44,10 @@ static guint32 fu_nordic_hid_firmware_crc32(const guint8 *buf, gsize bufsz) { guint crc32 = 0x01; - /* maybe skipped "^" step in fu_common_crc32_full()? + /* maybe skipped "^" step in fu_crc32_full()? * according https://github.com/madler/zlib/blob/master/crc32.c#L225 */ crc32 ^= 0xFFFFFFFFUL; - return fu_common_crc32_full(buf, bufsz, crc32, 0xEDB88320); + return fu_crc32_full(buf, bufsz, crc32, 0xEDB88320); } static gboolean diff --git a/plugins/pixart-rf/fu-pxi-ble-device.c b/plugins/pixart-rf/fu-pxi-ble-device.c index 1cb588b6c..49b3aa8ed 100644 --- a/plugins/pixart-rf/fu-pxi-ble-device.c +++ b/plugins/pixart-rf/fu-pxi-ble-device.c @@ -324,7 +324,7 @@ fu_pxi_ble_device_check_support_resume(FuPxiBleDevice *self, /* calculate device current checksum */ for (guint i = 0; i < self->fwstate.offset; i++) { FuChunk *chk = g_ptr_array_index(chunks, i); - checksum_tmp += fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum_tmp += fu_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); } /* check current file is different with previous fw bin or not */ @@ -496,7 +496,7 @@ fu_pxi_ble_device_write_chunk(FuPxiBleDevice *self, FuChunk *chk, GError **error } /* the last chunk */ - checksum = fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum = fu_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); self->fwstate.checksum += checksum; if (checksum_device != self->fwstate.checksum) { g_set_error(error, @@ -598,7 +598,7 @@ fu_pxi_ble_device_fw_upgrade(FuPxiBleDevice *self, fw = fu_firmware_get_bytes(firmware, error); if (fw == NULL) return FALSE; - checksum = fu_common_sum16_bytes(fw); + checksum = fu_sum16_bytes(fw); fu_byte_array_append_uint8(req, PXI_HID_DEV_OTA_FEATURE_REPORT_ID); fu_byte_array_append_uint8(req, FU_PXI_DEVICE_CMD_FW_UPGRADE); fu_byte_array_append_uint32(req, g_bytes_get_size(fw), G_LITTLE_ENDIAN); diff --git a/plugins/pixart-rf/fu-pxi-common.c b/plugins/pixart-rf/fu-pxi-common.c index fd6abbafd..7eb52040a 100644 --- a/plugins/pixart-rf/fu-pxi-common.c +++ b/plugins/pixart-rf/fu-pxi-common.c @@ -154,7 +154,7 @@ fu_pxi_composite_receiver_cmd(guint8 opcode, g_byte_array_prepend(wireless_mod_cmd, &rf_cmd_code, 0x01); /* command code */ /* prepend checksum */ - checksum = fu_common_sum8(wireless_mod_cmd->data, wireless_mod_cmd->len); + checksum = fu_sum8(wireless_mod_cmd->data, wireless_mod_cmd->len); g_byte_array_prepend(wireless_mod_cmd, &checksum, 0x01); /* prepend feature report id */ diff --git a/plugins/pixart-rf/fu-pxi-receiver-device.c b/plugins/pixart-rf/fu-pxi-receiver-device.c index 79478ddf5..f00346481 100644 --- a/plugins/pixart-rf/fu-pxi-receiver-device.c +++ b/plugins/pixart-rf/fu-pxi-receiver-device.c @@ -392,7 +392,7 @@ fu_pxi_receiver_device_write_chunk(FuDevice *device, FuChunk *chk, GError **erro self->fwstate.mtu_size); /* the checksum of chunk */ - checksum = fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum = fu_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); self->fwstate.checksum += checksum; for (guint i = 0; i < chunks->len; i++) { FuChunk *chk2 = g_ptr_array_index(chunks, i); @@ -447,7 +447,7 @@ fu_pxi_receiver_device_fw_upgrade(FuDevice *device, g_bytes_get_size(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command fw size */ fu_byte_array_append_uint16(ota_cmd, - fu_common_sum16_bytes(fw), + fu_sum16_bytes(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command checksum */ version = fu_firmware_get_version(firmware); diff --git a/plugins/pixart-rf/fu-pxi-wireless-device.c b/plugins/pixart-rf/fu-pxi-wireless-device.c index 25149a677..54cd72f07 100644 --- a/plugins/pixart-rf/fu-pxi-wireless-device.c +++ b/plugins/pixart-rf/fu-pxi-wireless-device.c @@ -340,7 +340,7 @@ fu_pxi_wireless_device_write_chunk(FuDevice *device, FuChunk *chk, GError **erro self->fwstate.mtu_size); /* calculate checksum of chunk */ - checksum = fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum = fu_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); self->fwstate.checksum += checksum; for (guint i = 0; i < chunks->len; i++) { @@ -496,7 +496,7 @@ fu_pxi_wireless_device_fw_upgrade(FuDevice *device, g_bytes_get_size(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command fw size */ fu_byte_array_append_uint16(ota_cmd, - fu_common_sum16_bytes(fw), + fu_sum16_bytes(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command checksum */ version = fu_firmware_get_version(firmware); diff --git a/plugins/steelseries/fu-steelseries-firmware.c b/plugins/steelseries/fu-steelseries-firmware.c index 5b2e4022c..8eeae06ca 100644 --- a/plugins/steelseries/fu-steelseries-firmware.c +++ b/plugins/steelseries/fu-steelseries-firmware.c @@ -36,8 +36,8 @@ fu_steelseries_firmware_parse(FuFirmware *firmware, G_LITTLE_ENDIAN, error)) return FALSE; - checksum_tmp = fu_common_crc32(g_bytes_get_data(fw, NULL), - g_bytes_get_size(fw) - sizeof(checksum_tmp)); + checksum_tmp = + fu_crc32(g_bytes_get_data(fw, NULL), g_bytes_get_size(fw) - sizeof(checksum_tmp)); if (checksum_tmp != checksum) { if ((flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { g_set_error(error, diff --git a/plugins/steelseries/fu-steelseries-gamepad.c b/plugins/steelseries/fu-steelseries-gamepad.c index dbd4c89f5..e5bdb948f 100644 --- a/plugins/steelseries/fu-steelseries-gamepad.c +++ b/plugins/steelseries/fu-steelseries-gamepad.c @@ -165,7 +165,7 @@ fu_steelseries_gamepad_write_firmware_chunks(FuDevice *device, /* block checksum */ /* probably not necessary */ - chunk_checksum = fu_common_sum16(data + 3, STEELSERIES_BUFFER_TRANSFER_SIZE); + chunk_checksum = fu_sum16(data + 3, STEELSERIES_BUFFER_TRANSFER_SIZE); if (!fu_common_write_uint16_safe(data, STEELSERIES_BUFFER_CONTROL_SIZE, 0x03 + STEELSERIES_BUFFER_TRANSFER_SIZE, diff --git a/plugins/steelseries/fu-steelseries-sonic.c b/plugins/steelseries/fu-steelseries-sonic.c index f6e70271e..8970b41ed 100644 --- a/plugins/steelseries/fu-steelseries-sonic.c +++ b/plugins/steelseries/fu-steelseries-sonic.c @@ -983,8 +983,8 @@ fu_steelseries_sonic_parse_firmware(FuFirmware *firmware, FwupdInstallFlags flag G_LITTLE_ENDIAN, error)) return FALSE; - checksum_tmp = fu_common_crc32(g_bytes_get_data(blob, NULL), - g_bytes_get_size(blob) - sizeof(checksum_tmp)); + checksum_tmp = + fu_crc32(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob) - sizeof(checksum_tmp)); checksum_tmp = ~checksum_tmp; if (checksum_tmp != checksum) { if ((flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { diff --git a/plugins/synaptics-mst/fu-synaptics-mst-device.c b/plugins/synaptics-mst/fu-synaptics-mst-device.c index 744bda1ae..00de2184d 100644 --- a/plugins/synaptics-mst/fu-synaptics-mst-device.c +++ b/plugins/synaptics-mst/fu-synaptics-mst-device.c @@ -319,7 +319,7 @@ fu_synaptics_mst_device_update_esm(FuSynapticsMstDevice *self, } /* ESM checksum same */ - checksum = fu_common_sum32(payload_data + EEPROM_ESM_OFFSET, esm_sz); + checksum = fu_sum32(payload_data + EEPROM_ESM_OFFSET, esm_sz); if (checksum == flash_checksum) { g_debug("ESM checksum already matches"); return TRUE; @@ -469,7 +469,7 @@ fu_synaptics_mst_device_update_tesla_leaf_firmware(FuSynapticsMstDevice *self, &flash_checksum, error)) return FALSE; - checksum = fu_common_sum32(payload_data, payload_len); + checksum = fu_sum32(payload_data, payload_len); if (checksum == flash_checksum) break; g_debug("attempt %u: checksum %x didn't match %x", diff --git a/plugins/uefi-capsule/fu-plugin-uefi-capsule.c b/plugins/uefi-capsule/fu-plugin-uefi-capsule.c index 53cda38f1..bce743755 100644 --- a/plugins/uefi-capsule/fu-plugin-uefi-capsule.c +++ b/plugins/uefi-capsule/fu-plugin-uefi-capsule.c @@ -304,9 +304,9 @@ fu_plugin_uefi_capsule_write_splash_data(FuPlugin *plugin, }; /* header, payload and image has to add to zero */ - csum += fu_common_sum8((guint8 *)&capsule_header, sizeof(capsule_header)); - csum += fu_common_sum8((guint8 *)&header, sizeof(header)); - csum += fu_common_sum8_bytes(blob); + csum += fu_sum8((guint8 *)&capsule_header, sizeof(capsule_header)); + csum += fu_sum8((guint8 *)&header, sizeof(header)); + csum += fu_sum8_bytes(blob); header.checksum = 0x100 - csum; /* write capsule file */ diff --git a/plugins/vli/fu-vli-pd-device.c b/plugins/vli/fu-vli-pd-device.c index 6cb00697c..8d3b88923 100644 --- a/plugins/vli/fu-vli-pd-device.c +++ b/plugins/vli/fu-vli-pd-device.c @@ -524,7 +524,7 @@ fu_vli_pd_device_write_dual_firmware(FuVliPdDevice *self, g_prefix_error(error, "failed to read file CRC: "); return FALSE; } - crc_actual = fu_common_crc16(sbuf, sbufsz - 2); + crc_actual = fu_crc16(sbuf, sbufsz - 2); fu_progress_step_done(progress); /* update fw2 first if fw1 correct */ diff --git a/plugins/vli/fu-vli-pd-firmware.c b/plugins/vli/fu-vli-pd-firmware.c index c8423a8ba..4a69bc34c 100644 --- a/plugins/vli/fu-vli-pd-firmware.c +++ b/plugins/vli/fu-vli-pd-firmware.c @@ -156,7 +156,7 @@ fu_vli_pd_firmware_parse(FuFirmware *firmware, g_prefix_error(error, "failed to read file CRC: "); return FALSE; } - crc_actual = fu_common_crc16(buf, bufsz - 2); + crc_actual = fu_crc16(buf, bufsz - 2); if (crc_actual != crc_file) { g_set_error(error, FWUPD_ERROR, diff --git a/plugins/vli/fu-vli-usbhub-common.c b/plugins/vli/fu-vli-usbhub-common.c index cda7fa58b..9509f968c 100644 --- a/plugins/vli/fu-vli-usbhub-common.c +++ b/plugins/vli/fu-vli-usbhub-common.c @@ -12,7 +12,7 @@ guint8 fu_vli_usbhub_header_crc8(FuVliUsbhubHeader *hdr) { - return ~fu_common_crc8((const guint8 *)hdr, sizeof(*hdr) - 1); + return ~fu_crc8((const guint8 *)hdr, sizeof(*hdr) - 1); } void diff --git a/plugins/wacom-raw/fu-wacom-emr-device.c b/plugins/wacom-raw/fu-wacom-emr-device.c index 7e3dac411..b3f379875 100644 --- a/plugins/wacom-raw/fu-wacom-emr-device.c +++ b/plugins/wacom-raw/fu-wacom-emr-device.c @@ -58,7 +58,7 @@ fu_wacom_emr_device_setup(FuDevice *device, GError **error) static guint8 fu_wacom_emr_device_calc_checksum(guint8 init1, const guint8 *buf, gsize bufsz) { - return init1 + ~(fu_common_sum8(buf, bufsz)) + 1; + return init1 + ~(fu_sum8(buf, bufsz)) + 1; } static gboolean diff --git a/plugins/wacom-usb/fu-wac-device.c b/plugins/wacom-usb/fu-wac-device.c index 4c8db1c6f..65082a10c 100644 --- a/plugins/wacom-usb/fu-wac-device.c +++ b/plugins/wacom-usb/fu-wac-device.c @@ -592,7 +592,7 @@ fu_wac_device_write_firmware(FuDevice *device, } /* calculate expected checksum and save to device RAM */ - csum_local[i] = GUINT32_TO_LE(fu_common_sum32w_bytes(blob_block, G_LITTLE_ENDIAN)); + csum_local[i] = GUINT32_TO_LE(fu_sum32w_bytes(blob_block, G_LITTLE_ENDIAN)); if (g_getenv("FWUPD_WACOM_USB_VERBOSE") != NULL) g_debug("block checksum %02u: 0x%08x", i, csum_local[i]); if (!fu_wac_device_set_checksum_of_block(self, i, csum_local[i], error)) diff --git a/plugins/wacom-usb/fu-wac-firmware.c b/plugins/wacom-usb/fu-wac-firmware.c index 014888656..7bc3f8925 100644 --- a/plugins/wacom-usb/fu-wac-firmware.c +++ b/plugins/wacom-usb/fu-wac-firmware.c @@ -315,7 +315,7 @@ fu_wac_firmware_parse(FuFirmware *firmware, static guint8 fu_wac_firmware_calc_checksum(GByteArray *buf) { - return fu_common_sum8(buf->data, buf->len) ^ 0xFF; + return fu_sum8(buf->data, buf->len) ^ 0xFF; } static GBytes * diff --git a/plugins/wacom-usb/fu-wac-module-bluetooth-id6.c b/plugins/wacom-usb/fu-wac-module-bluetooth-id6.c index 80f7cd5b0..84a8a315a 100644 --- a/plugins/wacom-usb/fu-wac-module-bluetooth-id6.c +++ b/plugins/wacom-usb/fu-wac-module-bluetooth-id6.c @@ -47,8 +47,7 @@ fu_wac_module_bluetooth_id6_reverse_bits(guint8 value) static guint8 fu_wac_module_bluetooth_id6_calculate_crc(const guint8 *data, gsize sz) { - guint8 crc = - ~fu_common_crc8_full(data, sz, 0x00, FU_WAC_MODULE_BLUETOOTH_ID6_CRC8_POLYNOMIAL); + guint8 crc = ~fu_crc8_full(data, sz, 0x00, FU_WAC_MODULE_BLUETOOTH_ID6_CRC8_POLYNOMIAL); return fu_wac_module_bluetooth_id6_reverse_bits(crc); }