crc32 cd/dvd verification [sector reading]

Share your advanced PureBasic knowledge/code with the community.
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

crc32 cd/dvd verification [sector reading]

Post by bingo »

Code updated For 5.20+
sample code ...
edit the line hDevice = CreateFile_("\\.\\D:"... with your right drive letter !

Code: Select all

EnableASM
Structure DISK_GEOMETRY
   Cylinders.q
   MediaType.l
   TracksPerCylinder.l
   SectorsPerTrack.l
   BytesPerSector.l 
EndStructure

Structure DISK_GEOMETRY_EX
pp1.DISK_GEOMETRY
DiskSize.q
byte.b[1];
EndStructure

pp.DISK_GEOMETRY_EX

#IOCTL_DISK_BASE                = 7  
#METHOD_BUFFERED                = 0
#FILE_ANY_ACCESS                = 0

;crc - routine by Horst Schaeffer - horst.schaeffer[at]gmx.net

  *CRCtable = AllocateMemory(1024)
  CLD 
  MOV Edi,*CRCtable
  SUB Edx,Edx    
  !.rpt_DX:
  MOV Eax,Edx
  MOV Ebx,8
  !.rpt_BX:
  SHR Eax,1
  !JNC .cont
  XOR Eax,0EDB88320h
  !.cont:
  DEC Ebx
  !JNZ .rpt_BX
  !stosd
  INC Edx
  CMP Edx,256
  !JB .rpt_DX

  
;  Compute CRC32 for data area given by *mem_ pointer and size_,
;  and crc_ of last chunk (first time: 0) 
;  returns (new) CRC

Procedure CRC32compute(*mem_,size_,crc_)
  Shared *CRCtable
  MOV Edi,*CRCtable
  MOV Esi,*mem_
  MOV Edx,size_
  MOV Eax,crc_
  NOT Eax
  !.rpt_EDX:
  XOR Ebx,Ebx
  MOV BL,AL 
  XOR BL,[Esi]
  SHL Ebx,2 
  SHR Eax,8 
  XOR Eax,[Edi+Ebx] 
  INC Esi
  DEC Edx
  !JNZ .rpt_EDX 
  NOT Eax 
ProcedureReturn ;EAX
EndProcedure
;----------------------------------------------------------------

Procedure.l CTL_CODE(DeviceType, Function, Method, Access)
  ProcedureReturn ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
EndProcedure

#FILE_READ_ACCESS = 1
#FILE_DEVICE_CD_ROM = 2
#IOCTL_CDROM_BASE = #FILE_DEVICE_CD_ROM

IOCTL_CDROM_GET_DRIVE_GEOMETRY_EX = CTL_CODE(#IOCTL_CDROM_BASE, $14, #METHOD_BUFFERED, #FILE_READ_ACCESS)

hDevice = CreateFile_("\\.\\D:", #GENERIC_READ,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,#FILE_ATTRIBUTE_NORMAL,0)

If hDevice

DeviceIoControl_(hDevice, IOCTL_CDROM_GET_DRIVE_GEOMETRY_EX,0,0,@pp,SizeOf(pp),@bytesret, 0)

summe_sektoren.l = (pp\DiskSize / pp\pp1\BytesPerSector) - 1

crc.l
proz.f
pmembuf.l = AllocateMemory(pp\pp1\BytesPerSector)

For i=0 To summe_sektoren
proz = (i/summe_sektoren) * 100
Debug StrF(proz,2) + "%"
If ReadFile_(hDevice,pmembuf,pp\pp1\BytesPerSector,@ret,0) = 0
Debug "read error ..."
Break
EndIf
crc = CRC32compute(pmembuf,pp\pp1\BytesPerSector,crc)
Next

FreeMemory(*CRCtable)
FreeMemory(pmembuf)

Debug Hex(crc)

EndIf
8)
["1:0>1"]