/** * Transforma el contenido del arreglo 'input' en * una cadena con la codificacion de este en base * 64. El resultado se almacena en el arreglo 'output'. * Devuelve true si la operacion fue exitosa. */ bool encode_to_base64(char input[3], char output[4]) { int32_t temporal = 0; int32_t index = 0; int i; char* temporal_array = (char*) &temporal; char* index_array = (char*) &index; memcpy(&temporal, input, 3); if (little_endian) array_invert(temporal_array); temporal = temporal >> 8; for (i = 0; i < 4; i++) { temporal = temporal << 6; if (little_endian) { //Little endian index_array[0] = temporal_array[3]; temporal_array[3] = 0; } else { //Big endian index_array[3] = temporal_array[0]; temporal_array[0] = 0; } output[i] = ALFABETO[index]; } return true; }
/** * Decodifica el contenido del arreglo 'input' en * codificado en base 64. El resultado se almacena * en el arreglo 'output'. Devuelve true si la * operacion fue exitosa. */ bool decode_from_base64(char input[4], char output[3], int* padding) { int32_t temporal = 0; int i; *padding = 0; char* temporal_array = (char*) &temporal; for (i = 3; i >= 0; i--) { int index = index_of(input[i], ALFABETO, 64); //TODO: largo de la base if (index < 0) { if (input[i] != PADDING_CHAR) return false; *padding += 1; if (little_endian) temporal_array[3] = 0; else temporal_array[0] = 0; } else { if (little_endian) temporal_array[3] = index; else temporal_array[0] = index; } temporal = temporal >> 6; } if (little_endian) array_invert(temporal_array); memcpy(output, (temporal_array + 1), 3); return true; }
int main(){ int x[1000]= {1,2,3,4,5,6}; int size = sizeof(x)/sizeof(int); size = array_elements_count(x,size); printf("Array elements count: %d", size); printf("\nArray data: "); print_array(x,size); array_sorter(x, size); printf("\nArray Sorted: "); print_array(x,size); printf("\nArray Uniq: "); array_remove_dupes(x, size); printf("\n\narray invert: "); array_invert(x, size); printf("\n"); }