Search

memmove()

[- Disclaimer -] 아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
memmove()
✦ <string.h>
✦ Destination 주소에 지정한 크기만큼 Source 주소의 값 복사
✦ Parameter1에 전달되는 Argument1
✧ Destination 주소
✦ Parameter2에 전달되는 Argument2
✧ Source 주소
✦ Parameter3에 전달되는 Argument3
✧ Byte 단위 범위 // 중복 가능
✦ Return 값
✧ 없음
✦ memcpy()는 Overwrite 과정에 데이터 손실이 일어 날 수 있지만 memmove()는 그렇지 않음
void *memmove(void *dest, const void *src, size_t count);
Plain Text
복사
Ex)
#include <stdio.h> #include <string.h> int main(void) { char str[32] = "hello world"; memcpy(&str[6], &str[0], 12); printf("%s", str); return 0; }
Plain Text
복사
hello hello world -------------------------------- Process exited after 0.5276 seconds with return value 0 계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
memcpy()의 Data 손실 - Ex)
✦ 아래 예시에서 i번째 증가하는 과정에 world가 덮어 없어져 버렸기 때문에 기존의 Data가 날라가 버리는 이상 현상 발생
#include <stdio.h> void* memcpy(void *destination, const void *source, size_t num); int main(void) { char str[32] = "hello world"; memcpy(&str[6], &str[0], 12); printf("%s", str); return 0; } void *memcpy(void *destination, const void *source, size_t num) { size_t i = 0; for(i = 0; i < num; i++) ((unsigned char*)destination)[i] = ((unsigned char*)source)[i]; return destination; }
Plain Text
복사
hello hello hello -------------------------------- Process exited after 0.2448 seconds with return value 0 계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
#include <stdio.h> #include <string.h> void *memmove(void *destination, const void *source, size_t num); int main(void) { char str[32] = "hello world"; memmove(&str[6], &str[0], 12); printf("%s", str); return 0; } void *memmove(void *destination, const void *source, size_t num) { size_t i = 0; // 동적 할당을 해서 num 만큼의 Source를 임시 저장해서 쓰면 손실 없이 될꺼다. unsigned char* buff = (unsigned char *)malloc(num); for(i = 0; i < num, i++;) buff[i] = ((unsigned char*)source)[i]; for(i = 0; i< num; i++) ((unsigned char*)destination)[i] = ((unsigned char*)buff)[i]; free(buff); buff = NULL; return destination; }
Plain Text
복사
hello -------------------------------- Process exited after 0.1778 seconds with return value 0 계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사