1 /*
2  * isotest - display cdrom information
3  */
4 
5 #define WIN32_NO_STATUS
6 #include <windows.h>
7 #define NTOS_MODE_USER
8 #include <ndk/ntndk.h>
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <wchar.h>
15 
16 
17 void HexDump(char *buffer, ULONG size)
18 {
19   ULONG offset = 0;
20   unsigned char *ptr;
21 
22   while (offset < (size & ~15))
23     {
24       ptr = (unsigned char*)((ULONG_PTR)buffer + offset);
25       printf("%08lx  %02hx %02hx %02hx %02hx %02hx %02hx %02hx %02hx-%02hx %02hx %02hx %02hx %02hx %02hx %02hx %02hx",
26 	     offset,
27 	     ptr[0],
28 	     ptr[1],
29 	     ptr[2],
30 	     ptr[3],
31 	     ptr[4],
32 	     ptr[5],
33 	     ptr[6],
34 	     ptr[7],
35 	     ptr[8],
36 	     ptr[9],
37 	     ptr[10],
38 	     ptr[11],
39 	     ptr[12],
40 	     ptr[13],
41 	     ptr[14],
42 	     ptr[15]);
43 
44       printf("  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
45 	     isprint(ptr[0])?ptr[0]:'.',
46 	     isprint(ptr[1])?ptr[1]:'.',
47 	     isprint(ptr[2])?ptr[2]:'.',
48 	     isprint(ptr[3])?ptr[3]:'.',
49 	     isprint(ptr[4])?ptr[4]:'.',
50 	     isprint(ptr[5])?ptr[5]:'.',
51 	     isprint(ptr[6])?ptr[6]:'.',
52 	     isprint(ptr[7])?ptr[7]:'.',
53 	     isprint(ptr[8])?ptr[8]:'.',
54 	     isprint(ptr[9])?ptr[9]:'.',
55 	     isprint(ptr[10])?ptr[10]:'.',
56 	     isprint(ptr[11])?ptr[11]:'.',
57 	     isprint(ptr[12])?ptr[12]:'.',
58 	     isprint(ptr[13])?ptr[13]:'.',
59 	     isprint(ptr[14])?ptr[14]:'.',
60 	     isprint(ptr[15])?ptr[15]:'.');
61 
62       offset += 16;
63     }
64 
65   ptr = (unsigned char*)((ULONG_PTR)buffer + offset);
66   if (offset < size)
67     {
68       printf("%08lx ", offset);
69       while (offset < size)
70 	{
71 	  printf(" %02hx", *ptr);
72 	  offset++;
73 	  ptr++;
74 	}
75     }
76 
77   printf("\n\n");
78 }
79 
80 
81 #ifndef EVENT_ALL_ACCESS
82 #define EVENT_ALL_ACCESS	(0x1f0003L)
83 #endif
84 
85 BOOL
86 ReadBlock(HANDLE FileHandle,
87 	  PVOID Buffer,
88 	  PLARGE_INTEGER Offset,
89 	  ULONG Length,
90 	  PULONG BytesRead)
91 {
92   IO_STATUS_BLOCK IoStatusBlock;
93   OBJECT_ATTRIBUTES ObjectAttributes;
94   NTSTATUS Status;
95   HANDLE EventHandle;
96 
97   InitializeObjectAttributes(&ObjectAttributes,
98 			     NULL, 0, NULL, NULL);
99 
100   Status = NtCreateEvent(&EventHandle,
101 			 EVENT_ALL_ACCESS,
102 			 &ObjectAttributes,
103 			 TRUE,
104 			 FALSE);
105   if (!NT_SUCCESS(Status))
106     {
107       printf("NtCreateEvent() failed\n");
108       return(FALSE);
109     }
110 
111   Status = NtReadFile(FileHandle,
112 		      EventHandle,
113 		      NULL,
114 		      NULL,
115 		      &IoStatusBlock,
116 		      Buffer,
117 		      Length,
118 		      Offset,
119 		      NULL);
120   if (Status == STATUS_PENDING)
121     {
122       NtWaitForSingleObject(EventHandle, FALSE, NULL);
123       Status = IoStatusBlock.Status;
124     }
125 
126   NtClose(EventHandle);
127 
128   if (Status != STATUS_PENDING && BytesRead != NULL)
129     {
130       *BytesRead = IoStatusBlock.Information;
131     }
132   if (!NT_SUCCESS(Status) && Status != STATUS_END_OF_FILE)
133     {
134       printf("ReadBlock() failed (Status: %lx)\n", Status);
135       return(FALSE);
136     }
137 
138   return(TRUE);
139 }
140 
141 
142 
143 int main (int argc, char *argv[])
144 {
145   HANDLE hDisk;
146   DWORD dwRead;
147   char *Buffer;
148   CHAR Filename[80];
149   LARGE_INTEGER FilePosition;
150 
151   if (argc != 2)
152     {
153       printf("Usage: isotest [Drive:]\n");
154       return 0;
155     }
156 
157   strcpy(Filename, "\\\\.\\");
158   strcat(Filename, argv[1]);
159 
160   hDisk = CreateFile(Filename,
161 		     GENERIC_READ,
162 		     FILE_SHARE_READ | FILE_SHARE_WRITE,
163 		     NULL,
164 		     OPEN_EXISTING,
165 		     0,
166 		     NULL);
167   if (hDisk == INVALID_HANDLE_VALUE)
168     {
169       printf("CreateFile(): Invalid disk handle!\n");
170       return 0;
171     }
172 
173   Buffer = (char*)malloc(2048);
174   if (Buffer == NULL)
175     {
176       CloseHandle(hDisk);
177       printf("Out of memory!\n");
178       return 0;
179     }
180   memset(Buffer, 0, 2048);
181 
182 
183   FilePosition.QuadPart = 16 * 2048;
184 #if 0
185   SetLastError(NO_ERROR);
186   SetFilePointer(hDisk,
187 		 FilePosition.u.LowPart,
188 		 &FilePosition.u.HighPart,
189 		 FILE_BEGIN);
190   if (GetLastError() != NO_ERROR)
191     {
192       CloseHandle(hDisk);
193       free(Buffer);
194       printf("SetFilePointer() failed!\n");
195       return 0;
196     }
197 
198   if (ReadFile(hDisk,
199 	       Buffer,
200 	       2048,
201 	       &dwRead,
202 	       NULL) == FALSE)
203     {
204       CloseHandle(hDisk);
205       free(Buffer);
206       printf("ReadFile() failed!\n");
207       return 0;
208     }
209 #endif
210 
211   if (ReadBlock(hDisk,
212 		Buffer,
213 		&FilePosition,
214 		2048,
215 		&dwRead) == FALSE)
216     {
217       CloseHandle(hDisk);
218       free(Buffer);
219 #if 0
220       printf("ReadBlock() failed!\n");
221 #endif
222       return 0;
223     }
224 
225   HexDump(Buffer, 128);
226 
227   CloseHandle(hDisk);
228 
229   free(Buffer);
230 
231   return 0;
232 }
233 
234 /* EOF */
235