/* Copies the source block of memory to the destination block of memory while * switching the endianness of .v64 and .n64 images to the .z64 format, which * is native to the Nintendo 64. The data extraction routines and MD5 hashing * function may only act on the .z64 big-endian format. * * IN: src: The source block of memory. This must be a valid Nintendo 64 ROM * image of 'len' bytes. * len: The length of the source and destination, in bytes. * OUT: dst: The destination block of memory. This must be a valid buffer for * at least 'len' bytes. * imagetype: A pointer to a byte that gets updated with the value of * V64IMAGE, N64IMAGE or Z64IMAGE according to the format of * the source block. The value is undefined if 'src' does not * represent a valid Nintendo 64 ROM image. */ static void swap_copy_rom(void* dst, const void* src, size_t len, unsigned char* imagetype) { if (memcmp(src, V64_SIGNATURE, sizeof(V64_SIGNATURE)) == 0) { size_t i; const uint16_t* src16 = (const uint16_t*) src; uint16_t* dst16 = (uint16_t*) dst; *imagetype = V64IMAGE; /* .v64 images have byte-swapped half-words (16-bit). */ for (i = 0; i < len; i += 2) { *dst16++ = m64p_swap16(*src16++); } } else if (memcmp(src, N64_SIGNATURE, sizeof(N64_SIGNATURE)) == 0) { size_t i; const uint32_t* src32 = (const uint32_t*) src; uint32_t* dst32 = (uint32_t*) dst; *imagetype = N64IMAGE; /* .n64 images have byte-swapped words (32-bit). */ for (i = 0; i < len; i += 4) { *dst32++ = m64p_swap32(*src32++); } } else { *imagetype = Z64IMAGE; memcpy(dst, src, len); } }
/********************** Byte swap utilities **********************/ void swap_buffer(void *buffer, size_t length, size_t count) { size_t i; if (length == 2) { unsigned short *pun = (unsigned short *)buffer; for (i = 0; i < count; i++) pun[i] = m64p_swap16(pun[i]); } else if (length == 4) { unsigned int *pun = (unsigned int *)buffer; for (i = 0; i < count; i++) pun[i] = m64p_swap32(pun[i]); } else if (length == 8) { unsigned long long *pun = (unsigned long long *)buffer; for (i = 0; i < count; i++) pun[i] = m64p_swap64(pun[i]); } }