Beispiel #1
0
/**
 * Create a new bit_vector with the specified number of bits.
 * TODO: Need to check return value of malloc
 */
bit_vector_t* new_bit_vector(unsigned int num_of_bits) {
    // Allocate the structure
    bit_vector_t *vec = (bit_vector_t *) malloc(sizeof(bit_vector_t));

    // Store number of bits
    vec->size = num_of_bits;

    // Allocate the bits
    vec->bits = (unsigned char *) malloc(calc_bytes(num_of_bits));

    return vec;
}
Beispiel #2
0
/* upload single file to the FTP server */
void ftp_upload_single(ftp_t *ftp, char *path) {
	char *line = NULL;
	char *filename = NULL;
	FILE *data;
	FILE *fp;
	char buffer[MAX_STR];
	long int dsize = 0;
	long int rsize = 0;
	long int fsize = 0;
	data_t d1;
	data_t d2;

	if ( path[0] == 0x2f || path[strlen(path)-1] == 0x2f )
		filename = strrchr(path, '/') + 1;
	else
		filename = path;

	if (( fp = fopen(path, "r")) == NULL )
		die("Cannot read %s.", path);

	fseek(fp, 0L, SEEK_END);
	fsize = ftell(fp);
	fseek(fp, 0L, SEEK_SET);

	ftp->dataport = ftp_getdataport(ftp);
	fprintf(ftp->FD, "TYPE I\r\n");
	line = ftp_getline(ftp);
	if ( atoi(line) == 500 )
		die("Failed to set TYPE.");

	fprintf(ftp->FD, "STOR %s\r\n", path);

	/* SIGALRM */
	line = ftp_getline(ftp);
	data = tcp_connect2(ip, ftp->dataport, "w");

	calc_bytes(&d1, fsize);
	while(( rsize = fread(buffer, 1, sizeof(buffer), fp) ) > 0 ) {
		if ( buffer[rsize +1] == '\r' )
			buffer[rsize +1] = '\0';
		dsize += fwrite(buffer, 1, rsize, data);
		calc_bytes(&d2, dsize);
		fprintf(stdout, "%s:: Uploading %s\'%s\'%s %s(%.2f%c) %.2f%c%s\r",
				WHT,GREEN,
				filename,END,
				YEL,
				d1.bytes,
				d1.bytes_postfix,
				d2.bytes,
				d2.bytes_postfix,
				END);

		fflush(stdout);
	}
	putchar(0x0a);

	if ( fsize == dsize )
		print(0, "File %s\'%s\'%s saved on the server.", GREEN,filename,END);
	else
		print(1, "File %s\'%s\'%s is corrupted, try uploading it again?", RED,filename,END);
		
	fclose(fp);
	fclose(data);
	close(dfd);
}
Beispiel #3
0
/* download single file from the FTP server */
void ftp_download_single(ftp_t *ftp, char *path) {
	char *line = NULL;
	FILE *data;
	FILE *fp;
	char buffer[MAX_STR];
	char *filename = NULL;
	long int dsize = 0;
	int rsize = 0;
	long int fsize = 0;
	long int lsize = 0;
	data_t d1;
	data_t d2;

	/* In case the URL have more then one / */
	if ( path[strlen(path)-1] == 0x2f )
		path[strlen(path)-1] = 0x00;

	/* strrchr return the last string */
	filename = strrchr(path, '/') + 1;

	/* Check if file exists before downloading */
	if (util_file_exists(filename) == 1 ) {
		print(1, "%s\'%s\' already exists.%s", WHT,filename,END);
		exit(0);
	}

	fsize = (int)ftp_size(ftp, path);
	ftp->dataport = ftp_getdataport(ftp);
	fprintf(ftp->FD, "TYPE I\r\n");
	line = ftp_getline(ftp);
	if ( atoi(line) == 500 )
		die("Failed to set TYPE.");
	fprintf(ftp->FD, "RETR %s\r\n", path);

	/* SIGALRM */
	line = ftp_getline(ftp);
	data = tcp_connect2(ip, ftp->dataport, "r");
		
	if (( fp = fopen(filename, "w")) == NULL )
		die("Cannot create %s.", filename);

	calc_bytes(&d1, fsize);
	while(( rsize = fread(buffer, 1, sizeof(buffer), data) ) > 0 ) {
		if ( buffer[rsize +1] == '\r' )
			buffer[rsize +1] = '\0';
		dsize += fwrite(buffer, 1, rsize, fp);
		calc_bytes(&d2, dsize);
		print(3, "%s Downloading %s\'%s\'%s (%.2f%c) %.2f %c\b\b\b\b\b\r", WHT,GREEN,
				filename,END,
				d1.bytes, d1.bytes_postfix,
				d2.bytes, d2.bytes_postfix);
		fflush(stdout);
	}
	putchar(0x0a);
	fseek(fp, 0L, SEEK_END);
	lsize = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
		
	/* All good */
	if ( lsize == fsize )
		print(0, "File %s\'%s\'%s saved.", GREEN,filename,END);
	else {
		/* inetutils <=1.9.4, 
		 * This bug discoverd by me in 05-10-2015,
		 *  SIZE command return file_size+23 bytes*/
		if ( lsize+23 == fsize ) {
			print(0, "File \'%s\' saved.", GREEN,filename,END);
			print(0, "%sYou have unpatched version of inetutils 1.x.x please upgrade.%s", YEL,END);
		}
		/* Bad */
		else
			print(1, "File %s\'%s\'%s is corrupted, try downloading it again?", RED,filename,END);
	}
	fclose(fp);
	fclose(data);
	close(dfd);
}