102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
stdio.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
9#include <intrin.h>
10#include <math.h>
11#include <stdarg.h>
12#include <string.h>
13#define nullptr 0
14
15// Original pcdogs.exe: 0x44615f
16// Flushes a CRT stream and commits its descriptor when requested by the stream flags. A NULL stream flushes the applicable open streams, and failures return -1.
17int32_t CRT_fflush(struct File_Handle *stream)
18{
19 if (!stream)
20 return CRT_flsall(0);
21
22 if (_flush(stream))
23 return 0xffffffff;
24
25 if (!(stream->buffering_flags & (CRT_STREAM_COMMIT >> 8)))
26 return 0;
27
28 return -((CRT_commit(stream->_file) ? 1 : 0));
29}
30
31// Original pcdogs.exe: 0x44619a
32// Writes pending stream-buffer bytes, resets the buffer cursor and count, and records an error when the write is short or fails.
33int32_t _flush(struct File_Handle *stream)
34{
35 int32_t result = 0;
36 int16_t _flag = (int16_t)stream->_flag;
37
39 {
40 char *_base = stream->_base;
41 uint32_t byte_count = stream->_ptr - _base;
42
43 if (byte_count > 0)
44 {
45 if ((uint32_t)CRT_write(stream->_file, _base, byte_count) != byte_count)
46 {
47 stream->_flag |= CRT_STREAM_ERROR;
48 result = 0xffffffff;
49 }
50 else
51 {
52 int32_t flags_after_write = stream->_flag;
53
54 if ((uint8_t)flags_after_write & CRT_STREAM_UPDATE)
55 {
56 flags_after_write &= ~CRT_STREAM_WRITE;
57 stream->_flag = flags_after_write;
58 }
59 }
60 }
61 }
62
63 char *buffer_start = stream->_base;
64
65 stream->_cnt = 0;
66 stream->_ptr = buffer_start;
67
68 return result;
69}
70
71// Original pcdogs.exe: 0x4461f6
72// Flushes all active CRT streams and returns the number successfully flushed.
73int32_t CRT_flushall()
74{
75 return CRT_flsall(1);
76}
77
78// Original pcdogs.exe: 0x4462cf
79// Flushes and closes a CRT stream, releases its buffer, and clears its active flags. The temporary filename is freed only if descriptor closing succeeds, and flushing or closing failures produce an error result.
80int32_t CRT_fclose(struct File_Handle *stream)
81{
82 char _flag = (char)stream->_flag;
83
84 if (_flag & CRT_STREAM_STRING)
85 {
86 stream->_flag = 0;
87
88 return 0xffffffff;
89 }
90
92 {
93 stream->_flag = 0;
94
95 return 0xffffffff;
96 }
97
98 int32_t result = _flush(stream);
99
100 __freebuf(stream);
101
102 if (CRT_close(stream->_file) >= 0)
103 {
104 char *_tmpfname = stream->_tmpfname;
105
106 if (_tmpfname)
107 {
108 CRT_free(_tmpfname);
109 stream->_tmpfname = NULL;
110 }
111 }
112 else
113 result = 0xffffffff;
114
115 stream->_flag = 0;
116
117 return result;
118}
119
120// Original pcdogs.exe: 0x4463d3
121// Finds an available CRT stream and opens the requested path with the specified mode and sharing policy.
122struct File_Handle *CRT_fsopen(char const *filename, char const *mode, int32_t sharing_flag)
123{
124 struct File_Handle *stream = _getstream();
125
126 if (stream)
127 return _openfile(filename, mode, sharing_flag, stream);
128
129 return stream;
130}
131
132// Original pcdogs.exe: 0x4463f3
133// Opens a CRT stream using the default sharing policy and returns NULL on failure.
134struct File_Handle *CRT_fopen(char const *filename, char const *mode)
135{
136 return CRT_fsopen(filename, mode, CRT_SHARE_DENY_NONE);
137}
138
139// Original pcdogs.exe: 0x4466d0
140// Returns a stream's logical file position, accounting for buffered input or output and text-mode newline translation.
141int32_t CRT_ftell(struct File_Handle *stream)
142{
143 int32_t _file = stream->_file;
144
145 if (stream->_cnt < 0)
146 stream->_cnt = 0;
147
148 int32_t offset = CRT_lseek(_file, 0, CRT_SEEK_CURRENT);
149
150 if (offset < 0)
151 return 0xffffffff;
152
153 int16_t initial_stream_flags = (int16_t)stream->_flag;
154
155 if (!(initial_stream_flags & (CRT_STREAM_OWN_BUFFER | CRT_STREAM_USER_BUFFER)))
156 return offset - stream->_cnt;
157
158 char *_ptr = stream->_ptr;
159 char *buffer_start = stream->_base;
160 int32_t result = _ptr - buffer_start;
161
162 if ((uint8_t)initial_stream_flags & (CRT_STREAM_READ | CRT_STREAM_WRITE))
163 {
165 {
166 for (char *newline_cursor = buffer_start; newline_cursor < _ptr; newline_cursor = &newline_cursor[1])
167 {
168 if (*(uint8_t *)newline_cursor == 0xa)
169 result += 1;
170 }
171 }
172 }
173 else if (!((uint8_t)initial_stream_flags & CRT_STREAM_UPDATE))
174 {
176
177 return 0xffffffff;
178 }
179
180 if (!offset)
181 return result;
182
183 if (!(stream->_flag & CRT_STREAM_READ))
184 return result + offset;
185
186 int32_t _cnt = stream->_cnt;
187
188 if (!_cnt)
189 return offset;
190
191 int32_t buffered = _ptr - buffer_start + _cnt;
192
194 return result + offset - buffered;
195
196 bool no_extra_text_byte;
197
198 if (CRT_lseek(_file, 0, CRT_SEEK_END) != offset)
199 {
201
202 int32_t _bufsiz = 0x200;
203 int16_t _flag;
204
205 if (buffered <= 0x200)
206 _flag = (int16_t)stream->_flag;
207
208 if (buffered > 0x200 || !((uint8_t)_flag & CRT_STREAM_OWN_BUFFER) || _flag & CRT_STREAM_SET_BUFFER)
209 _bufsiz = stream->_bufsiz;
210
211 buffered = _bufsiz;
213 }
214 else
215 {
216 char *_base = stream->_base;
217
218 for (char *buffer_scan_end = _base + buffered; _base < buffer_scan_end; _base = &_base[1])
219 {
220 if (*(uint8_t *)_base == 0xa)
221 buffered += 1;
222 }
223
224 no_extra_text_byte = !(stream->buffering_flags & (CRT_STREAM_CTRL_Z >> 8));
225 }
226
227 if (!no_extra_text_byte)
228 buffered += 1;
229
230 return result + offset - buffered;
231}
232
233// Original pcdogs.exe: 0x446828
234// Repositions a CRT stream after validating the origin and reconciling buffered state, using the logical position for relative seeks. Invalid streams or origins and failed final descriptor seeks return -1, while intermediate flush and logical-position errors are not checked.
235int32_t CRT_fseek(struct File_Handle *stream, int32_t offset, int32_t origin)
236{
237 int32_t _flag = stream->_flag;
238
240 {
242
243 return 0xffffffff;
244 }
245
246 int32_t effective_origin = origin;
247
248 if (effective_origin && effective_origin != CRT_SEEK_CURRENT && effective_origin != CRT_SEEK_END)
249 {
251
252 return 0xffffffff;
253 }
254
256 stream->_flag = _flag;
257
258 if (effective_origin == CRT_SEEK_CURRENT)
259 {
260 offset += CRT_ftell(stream);
261 effective_origin = CRT_SEEK_BEGIN;
262 }
263
264 _flush(stream);
265
266 int32_t flags_after_flush = stream->_flag;
267
268 if ((uint8_t)flags_after_flush & CRT_STREAM_UPDATE)
269 {
270 flags_after_flush &= ~(CRT_STREAM_READ | CRT_STREAM_WRITE);
271 stream->_flag = flags_after_flush;
272 }
273 else if ((uint8_t)flags_after_flush & CRT_STREAM_READ && (uint8_t)flags_after_flush & CRT_STREAM_OWN_BUFFER && !(flags_after_flush & CRT_STREAM_SET_BUFFER))
274 stream->_bufsiz = 0x200;
275
276 int32_t seek_succeeded = CRT_lseek(stream->_file, offset, effective_origin) != -1;
277
278 return seek_succeeded - 1;
279}
280
281// Original pcdogs.exe: 0x4468b4
282// Reads whole elements through the stream buffer and direct descriptor reads, returning the number of complete elements obtained.
283uint32_t CRT_fread(void *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
284{
285 uint32_t requested_bytes = size * count, bytes_remaining = requested_bytes;
286 unsigned char *output_cursor = buffer;
287
288 if (!requested_bytes)
289 return 0;
290
291 uint32_t buffer_size = (stream->_flag & (CRT_STREAM_UNBUFFERED | CRT_STREAM_OWN_BUFFER | CRT_STREAM_USER_BUFFER)) ? stream->_bufsiz : 0x1000;
292
293 while (bytes_remaining)
294 {
296 {
297 uint32_t chunk_bytes = bytes_remaining < (uint32_t)stream->_cnt ? bytes_remaining : (uint32_t)stream->_cnt;
298
299 CRT_MoveBytesOverlapSafe((char *)output_cursor, stream->_ptr, chunk_bytes);
300 bytes_remaining -= chunk_bytes;
301 output_cursor += chunk_bytes;
302 stream->_cnt -= chunk_bytes;
303 stream->_ptr += chunk_bytes;
304 }
305 else if (bytes_remaining >= buffer_size)
306 {
307 uint32_t chunk_bytes = buffer_size ? bytes_remaining - bytes_remaining % buffer_size : bytes_remaining;
308 int32_t bytes_read = CRT_read(stream->_file, output_cursor, chunk_bytes);
309
310 if (bytes_read <= 0)
311 {
312 stream->_flag |= bytes_read ? CRT_STREAM_ERROR : CRT_STREAM_EOF;
313
314 break;
315 }
316
317 bytes_remaining -= bytes_read;
318 output_cursor += bytes_read;
319 }
320 else
321 {
322 int next_character = CRT_filbuf(stream);
323
324 if (next_character < 0)
325 break;
326
327 *output_cursor++ = (unsigned char)next_character;
328 --bytes_remaining;
329 buffer_size = stream->_bufsiz;
330 }
331 }
332
333 return (requested_bytes - bytes_remaining) / size;
334}
335
336// Original pcdogs.exe: 0x44699c
337// Writes elements through the stream buffer and direct descriptor writes, returning the number of complete elements accepted.
338uint32_t CRT_fwrite(void const *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
339{
340 uint32_t requested_bytes = size * count;
341 uint32_t bytes_remaining = requested_bytes;
342
343 if (!requested_bytes)
344 return 0;
345
346 uint32_t block_size;
347
349 block_size = 0x1000;
350 else
351 block_size = stream->_bufsiz;
352
353 while (true)
354 {
355 int32_t buffered_flags = stream->_flag & (CRT_STREAM_OWN_BUFFER | CRT_STREAM_USER_BUFFER);
356 int32_t _cnt;
357
358 if (buffered_flags)
359 _cnt = stream->_cnt;
360
361 if (buffered_flags && _cnt)
362 {
363 uint32_t buffer_copy_bytes = bytes_remaining;
364
365 if (bytes_remaining >= (uint32_t)_cnt)
366 buffer_copy_bytes = _cnt;
367
368 CRT_MoveBytesOverlapSafe(stream->_ptr, buffer, buffer_copy_bytes);
369 stream->_cnt -= buffer_copy_bytes;
370 stream->_ptr = &stream->_ptr[buffer_copy_bytes];
371 bytes_remaining -= buffer_copy_bytes;
372 buffer += buffer_copy_bytes;
373 }
374 else if (bytes_remaining < block_size)
375 {
376 if (CRT_flsbuf((int32_t)*(int8_t *)buffer, stream) == -1)
377 {
378 break;
379 }
380
381 buffer += 1;
382
383 int32_t _bufsiz = stream->_bufsiz;
384
385 bytes_remaining -= 1;
386 block_size = _bufsiz;
387
388 if (_bufsiz <= 0)
389 block_size = 1;
390 }
391 else
392 {
393 bool flush_failed;
394
395 if (buffered_flags)
396 flush_failed = _flush(stream);
397
398 if (buffered_flags && flush_failed)
399 {
400 break;
401 }
402
403 uint32_t byte_count;
404
405 byte_count = !block_size ? bytes_remaining : bytes_remaining - bytes_remaining % block_size;
406
407 int32_t bytes_written = CRT_write(stream->_file, buffer, byte_count);
408
409 if (bytes_written != -1)
410 {
411 buffer += bytes_written;
412 bytes_remaining -= bytes_written;
413 }
414
415 if (bytes_written == -1 || (uint32_t)bytes_written < byte_count)
416 {
417 stream->_flag |= CRT_STREAM_ERROR;
418
419 break;
420 }
421 }
422
423 if (!bytes_remaining)
424 return count;
425 }
426
427 return (requested_bytes - bytes_remaining) / size;
428}
429
430// Original pcdogs.exe: 0x446cf7
431// Allocates the CRT stream table, falling back to its minimum capacity when needed, and installs all twenty static stream records. Failure to allocate even the minimum table follows the fatal-error path.
433{
434 int32_t stream_capacity = g_maxFileHandleCount;
435
436 if (!stream_capacity)
437 {
438 stream_capacity = 0x200;
439 g_maxFileHandleCount = stream_capacity;
440 }
441 else if (stream_capacity < 0x14)
442 {
443 stream_capacity = 0x14;
444 g_maxFileHandleCount = stream_capacity;
445 }
446
447 struct File_Handle **stream_table = CRT_calloc(stream_capacity, 4);
448
449 g_fileHandleArray = stream_table;
450
451 if (!stream_table)
452 {
454
455 struct File_Handle **fallback_stream_table = CRT_calloc(0x14, 4);
456
457 g_fileHandleArray = fallback_stream_table;
458
459 if (!fallback_stream_table)
460 {
462 /* no return */
463 }
464 }
465
466 uint32_t static_stream_index = 0;
467
468 for (struct File_Handle *static_stream = g_crtStaticStreams;
469 (uintptr_t)static_stream < (uintptr_t)&g_pCRT_ErrorHandler;)
470 {
471 g_fileHandleArray[static_stream_index] = static_stream;
472 static_stream += 1;
473 static_stream_index += 1;
474 }
475
476 int32_t standard_fd = 0;
477
478 for (struct File_Handle *standard_stream = g_crtStaticStreams; standard_stream < &g_crtStaticStreams[3];)
479 {
480 HANDLE os_handle = g_fileDescriptorTable[standard_fd >> CRT_FD_CHUNK_SHIFT][standard_fd & CRT_FD_CHUNK_INDEX_MASK].os_handle;
481
482 if (os_handle == INVALID_HANDLE_VALUE || !os_handle)
483 standard_stream->_file = -1;
484
485 standard_stream += 1;
486 standard_fd += 1;
487 }
488}
489
490// Original pcdogs.exe: 0x446d9c
491// Always flushes CRT streams. It closes the remaining eligible streams only when the CRT exit-processing flag is set.
493{
494 CRT_flushall();
495
497 return;
498
499 /* tailcall */
501
502 return;
503}
504
505// Original pcdogs.exe: 0x4474a4
506// Prepares a stream for output, draining or allocating its buffer as needed, and writes one byte. It returns the byte on success or the end-of-file error value on failure.
507int32_t CRT_flsbuf(int32_t ch, struct File_Handle *stream)
508{
509 struct File_Handle *active_stream = stream;
510 int32_t _flag = active_stream->_flag;
511 int32_t _file = active_stream->_file;
512
513 if (!((uint8_t)_flag & (CRT_STREAM_WRITE | CRT_STREAM_UPDATE)) || (uint8_t)_flag & CRT_STREAM_STRING)
514 {
516 active_stream->_flag = _flag;
517
518 return 0xffffffff;
519 }
520
521 if ((uint8_t)_flag & CRT_STREAM_READ)
522 {
523 active_stream->_cnt = 0;
524
525 if (!((uint8_t)_flag & CRT_STREAM_EOF))
526 {
528 active_stream->_flag = _flag;
529
530 return 0xffffffff;
531 }
532
534 active_stream->_ptr = active_stream->_base;
535 active_stream->_flag = _flag;
536 }
537
538 int32_t write_mode_flags = active_stream->_flag;
539
540 active_stream->_cnt = 0;
541
542 int32_t bytes_written = 0;
543
544 write_mode_flags &= ~CRT_STREAM_EOF;
545 write_mode_flags |= CRT_STREAM_WRITE;
546 active_stream->_flag = write_mode_flags;
547
548 if (!((uint16_t)write_mode_flags & (CRT_STREAM_UNBUFFERED | CRT_STREAM_OWN_BUFFER | CRT_STREAM_USER_BUFFER)))
549 {
550 bool is_terminal;
551
552 if (active_stream == &g_crtStaticStreams[1] || active_stream == &g_crtStaticStreams[2])
553 is_terminal = CRT_isatty(_file);
554
555 if ((active_stream != &g_crtStaticStreams[1] && active_stream != &g_crtStaticStreams[2]) || !is_terminal)
556 _getbuf(active_stream);
557 }
558
559 uint32_t byte_count;
560
561 if (!(active_stream->_flag & (CRT_STREAM_OWN_BUFFER | CRT_STREAM_USER_BUFFER)))
562 {
563 byte_count = 1;
564 bytes_written = CRT_write(_file, &ch, 1);
565 }
566 else
567 {
568 char *_base = active_stream->_base;
569
570 byte_count = active_stream->_ptr - _base;
571 active_stream->_ptr = &_base[1];
572 active_stream->_cnt = active_stream->_bufsiz - 1;
573
574 if (byte_count <= 0)
575 {
576 struct CRT_FileDescriptor *descriptor;
577
578 if (_file == -1)
579 descriptor = (struct CRT_FileDescriptor *)&g_CRT_FileCharBuffer;
580 else
582
583 if (descriptor->flags & CRT_FD_APPEND)
584 CRT_lseek(_file, 0, CRT_SEEK_END);
585 }
586 else
587 bytes_written = CRT_write(_file, _base, byte_count);
588
589 *(uint8_t *)active_stream->_base = (uint8_t)ch;
590 }
591
592 if ((uint32_t)bytes_written == byte_count)
593 return (uint32_t)(uint8_t)ch;
594
595 active_stream->_flag |= CRT_STREAM_ERROR;
596
597 return 0xffffffff;
598}
599
600// Original pcdogs.exe: 0x44811b
601// Temporarily buffers unbuffered terminal stdout or stderr, reusing a cached buffer or the stream's small fallback buffer. It reports whether a temporary buffer was installed.
602int32_t _stbuf(struct File_Handle *stream)
603{
604 if (!CRT_isatty(stream->_file))
605 return 0;
606
607 int32_t standard_buffer_index;
608
609 if ((uintptr_t)stream == (uintptr_t)&g_crtStaticStreams[1])
610 standard_buffer_index = 0;
611 else if ((uintptr_t)stream == (uintptr_t)&g_crtStaticStreams[2])
612 standard_buffer_index = 1;
613 else
614 return 0;
615
617
619 {
620 void *new_buffer;
621
622 if (!g_crtStandardBuffers[standard_buffer_index])
623 {
624 new_buffer = CRT_malloc(0x1000);
625 g_crtStandardBuffers[standard_buffer_index] = new_buffer;
626 }
627
628 if (g_crtStandardBuffers[standard_buffer_index] || new_buffer)
629 {
630 char *standard_buffer = g_crtStandardBuffers[standard_buffer_index];
631
632 stream->_bufsiz = 0x1000;
633 stream->_base = standard_buffer;
634 stream->_ptr = standard_buffer;
635 stream->_cnt = 0x1000;
636 }
637 else
638 {
639 stream->_base = (char *)(&stream->_charbuf);
640 stream->_ptr = (char *)(&stream->_charbuf);
641 stream->_bufsiz = 2;
642 stream->_cnt = 2;
643 }
644
646
647 return 1;
648 }
649
650 return 0;
651}
652
653// Original pcdogs.exe: 0x4481a8
654// Flushes a stream's temporary output buffer. The clear flag controls whether that buffer is detached, and detaching does not free the cached storage.
655void _ftbuf(int32_t clear_buffer, struct File_Handle *stream)
656{
657 if (!clear_buffer)
658 {
659 if (stream->buffering_flags & (CRT_STREAM_TEMP_BUFFER >> 8))
660 _flush(stream);
661 }
662 else if (stream->buffering_flags & (CRT_STREAM_TEMP_BUFFER >> 8))
663 {
664 _flush(stream);
665 stream->buffering_flags &= (UINT8_MAX ^ ((CRT_STREAM_TEMP_BUFFER | CRT_STREAM_USER_BUFFER) >> 8));
666 stream->_bufsiz = 0;
667 stream->_ptr = 0;
668 stream->_base = NULL;
669 }
670}
671
672// Original pcdogs.exe: 0x448298
673// Frees an active stream's heap-owned buffer and clears its buffer pointers and count.
674void __freebuf(struct File_Handle *stream)
675{
676 char _flag = (char)stream->_flag;
677
679 return;
680
681 CRT_free(stream->_base);
683 stream->_ptr = 0;
684 stream->_base = nullptr;
685 stream->_cnt = 0;
686}
687
688// Original pcdogs.exe: 0x4482ec
689// Parses the stream mode and opens its CRT descriptor, then initializes the stream on success. An unsupported or repeated modifier stops mode scanning but still proceeds with the flags accumulated so far.
690struct File_Handle *_openfile(char const *filename, char const *mode, int32_t sharing_flag, struct File_Handle *stream)
691{
692 int32_t default_text_mode = g_crtDefaultTextMode;
693 char const *mode_cursor = mode;
694 int32_t commit_mode_seen = 0;
695 int32_t access_hint_seen = 0;
696 char mode_character = *(uint8_t *)mode_cursor;
697 int32_t open_flags;
698 int32_t stream_flags;
699
700 if (mode_character == 'a')
701 {
703 stream_flags = default_text_mode | CRT_STREAM_WRITE;
704 }
705 else if (mode_character == 'r')
706 {
707 open_flags = 0;
708 stream_flags = default_text_mode | CRT_STREAM_READ;
709 }
710 else
711 {
712 if (mode_character != 'w')
713 return NULL;
714
716 stream_flags = default_text_mode | CRT_STREAM_WRITE;
717 }
718
719 int32_t continue_parsing = 1;
720
721 while (true)
722 {
723 mode_character = mode_cursor[1];
724 mode_cursor = &mode_cursor[1];
725
726 if (!mode_character)
727 break;
728
729 if (!continue_parsing)
730 break;
731
732 int32_t mode_character_extended = (int32_t)mode_character;
733
734 if (mode_character_extended > 'T')
735 {
736 if (mode_character_extended == 'b')
737 {
738 if (!(open_flags & (CRT_OPEN_TEXT | CRT_OPEN_BINARY)))
739 {
740 open_flags |= CRT_OPEN_BINARY;
741
742 continue;
743 }
744 }
745 else if (mode_character_extended == 'c')
746 {
747 if (!commit_mode_seen)
748 {
749 commit_mode_seen = 1;
750 stream_flags |= CRT_STREAM_COMMIT;
751
752 continue;
753 }
754 }
755 else if (mode_character_extended == 'n')
756 {
757 if (!commit_mode_seen)
758 {
759 commit_mode_seen = 1;
760 stream_flags &= (~(uint32_t)CRT_STREAM_COMMIT);
761
762 continue;
763 }
764 }
765 else if (mode_character_extended == 't' && !(open_flags & (CRT_OPEN_TEXT | CRT_OPEN_BINARY)))
766 {
767 open_flags |= CRT_OPEN_TEXT;
768
769 continue;
770 }
771 }
772 else if (mode_character_extended == 'T')
773 {
774 if (!(CRT_OPEN_SHORT_LIVED & open_flags))
775 {
776 open_flags |= CRT_OPEN_SHORT_LIVED;
777
778 continue;
779 }
780 }
781 else if (mode_character_extended == '+')
782 {
783 if (!((uint8_t)open_flags & CRT_OPEN_READ_WRITE))
784 {
785 open_flags = (open_flags & (~(uint32_t)CRT_OPEN_WRITE_ONLY)) | CRT_OPEN_READ_WRITE;
786 stream_flags = (stream_flags & (~(uint32_t)(CRT_STREAM_READ | CRT_STREAM_WRITE))) | CRT_STREAM_UPDATE;
787
788 continue;
789 }
790 }
791 else if (mode_character_extended == 'D')
792 {
793 if (!((uint8_t)open_flags & CRT_OPEN_TEMPORARY))
794 {
795 open_flags |= CRT_OPEN_TEMPORARY;
796
797 continue;
798 }
799 }
800 else if (mode_character_extended == 'R')
801 {
802 if (!access_hint_seen)
803 {
804 access_hint_seen = 1;
805 open_flags |= CRT_OPEN_RANDOM;
806
807 continue;
808 }
809 }
810 else if (mode_character_extended == 'S' && !access_hint_seen)
811 {
812 access_hint_seen = 1;
813 open_flags |= CRT_OPEN_SEQUENTIAL;
814
815 continue;
816 }
817
818 continue_parsing = 0;
819 }
820
821 int32_t file_descriptor = CRT_sopen(filename, open_flags, sharing_flag, (CRT_PERMISSION_READ | CRT_PERMISSION_WRITE | CRT_PERMISSION_GROUP_READ | CRT_PERMISSION_OTHER_READ));
822
823 if (file_descriptor < 0)
824 return NULL;
825
827 stream->_flag = stream_flags;
828 stream->_cnt = 0;
829 stream->_ptr = 0;
830 stream->_base = NULL;
831 stream->_tmpfname = NULL;
832 stream->_file = file_descriptor;
833
834 return stream;
835}
836
837// Original pcdogs.exe: 0x44845c
838// Finds an unused CRT stream record, allocating one for an unused table slot when needed. It resets active buffer, count, and flag fields and the temporary-name pointer and initializes the descriptor to -1.
840{
841 int32_t stream_capacity = g_maxFileHandleCount;
842 struct File_Handle *result = nullptr;
843 int32_t stream_index = 0;
844
845 if (stream_capacity > 0)
846 {
847 struct File_Handle **stream_table = g_fileHandleArray;
848 struct File_Handle **stream_slot = stream_table;
849
850 while (true)
851 {
852 struct File_Handle *candidate_stream = *stream_slot;
853
854 if (!candidate_stream)
855 {
856 g_fileHandleArray[stream_index] = CRT_malloc(0x20);
857
858 struct File_Handle *allocated_stream = g_fileHandleArray[stream_index];
859
860 if (!allocated_stream)
861 break;
862
863 result = allocated_stream;
864 }
865 else if (!(candidate_stream->_flag & (CRT_STREAM_READ | CRT_STREAM_WRITE | CRT_STREAM_UPDATE)))
866 result = stream_table[stream_index];
867 else
868 {
869 stream_index += 1;
870 stream_slot = &stream_slot[1];
871
872 if (stream_index >= stream_capacity)
873 break;
874
875 continue;
876 }
877
878 if (!result)
879 break;
880
881 result->_file = -1;
882 result->_cnt = 0;
883 result->_flag = 0;
884 result->_base = NULL;
885 result->_ptr = NULL;
886 result->_tmpfname = NULL;
887
888 break;
889 }
890 }
891
892 return result;
893}
894
895// Original pcdogs.exe: 0x449381
896// Allocates or refills a stream's input buffer and returns the next unsigned byte. End of file and read failures return -1 and update the stream flags.
897int32_t CRT_filbuf(struct File_Handle *stream)
898{ // Refills FILE stream buffer via fd read and returns next byte/EOF.
899 int32_t _flag = stream->_flag;
900
902 return 0xffffffff;
903
904 if ((uint8_t)_flag & CRT_STREAM_WRITE)
905 {
907 stream->_flag = _flag;
908
909 return 0xffffffff;
910 }
911
913 stream->_flag = _flag;
914
916 stream->_ptr = stream->_base;
917 else
918 _getbuf(stream);
919
920 int32_t bytes_read = CRT_read(stream->_file, stream->_base, stream->_bufsiz);
921
922 stream->_cnt = bytes_read;
923
924 if (!bytes_read || bytes_read == -1)
925 {
926 stream->_flag |= ((bytes_read) ? CRT_STREAM_ERROR : CRT_STREAM_EOF);
927 stream->_cnt = 0;
928
929 return 0xffffffff;
930 }
931
932 int32_t flags_after_read = stream->_flag;
933
934 if (!((uint8_t)flags_after_read & (CRT_STREAM_WRITE | CRT_STREAM_UPDATE)))
935 {
936 int32_t _file = stream->_file;
937 struct CRT_FileDescriptor *descriptor;
938
939 if (_file == -1)
940 descriptor = (struct CRT_FileDescriptor *)&g_CRT_FileCharBuffer;
941 else
943
944 if ((descriptor->flags & (CRT_FD_EOF | CRT_FD_TEXT)) == (CRT_FD_EOF | CRT_FD_TEXT))
945 {
946 flags_after_read |= CRT_STREAM_CTRL_Z;
947 stream->_flag = flags_after_read;
948 }
949 }
950
951 if (stream->_bufsiz == 0x200)
952 {
953 int16_t buffer_growth_flags = (int16_t)stream->_flag;
954
955 if ((uint8_t)buffer_growth_flags & CRT_STREAM_OWN_BUFFER && !(buffer_growth_flags & CRT_STREAM_SET_BUFFER))
956 stream->_bufsiz = 0x1000;
957 }
958
959 char *_ptr = stream->_ptr;
960
961 stream->_cnt = bytes_read - 1;
962
963 uint32_t result = (uint32_t)*(uint8_t *)_ptr;
964
965 stream->_ptr = &_ptr[1];
966
967 return result;
968}
969
970// Original pcdogs.exe: 0x449a02
971// Closes active streams other than the first three standard streams and frees dynamically allocated stream records. It returns the number of streams successfully closed.
973{
974 int32_t result = 0;
975 int32_t i = 3;
976
977 if (g_maxFileHandleCount > 3)
978 {
979 do
980 {
981 struct File_Handle *stream = g_fileHandleArray[i];
982
983 if (stream)
984 {
985 if (stream->_flag & (CRT_STREAM_READ | CRT_STREAM_WRITE | CRT_STREAM_UPDATE) && CRT_fclose(stream) != -1)
986 result += 1;
987
988 if (i >= 0x14)
989 {
991 g_fileHandleArray[i] = 0;
992 }
993 }
994
995 i += 1;
996 } while (i < g_maxFileHandleCount);
997 }
998
999 return result;
1000}
1001
1002// Original pcdogs.exe: 0x44b0fd
1003// Allocates a full-size stream buffer or selects the stream's small inline fallback, then initializes the buffer cursor and count.
1004void _getbuf(struct File_Handle *stream)
1005{ // Allocates stream buffer; uses 0x1000 heap block when available, else inline charbuf.
1007
1008 char *allocated_buffer = CRT_malloc(0x1000);
1009
1010 stream->_base = allocated_buffer;
1011
1012 if (!allocated_buffer)
1013 {
1014 stream->_flag |= CRT_STREAM_UNBUFFERED;
1015 stream->_base = (char *)&stream->_charbuf;
1016 stream->_bufsiz = 2;
1017 }
1018 else
1019 {
1020 stream->_flag |= CRT_STREAM_OWN_BUFFER;
1021 stream->_bufsiz = 0x1000;
1022 }
1023
1024 char *_base = stream->_base;
1025
1026 stream->_cnt = 0;
1027 stream->_ptr = _base;
1028}
1029
1030// Original pcdogs.exe: 0x4461ff
1031// Flushes the selected active streams. The all-stream mode counts successful flushes, while the output-only mode returns an aggregate error status.
1032int32_t CRT_flsall(int32_t flush_mode)
1033{
1034 int32_t stream_index = 0;
1035 int32_t flushed_stream_count = 0;
1036 int32_t flush_error = 0;
1037
1038 if (g_maxFileHandleCount > 0)
1039 {
1040 do
1041 {
1042 struct File_Handle *stream = g_fileHandleArray[stream_index];
1043
1044 if (stream)
1045 {
1046 char _flag = (char)stream->_flag;
1047
1049 {
1050 if (flush_mode == 1)
1051 {
1052 if (CRT_fflush(stream) != -1)
1053 flushed_stream_count += 1;
1054 }
1055 else if (!flush_mode && _flag & CRT_STREAM_WRITE)
1056 {
1057 int32_t stream_flush_result = CRT_fflush(stream);
1058
1059 if (stream_flush_result == -1)
1060 flush_error |= stream_flush_result;
1061 }
1062 }
1063 }
1064
1065 stream_index += 1;
1066 } while (stream_index < g_maxFileHandleCount);
1067 }
1068
1069 if (flush_mode == 1)
1070 return flushed_stream_count;
1071
1072 return flush_error;
1073}
#define CRT_STREAM_OWN_BUFFER
Definition constants.h:18
#define CRT_FD_APPEND
Definition constants.h:11
#define CRT_PERMISSION_WRITE
Definition constants.h:69
#define CRT_OPEN_RANDOM
Definition constants.h:32
#define CRT_PERMISSION_GROUP_READ
Definition constants.h:306
#define CRT_PERMISSION_OTHER_READ
Definition constants.h:307
#define CRT_STREAM_STRING
Definition constants.h:21
#define CRT_STREAM_EOF
Definition constants.h:19
#define CRT_STREAM_SET_BUFFER
Definition constants.h:24
@ CRT_ERROR_INVALID_ARGUMENT
Definition constants.h:248
#define CRT_FD_TEXT
Definition constants.h:13
#define CRT_OPEN_TEMPORARY
Definition constants.h:34
#define CRT_STREAM_COMMIT
Definition constants.h:27
#define CRT_OPEN_READ_WRITE
Definition constants.h:30
#define CRT_OPEN_BINARY
Definition constants.h:41
#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_STREAM_USER_BUFFER
Definition constants.h:23
#define CRT_FD_CRLF
Definition constants.h:8
#define CRT_STREAM_TEMP_BUFFER
Definition constants.h:25
#define CRT_FD_CHUNK_INDEX_MASK
Definition constants.h:275
#define CRT_FD_EOF
Definition constants.h:7
#define CRT_STREAM_ERROR
Definition constants.h:20
#define CRT_FD_CHUNK_SHIFT
Definition constants.h:274
#define CRT_STREAM_WRITE
Definition constants.h:16
#define CRT_OPEN_APPEND
Definition constants.h:31
#define CRT_STREAM_UNBUFFERED
Definition constants.h:17
#define CRT_STREAM_READ
Definition constants.h:15
@ CRT_RUNTIME_STDIO_INIT
Definition constants.h:260
@ CRT_SHARE_DENY_NONE
Definition constants.h:110
#define CRT_STREAM_CTRL_Z
Definition constants.h:26
#define CRT_STREAM_UPDATE
Definition constants.h:22
#define CRT_PERMISSION_READ
Definition constants.h:305
int32_t CRT_fclose(struct File_Handle *stream)
Definition stdio.c:80
int32_t CRT_close(int32_t file_no)
void * CRT_malloc(uint32_t size)
Definition heap.c:16
int32_t CRT_read(int32_t fd, void *buffer, uint32_t count)
void * CRT_calloc(uint32_t count, uint32_t element_size)
Definition heap.c:1080
void __amsg_exit(int32_t runtime_error)
Definition startup.c:205
void CRT_free(void *ptr)
Definition heap.c:164
int32_t CRT_write(int32_t file_no, void const *buffer, uint32_t byte_count)
int32_t CRT_commit(int32_t file_no)
char * CRT_MoveBytesOverlapSafe(char *dest, char const *src, uint32_t count)
Definition strings.c:134
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,...)
int32_t CRT_isatty(int32_t fd)
#define g_fileDescriptorTable
Definition globals.h:274
#define g_crtExitProcessing
Definition globals.h:175
#define g_crtDefaultTextMode
Definition globals.h:235
#define g_crtLastError
Definition globals.h:163
#define g_crtStandardBuffers
Definition globals.h:342
#define g_pCRT_ErrorHandler
Definition globals.h:94
#define g_crtFileBufferSerial
Definition globals.h:184
#define g_fileHandleArray
Definition globals.h:289
#define g_crtStaticStreams
Definition globals.h:336
#define g_maxFileHandleCount
Definition globals.h:292
#define g_CRT_FileCharBuffer
Definition globals.h:124
int32_t CRT_fclose(struct File_Handle *stream)
Definition stdio.c:80
void __freebuf(struct File_Handle *stream)
Definition stdio.c:674
int32_t CRT_flsall(int32_t flush_mode)
Definition stdio.c:1032
void _ftbuf(int32_t clear_buffer, struct File_Handle *stream)
Definition stdio.c:655
int32_t CRT_fcloseall()
Definition stdio.c:972
uint32_t CRT_fwrite(void const *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
Definition stdio.c:338
int32_t _stbuf(struct File_Handle *stream)
Definition stdio.c:602
struct File_Handle * _openfile(char const *filename, char const *mode, int32_t sharing_flag, struct File_Handle *stream)
Definition stdio.c:690
struct File_Handle * CRT_fsopen(char const *filename, char const *mode, int32_t sharing_flag)
Definition stdio.c:122
void _initstdio()
Definition stdio.c:432
int32_t CRT_fseek(struct File_Handle *stream, int32_t offset, int32_t origin)
Definition stdio.c:235
int32_t CRT_flushall()
Definition stdio.c:73
void _endstdio()
Definition stdio.c:492
int32_t CRT_fflush(struct File_Handle *stream)
Definition stdio.c:17
void _getbuf(struct File_Handle *stream)
Definition stdio.c:1004
int32_t CRT_flsbuf(int32_t ch, struct File_Handle *stream)
Definition stdio.c:507
int32_t _flush(struct File_Handle *stream)
Definition stdio.c:33
uint32_t CRT_fread(void *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
Definition stdio.c:283
struct File_Handle * CRT_fopen(char const *filename, char const *mode)
Definition stdio.c:134
int32_t CRT_filbuf(struct File_Handle *stream)
Definition stdio.c:897
int32_t CRT_ftell(struct File_Handle *stream)
Definition stdio.c:141
struct File_Handle * _getstream()
Definition stdio.c:839
uint8_t flags
Definition types.h:49
int32_t _flag
Definition types.h:235
char * _base
Definition types.h:231
uint8_t buffering_flags
Definition types.h:240
char * _ptr
Definition types.h:229
int32_t _cnt
Definition types.h:230
int32_t _file
Definition types.h:245
char * _tmpfname
Definition types.h:248
int32_t _charbuf
Definition types.h:246
int32_t _bufsiz
Definition types.h:247