Skip to content

Instantly share code, notes, and snippets.

@ti-kamlesh
Last active August 1, 2023 06:51
Show Gist options
  • Save ti-kamlesh/73abfcc1a33318bb3b199d36b6209e59 to your computer and use it in GitHub Desktop.
Save ti-kamlesh/73abfcc1a33318bb3b199d36b6209e59 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if_alg.h>
#include <string.h>
#define BUFFER_SIZE 4096
#define SECTOR 4096
int main (int argc, char **argv) {
int desc[2] = { -1, -1 };
struct sockaddr_alg sock = {
.salg_family = AF_ALG,
.salg_type = "hash",
.salg_name = "crc64-iso"
};
if ((desc[0] = socket(AF_ALG, SOCK_SEQPACKET, 0)) == -1 ) {
perror("socket");
return -1;
}
if( bind(desc[0], (struct sockaddr *) &sock, sizeof(sock)) != 0 ) {
perror("bind");
return -1;
}
if( (desc[1] = accept(desc[0], NULL, 0)) == -1 )
return -1;
if (argc != 2 || strlen(argv[1]) == 0) {
printf("Please specify filename\n");
return -1; // empty string
}
FILE* file;
unsigned char *buffer;
if(posix_memalign((void *)&buffer, SECTOR, BUFFER_SIZE)) {
perror("posix_memalign failed");
return -1;
}
file = fopen(argv[1], "rb");
if(file == NULL)
{
printf("An error occured while opening file: %s\n", argv[1]);
free(buffer);
return -1;
}
while(!feof(file))
{
unsigned int count = fread(buffer, sizeof(char), BUFFER_SIZE, file);
if(ferror(file))
{
printf("An error occurred while accessing the file: %s\n", argv[1]);
fclose(file);
free(buffer);
return -1;
}
if (send(desc[1], buffer, count, MSG_MORE) != count) {
free(buffer);
return -1;
}
}
long int crc64 = 0x0000000000000000;
if(read(desc[1], &crc64, 8) != 8) {
free(buffer);
return -1;
}
printf("0x%llx\n", crc64);
free(buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment