Debian Codegraph @

libcfile/libcfile_winapi.c

raw symbols SCIP
1 /*
2 * WINAPI fallback functions
3 *
4 * Copyright (C) 2008-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <narrow_string.h>
24 #include <system_string.h>
25 #include <types.h>
26 #include <wide_string.h>
27
28 #if defined( WINAPI )
29 #include <io.h>
30 #endif
31
32 #if defined( WINAPI ) && !defined( __CYGWIN__ )
33 #include <share.h>
34 #endif
35
36 #include "libcfile_winapi.h"
37
38 #if defined( WINAPI ) && ( WINVER <= 0x0500 )
39
40 /* Cross Windows safe version of CloseHandle
41 * Returns TRUE if successful or FALSE on error
42 */
43 BOOL libcfile_CloseHandle(
44 HANDLE file_handle )
45 {
46 FARPROC function = NULL;
47 HMODULE library_handle = NULL;
48 BOOL result = FALSE;
49
50 if( file_handle == NULL )
51 {
52 return( FALSE );
53 }
54 library_handle = LoadLibrary(
55 _SYSTEM_STRING( "kernel32.dll" ) );
56
57 if( library_handle == NULL )
58 {
59 return( FALSE );
60 }
61 function = GetProcAddress(
62 library_handle,
63 (LPCSTR) "CloseHandle" );
64
65 if( function != NULL )
66 {
67 result = function(
68 file_handle );
69 }
70 /* This call should be after using the function
71 * in most cases kernel32.dll will still be available after free
72 */
73 if( FreeLibrary(
74 library_handle ) != TRUE )
75 {
76 result = FALSE;
77 }
78 return( result );
79 }
80
81 /* Cross Windows safe version of CreateFileA
82 * Returns a handle if successful or INVALID_HANDLE_VALUE on error
83 */
84 HANDLE libcfile_CreateFileA(
85 LPCSTR filename,
86 DWORD desired_access,
87 DWORD share_mode,
88 SECURITY_ATTRIBUTES *security_attributes,
89 DWORD creation_disposition,
90 DWORD flags_and_attributes,
91 HANDLE template_file )
92 {
93 FARPROC function = NULL;
94 HANDLE result = INVALID_HANDLE_VALUE;
95 HMODULE library_handle = NULL;
96
97 if( filename == NULL )
98 {
99 return( INVALID_HANDLE_VALUE );
100 }
101 library_handle = LoadLibrary(
102 _SYSTEM_STRING( "kernel32.dll" ) );
103
104 if( library_handle == NULL )
105 {
106 return( INVALID_HANDLE_VALUE );
107 }
108 function = GetProcAddress(
109 library_handle,
110 (LPCSTR) "CreateFileA" );
111
112 if( function != NULL )
113 {
114 result = (HANDLE) function(
115 filename,
116 desired_access,
117 share_mode,
118 security_attributes,
119 creation_disposition,
120 flags_and_attributes,
121 template_file );
122 }
123 /* This call should be after using the function
124 * in most cases kernel32.dll will still be available after free
125 */
126 if( FreeLibrary(
127 library_handle ) != TRUE )
128 {
129 libcfile_CloseHandle(
130 result );
131
132 return( INVALID_HANDLE_VALUE );
133 }
134 return( result );
135 }
136
137 #if defined( HAVE_WIDE_CHARACTER_TYPE )
138
139 /* Cross Windows safe version of CreateFileW
140 * Returns a handle if successful or INVALID_HANDLE_VALUE on error
141 */
142 HANDLE libcfile_CreateFileW(
143 LPCWSTR filename,
144 DWORD desired_access,
145 DWORD share_mode,
146 SECURITY_ATTRIBUTES *security_attributes,
147 DWORD creation_disposition,
148 DWORD flags_and_attributes,
149 HANDLE template_file )
150 {
151 FARPROC function = NULL;
152 HANDLE result = INVALID_HANDLE_VALUE;
153 HMODULE library_handle = NULL;
154
155 if( filename == NULL )
156 {
157 return( INVALID_HANDLE_VALUE );
158 }
159 library_handle = LoadLibrary(
160 _SYSTEM_STRING( "kernel32.dll" ) );
161
162 if( library_handle == NULL )
163 {
164 return( INVALID_HANDLE_VALUE );
165 }
166 function = GetProcAddress(
167 library_handle,
168 (LPCSTR) "CreateFileW" );
169
170 if( function != NULL )
171 {
172 result = (HANDLE) function(
173 filename,
174 desired_access,
175 share_mode,
176 security_attributes,
177 creation_disposition,
178 flags_and_attributes,
179 template_file );
180 }
181 /* This call should be after using the function
182 * in most cases kernel32.dll will still be available after free
183 */
184 if( FreeLibrary(
185 library_handle ) != TRUE )
186 {
187 libcfile_CloseHandle(
188 result );
189
190 return( INVALID_HANDLE_VALUE );
191 }
192 return( result );
193 }
194
195 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
196
197 /* Cross Windows safe version of GetOverlappedResult
198 * Returns TRUE if successful or FALSE on error
199 */
200 BOOL libcfile_GetOverlappedResult(
201 HANDLE file_handle,
202 OVERLAPPED *overlapped,
203 DWORD *read_count,
204 BOOL wait_io_complete )
205 {
206 FARPROC function = NULL;
207 HMODULE library_handle = NULL;
208 BOOL result = FALSE;
209
210 if( file_handle == NULL )
211 {
212 return( FALSE );
213 }
214 if( overlapped == NULL )
215 {
216 return( FALSE );
217 }
218 if( read_count == NULL )
219 {
220 return( FALSE );
221 }
222 library_handle = LoadLibrary(
223 _SYSTEM_STRING( "kernel32.dll" ) );
224
225 if( library_handle == NULL )
226 {
227 return( FALSE );
228 }
229 function = GetProcAddress(
230 library_handle,
231 (LPCSTR) "GetOverlappedResult" );
232
233 if( function != NULL )
234 {
235 result = function(
236 file_handle,
237 overlapped,
238 read_count,
239 wait_io_complete );
240 }
241 /* This call should be after using the function
242 * in most cases kernel32.dll will still be available after free
243 */
244 if( FreeLibrary(
245 library_handle ) != TRUE )
246 {
247 result = FALSE;
248 }
249 return( result );
250 }
251
252 /* Cross Windows safe version of ReadFile
253 * Returns TRUE if successful or FALSE on error
254 */
255 BOOL libcfile_ReadFile(
256 HANDLE file_handle,
257 VOID *buffer,
258 DWORD read_size,
259 DWORD *read_count,
260 OVERLAPPED *overlapped )
261 {
262 FARPROC function = NULL;
263 HMODULE library_handle = NULL;
264 BOOL result = FALSE;
265
266 if( file_handle == NULL )
267 {
268 return( FALSE );
269 }
270 if( buffer == NULL )
271 {
272 return( FALSE );
273 }
274 if( read_count == NULL )
275 {
276 return( FALSE );
277 }
278 library_handle = LoadLibrary(
279 _SYSTEM_STRING( "kernel32.dll" ) );
280
281 if( library_handle == NULL )
282 {
283 return( FALSE );
284 }
285 function = GetProcAddress(
286 library_handle,
287 (LPCSTR) "ReadFile" );
288
289 if( function != NULL )
290 {
291 result = function(
292 file_handle,
293 buffer,
294 read_size,
295 read_count,
296 overlapped );
297 }
298 /* This call should be after using the function
299 * in most cases kernel32.dll will still be available after free
300 */
301 if( FreeLibrary(
302 library_handle ) != TRUE )
303 {
304 result = FALSE;
305 }
306 return( result );
307 }
308
309 /* Cross Windows safe version of WriteFile
310 * Returns TRUE if successful or FALSE on error
311 */
312 BOOL libcfile_WriteFile(
313 HANDLE file_handle,
314 VOID *buffer,
315 DWORD write_size,
316 DWORD *write_count,
317 OVERLAPPED *overlapped )
318 {
319 FARPROC function = NULL;
320 HMODULE library_handle = NULL;
321 BOOL result = FALSE;
322
323 if( file_handle == NULL )
324 {
325 return( FALSE );
326 }
327 if( buffer == NULL )
328 {
329 return( FALSE );
330 }
331 if( write_count == NULL )
332 {
333 return( FALSE );
334 }
335 library_handle = LoadLibrary(
336 _SYSTEM_STRING( "kernel32.dll" ) );
337
338 if( library_handle == NULL )
339 {
340 return( FALSE );
341 }
342 function = GetProcAddress(
343 library_handle,
344 (LPCSTR) "WriteFile" );
345
346 if( function != NULL )
347 {
348 result = function(
349 file_handle,
350 buffer,
351 write_size,
352 write_count,
353 overlapped );
354 }
355 /* This call should be after using the function
356 * in most cases kernel32.dll will still be available after free
357 */
358 if( FreeLibrary(
359 library_handle ) != TRUE )
360 {
361 result = FALSE;
362 }
363 return( result );
364 }
365
366 #if !defined( INVALID_SET_FILE_POINTER )
367 #define INVALID_SET_FILE_POINTER ((LONG) -1)
368 #endif
369
370 /* Cross Windows safe version of SetFilePointerEx
371 * Returns TRUE if successful or FALSE on error
372 */
373 BOOL libcfile_SetFilePointerEx(
374 HANDLE file_handle,
375 LARGE_INTEGER distance_to_move_large_integer,
376 LARGE_INTEGER *new_file_pointer_large_integer,
377 DWORD move_method )
378 {
379 FARPROC function = NULL;
380 HMODULE library_handle = NULL;
381 LONG distance_to_move_lower_long = 0;
382 LONG distance_to_move_upper_long = 0;
383 DWORD error_number = 0;
384 BOOL result = FALSE;
385
386 if( file_handle == NULL )
387 {
388 return( FALSE );
389 }
390 if( new_file_pointer_large_integer == NULL )
391 {
392 return( FALSE );
393 }
394 library_handle = LoadLibrary(
395 _SYSTEM_STRING( "kernel32.dll" ) );
396
397 if( library_handle == NULL )
398 {
399 return( FALSE );
400 }
401 function = GetProcAddress(
402 library_handle,
403 (LPCSTR) "SetFilePointerEx" );
404
405 if( function != NULL )
406 {
407 result = function(
408 file_handle,
409 distance_to_move_large_integer,
410 new_file_pointer_large_integer,
411 move_method );
412 }
413 else
414 {
415 function = GetProcAddress(
416 library_handle,
417 (LPCSTR) "SetFilePointer" );
418
419 if( function != NULL )
420 {
421 #if defined( __BORLANDC__ ) && __BORLANDC__ <= 0x520
422 distance_to_move_lower_long = distance_to_move_large_integer.QuadPart & 0xffffffffUL;
423 distance_to_move_upper_long = distance_to_move_large_integer.QuadPart >> 32;
424 #else
425 distance_to_move_lower_long = distance_to_move_large_integer.LowPart;
426 distance_to_move_upper_long = distance_to_move_large_integer.HighPart;
427 #endif
428
429 distance_to_move_lower_long = function(
430 file_handle,
431 distance_to_move_lower_long,
432 &distance_to_move_upper_long,
433 move_method );
434
435 error_number = GetLastError();
436
437 if( ( distance_to_move_lower_long != (LONG) INVALID_SET_FILE_POINTER )
438 || ( error_number == NO_ERROR ) )
439 {
440 #if defined( __BORLANDC__ ) && __BORLANDC__ <= 0x520
441 new_file_pointer_large_integer->QuadPart = distance_to_move_upper_long;
442 new_file_pointer_large_integer->QuadPart <<= 32;
443 new_file_pointer_large_integer->QuadPart += distance_to_move_lower_long;
444 #else
445 new_file_pointer_large_integer->HighPart = distance_to_move_upper_long;
446 new_file_pointer_large_integer->LowPart = distance_to_move_lower_long;
447 #endif
448
449 result = TRUE;
450 }
451 }
452 }
453 /* This call should be after using the function
454 * in most cases kernel32.dll will still be available after free
455 */
456 if( FreeLibrary(
457 library_handle ) != TRUE )
458 {
459 result = FALSE;
460 }
461 return( result );
462 }
463
464 /* Cross Windows safe version of SetEndOfFile
465 * Returns TRUE if successful or FALSE on error
466 */
467 BOOL libcfile_SetEndOfFile(
468 HANDLE file_handle )
469 {
470 FARPROC function = NULL;
471 HMODULE library_handle = NULL;
472 BOOL result = FALSE;
473
474 if( file_handle == NULL )
475 {
476 return( FALSE );
477 }
478 library_handle = LoadLibrary(
479 _SYSTEM_STRING( "kernel32.dll" ) );
480
481 if( library_handle == NULL )
482 {
483 return( FALSE );
484 }
485 function = GetProcAddress(
486 library_handle,
487 (LPCSTR) "SetEndOfFile" );
488
489 if( function != NULL )
490 {
491 result = function(
492 file_handle );
493 }
494 /* This call should be after using the function
495 * in most cases kernel32.dll will still be available after free
496 */
497 if( FreeLibrary(
498 library_handle ) != TRUE )
499 {
500 result = FALSE;
501 }
502 return( result );
503 }
504
505 /* Cross Windows safe version of GetFileSizeEx
506 * Returns TRUE if successful or FALSE on error
507 */
508 BOOL libcfile_GetFileSizeEx(
509 HANDLE file_handle,
510 LARGE_INTEGER *file_size_large_integer )
511 {
512 FARPROC function = NULL;
513 HMODULE library_handle = NULL;
514 DWORD error_number = 0;
515 DWORD file_size_upper_dword = 0;
516 DWORD file_size_lower_dword = 0;
517 BOOL result = FALSE;
518
519 if( file_handle == NULL )
520 {
521 return( FALSE );
522 }
523 if( file_size_large_integer == NULL )
524 {
525 return( FALSE );
526 }
527 library_handle = LoadLibrary(
528 _SYSTEM_STRING( "kernel32.dll" ) );
529
530 if( library_handle == NULL )
531 {
532 return( FALSE );
533 }
534 function = GetProcAddress(
535 library_handle,
536 (LPCSTR) "GetFileSizeEx" );
537
538 if( function != NULL )
539 {
540 result = function(
541 file_handle,
542 file_size_large_integer );
543 }
544 else
545 {
546 function = GetProcAddress(
547 library_handle,
548 (LPCSTR) "GetFileSize" );
549
550 if( function != NULL )
551 {
552 file_size_lower_dword = function(
553 file_handle,
554 &file_size_upper_dword );
555
556 error_number = GetLastError();
557
558 if( ( file_size_lower_dword != INVALID_FILE_SIZE )
559 || ( error_number == NO_ERROR ) )
560 {
561 #if defined( __BORLANDC__ ) && __BORLANDC__ <= 0x520
562 file_size_large_integer->QuadPart = file_size_upper_dword;
563 file_size_large_integer->QuadPart <<= 32;
564 file_size_large_integer->QuadPart += file_size_lower_dword;
565 #else
566 file_size_large_integer->HighPart = file_size_upper_dword;
567 file_size_large_integer->LowPart = file_size_lower_dword;
568 #endif
569
570 result = TRUE;
571 }
572 }
573 }
574 /* This call should be after using the function
575 * in most cases kernel32.dll will still be available after free
576 */
577 if( FreeLibrary(
578 library_handle ) != TRUE )
579 {
580 result = FALSE;
581 }
582 return( result );
583 }
584
585 /* Cross Windows safe version of GetFileType
586 * Returns the file type if successful or FILE_TYPE_UNKNOWN on error
587 */
588 DWORD libcfile_GetFileType(
589 HANDLE file_handle )
590 {
591 FARPROC function = NULL;
592 HMODULE library_handle = NULL;
593 DWORD result = FILE_TYPE_UNKNOWN;
594
595 if( file_handle == NULL )
596 {
597 return( FILE_TYPE_UNKNOWN );
598 }
599 library_handle = LoadLibrary(
600 _SYSTEM_STRING( "kernel32.dll" ) );
601
602 if( library_handle == NULL )
603 {
604 return( FILE_TYPE_UNKNOWN );
605 }
606 function = GetProcAddress(
607 library_handle,
608 (LPCSTR) "GetFileType" );
609
610 if( function != NULL )
611 {
612 result = function(
613 file_handle );
614 }
615 /* This call should be after using the function
616 * in most cases kernel32.dll will still be available after free
617 */
618 if( FreeLibrary(
619 library_handle ) != TRUE )
620 {
621 result = FILE_TYPE_UNKNOWN;
622 }
623 return( result );
624 }
625
626 #endif /* defined( WINAPI ) && ( WINVER <= 0x0500 ) */
627
628 #if defined( WINAPI ) && ( WINVER <= 0x0500 )
629
630 /* Cross Windows safe version of GetFileAttributesA
631 * Returns the file attributs if successful or INVALID_FILE_ATTRIBUTES on error
632 */
633 DWORD libcfile_GetFileAttributesA(
634 LPCSTR filename )
635 {
636 FARPROC function = NULL;
637 HMODULE library_handle = NULL;
638 DWORD result = 0;
639
640 if( filename == NULL )
641 {
642 return( INVALID_FILE_ATTRIBUTES );
643 }
644 library_handle = LoadLibrary(
645 _SYSTEM_STRING( "kernel32.dll" ) );
646
647 if( library_handle == NULL )
648 {
649 return( INVALID_FILE_ATTRIBUTES );
650 }
651 function = GetProcAddress(
652 library_handle,
653 (LPCSTR) "GetFileAttributesA" );
654
655 if( function != NULL )
656 {
657 result = function(
658 filename );
659 }
660 /* This call should be after using the function
661 * in most cases kernel32.dll will still be available after free
662 */
663 if( FreeLibrary(
664 library_handle ) != TRUE )
665 {
666 result = INVALID_FILE_ATTRIBUTES;
667 }
668 return( result );
669 }
670
671 #endif /* defined( WINAPI ) && ( WINVER <= 0x0500 ) */
672
673 #if defined( HAVE_WIDE_CHARACTER_TYPE )
674
675 #if defined( WINAPI ) && ( WINVER <= 0x0500 )
676
677 /* Cross Windows safe version of GetFileAttributesW
678 * Returns the file attributs if successful or INVALID_FILE_ATTRIBUTES on error
679 */
680 DWORD libcfile_GetFileAttributesW(
681 LPCWSTR filename )
682 {
683 FARPROC function = NULL;
684 HMODULE library_handle = NULL;
685 DWORD result = 0;
686
687 if( filename == NULL )
688 {
689 return( INVALID_FILE_ATTRIBUTES );
690 }
691 library_handle = LoadLibrary(
692 _SYSTEM_STRING( "kernel32.dll" ) );
693
694 if( library_handle == NULL )
695 {
696 return( INVALID_FILE_ATTRIBUTES );
697 }
698 function = GetProcAddress(
699 library_handle,
700 (LPCSTR) "GetFileAttributesW" );
701
702 if( function != NULL )
703 {
704 result = function(
705 filename );
706 }
707 /* This call should be after using the function
708 * in most cases kernel32.dll will still be available after free
709 */
710 if( FreeLibrary(
711 library_handle ) != TRUE )
712 {
713 result = INVALID_FILE_ATTRIBUTES;
714 }
715 return( result );
716 }
717
718 #endif /* defined( WINAPI ) && ( WINVER <= 0x0500 ) */
719
720 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
721
722 #if defined( WINAPI ) && ( WINVER <= 0x0500 )
723
724 /* Cross Windows safe version of DeleteFileA
725 * Returns TRUE if successful or FALSE on error
726 */
727 BOOL libcfile_DeleteFileA(
728 LPCSTR filename )
729 {
730 FARPROC function = NULL;
731 HMODULE library_handle = NULL;
732 BOOL result = FALSE;
733
734 if( filename == NULL )
735 {
736 return( FALSE );
737 }
738 library_handle = LoadLibrary(
739 _SYSTEM_STRING( "kernel32.dll" ) );
740
741 if( library_handle == NULL )
742 {
743 return( FALSE );
744 }
745 function = GetProcAddress(
746 library_handle,
747 (LPCSTR) "DeleteFileA" );
748
749 if( function != NULL )
750 {
751 result = function(
752 filename );
753 }
754 /* This call should be after using the function
755 * in most cases kernel32.dll will still be available after free
756 */
757 if( FreeLibrary(
758 library_handle ) != TRUE )
759 {
760 result = FALSE;
761 }
762 return( result );
763 }
764
765 #endif /* defined( WINAPI ) && ( WINVER <= 0x0500 ) */
766
767 #if defined( HAVE_WIDE_CHARACTER_TYPE )
768
769 #if defined( WINAPI ) && ( WINVER <= 0x0500 )
770
771 /* Cross Windows safe version of DeleteFileW
772 * Returns TRUE if successful or FALSE on error
773 */
774 BOOL libcfile_DeleteFileW(
775 LPCWSTR filename )
776 {
777 FARPROC function = NULL;
778 HMODULE library_handle = NULL;
779 BOOL result = FALSE;
780
781 if( filename == NULL )
782 {
783 return( FALSE );
784 }
785 library_handle = LoadLibrary(
786 _SYSTEM_STRING( "kernel32.dll" ) );
787
788 if( library_handle == NULL )
789 {
790 return( FALSE );
791 }
792 function = GetProcAddress(
793 library_handle,
794 (LPCSTR) "DeleteFileW" );
795
796 if( function != NULL )
797 {
798 result = function(
799 filename );
800 }
801 /* This call should be after using the function
802 * in most cases kernel32.dll will still be available after free
803 */
804 if( FreeLibrary(
805 library_handle ) != TRUE )
806 {
807 result = FALSE;
808 }
809 return( result );
810 }
811
812 #endif /* defined( WINAPI ) && ( WINVER <= 0x0500 ) */
813
814 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
815