RE4B Challenge 5

Challenge

What does this code do?

Optimizing GCC 4.8.4 (x64):

f:
    cmp rcx, rsi
    ja  .L10
    sub rsi, rcx
    add rsi, 1
    mov r11, rsi
    je  .L10
    test    rcx, rcx
    jne .L16
    mov rax, rdi
    ret
.L10:
    xor eax, eax
    ret
.L16:
    push    rbx
    xor r10d, r10d
    mov r9d, 1
.L4:
    lea rax, [rdi+r10]
    xor esi, esi
    xor r8d, r8d
.L8:
    movzx   ebx, BYTE PTR [rdx+rsi]
    cmp BYTE PTR [rax+rsi], bl
    cmovne  r8d, r9d
    add rsi, 1
    cmp rsi, rcx
    jne .L8
    test    r8d, r8d
    je  .L12
    add r10, 1
    cmp r10, r11
    jne .L4
    xor eax, eax
.L12:
    pop rbx

Reslove

参数:

等价于strstr,即寻找a2中的子串a3。

成功返回指向a2的指针,查找失败返回NULL(0);

char *f(int len_str1, char *str1, char *str2, int len_str2){
    if(len_str2 >= len_str1){
        return NULL;
    }

    int sub = len_str1 - len_str2 + 1;
    
    for(int i = 0; i < sub; ++i){
        int flag = 0;

        for(int k = 0; k < len_str2; ++k){
            if(str2[k] != str1[k + i]){
                flag = 1;
            }
        }
        
        if(flag == 0){
            return (str2 + i);
        }
    }

    return NULL;
}

分析思路

Table of Contents