// File: /workspace/github_cpp_makefile_star2_O2/carellano8[P]Proyecto-Redes-I-2017/hamming.cpp
// Function: hamming::recibir(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void hamming::recibir(binario bin){
	size_t i,li,ls,j,a,b;
    int r;
    bool error=false;
    binario aux="";
    binario bufferB[MAXMSJ/MAXT*8];
    binario buffer2[MAXMSJ/MAXT*8];
    string bufferMsj;
    ofstream archivo("salidah.txt");
    
   
     i=0;j=0;
    while(i<bin.size()){

        li=bin.find("01111110",i);
        ls=bin.find("01111110",i+8);
        bufferB[j]=bin.substr(li,(ls+8)-li);

        //~ cout<<"Inferior "<<li<<" Superior "<<ls<<" Distancia "<<(ls+8)-li<<endl;
        //~ cout<<bufferB[j]<<endl;
        j++;
        i=ls+8;;
    }
    if(j>16){error=true;}// maximo 16 tramas 
    i=0;
    while(!error&&i<j){// Verfificar que no hay error en las banderas 
        if(bufferB[i].size()<=30){
            error=true;
        }
        if(!error){
            bufferB[i]=bufferB[i].substr(8,bufferB[i].size()-16);//quita las banderas 
        }
        i++;
    }
   	if(!error){// Quitar relleno de bits
        for (i = 0; i < j; i++)
        {
        	b=0;aux="";a=0;
        	while(a < bufferB[i].size())
        	{
        		if(bufferB[i].at(a)=='1'){
                    b++;
                }else{
                    b=0;
                    }
                aux+=bufferB[i].at(a);		
                if(b==5){
                a++;b=0;
                	}
            a++;
        	}
        	
        	bufferB[i]=aux;

       	}
    }
            
    
    if(!error){// decodificar hamming
        i=0;
        while(!error&&i<j){// decodificar hamming
        	// cout<<bufferB[i]<<endl;
        	// cout<<crc(bufferB[i])<<endl;
        	bufferB[i]=decodificadorHamming(bufferB[i],&r);
        	//~ cout<<"HOLA "<<r<<endl;
            if(r==-1){
                error=true;
            }
            i++;
        }
    }
   
       
    if(!error){// Cabecera, Que no hay desorden en las tramas y que hay trama final
        i=0;
        while(!error&&i<j){// 
            a=BitStringToInt(bufferB[i].substr(1,4));
            b=BitStringToInt(bufferB[i].substr(5,1));
            buffer2[a]=bufferB[i].substr(6);
            //cout<<a<<" "<<b<<" "<<buffer2[a]<<endl;
            i++;
        }
        if(!b){
            error=true;
        }
    }
    if(!error){
        bufferMsj="";
        for (i = 0; i <j ; i++)
        {
            for (a = 0; a <buffer2[i].size() ; a+=8)
            {
                bufferMsj+=(char)(BitStringToInt(buffer2[i].substr(a,8)));
            }   
        }

        cout<<bufferMsj<<endl;
        if(archivo.is_open()){
            archivo<<bufferMsj;
            if(r==0){
            	cout<<"Se ha detectado y corregido un error"<<endl;
            	archivo<<endl<<"Se ha detectado y corregido un error"<<endl;
            }
            archivo.close();
        }else{
        	cout<<"Se ha detectado algun un error"<<endl;
        }
    }
    
    
    
    
    if(error){
        cout<<"Se ha detectado algun un error"<<endl;
        if(archivo.is_open()){
            archivo<<"Se ha detectado algun un error"<<endl;
            archivo.close();
        }else{
        	cout<<"Se ha detectado algun un error"<<endl;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2318, %rsp           # imm = 0x2318
movq %rdi, %r15
leaq 0x1855(%rip), %rsi      # 0x6159
leaq -0xb8(%rbp), %rdi
leaq -0x1340(%rbp), %rbx
movq %rbx, %rdx
callq 0x2c1a
pushq $0x10
popq %rax
leaq (%rax,%rbp), %rcx
addq $-0x1340, %rcx          # imm = 0xECC0
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq $0x20, %rax
cmpq $0x1010, %rax           # imm = 0x1010
jne 0x491d
pushq $0x10
popq %rax
leaq (%rax,%rbp), %rcx
addq $-0x2340, %rcx          # imm = 0xDCC0
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq $0x20, %rax
cmpq $0x1010, %rax           # imm = 0x1010
jne 0x4943
leaq -0x68(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x174a(%rip), %rsi      # 0x60c7
leaq -0x338(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rdi, -0x50(%rbp)
callq 0x2180
xorl %r13d, %r13d
movq $0x0, -0x40(%rbp)
cmpq 0x8(%r15), %r13
jae 0x4a0a
movq %r15, %rdi
leaq 0x17af(%rip), %r12      # 0x615a
movq %r12, %rsi
movq %r13, %rdx
callq 0x22a0
movq %rax, %r14
addq $0x8, %r13
movq %r15, %rdi
movq %r12, %rsi
leaq -0x98(%rbp), %r12
movq %r13, %rdx
callq 0x22a0
movq %rax, %r13
addq $0x8, %r13
movq %r13, %rcx
subq %r14, %rcx
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2160
movq %rbx, %rdi
movq %r12, %rsi
callq 0x21a0
movq %r12, %rdi
callq 0x20a0
incq -0x40(%rbp)
addq $0x20, %rbx
jmp 0x499b
cmpq $0x11, -0x40(%rbp)
setae %r13b
leaq -0x1340(%rbp), %rbx
xorl %r12d, %r12d
leaq -0x98(%rbp), %r14
pushq $0x8
popq %r15
testb $0x1, %r13b
jne 0x4a7b
cmpq -0x40(%rbp), %r12
jae 0x4a7b
movq 0x8(%rbx), %rcx
cmpq $0x1f, %rcx
movzbl %r13b, %r13d
pushq $0x1
popq %rax
cmovbl %eax, %r13d
testb $0x1, %r13b
jne 0x4a72
addq $-0x10, %rcx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2160
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21a0
movq %r14, %rdi
callq 0x20a0
incq %r12
addq $0x20, %rbx
jmp 0x4a28
testb $0x1, %r13b
je 0x4a86
jmp 0x4bab
xorl %eax, %eax
leaq -0xb8(%rbp), %r15
movq %rax, -0x38(%rbp)
cmpq %rax, -0x40(%rbp)
je 0x4b28
movq %r15, %rdi
leaq 0x16b2(%rip), %rsi      # 0x6159
callq 0x2270
movq -0x38(%rbp), %rax
shlq $0x5, %rax
leaq (%rax,%rbp), %rbx
addq $-0x1340, %rbx          # imm = 0xECC0
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpq 0x8(%rbx), %r12
jae 0x4b11
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2280
cmpb $0x31, (%rax)
je 0x4ae0
xorl %r14d, %r14d
jmp 0x4ae3
incq %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2280
movsbl (%rax), %esi
movq %r15, %rdi
callq 0x2110
xorl %eax, %eax
cmpq $0x5, %r14
sete %cl
jne 0x4b07
xorl %r14d, %r14d
movb %cl, %al
addq %rax, %r12
incq %r12
jmp 0x4ac5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20c0
movq -0x38(%rbp), %rax
incq %rax
jmp 0x4a8f
leaq -0x1340(%rbp), %rbx
movq $0x0, -0x38(%rbp)
testb $0x1, %r13b
jne 0x4ba5
movq -0x38(%rbp), %rcx
cmpq -0x40(%rbp), %rcx
jae 0x4ba5
leaq -0x138(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x21c0
leaq -0x98(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
leaq -0x48(%rbp), %rdx
callq 0x4f2a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x21a0
movq %r12, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x20a0
movl -0x48(%rbp), %eax
cmpl $-0x1, %eax
movzbl %r13b, %r13d
pushq $0x1
popq %rcx
cmovel %ecx, %r13d
incq -0x38(%rbp)
addq $0x20, %rbx
jmp 0x4b37
testl %eax, %eax
sete -0x29(%rbp)
testb $0x1, %r13b
jne 0x4c9e
leaq -0x1340(%rbp), %rax
leaq -0xd8(%rbp), %rcx
leaq -0x118(%rbp), %r15
movq -0x40(%rbp), %r12
movl %r13d, -0x44(%rbp)
subq $0x1, %r12
jb 0x4c90
movq %rcx, %rbx
movq %rcx, %rdi
movq %rax, %r14
movq %rax, %rsi
pushq $0x1
popq %rdx
pushq $0x4
popq %rcx
callq 0x2160
movq %rbx, %rdi
callq 0x514e
movl %eax, -0x38(%rbp)
movq %rbx, %rdi
callq 0x20a0
movq %r15, %rdi
movq %r14, %rsi
pushq $0x5
popq %rdx
pushq $0x1
popq %rcx
callq 0x2160
movq %r12, -0x58(%rbp)
movq %r15, %rdi
callq 0x514e
movq %r15, %rdi
movl %eax, %r15d
movq %rdi, %r12
callq 0x20a0
leaq -0x98(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x6
popq %rdx
pushq $-0x1
popq %rcx
callq 0x2160
movq %r14, %r13
movslq -0x38(%rbp), %rax
movslq %r15d, %r14
shlq $0x5, %rax
leaq (%rax,%rbp), %rdi
addq $-0x2340, %rdi          # imm = 0xDCC0
movq %rbx, %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x20a0
addq $0x20, %r13
movq %r13, %rax
movl -0x44(%rbp), %r13d
leaq -0xd8(%rbp), %rcx
movq %r12, %r15
movq -0x58(%rbp), %r12
jmp 0x4bd2
testq %r14, %r14
movzbl %r13b, %r13d
pushq $0x1
popq %rax
cmovel %eax, %r13d
testb $0x1, %r13b
je 0x4cdb
movq 0x4325(%rip), %rdi      # 0x8fd0
leaq 0x13dd(%rip), %rbx      # 0x608f
movq %rbx, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x2c8(%rbp), %rdi
callq 0x2050
testb %al, %al
jne 0x4dc6
jmp 0x4de8
leaq 0x1477(%rip), %rsi      # 0x6159
leaq -0x78(%rbp), %rdi
callq 0x2270
xorl %eax, %eax
leaq -0xf8(%rbp), %rbx
pushq $0x8
popq %r14
leaq -0x78(%rbp), %r15
cmpq %rax, -0x40(%rbp)
je 0x4d5a
movq %rax, -0x38(%rbp)
shlq $0x5, %rax
leaq (%rax,%rbp), %r12
addq $-0x2340, %r12          # imm = 0xDCC0
xorl %r13d, %r13d
cmpq 0x8(%r12), %r13
jae 0x4d51
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2160
movq %rbx, %rdi
callq 0x514e
movsbl %al, %esi
movq %r15, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x20a0
addq $0x8, %r13
jmp 0x4d18
movq -0x38(%rbp), %rax
incq %rax
jmp 0x4cfc
movq 0x426f(%rip), %rdi      # 0x8fd0
leaq -0x78(%rbp), %rsi
callq 0x2130
movq %rax, %rdi
callq 0x20b0
leaq -0x2c8(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x4de8
leaq -0x338(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2130
testb $0x1, -0x29(%rbp)
je 0x4dda
movq 0x4231(%rip), %rdi      # 0x8fd0
leaq 0x13da(%rip), %rbx      # 0x6180
movq %rbx, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x338(%rbp), %rdi
callq 0x20b0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x338(%rbp), %rdi
callq 0x21d0
jmp 0x4e03
movq 0x41e1(%rip), %rdi      # 0x8fd0
leaq 0x1299(%rip), %rsi      # 0x608f
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x338(%rbp), %rdi
callq 0x21e0
leaq -0x78(%rbp), %rdi
callq 0x20a0
movl $0xfe0, %ebx            # imm = 0xFE0
leaq (%rbx,%rbp), %rdi
addq $-0x2340, %rdi          # imm = 0xDCC0
callq 0x20a0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x4e1d
movl $0xfe0, %ebx            # imm = 0xFE0
leaq (%rbx,%rbp), %rdi
addq $-0x1340, %rdi          # imm = 0xECC0
callq 0x20a0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x4e3c
leaq -0xb8(%rbp), %rdi
callq 0x20a0
addq $0x2318, %rsp           # imm = 0x2318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x4ecd
jmp 0x4ebe
jmp 0x4ebe
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
jmp 0x4eb7
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
jmp 0x4eb7
jmp 0x4ebe
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
jmp 0x4eb7
jmp 0x4ebe
jmp 0x4ebe
jmp 0x4ebe
jmp 0x4ebe
jmp 0x4ebe
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x20a0
jmp 0x4ec1
movq %rax, %rbx
leaq -0x338(%rbp), %rdi
callq 0x21e0
leaq -0x78(%rbp), %rdi
callq 0x20a0
movl $0xfe0, %r14d           # imm = 0xFE0
leaq (%r14,%rbp), %rdi
addq $-0x2340, %rdi          # imm = 0xDCC0
callq 0x20a0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x4edc
movl $0xfe0, %r14d           # imm = 0xFE0
leaq (%r14,%rbp), %rdi
addq $-0x1340, %rdi          # imm = 0xECC0
callq 0x20a0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x4efc
leaq -0xb8(%rbp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2230