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