102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
file_descriptors.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include <intrin.h>
11#include <math.h>
12#include <stdarg.h>
13#include <string.h>
14#define nullptr 0
15
16// Original pcdogs.exe: 0x447f17
17// Flushes a valid CRT descriptor's Windows handle to disk and maps failures into CRT error state. It returns zero on success and -1 on failure.
18int32_t CRT_commit(int32_t file_no)
19{
20 if ((uint32_t)file_no < (uint32_t)g_ioBufferHighWaterMark && g_fileDescriptorTable[file_no >> CRT_FD_CHUNK_SHIFT][file_no & CRT_FD_CHUNK_INDEX_MASK].flags & CRT_FD_OPEN)
21 {
22 enum WIN32_ERROR result;
23
24 if (FlushFileBuffers(CRT_get_osfhandle(file_no)))
25 result = NO_ERROR;
26 else
27 result = GetLastError();
28
29 if (!result)
30 return result;
31
32 g_crtLastOSError = result;
33 }
34
36
37 return ~NO_ERROR;
38}
39
40// Original pcdogs.exe: 0x447f6e
41// Writes bytes through a CRT descriptor, inserting carriage returns before newlines in text mode. A short successful text write subtracts the carriage-return count for the entire prepared chunk, preserving the original accounting behavior.
42int32_t CRT_write(int32_t file_no, void const *buffer, uint32_t byte_count)
43{
44 if ((uint32_t)file_no >= (uint32_t)g_ioBufferHighWaterMark || !(g_fileDescriptorTable[file_no >> CRT_FD_CHUNK_SHIFT][file_no & CRT_FD_CHUNK_INDEX_MASK].flags & CRT_FD_OPEN))
45 {
48
49 return -1;
50 }
51
53
54 if (!byte_count)
55 return 0;
56
57 if (fd->flags & CRT_FD_APPEND)
58 CRT_lseek(file_no, 0, CRT_SEEK_END);
59
60 DWORD error = 0, chunk_written = 0, total_written = 0;
61 uint32_t inserted_carriage_returns = 0;
62 const unsigned char *input_cursor = buffer, *input_end = input_cursor + byte_count;
63
64 if (!(fd->flags & CRT_FD_TEXT))
65 {
66 if (!WriteFile(fd->os_handle, buffer, byte_count, &chunk_written, NULL))
67 error = GetLastError();
68 else
69 total_written = chunk_written;
70 }
71 else
72 {
73 while (input_cursor < input_end)
74 {
75 unsigned char text_buffer[0x404], *output_cursor = text_buffer;
76
77 while (input_cursor < input_end)
78 {
79 unsigned char input_character = *input_cursor++;
80
81 if (input_character == '\n')
82 {
83 *output_cursor++ = '\r';
84 ++inserted_carriage_returns;
85 }
86
87 *output_cursor++ = input_character;
88
89 if (output_cursor - text_buffer >= 0x400)
90 break;
91 }
92
93 DWORD chunk_bytes = (DWORD)(output_cursor - text_buffer);
94
95 if (!WriteFile(fd->os_handle, text_buffer, chunk_bytes, &chunk_written, NULL))
96 {
97 error = GetLastError();
98
99 break;
100 }
101
102 total_written += chunk_written;
103
104 if (chunk_written < chunk_bytes)
105 break;
106 }
107 }
108
109 if (total_written)
110 return total_written - inserted_carriage_returns;
111
112 if (error)
113 {
114 if (error == ERROR_ACCESS_DENIED)
115 {
118 }
119 else
120 _dosmaperr(error);
121
122 return -1;
123 }
124
125 if ((fd->flags & CRT_FD_DEVICE) && *(const unsigned char *)buffer == CRT_TEXT_END_OF_FILE)
126 return 0;
127
130
131 return -1;
132}
133
134// Original pcdogs.exe: 0x4481e5
135// Closes a CRT descriptor and releases its slot, preserving the shared stdout/stderr handle exception. Slot cleanup still occurs when closing the operating-system handle fails.
136int32_t CRT_close(int32_t file_no)
137{
138 if ((uint32_t)file_no >= (uint32_t)g_ioBufferHighWaterMark || !(g_fileDescriptorTable[file_no >> CRT_FD_CHUNK_SHIFT][file_no & CRT_FD_CHUNK_INDEX_MASK].flags & CRT_FD_OPEN))
139 {
142
143 return 0xffffffff;
144 }
145
146 int32_t os_error;
147
148 if (CRT_get_osfhandle(file_no) == INVALID_HANDLE_VALUE)
149 os_error = 0;
150 else if (file_no == 1 || file_no == 2)
151 {
152 HANDLE stderr_handle = CRT_get_osfhandle(2);
153
154 if (CRT_get_osfhandle(1) != stderr_handle)
155 {
156 if (CloseHandle(CRT_get_osfhandle(file_no)))
157 os_error = 0;
158 else
159 os_error = GetLastError();
160 }
161 else
162 os_error = 0;
163 }
164 else
165 {
166 if (CloseHandle(CRT_get_osfhandle(file_no)))
167 os_error = 0;
168 else
169 os_error = GetLastError();
170 }
171
172 _free_osfhnd(file_no);
174
175 if (!os_error)
176 return 0;
177
178 _dosmaperr(os_error);
179
180 return 0xffffffff;
181}
182
183// Original pcdogs.exe: 0x44913c
184// Initializes descriptor tables, inherited handles, and the standard descriptors, then requests the corresponding Windows handle capacity. Allocation failure follows the CRT fatal-error path.
186{
187 struct CRT_FileDescriptor *fd_chunk = CRT_malloc(0x100);
188
189 if (!fd_chunk)
190 {
192 /* no return */
193 }
194
195 g_fileDescriptorTable[0] = fd_chunk;
197
198 for (struct CRT_FileDescriptor *first_chunk_end = &fd_chunk[CRT_FD_CHUNK_CAPACITY]; fd_chunk < first_chunk_end;
199 first_chunk_end = &g_fileDescriptorTable[0][CRT_FD_CHUNK_CAPACITY])
200 {
201 fd_chunk->flags = 0;
202 fd_chunk->os_handle = INVALID_HANDLE_VALUE;
203 fd_chunk->lookahead = 0xa;
204 fd_chunk = &fd_chunk[1];
205 }
206
207 STARTUPINFOA startup_info;
208 GetStartupInfoA(&startup_info);
209
210 if (startup_info.cbReserved2)
211 {
212 LPBYTE lp_reserved2 = startup_info.lpReserved2;
213
214 if (lp_reserved2)
215 {
216 int32_t startup_fd_count;
217 memcpy(&startup_fd_count, lp_reserved2, sizeof(startup_fd_count));
218 uint8_t *startup_flags = &lp_reserved2[4];
219 const uint8_t *startup_handles = startup_flags + startup_fd_count;
220
221 if (startup_fd_count >= CRT_FD_LIMIT)
222 startup_fd_count = CRT_FD_LIMIT;
223
224 if (g_ioBufferHighWaterMark < startup_fd_count)
225 {
226 struct CRT_FileDescriptor **chunk_slot = &g_fileDescriptorTable[1];
227
228 do
229 {
230 struct CRT_FileDescriptor *new_chunk_cursor = CRT_malloc(0x100);
231
232 if (!new_chunk_cursor)
233 {
234 startup_fd_count = g_ioBufferHighWaterMark;
235
236 break;
237 }
238
240 *chunk_slot = new_chunk_cursor;
241
242 for (struct CRT_FileDescriptor *new_chunk_end = &new_chunk_cursor[CRT_FD_CHUNK_CAPACITY]; new_chunk_cursor < new_chunk_end;
243 new_chunk_end = &(*chunk_slot)[CRT_FD_CHUNK_CAPACITY])
244 {
245 new_chunk_cursor->flags = 0;
246 new_chunk_cursor->os_handle = INVALID_HANDLE_VALUE;
247 new_chunk_cursor->lookahead = 0xa;
248 new_chunk_cursor = &new_chunk_cursor[1];
249 }
250
251 chunk_slot = &chunk_slot[1];
252 } while (g_ioBufferHighWaterMark < startup_fd_count);
253 }
254
255 int32_t file_no = 0;
256
257 if (startup_fd_count > 0)
258 {
259 do
260 {
261 HANDLE h_file;
262 memcpy(&h_file, startup_handles, sizeof(h_file));
263
264 if (h_file != INVALID_HANDLE_VALUE)
265 {
266 uint8_t inherited_flags = *startup_flags;
267
268 if (inherited_flags & CRT_FD_OPEN)
269 {
270 if ((inherited_flags & CRT_FD_PIPE) || GetFileType(h_file))
271 {
272 struct CRT_FileDescriptor *descriptor =
274 descriptor->os_handle = h_file;
275 descriptor->flags = inherited_flags;
276 }
277 }
278 }
279
280 file_no += 1;
281 startup_flags = &startup_flags[1];
282 startup_handles += sizeof(h_file);
283 } while (file_no < startup_fd_count);
284 }
285 }
286 }
287
288 for (int32_t standard_fd = 0; standard_fd < 3; standard_fd += 1)
289 {
290 struct CRT_FileDescriptor *standard_descriptor_table = g_fileDescriptorTable[0];
291 struct CRT_FileDescriptor *descriptor = &standard_descriptor_table[standard_fd];
292
293 if (standard_descriptor_table[standard_fd].os_handle != INVALID_HANDLE_VALUE)
294 descriptor->flags |= CRT_FD_TEXT;
295 else
296 {
297 descriptor->flags = CRT_FD_OPEN | CRT_FD_TEXT;
298
299 enum STD_HANDLE n_std_handle;
300
301 n_std_handle = standard_fd ? (DWORD)((standard_fd != 1) ? -12 : -11) : STD_INPUT_HANDLE;
302
303 HANDLE standard_handle = GetStdHandle(n_std_handle);
304
305 if (standard_handle == INVALID_HANDLE_VALUE)
306 descriptor->flags |= CRT_FD_DEVICE;
307 else
308 {
309 enum FILE_TYPE standard_file_type = GetFileType(standard_handle);
310
311 if (!standard_file_type)
312 descriptor->flags |= CRT_FD_DEVICE;
313 else
314 {
315 int32_t standard_type_byte = standard_file_type & 0xff;
316
317 descriptor->os_handle = standard_handle;
318
319 if (standard_type_byte == FILE_TYPE_CHAR)
320 descriptor->flags |= CRT_FD_DEVICE;
321 else if (standard_type_byte == FILE_TYPE_PIPE)
322 descriptor->flags |= CRT_FD_PIPE;
323 }
324 }
325 }
326 }
327
328 SetHandleCount(g_ioBufferHighWaterMark);
329}
330
331// Original pcdogs.exe: 0x4492e7
332// Seeks a valid CRT descriptor through its Windows handle, clearing its end-of-file state on success. It maps operating-system failures into CRT error state and returns -1.
333int32_t CRT_lseek(int32_t fd, int32_t offset, int32_t origin)
334{
336 {
339
340 return 0xffffffff;
341 }
342
343 HANDLE h_file = CRT_get_osfhandle(fd);
344
345 if (h_file == INVALID_HANDLE_VALUE)
346 {
348
349 return 0xffffffff;
350 }
351
352 uint32_t result = SetFilePointer(h_file, offset, nullptr, origin);
353 enum WIN32_ERROR os_error_code;
354
355 if (result != 0xffffffff)
356 os_error_code = NO_ERROR;
357 else
358 os_error_code = GetLastError();
359
360 if (os_error_code)
361 {
362 _dosmaperr(os_error_code);
363
364 return 0xffffffff;
365 }
366
368
369 return result;
370}
371
372// Original pcdogs.exe: 0x44945a
373// Reads bytes through a CRT descriptor and translates text-mode carriage-return/newline pairs. Control-Z handling and the saved lookahead byte preserve text-file and pipe boundary behavior.
374int32_t CRT_read(int32_t fd, void *buffer, uint32_t count)
375{
376 int32_t descriptor_chunk_index;
377 struct CRT_FileDescriptor *fd_entry;
378 uint8_t fd_flags;
379
380 if ((uint32_t)fd < (uint32_t)g_ioBufferHighWaterMark)
381 {
382 descriptor_chunk_index = fd >> CRT_FD_CHUNK_SHIFT;
383 fd_entry = &g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK];
384 fd_flags = fd_entry->flags;
385 }
386
387 if ((uint32_t)fd >= (uint32_t)g_ioBufferHighWaterMark || !(fd_flags & CRT_FD_OPEN))
388 {
391
392 return 0xffffffff;
393 }
394
395 int32_t lookahead_bytes = 0;
396 uint8_t *output_cursor = buffer;
397 uint8_t *lp_buffer = output_cursor;
398
399 if (!count || fd_flags & CRT_FD_EOF)
400 return 0;
401
402 if (fd_flags & (CRT_FD_PIPE | CRT_FD_DEVICE))
403 {
404 uint8_t lookahead = fd_entry->lookahead;
405
406 if (lookahead != 0xa)
407 {
408 count -= 1;
409 *(uint8_t *)output_cursor = lookahead;
410 lp_buffer = &output_cursor[1];
411 lookahead_bytes = 1;
412 g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK].lookahead = 0xa;
413 }
414 }
415
416 DWORD bytes_read;
417
418 if (ReadFile(g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK].os_handle, lp_buffer, count, &bytes_read,
419 NULL))
420 {
421 uint32_t initial_read_count = bytes_read;
422 uint32_t result = lookahead_bytes + initial_read_count;
423 char updated_flags = fd_entry->flags;
424
425 if (!(updated_flags & CRT_FD_TEXT))
426 return result;
427
428 if (!initial_read_count || *(uint8_t *)output_cursor != 0xa)
429 updated_flags &= (UINT8_MAX ^ CRT_FD_CRLF);
430 else
431 updated_flags |= CRT_FD_CRLF;
432
433 fd_entry->flags = updated_flags;
434
435 uint8_t *input_cursor = buffer;
436 uint8_t *input_end = (uint8_t *)buffer + result;
437
438 if ((uint8_t *)buffer >= input_end)
439 return output_cursor - (uint8_t *)buffer;
440
441 do
442 {
443 uint8_t input_byte = *(uint8_t *)input_cursor;
444
445 if (input_byte == CRT_TEXT_END_OF_FILE)
446 {
447 uint8_t *flags_address = &g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK].flags;
448 uint8_t eof_flags = *flags_address;
449
450 if (!(eof_flags & CRT_FD_DEVICE))
451 *flags_address = eof_flags | CRT_FD_EOF;
452
453 break;
454 }
455
456 if (input_byte != 0xd)
457 {
458 *(uint8_t *)output_cursor = input_byte;
459 output_cursor = &output_cursor[1];
460 input_cursor += 1;
461 }
462 else if (input_cursor >= input_end - 1)
463 {
464 input_cursor += 1;
465
466 uint8_t cr_lookahead_byte;
467
468 if ((ReadFile(g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK].os_handle, &cr_lookahead_byte, 1,
469 &bytes_read, NULL) ||
470 !GetLastError()) &&
471 bytes_read)
472 {
473 if (g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK].flags & (CRT_FD_PIPE | CRT_FD_DEVICE))
474 {
475 uint8_t saved_lookahead_byte = cr_lookahead_byte;
476
477 if (saved_lookahead_byte == 0xa)
478 {
479 *(uint8_t *)output_cursor = 0xa;
480 output_cursor = &output_cursor[1];
481 }
482 else
483 {
484 *(uint8_t *)output_cursor = 0xd;
485 output_cursor = &output_cursor[1];
486 g_fileDescriptorTable[descriptor_chunk_index][fd & CRT_FD_CHUNK_INDEX_MASK].lookahead =
487 saved_lookahead_byte;
488 }
489 }
490 else if (output_cursor != buffer || cr_lookahead_byte != 0xa)
491 {
492 CRT_lseek(fd, 0xffffffff, CRT_SEEK_CURRENT);
493
494 if (cr_lookahead_byte != 0xa)
495 {
496 *(uint8_t *)output_cursor = 0xd;
497 output_cursor = &output_cursor[1];
498 }
499 }
500 else
501 {
502 *(uint8_t *)output_cursor = 0xa;
503 output_cursor = &output_cursor[1];
504 }
505 }
506 else
507 {
508 *(uint8_t *)output_cursor = 0xd;
509 output_cursor = &output_cursor[1];
510 }
511 }
512 else if (*(uint8_t *)(input_cursor + 1) != 0xa)
513 {
514 *(uint8_t *)output_cursor = 0xd;
515 output_cursor = &output_cursor[1];
516 input_cursor += 1;
517 }
518 else
519 {
520 input_cursor += 2;
521 *(uint8_t *)output_cursor = 0xa;
522 output_cursor = &output_cursor[1];
523 }
524 } while (input_cursor < input_end);
525
526 return output_cursor - (uint8_t *)buffer;
527 }
528
529 enum WIN32_ERROR os_error_code = GetLastError();
530
531 if (os_error_code != ERROR_ACCESS_DENIED)
532 {
533 if (os_error_code == ERROR_BROKEN_PIPE)
534 return 0;
535
536 _dosmaperr(os_error_code);
537 }
538 else
539 {
542 }
543
544 return 0xffffffff;
545}
546
547// Original pcdogs.exe: 0x44b141
548// Returns the character-device flag for a valid descriptor and zero for an out-of-range descriptor.
549int32_t CRT_isatty(int32_t fd)
550{
551 if ((uint32_t)fd >= (uint32_t)g_ioBufferHighWaterMark)
552 return 0;
553
555}
556
557// Original pcdogs.exe: 0x44b2b5
558// Finds an available descriptor slot or allocates and initializes a new descriptor block, then resets the selected handle. The returned slot is not marked open by this operation.
559int32_t _alloc_osfhnd(void)
560{
562
563 for (int block = 0; block < 64; ++block)
564 {
565 if (!tables[block])
566 {
567 struct CRT_FileDescriptor *allocated_descriptors = CRT_malloc(256);
568
569 if (!allocated_descriptors)
570 return -1;
571
572 tables[block] = allocated_descriptors;
574
575 for (int j = 0; j < 32; ++j)
576 {
577 allocated_descriptors[j].flags = 0;
578 allocated_descriptors[j].os_handle = INVALID_HANDLE_VALUE;
579 allocated_descriptors[j].lookahead = 10;
580 }
581
582 return block * 32;
583 }
584
585 for (int j = 0; j < 32; ++j)
586 if (!(tables[block][j].flags & CRT_FD_OPEN))
587 {
588 tables[block][j].os_handle = INVALID_HANDLE_VALUE;
589
590 return block * 32 + j;
591 }
592 }
593
594 return -1;
595}
596
597// Original pcdogs.exe: 0x44b34a
598// Associates an unused valid CRT descriptor slot with a Windows handle and updates standard-handle state when appropriate. Invalid or already-associated slots fail with a bad-descriptor error.
599int32_t _set_osfhnd(int32_t fd, HANDLE os_handle)
600{
601 if ((uint32_t)fd >= (uint32_t)g_ioBufferHighWaterMark || g_fileDescriptorTable[fd >> CRT_FD_CHUNK_SHIFT][fd & CRT_FD_CHUNK_INDEX_MASK].os_handle != INVALID_HANDLE_VALUE)
602 {
605
606 return 0xffffffff;
607 }
608
610 {
611 if (!fd)
612 SetStdHandle(STD_INPUT_HANDLE, os_handle);
613 else if (fd == 1)
614 SetStdHandle(STD_OUTPUT_HANDLE, os_handle);
615 else if (fd == 2)
616 SetStdHandle(STD_ERROR_HANDLE, os_handle);
617 }
618
620
621 return 0;
622}
623
624// Original pcdogs.exe: 0x44b3c1
625// Clears the Windows handle associated with an open CRT descriptor and updates standard-handle state when appropriate. Invalid descriptors fail with a bad-descriptor error.
626int32_t _free_osfhnd(int32_t fd)
627{
628 if ((uint32_t)fd >= (uint32_t)g_ioBufferHighWaterMark)
629 {
632
633 return 0xffffffff;
634 }
635
637
638 if (!(descriptor->flags & CRT_FD_OPEN) || descriptor->os_handle == INVALID_HANDLE_VALUE)
639 {
642
643 return 0xffffffff;
644 }
645
647 {
648 if (!fd)
649 {
650 SetStdHandle(STD_INPUT_HANDLE, NULL);
651 }
652 else if (fd == 1)
653 {
654 SetStdHandle(STD_OUTPUT_HANDLE, NULL);
655 }
656 else if (fd == 2)
657 {
658 SetStdHandle(STD_ERROR_HANDLE, NULL);
659 }
660 }
661
662 g_fileDescriptorTable[fd >> CRT_FD_CHUNK_SHIFT][fd & CRT_FD_CHUNK_INDEX_MASK].os_handle = INVALID_HANDLE_VALUE;
663
664 return 0;
665}
666
667// Original pcdogs.exe: 0x44b43b
668// Returns the Windows handle for an open CRT descriptor or reports a bad-descriptor error and returns -1.
669HANDLE CRT_get_osfhandle(int32_t fd)
670{
671 if ((uint32_t)fd < (uint32_t)g_ioBufferHighWaterMark)
672 {
673 const struct CRT_FileDescriptor *descriptor =
675 if (descriptor->flags & CRT_FD_OPEN)
676 return descriptor->os_handle;
677 }
678
681
682 return INVALID_HANDLE_VALUE;
683}
684
685// Original pcdogs.exe: 0x44b478
686// Stores a Windows error code and maps it to the CRT errno table. Unlisted errors use the original range-based access, memory, or invalid-argument fallback.
687void _dosmaperr(uint32_t os_error_code)
688{
689 int32_t error_pair_index = 0;
690
691 g_crtLastOSError = os_error_code;
692
693 struct CRT_OSErrorMapping *error_pair = g_crtOSErrorMappings;
694
695 while (os_error_code != error_pair->os_error)
696 {
697 error_pair = &error_pair[1];
698 error_pair_index += 1;
699
700 if (error_pair >= &g_crtOSErrorMappings[45])
701 {
702 if (os_error_code >= ERROR_WRITE_PROTECT && os_error_code <= ERROR_SHARING_BUFFER_EXCEEDED)
703 {
705
706 return;
707 }
708
709 if (os_error_code < ERROR_INVALID_STARTING_CODESEG)
710 {
712
713 return;
714 }
715
717
718 if (os_error_code > ERROR_INFLOOP_IN_RELOC_CHAIN)
720
721 return;
722 }
723 }
724
725 g_crtLastError = g_crtOSErrorMappings[error_pair_index].crt_error;
726}
727
728// Original pcdogs.exe: 0x44b4df
729// Opens a file with the requested access, sharing, and creation flags and initializes its CRT descriptor. The text-mode trailing-Control-Z truncation path requires the byte read to return zero, preserving the original condition, and setup failures close the newly opened handle.
730int32_t CRT_sopen(char const *filename, int32_t oflag, int32_t shflag, ...)
731{
732 SECURITY_ATTRIBUTES security_attributes;
733 security_attributes.nLength = 0xc;
734 security_attributes.lpSecurityDescriptor = 0;
735
736 uint8_t fd_flags;
737
738 if (!((uint8_t)oflag & CRT_OPEN_NO_INHERIT))
739 {
740 fd_flags = 0;
741 security_attributes.bInheritHandle = 1;
742 }
743 else
744 {
745 security_attributes.bInheritHandle = 0;
746 fd_flags = CRT_FD_NO_INHERIT;
747 }
748
749 if (!(CRT_OPEN_BINARY & oflag) && (oflag & CRT_OPEN_TEXT || g_crtUmask != CRT_OPEN_BINARY))
750 fd_flags |= CRT_FD_TEXT;
751
752 int32_t access_mode = oflag & (CRT_OPEN_WRITE_ONLY | CRT_OPEN_READ_WRITE);
753 uint32_t desired_access;
754
755 if (!access_mode)
756 desired_access = GENERIC_READ;
757 else if (access_mode == CRT_OPEN_WRITE_ONLY)
758 desired_access = GENERIC_WRITE;
759 else if (access_mode == CRT_OPEN_READ_WRITE)
760 desired_access = GENERIC_READ | GENERIC_WRITE;
761 else
762 {
765
766 return 0xffffffff;
767 }
768
769 uint32_t share_mode;
770
771 if (shflag == CRT_SHARE_DENY_READ_WRITE)
772 share_mode = FILE_SHARE_NONE;
773 else if (shflag == CRT_SHARE_DENY_WRITE)
774 share_mode = FILE_SHARE_READ;
775 else if (shflag == CRT_SHARE_DENY_READ)
776 share_mode = FILE_SHARE_WRITE;
777 else if (shflag == CRT_SHARE_DENY_NONE)
778 share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
779 else
780 {
783
784 return 0xffffffff;
785 }
786
787 int32_t creation_flags = oflag & (CRT_OPEN_CREATE | CRT_OPEN_TRUNCATE | CRT_OPEN_EXCLUSIVE);
788 uint32_t creation_disposition;
789
790 if (creation_flags <= CRT_OPEN_EXCLUSIVE)
791 {
792 if (creation_flags == CRT_OPEN_EXCLUSIVE || !creation_flags)
793 creation_disposition = OPEN_EXISTING;
794 else if (creation_flags == CRT_OPEN_CREATE)
795 creation_disposition = OPEN_ALWAYS;
796 else if (creation_flags == CRT_OPEN_TRUNCATE)
797 creation_disposition = TRUNCATE_EXISTING;
798 else if (creation_flags == (CRT_OPEN_CREATE | CRT_OPEN_TRUNCATE))
799 creation_disposition = CREATE_ALWAYS;
800 else
801 {
804
805 return 0xffffffff;
806 }
807 }
808 else if (creation_flags == (CRT_OPEN_CREATE | CRT_OPEN_EXCLUSIVE))
809 creation_disposition = CREATE_NEW;
810 else if (creation_flags == (CRT_OPEN_TRUNCATE | CRT_OPEN_EXCLUSIVE))
811 creation_disposition = TRUNCATE_EXISTING;
812 else if (creation_flags == (CRT_OPEN_CREATE | CRT_OPEN_TRUNCATE | CRT_OPEN_EXCLUSIVE))
813 creation_disposition = CREATE_NEW;
814 else
815 {
818
819 return 0xffffffff;
820 }
821
822 uint32_t attrs_and_flags = FILE_ATTRIBUTE_NORMAL;
823
824 va_list args;
825 va_start(args, shflag);
826
827 int32_t pmode = (oflag & CRT_OPEN_CREATE) ? va_arg(args, int) : 0;
828
829 va_end(args);
830
831 if (CRT_OPEN_CREATE & oflag && !((char)~g_crtOpenFileFlags & (uint8_t)pmode & CRT_PERMISSION_WRITE))
832 attrs_and_flags = FILE_ATTRIBUTE_READONLY;
833
834 if ((uint8_t)oflag & CRT_OPEN_TEMPORARY)
835 {
836 attrs_and_flags |= FILE_FLAG_DELETE_ON_CLOSE;
837 desired_access |= DELETE;
838 }
839
840 if (oflag & CRT_OPEN_SHORT_LIVED)
841 attrs_and_flags |= FILE_ATTRIBUTE_TEMPORARY;
842
843 if ((uint8_t)oflag & CRT_OPEN_SEQUENTIAL)
844 attrs_and_flags |= FILE_FLAG_SEQUENTIAL_SCAN;
845 else if ((uint8_t)oflag & CRT_OPEN_RANDOM)
846 attrs_and_flags |= FILE_FLAG_RANDOM_ACCESS;
847
848 int32_t file_no = _alloc_osfhnd();
849
850 if (file_no == -1)
851 {
854
855 return 0xffffffff;
856 }
857
858 HANDLE os_handle = CreateFileA(filename, desired_access, share_mode, &security_attributes,
859 creation_disposition, attrs_and_flags, nullptr);
860
861 if (os_handle == INVALID_HANDLE_VALUE)
862 {
863 _dosmaperr(GetLastError());
864
865 return 0xffffffff;
866 }
867
868 enum FILE_TYPE file_type = GetFileType(os_handle);
869
870 if (!file_type)
871 {
872 CloseHandle(os_handle);
873 _dosmaperr(GetLastError());
874
875 return 0xffffffff;
876 }
877
878 if (file_type == FILE_TYPE_CHAR)
879 fd_flags |= CRT_FD_DEVICE;
880 else if (file_type == FILE_TYPE_PIPE)
881 fd_flags |= CRT_FD_PIPE;
882
883 _set_osfhnd(file_no, os_handle);
884
885 uint8_t open_descriptor_flags = fd_flags | CRT_FD_OPEN;
886 char device_or_pipe_flags = open_descriptor_flags & (CRT_FD_PIPE | CRT_FD_DEVICE);
887
888 g_fileDescriptorTable[file_no >> CRT_FD_CHUNK_SHIFT][file_no & CRT_FD_CHUNK_INDEX_MASK].flags = open_descriptor_flags;
889
890 if (!device_or_pipe_flags && (open_descriptor_flags & CRT_FD_TEXT) && ((uint8_t)oflag & CRT_OPEN_READ_WRITE))
891 {
892 int32_t size = CRT_lseek(file_no, 0xffffffff, CRT_SEEK_END);
893
894 if (size != -1)
895 {
896 uint8_t trailing_character = 0;
897
898 if (!CRT_read(file_no, &trailing_character, 1) && trailing_character == CRT_TEXT_END_OF_FILE)
899 {
900 if (CRT_chsize(file_no, size) == -1)
901 {
902 CRT_close(file_no);
903
904 return 0xffffffff;
905 }
906 }
907
908 if (CRT_lseek(file_no, 0, CRT_SEEK_BEGIN) == -1)
909 {
910 CRT_close(file_no);
911
912 return 0xffffffff;
913 }
914 }
915 else if (g_crtLastOSError != 0x83)
916 {
917 CRT_close(file_no);
918
919 return 0xffffffff;
920 }
921 }
922
923 if (!device_or_pipe_flags && (uint8_t)oflag & CRT_OPEN_APPEND)
925
926 return file_no;
927}
928
929// Original pcdogs.exe: 0x44c8a2
930// Changes a file's length by truncating it or writing zero-filled extension bytes, then attempts to restore the saved file position. The final position-restoration error is ignored.
931int32_t CRT_chsize(int32_t fd, int32_t size)
932{ // Extends or truncates CRT fd to target size; preserves/restores file position where needed.
933 int32_t result = 0;
934
935 if ((uint32_t)fd >= (uint32_t)g_ioBufferHighWaterMark || !(g_fileDescriptorTable[fd >> CRT_FD_CHUNK_SHIFT][fd & CRT_FD_CHUNK_INDEX_MASK].flags & CRT_FD_OPEN))
936 {
938
939 return 0xffffffff;
940 }
941
942 int32_t offset = CRT_lseek(fd, 0, CRT_SEEK_CURRENT);
943
944 if (offset == -1)
945 return 0xffffffff;
946
947 int32_t current_size = CRT_lseek(fd, 0, CRT_SEEK_END);
948
949 if (current_size == -1)
950 return 0xffffffff;
951
952 int32_t bytes_to_extend = size - current_size;
953
954 if (bytes_to_extend > 0)
955 {
956 uint8_t zero_buffer[0x1000];
957
958 _memset(&zero_buffer, 0, 0x1000);
959
960 int32_t mode = CRT_setmode(fd, CRT_OPEN_BINARY);
961
962 do
963 {
964 int32_t byte_count = 0x1000;
965
966 if (bytes_to_extend < 0x1000)
967 byte_count = bytes_to_extend;
968
969 int32_t bytes_written = CRT_write(fd, &zero_buffer, byte_count);
970
971 if (bytes_written == -1)
972 {
973 if (g_crtLastOSError == 5)
975
976 result = 0xffffffff;
977
978 break;
979 }
980
981 bytes_to_extend -= bytes_written;
982 } while (bytes_to_extend > 0);
983
984 CRT_setmode(fd, mode);
985 }
986 else if (bytes_to_extend < 0)
987 {
988 CRT_lseek(fd, size, CRT_SEEK_BEGIN);
989 result = ((SetEndOfFile(CRT_get_osfhandle(fd))) ? 0 : -1);
990
991 if (result == -1)
992 {
994 g_crtLastOSError = GetLastError();
995 }
996 }
997
998 CRT_lseek(fd, offset, CRT_SEEK_BEGIN);
999
1000 return result;
1001}
1002
1003// Original pcdogs.exe: 0x44cc84
1004// Changes an open descriptor between text and binary mode and returns its previous mode. Unsupported modes and invalid descriptors fail without applying the requested change.
1005int32_t CRT_setmode(int32_t fd, int32_t mode)
1006{
1007 struct CRT_FileDescriptor *descriptor = NULL;
1008 if ((uint32_t)fd < (uint32_t)g_ioBufferHighWaterMark)
1010
1011 if (!descriptor || !(descriptor->flags & CRT_FD_OPEN))
1012 {
1014 return -1;
1015 }
1016
1017 uint8_t previous_flags = descriptor->flags;
1018 if (mode == CRT_OPEN_BINARY)
1019 descriptor->flags &= ~CRT_FD_TEXT;
1020 else if (mode == CRT_OPEN_TEXT)
1021 descriptor->flags |= CRT_FD_TEXT;
1022 else
1023 {
1025 return -1;
1026 }
1027
1028 return (previous_flags & CRT_FD_TEXT) ? CRT_OPEN_TEXT : CRT_OPEN_BINARY;
1029}
int32_t CRT_close(int32_t file_no)
int32_t CRT_read(int32_t fd, void *buffer, uint32_t count)
int32_t _set_osfhnd(int32_t fd, HANDLE os_handle)
int32_t _alloc_osfhnd(void)
int32_t CRT_chsize(int32_t fd, int32_t size)
int32_t CRT_write(int32_t file_no, void const *buffer, uint32_t byte_count)
int32_t CRT_commit(int32_t file_no)
int32_t _free_osfhnd(int32_t fd)
int32_t CRT_lseek(int32_t fd, int32_t offset, int32_t origin)
int32_t CRT_sopen(char const *filename, int32_t oflag, int32_t shflag,...)
void _ioinit()
int32_t CRT_isatty(int32_t fd)
HANDLE CRT_get_osfhandle(int32_t fd)
void _dosmaperr(uint32_t os_error_code)
int32_t CRT_setmode(int32_t fd, int32_t mode)
#define CRT_FD_APPEND
Definition constants.h:11
#define CRT_FD_DEVICE
Definition constants.h:12
#define CRT_PERMISSION_WRITE
Definition constants.h:69
#define CRT_OPEN_RANDOM
Definition constants.h:32
#define CRT_FD_LIMIT
Definition constants.h:277
#define CRT_FD_PIPE
Definition constants.h:9
#define CRT_TEXT_END_OF_FILE
Definition constants.h:278
@ CRT_ERROR_INVALID_ARGUMENT
Definition constants.h:248
@ CRT_ERROR_ACCESS_DENIED
Definition constants.h:247
@ CRT_ERROR_EXEC_FORMAT
Definition constants.h:245
@ CRT_ERROR_BAD_DESCRIPTOR
Definition constants.h:246
@ CRT_ERROR_NO_SPACE
Definition constants.h:250
@ CRT_ERROR_TOO_MANY_FILES
Definition constants.h:249
#define CRT_FD_TEXT
Definition constants.h:13
#define CRT_OPEN_TEMPORARY
Definition constants.h:34
#define CRT_FD_NO_INHERIT
Definition constants.h:10
#define CRT_OPEN_READ_WRITE
Definition constants.h:30
#define CRT_OPEN_BINARY
Definition constants.h:41
#define CRT_OPEN_EXCLUSIVE
Definition constants.h:38
#define CRT_OPEN_TEXT
Definition constants.h:40
@ CRT_SEEK_END
Definition constants.h:271
@ CRT_SEEK_CURRENT
Definition constants.h:270
@ CRT_SEEK_BEGIN
Definition constants.h:269
#define CRT_OPEN_CREATE
Definition constants.h:36
#define CRT_OPEN_WRITE_ONLY
Definition constants.h:29
#define CRT_OPEN_SHORT_LIVED
Definition constants.h:39
#define CRT_OPEN_SEQUENTIAL
Definition constants.h:33
#define CRT_OPEN_TRUNCATE
Definition constants.h:37
#define CRT_FD_CRLF
Definition constants.h:8
#define CRT_FD_CHUNK_INDEX_MASK
Definition constants.h:275
#define CRT_FD_EOF
Definition constants.h:7
#define CRT_FD_CHUNK_SHIFT
Definition constants.h:274
#define CRT_OPEN_APPEND
Definition constants.h:31
@ CRT_RUNTIME_LOWIO_INIT
Definition constants.h:261
@ CRT_SHARE_DENY_NONE
Definition constants.h:110
@ CRT_SHARE_DENY_READ_WRITE
Definition constants.h:107
@ CRT_SHARE_DENY_READ
Definition constants.h:109
@ CRT_SHARE_DENY_WRITE
Definition constants.h:108
@ CRT_APPLICATION_CONSOLE
Definition constants.h:239
#define CRT_FD_OPEN
Definition constants.h:6
#define CRT_FD_CHUNK_CAPACITY
Definition constants.h:276
#define CRT_OPEN_NO_INHERIT
Definition constants.h:35
#define FILE_SHARE_NONE
Definition enums.h:202
#define FILE_SHARE_WRITE
Definition enums.h:211
#define FILE_ATTRIBUTE_READONLY
Definition enums.h:62
@ OPEN_EXISTING
Definition enums.h:47
@ CREATE_NEW
Definition enums.h:41
@ TRUNCATE_EXISTING
Definition enums.h:53
@ OPEN_ALWAYS
Definition enums.h:50
@ CREATE_ALWAYS
Definition enums.h:44
#define FILE_FLAG_RANDOM_ACCESS
Definition enums.h:137
#define FILE_FLAG_DELETE_ON_CLOSE
Definition enums.h:143
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition enums.h:140
#define FILE_ATTRIBUTE_NORMAL
Definition enums.h:80
#define FILE_SHARE_READ
Definition enums.h:208
#define FILE_ATTRIBUTE_TEMPORARY
Definition enums.h:83
FILE_TYPE
Definition enums.h:217
@ FILE_TYPE_CHAR
Definition enums.h:226
@ FILE_TYPE_PIPE
Definition enums.h:229
void * CRT_malloc(uint32_t size)
Definition heap.c:16
void __amsg_exit(int32_t runtime_error)
Definition startup.c:205
void * _memset(void *dest, int32_t value, uint32_t count)
Definition strings.c:189
#define g_fileDescriptorTable
Definition globals.h:274
#define g_crtUmask
Definition globals.h:250
#define g_crtOSErrorMappings
Definition globals.h:339
#define g_crtLastError
Definition globals.h:163
#define g_crtConsoleMode
Definition globals.h:97
#define g_crtOpenFileFlags
Definition globals.h:169
#define g_crtLastOSError
Definition globals.h:166
uint8_t lookahead
Definition types.h:50
HANDLE os_handle
Definition types.h:48
uint8_t flags
Definition types.h:49
uint32_t os_error
Definition types.h:322
#define g_ioBufferHighWaterMark
Definition values.h:864
WIN32_ERROR
Definition win32_errors.h:7
@ ERROR_INFLOOP_IN_RELOC_CHAIN
@ ERROR_INVALID_STARTING_CODESEG
@ ERROR_BROKEN_PIPE
@ ERROR_ACCESS_DENIED
@ NO_ERROR
@ ERROR_SHARING_BUFFER_EXCEEDED
@ ERROR_WRITE_PROTECT
STD_HANDLE
Definition win32_file.h:23
@ STD_OUTPUT_HANDLE
Definition win32_file.h:29
@ STD_ERROR_HANDLE
Definition win32_file.h:32
@ STD_INPUT_HANDLE
Definition win32_file.h:26