Exemple #1
0
int main(int argc, char **argv) {
	int u1, u2;
	size_t l1, l2;
	int of;
	char *fc1, *fc2;
	Header hdr;
	if (argc!=5) {
		printf("Usage: %s user1.bin user2.bin tagname outfile.bin\n", argv[0]);
		exit(1);
	}
	if (strlen(argv[3])>27) {
		printf("Error: Tag can't be longer than 27 characters.\n");
		exit(1);
	}
	memset(&hdr, 0, sizeof(hdr));
	memcpy(hdr.magic, "EHUG", 4);
	strcpy(hdr.tag, argv[3]);
	u1=openFile(argv[1]);
	u2=openFile(argv[2]);
	l1=fileLen(u1);
	l2=fileLen(u2);
	hdr.len1=intToEsp(l1);
	hdr.len2=intToEsp(l2);
	fc1=malloc(l1);
	fc2=malloc(l2);
	if (read(u1, fc1, l1)!=l1) {
		perror(argv[1]);
		exit(1);
	}
	if (read(u2, fc2, l2)!=l2) {
		perror(argv[2]);
		exit(1);
	}
	close(u1);
	close(u2);
	of=open(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
	if (of<=0) {
		perror(argv[4]);
		exit(1);
	}
	write(of, &hdr, sizeof(hdr));
	write(of, fc1, l1);
	write(of, fc2, l2);
	printf("Header: %d bytes, user1: %d bytes, user2: %d bytes.\n", sizeof(hdr), (int)l1, (int)l2);
	close(of);
	exit(0);
}
int calculateChecksumProto(FILE *file, Song *s)
{
	unsigned char digest[EVP_MAX_MD_SIZE];
	EVP_MD_CTX *mdctx;
	const EVP_MD *md;
	int md_len;

    md = EVP_get_digestbyname("SHA256");
    mdctx = EVP_MD_CTX_create();
    EVP_DigestInit_ex(mdctx, md, NULL);
	
	int bufferSize = 4096;

	char *buff = malloc(bufferSize);
	if(!buff)
		fatal_error("malloc failed\n");

	int numBytesToRead = fileLen(file);
	int currBytesRead =0;
	int bytesRead = 0;

	while(numBytesToRead>bytesRead)
	{

		/* checks if bytes to be read in this iteration are greater than buffer */
		int currNumBytesToRead=(numBytesToRead-bytesRead>bufferSize)?bufferSize:numBytesToRead-bytesRead;

		/* read bytes */
		currBytesRead=fread(buff,1,currNumBytesToRead,file);
		if(currBytesRead<= 0)
			fatal_error("read failed\n"); 
		

		/* update md5 calculation */
		EVP_DigestUpdate(mdctx, buff, currNumBytesToRead);

		/* calculate the total number of bytes read thus far */
		bytesRead += currBytesRead;
	}

	EVP_DigestFinal_ex(mdctx, digest, &md_len);
	EVP_MD_CTX_destroy(mdctx);

	free(buff);
	
	if(fseek(file, 0, SEEK_SET))
		fatal_error("failed to return back to the beginning of the file\n");

	//s->checksum = malloc(sizeof (unsigned char) * (md_len));
	//memcpy(s->checksum, digest, md_len);
	s->checksum.data = malloc(sizeof(unsigned char) * (md_len));
	s->checksum.len = md_len;
	memcpy(s->checksum.data, digest, md_len);
	
	return 1;
}
/* crawls through directory and creates an array of song structs */
song *createSongArray(int numSongs)
{	
	if(!numSongs)
		return 0;

	song *songBuf= (song *)malloc(sizeof(song)*numSongs);
	if(!songBuf)
		fatal_error("malloc memory for songBuf failed\n");
	
	memset(songBuf,0,sizeof(song)*numSongs);
	int i =0;
	
	DIR *d;
	struct dirent *dir;
	d = opendir(".");
	if (d)
	{
		while ((dir = readdir(d)) != NULL)
		{
			char *name=dir->d_name;
			int len =strlen(name);
			if(len>4&&len<TITLELEN&&name[len-4]=='.'&&name[len-3]=='m'&&name[len-2]=='p'&&name[len-1]=='3')
			{
				memcpy(&(songBuf[i].title),(name),len);
				
				FILE *file = fopen(name,"r+"); 

				songBuf[i].lenOfSong=fileLen(file);
				
				if(!calculateChecksum(file,&(songBuf[i])))
					fatal_error("checksum failed to calculate\n");
	
				fclose(file);

			
				i++;
			}
		}

		closedir(d);
	}
	return songBuf;
}
/* sends file, creates buffers as needed, no limit on size it can send, closes file when done, returns number of bytes sent */
int sendFile(FILE *file, int sock)
{
	if(!file)
		return 0;
	

	/* find size of file */
	int numBytesToSend = fileLen(file);		/* total bytes that should be sent */
	if(!numBytesToSend)
		return 0;
	int bufferSize = SNDBUFSIZE;


	/* create send buffer */
	char *sndBuf;                  
	sndBuf = (char *)malloc(bufferSize);
	if(!sndBuf)
		fatal_error("malloc memory for sndBuf failed\n");
		

	memset(sndBuf, 0, bufferSize);
	
	
	int totalFileBytesSent = 0;  			/* total bytes of the entire file that have been sent */ 
	int bytesRead = 0;  					/* total bytes that have been read thus far */
	int bytesSent = 0;  					/* total bytes that have been sent during the inner while loop, this value is reset at the beginning of the outer while loop */
	int currBytesRead=0;					/* current number of bytes read during one iteration of the outer while loop */
	int currBytesSent = 0;  				/* current number of bytes sent during one iteration of the iner while loop */
	int currNumBytesToRead = 0;				/* max number of bytes to be read in an iteration of the outer while loop */
	
	/* while the file has not been sent */
	while(numBytesToSend>totalFileBytesSent) 
	{ 
		currNumBytesToRead = bufferSize;
		
 
		/* read bytes */
		currBytesRead=fread(sndBuf,1,currNumBytesToRead,file);
		if(currBytesRead<= 0)
			fatal_error("read failed\n"); 
		

		/* calculate the total number of bytes read thus far */
		bytesRead += currBytesRead;
					
		/* set the total number of bytes sent in one iteration of the outer while loop and the total number of bytes sent in one iteration of the inner while loop to 0 */
		currBytesSent = 0;
		bytesSent = 0;
		

		/* while the bytes that have been read are greater than the bytes that have been sent */
		while(bytesSent < currBytesRead)
		{
			/* send maximum number of bytes */
			currBytesSent= send(sock, &(sndBuf[bytesSent]), currBytesRead-bytesSent, 0);
			if(currBytesSent<0)
				fatal_error("send failed");
				
			bytesSent+=currBytesSent;

			/*
			printf("curr bytes read: %d\n", currBytesRead);
			printf("bytes sent: %d\n", bytesSent);
			printf("numBytesToSend: %d\n", numBytesToSend);
			printf("total sent: %d\n", totalFileBytesSent);
			*/
		}
		/* keep track of the total number of bytes that have been sent */
		totalFileBytesSent += bytesSent;
	}

	printf("done sending\n");

	free(sndBuf);

	//fclose(file);

	return totalFileBytesSent;
}