int main() { LOG_VAR_HEX(crc32("123456789", 9)); LOG_VAR_HEX(crc16("123456789", 9)); LOG_VAR_HEX(crc24("123456789", 9)); return 0; }
/*! * @brief Calculates and reports the Information Block Checksum. * @return #mxt_rc */ static int calculate_crc(struct mxt_device *mxt, uint32_t read_crc, uint8_t *base_addr, size_t size) { uint32_t calc_crc = 0; /* Checksum calculated by the driver code */ uint16_t crc_byte_index = 0; /* Call the CRC function crc24() iteratively to calculate the CRC, * passing it two characters at a time. */ while (crc_byte_index < ((size % 2) ? (size - 1) : size)) { calc_crc = crc24(calc_crc, *(base_addr + crc_byte_index), *(base_addr + crc_byte_index + 1)); crc_byte_index += 2; } /* Call crc24() for the final byte, plus an extra * 0 value byte to make the sequence even if it's odd */ if (size % 2) { calc_crc = crc24(calc_crc, *(base_addr + crc_byte_index), 0); } /* Mask 32-bit calculated checksum to 24-bit */ calc_crc &= calc_crc & 0x00FFFFFF; /* A zero CRC indicates a communications error */ if (calc_crc == 0) { mxt_err(mxt->ctx, "Information Block Checksum zero"); return MXT_ERROR_IO; } /* Compare the read checksum with calculated checksum */ if (read_crc != calc_crc) { mxt_err(mxt->ctx, "Information Block Checksum error calc=%06X read=%06X", calc_crc, read_crc); return MXT_ERROR_INFO_CHECKSUM_MISMATCH; } mxt_dbg(mxt->ctx, "Information Block Checksum verified %06X", calc_crc); return MXT_SUCCESS; }
static unsigned char * unarmor(char *pubkey, int *pktlp) { char *p; int l, eof; unsigned char *buf, *bp; unsigned int v; *pktlp = 0; while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0) { pubkey = strchr(pubkey, '\n'); if (!pubkey) return 0; pubkey++; } pubkey = strchr(pubkey, '\n'); if (!pubkey++) return 0; /* skip header lines */ for (;;) { while (*pubkey == ' ' || *pubkey == '\t') pubkey++; if (*pubkey == '\n') break; pubkey = strchr(pubkey, '\n'); if (!pubkey++) return 0; } pubkey++; p = strchr(pubkey, '='); if (!p) return 0; l = p - pubkey; bp = buf = solv_malloc(l * 3 / 4 + 4); eof = 0; while (!eof) { pubkey = r64dec1(pubkey, &v, &eof); if (!pubkey) { solv_free(buf); return 0; } *bp++ = v >> 16; *bp++ = v >> 8; *bp++ = v; } while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r') pubkey++; bp -= eof; if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0) { solv_free(buf); return 0; } if (v != crc24(buf, bp - buf)) { solv_free(buf); return 0; } while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r') pubkey++; if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0) { solv_free(buf); return 0; } *pktlp = bp - buf; return buf; }