MCPcopy Create free account
hub / github.com/cgsecurity/testdisk / set_next_cluster

Function set_next_cluster

src/fat.c:433–509  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

431}
432
433int set_next_cluster(disk_t *disk_car,const partition_t *partition, const upart_type_t upart_type,const int offset, const unsigned int cluster, const unsigned int next_cluster)
434{
435 unsigned char *buffer;
436 unsigned long int offset_s,offset_o;
437 const unsigned int buffer_size=(upart_type==UP_FAT12?2*disk_car->sector_size:disk_car->sector_size);
438 buffer=(unsigned char*)MALLOC(buffer_size);
439 /* Offset can be offset to FAT1 or to FAT2 */
440 /* log_trace("set_next_cluster(upart_type=%u,offset=%u,cluster=%u,next_cluster=%u)\n",upart_type,offset,cluster,next_cluster); */
441 switch(upart_type)
442 {
443 case UP_FAT12:
444 offset_s=(cluster+cluster/2)/disk_car->sector_size;
445 offset_o=(cluster+cluster/2)%disk_car->sector_size;
446 break;
447 case UP_FAT16:
448 offset_s=cluster/(disk_car->sector_size/2);
449 offset_o=cluster%(disk_car->sector_size/2);
450 break;
451 case UP_FAT32:
452 offset_s=cluster/(disk_car->sector_size/4);
453 offset_o=cluster%(disk_car->sector_size/4);
454 break;
455 default:
456#ifndef DISABLED_FOR_FRAMAC
457 log_critical("fat.c set_next_cluster unknown fat type\n");
458#endif
459 free(buffer);
460 return 1;
461 }
462 if((unsigned)disk_car->pread(disk_car, buffer, buffer_size,
463 partition->part_offset + (uint64_t)(offset + offset_s) * disk_car->sector_size) != buffer_size)
464 {
465#ifndef DISABLED_FOR_FRAMAC
466 log_error("set_next_cluster read error\n");
467#endif
468 free(buffer);
469 return 1;
470 }
471 switch(upart_type)
472 {
473 case UP_FAT12:
474 if((cluster&1)!=0)
475 (*((uint16_t*)&buffer[offset_o]))=le16((next_cluster<<4) | (le16(*((uint16_t*)&buffer[offset_o]))&0xF));
476 else
477 (*((uint16_t*)&buffer[offset_o]))=le16((next_cluster) | (le16(*((uint16_t*)&buffer[offset_o]))&0xF000));
478 break;
479 case UP_FAT16:
480 {
481 uint16_t *p16=(uint16_t*)buffer;
482 p16[offset_o]=le16(next_cluster);
483 }
484 break;
485 case UP_FAT32:
486 {
487 uint32_t *p32=(uint32_t*)buffer;
488 /* FAT32 used 28 bits, the 4 high bits are reserved
489 * 0x00000000: free cluster
490 * 0x0FFFFFF7: bad cluster

Callers 1

fat32_create_rootdirFunction · 0.85

Calls 1

MALLOCFunction · 0.85

Tested by

no test coverage detected