Newer
Older
Hardware / FaultInjection / examples / CuriousBolt / Level-1 / Chall-4-Firmware / memset.c
// memset.c
void *memset(void *dest, int val, unsigned int len) {
    unsigned char *ptr = dest;
    unsigned int value = (unsigned char)val; // Promote to avoid unnecessary casting

    // Use a word-by-word copy if possible
    while (len >= 4) {
        *(unsigned int*)ptr = value | (value << 8) | (value << 16) | (value << 24);
        ptr += 4;
        len -= 4;
    }

    // If any bytes are left, fill them
    while (len--) {
        *ptr++ = (unsigned char)value;
    }

    return dest;
}