Newer
Older
Hardware / FaultInjection / examples / CuriousBolt / Level-1 / Chall-4-Firmware / memset.c
  1. // memset.c
  2. void *memset(void *dest, int val, unsigned int len) {
  3. unsigned char *ptr = dest;
  4. unsigned int value = (unsigned char)val; // Promote to avoid unnecessary casting
  5.  
  6. // Use a word-by-word copy if possible
  7. while (len >= 4) {
  8. *(unsigned int*)ptr = value | (value << 8) | (value << 16) | (value << 24);
  9. ptr += 4;
  10. len -= 4;
  11. }
  12.  
  13. // If any bytes are left, fill them
  14. while (len--) {
  15. *ptr++ = (unsigned char)value;
  16. }
  17.  
  18. return dest;
  19. }
Buy Me A Coffee