示例#1
0
文件: pbm.c 项目: Gctucci/mc514-labs
/**
 * Função que lê uma imagem pgm e retorna uma matriz de ints
 * 
 * @param arq Caminho para o arquivo pbm
 * @param tabuleiro Ponteiro para a matriz de ints
 * @param nlin Ponteiro para o número de linhas
 * @param ncol Ponteiro para o número de colunas
 */
void pbm(char *arq, int ***tabuleiro, int *nlin, int *ncol)
{
	FILE *img;
	bit **tab;
	
	img = fopen(arq, "r");
	if(img)
	{
		//void bit** pbm_readpbm(FILE * fp, int *colsP, int *rowsP);
		tab = pbm_readpbm(img, ncol, nlin);
		bit2int(tab, nlin, ncol, tabuleiro);
		
		pbm_freearray(tab, *nlin);
		fclose(img);
		return;
	}
	else
	{
		// Erro na leitura
		printf("Arquivo não encontrado\n");
		exit(0);
	}
}
示例#2
0
//This function performs the SBox substitution
//The input is a 48 bits array, and the output is a 32 bits array
void SBox_Function(unsigned int *oldBlock, unsigned int *compressedBlock)
{
    int beg,end,row,col,index;
    unsigned int aux[4]; //Auxiliary array that stored the Sbox value in binary
    beg = 0; //flag to the beginning of the 48 bits array
    end = 5; //flag to the end of the 48 bits array
    index = 0; //flag that controls the array that stores substitutions
    unsigned int rowBin[2], colBin[4]; // arrays that store both row and column of Sbox in binary
    //Execute this until the end of 48 bits array
    while(end<48)
    {
        //Extract both first and last bits in a range of 6 bits
        rowBin[0] = oldBlock[beg];
        rowBin[1] = oldBlock[end];

        //Extract the 4 bits in the middle of beg and end
        for(int i=beg+1,j=0;i<end;i++,j++)
        {
            colBin[j] = oldBlock[i];
        }

        //Convert Row and Col to numbers
        row = bit2int(rowBin,2);
        col = bit2int(colBin,4);

        //Select the correspondent Sbox number and transforms it in binary
        switch(end)
        {
            case 5:
                   int2bin(aux,(int)S1[row][col],4);
                   break;
            case 11:
                   int2bin(aux,(int)S2[row][col],4);
                   break;
            case 17:
                   int2bin(aux,(int)S3[row][col],4);
                   break;
            case 23:
                   int2bin(aux,(int)S4[row][col],4);
                   break;
            case 29:
                   int2bin(aux,(int)S5[row][col],4);
                   break;
            case 35:
                   int2bin(aux,(int)S6[row][col],4);
                   break;
            case 41:
                   int2bin(aux,(int)S7[row][col],4);
                   break;
            case 47:
                   int2bin(aux,(int)S8[row][col],4);
                   break;

        }

        //Append the 4 bits array generated to the 32 bits array
        for(int j=0;j<4;j++,index++)
            compressedBlock[index] = aux[j];


        beg+=6;
        end+=6;

    }
}