Commits

Anonymous committed 0765d18 Merge

merged atomicparsley upstream fixes

  • Participants
  • Parent commits 814d6c6, 6dc1fe5

Comments (0)

Files changed (7)

File configure.ac

 CXXFLAGS="-D_FILE_OFFSET_BITS=64 $CXXFLAGS"
 
 AC_ARG_ENABLE(debug, dnl
-[  --disable-debug_build   do not build a debug version [default=yes]],
+[  --disable-debug         do not build a debug version [default=yes]],
               debug=$enableval, debug=no)
 if test "$debug" = "yes" ; then
   AC_DEFINE_UNQUOTED(DEBUG, $debug, [build binary with debug output])

File src/extracts.cpp

 	uint32_t compatible_brand = 0;
 	bool cb_V2ISOBMFF = false;
 	
-	fseek(a_file, 4, SEEK_SET); //this fseek will to.... the first 30 or so bytes; fseeko isn't required
-	fread(buffer, 1, 4, a_file);
+	APar_read32(buffer, a_file, 4);
 	if (memcmp(buffer, "ftyp", 4) == 0) {
 		atom_length = APar_read32(buffer, a_file, 0);
 	} else {

File src/id3v2.cpp

 		}
 		case ID3_PLAYCOUNTER_FRAME : {
 			uint64_t playcount = 0;
-			char play_count_syncsafe[15];
-			memset(play_count_syncsafe, 0, 16);
+			char play_count_syncsafe[16];
+
+			memset(play_count_syncsafe, 0, sizeof(play_count_syncsafe));
 
 			if (memcmp(frame_payload, "+1", 3) == 0) {
 				if (thisFrame->ID3v2_Frame_Length == 4) {
 		case ID3_POPULAR_FRAME : {
 			unsigned char popm_rating = 0;
 			uint64_t popm_playcount = 0;
-			char popm_play_count_syncsafe[15];
-			memset(popm_play_count_syncsafe, 0, 16);
+			char popm_play_count_syncsafe[16];
+
+			memset(popm_play_count_syncsafe, 0,
+				sizeof(popm_play_count_syncsafe));
 			
 			if (adjunct_payload->ratingArg != NULL) {
 				sscanf(adjunct_payload->ratingArg, "%hhu", &popm_rating);

File src/metalist.cpp

 	}
 	char* uuid_payload = (char*)calloc(1, sizeof(char) * (uuid_atom->AtomicLength - 36 +1) );
 			
-	fseeko(source_file, uuid_atom->AtomicStart + 36, SEEK_SET);
-	fread(uuid_payload, 1, uuid_atom->AtomicLength - 36, source_file);
+	APar_readX(uuid_payload, source_file, uuid_atom->AtomicStart + 36, uuid_atom->AtomicLength - 36);
 	
 	uint32_t descrip_len = UInt32FromBigEndian(uuid_payload);
 	atom_offsets+=4+descrip_len;
 	
 	char* art_payload = (char*)malloc( sizeof(char) * (parsedAtoms[this_atom_num].AtomicLength-16) +1 );	
 	memset(art_payload, 0, (parsedAtoms[this_atom_num].AtomicLength-16) +1 );
-			
-	fseeko(source_file, parsedAtoms[this_atom_num].AtomicStart+16, SEEK_SET);
-	fread(art_payload, 1, parsedAtoms[this_atom_num].AtomicLength-16, source_file);
+	
+	APar_readX(art_payload, source_file, parsedAtoms[this_atom_num].AtomicStart+16, parsedAtoms[this_atom_num].AtomicLength-16);
 	
 	char* suffix = (char *)malloc(sizeof(char)*5);
 	memset(suffix, 0, sizeof(char)*5);
 			char* data_payload = (char*)malloc( sizeof(char) * (thisAtom->AtomicLength - atom_header_size +1) );
 			memset(data_payload, 0, sizeof(char) * (thisAtom->AtomicLength - atom_header_size +1) );
 			
-			fseeko(source_file, thisAtom->AtomicStart + atom_header_size, SEEK_SET);
-			fread(data_payload, 1, thisAtom->AtomicLength - atom_header_size, source_file);
+			APar_readX(data_payload, source_file, thisAtom->AtomicStart + atom_header_size, thisAtom->AtomicLength - atom_header_size);
 			
 			if (thisAtom->AtomicVerFlags == AtomFlags_Data_Text) {
 				if (thisAtom->AtomicLength < (atom_header_size + 4) ) {

File src/parsley.cpp

 	int artwork_dataType = 0;
 	FILE *artfile = APar_OpenFile(artworkPath, "rb");
 	if (artfile != NULL) {		
-		fread(twenty_byte_buffer, 1, 8, artfile);
+		APar_read64(twenty_byte_buffer, artfile, 0);
 		if ( strncmp(twenty_byte_buffer, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0 ) {
 			artwork_dataType = AtomFlags_Data_PNGBinary;
 		} else if ( strncmp(twenty_byte_buffer, "\xFF\xD8\xFF\xE0", 4) == 0 || memcmp(twenty_byte_buffer, "\xFF\xD8\xFF\xE1", 4) == 0 ) {
 
 void APar_Extract_stsd_codec(FILE* file, uint64_t midJump) {
 	memset(twenty_byte_buffer, 0, 12);
-	fseeko(file, midJump, SEEK_SET);
-	fread(twenty_byte_buffer, 1, 12, file);
+	APar_readX(twenty_byte_buffer, file, midJump, 12);
 	parsedAtoms[atom_number-1].ancillary_data = UInt32FromBigEndian( twenty_byte_buffer +4 );
   return;
 }
 			uint64_t dataSize = 0;
 			uint64_t jump = 0;
 			
-			fread(data, 1, 12, file);
+			APar_readX(data, file, 0, 12);
 			char *atom = data+4;
 			
 			if ( memcmp(data, "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A ", 12) == 0 ) {
 					
 					uuid_info.uuid_form = UUID_DEPRECATED_FORM; //start with the assumption that any found atom is in the depracted uuid form
 					
-					fread(data, 1, 12, file);
+					APar_readX_noseek(data, file, 12);
 					char *atom = data+4;
 					dataSize = UInt32FromBigEndian(data);
 					
 					if (memcmp(atom, "mean", 4) == 0 && memcmp(parsedAtoms[atom_number-2].AtomicName, "----", 4) == 0) {
 							parsedAtoms[atom_number-1].ReverseDNSdomain = (char *)calloc(1, sizeof(char) * dataSize);
 							
-							fseeko(file, jump + 12, SEEK_SET); //'name' atom is the 2nd child
-							fread(parsedAtoms[atom_number-1].ReverseDNSdomain, 1, dataSize - 12, file);
+							//jump + 12 because 'name' atom is the 2nd child
+							APar_readX(parsedAtoms[atom_number-1].ReverseDNSdomain, file, jump + 12, dataSize - 12);
 					}
 					if (memcmp(atom, "name", 4) == 0 && 
 					    memcmp(parsedAtoms[atom_number-2].AtomicName, "mean", 4) == 0 &&
 							
 							parsedAtoms[atom_number-1].ReverseDNSname = (char *)calloc(1, sizeof(char) * dataSize);
 							
-							fseeko(file, jump + 12, SEEK_SET); //'name' atom is the 2nd child
-							fread(parsedAtoms[atom_number-1].ReverseDNSname, 1, dataSize - 12, file);
+							//jump + 12 because 'name' atom is the 2nd child
+							APar_readX(parsedAtoms[atom_number-1].ReverseDNSname, file, jump + 12, dataSize - 12);
 					}
 					
 					if (dataSize == 0) { // length = 0 means it reaches to EOF
 			file_name_len = strlen(file_name);
 			memcpy(temp_path, filePath, filepath_len-file_name_len+1);
 		} else {
-			getcwd(temp_path, MAXPATHLEN);
+			if( getcwd(temp_path, MAXPATHLEN) == NULL ) {
+				printf("Error getting working directory: %s\n", strerror(errno));
+				exit(1);
+			}
 			file_name = (char*)filePath;
 			file_name_len = strlen(file_name);
 			memcpy(temp_path + strlen(temp_path), "/", 1);
 		if (dump_file != NULL) {
 			//body of atom writing here
 			
-			fseeko(source_file, userdata_atom->AtomicStart, SEEK_SET);
-			fread(dump_buffer, 1, (size_t)userdata_atom->AtomicLength, source_file);
+			APar_readX(dump_buffer, source_file, userdata_atom->AtomicStart, (size_t)userdata_atom->AtomicLength);
 			
 			fwrite(dump_buffer, (size_t)userdata_atom->AtomicLength, 1, dump_file);
 			fclose(dump_file);
 	uint64_t file_pos = 0;
 	while (file_pos <= src_file_size) {
 		if (file_pos + max_buffer <= src_file_size ) {
-			fseeko(src_file, file_pos, SEEK_SET);
-			fread(buffer, 1, max_buffer, src_file);
+			APar_readX(buffer, src_file, file_pos, max_buffer);
 			
 			//fseek(dest_file, dest_position + file_pos, SEEK_SET);
 #if defined(_MSC_VER)
 			file_pos += max_buffer;
 			
 		} else {
-			fseeko(src_file, file_pos, SEEK_SET);
-			fread(buffer, 1, src_file_size - file_pos, src_file);
+			APar_readX(buffer, src_file, file_pos, src_file_size - file_pos);
 			//fprintf(stdout, "buff starts with %s\n", buffer+4);
 #if defined(_MSC_VER)
 			fpos_t file_offset = dest_position + file_pos;
 		fflush(dest_file);
 		SetEndOfFile((HANDLE)_get_osfhandle(fileno(dest_file)));
 #else
-		ftruncate(fileno(dest_file), src_file_size+dest_position);
+		if(ftruncate(fileno(dest_file), src_file_size+dest_position) == -1) {
+			perror("Failed to truncate file: ");
+			exit(1);
+		}
 #endif
 	}
 	return;

File src/util.cpp

 	}
 	
 	if (!aFile) {
-		fprintf(stdout, "AP error trying to fopen: %s\n", strerror(errno));
+		fprintf(stdout, "AP error trying to fopen %s: %s\n", utf8_filepath, strerror(errno));
 	}
 	return aFile;
 }
 
 uint8_t APar_read8(FILE* ISObasemediafile, uint64_t pos) {
 	uint8_t a_byte = 0;
+	size_t size;
 	fseeko(ISObasemediafile, pos, SEEK_SET);
-	fread(&a_byte, 1, 1, ISObasemediafile);
+	size = fread(&a_byte, 1, 1, ISObasemediafile);
+	if(size != 1) {
+		printf("%s read failed, expect 1, got %u: %s\n", __func__, (unsigned int)size, strerror(errno));
+		exit(1);
+	}
 	return a_byte;
 }
 
 uint16_t APar_read16(char* buffer, FILE* ISObasemediafile, uint64_t pos) {
+	size_t size;
 	fseeko(ISObasemediafile, pos, SEEK_SET);
-	fread(buffer, 1, 2, ISObasemediafile);
+	size = fread(buffer, 1, 2, ISObasemediafile);
+	if(size != 2) {
+		printf("%s read failed, expect 2, got %u: %s\n", __func__, (unsigned int)size, strerror(errno));
+		exit(1);
+	}
 	return UInt16FromBigEndian(buffer);
 }
 
 uint32_t APar_read32(char* buffer, FILE* ISObasemediafile, uint64_t pos) {
+	size_t size;
 	fseeko(ISObasemediafile, pos, SEEK_SET);
-	fread(buffer, 1, 4, ISObasemediafile);
+	size = fread(buffer, 1, 4, ISObasemediafile);
+	if(size != 4) {
+		printf("%s read failed, expect 4, got %u: %s\n", __func__, (unsigned int)size, strerror(errno));
+		exit(1);
+	}
 	return UInt32FromBigEndian(buffer);
 }
 
 uint64_t APar_read64(char* buffer, FILE* ISObasemediafile, uint64_t pos) {
+	size_t size;
 	fseeko(ISObasemediafile, pos, SEEK_SET);
-	fread(buffer, 1, 8, ISObasemediafile);
+	size = fread(buffer, 1, 8, ISObasemediafile);
+	if(size != 8) {
+		printf("%s read failed, expect 8, got %u: %s\n", __func__, (unsigned int)size, strerror(errno));
+		exit(1);
+	}
 	return UInt64FromBigEndian(buffer);
 }
 
+void APar_readX_noseek(char* buffer, FILE* ISObasemediafile, uint32_t length) {
+	size_t size;
+	size = fread(buffer, 1, length, ISObasemediafile);
+	if(size != length) {
+		printf("%s read failed, expect %u, got %u: %s\n", __func__, length, (unsigned int)size, strerror(errno));
+		exit(1);
+	}
+	return;
+}
+
 void APar_readX(char* buffer, FILE* ISObasemediafile, uint64_t pos, uint32_t length) {
+	size_t size;
 	fseeko(ISObasemediafile, pos, SEEK_SET);
-	fread(buffer, 1, length, ISObasemediafile);
+	size = fread(buffer, 1, length, ISObasemediafile);
+	if(size != length) {
+		printf("%s read failed, expect %u, got %u: %s\n", __func__, length, (unsigned int) size, strerror(errno));
+		exit(1);
+	}
 	return;
 }
 
 uint16_t APar_read16(char* buffer, FILE* ISObasemediafile, uint64_t pos);
 uint32_t APar_read32(char* buffer, FILE* ISObasemediafile, uint64_t pos);
 uint64_t APar_read64(char* buffer, FILE* ISObasemediafile, uint64_t pos);
+void APar_readX_noseek(char* buffer, FILE* ISObasemediafile, uint32_t length);
 void APar_readX(char* buffer, FILE* ISObasemediafile, uint64_t pos, uint32_t length);
 uint32_t APar_ReadFile(char* destination_buffer, FILE* a_file, uint32_t bytes_to_read);
 uint32_t APar_FindValueInAtom(char* uint32_buffer, FILE* ISObasemediafile, short an_atom, uint64_t start_position, uint32_t eval_number);