forked from Community-Duris/DurisMUD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfile_converter.c
More file actions
1421 lines (1193 loc) · 44.4 KB
/
Copy pathpfile_converter.c
File metadata and controls
1421 lines (1193 loc) · 44.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* DurisMUD Player File Converter - Integrated Version
*
* Converts player files from big-endian (old format) to little-endian (new format)
*
* This converter handles the endianness change introduced by the testmud merge
* (commit 2978989b) which removed htonl()/htons() calls from ADD_SHORT, ADD_INT,
* and ADD_LONG macros in src/files.h.
*
* IMPORTANT: This also handles the pc_timer format change from INT to LONG,
* which expands the status section by 40 bytes.
*
* Usage:
* pfile_converter [OPTIONS] <input_file> [output_file]
*
* Options:
* -v, --verbose Verbose diagnostic output
* --dry-run Validate input only, don't write output
* --backup Create .backup file before conversion
* -h, --help Show this help message
*
* !! I only tested this with 5 char. !!
* Date: 15/10/2025 - Arih
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
/***************************************************************************
* Constants and Defines
***************************************************************************/
/* File format version constants */
#define SAV_SAVEVERS 5 /* Overall save format version */
#define SAV_STATVERS 47 /* Status section version */
#define SAV_SKILLVERS 2 /* Skills section version */
#define SAV_WTNSVERS 2 /* Witness section version */
#define SAV_AFFVERS 7 /* Affects section version */
#define SAV_ITEMVERS 35 /* Items section version */
/* Size constants from source code */
#define MAX_CIRCLE 12
#define MAX_TONGUE 29
#define MAX_INTRO 150
#define MAX_FORGE_ITEMS 1000
#define NUMB_PC_TIMERS 10
#define MAX_COND 5
#define MAX_SKILLS 2000
#define MAX_OBJ_AFFECT 4
/* Item flags (from src/files.h) */
#define O_F_WORN 1 /* item was equipped when saved */
#define O_F_CONTAINS 2 /* item was a container that had something in it */
#define O_F_UNIQUE 4 /* item was 'non-stock' in some way */
#define O_F_COUNT 8 /* more than 1 item of this type */
#define O_F_EOL 16 /* marks end of a 'contents' list */
#define O_F_AFFECTS 32 /* object had a decay event when saved */
#define O_F_SPELLBOOK 64 /* has spellbook array */
/* Unique field flags - BIT_N = 2^(N-1) */
#define O_U_KEYS (1U) /* BIT_1 */
#define O_U_DESC1 (2U) /* BIT_2 */
#define O_U_DESC2 (4U) /* BIT_3 */
#define O_U_DESC3 (8U) /* BIT_4 */
#define O_U_VAL0 (16U) /* BIT_5 */
#define O_U_VAL1 (32U) /* BIT_6 */
#define O_U_VAL2 (64U) /* BIT_7 */
#define O_U_VAL3 (128U) /* BIT_8 */
#define O_U_TYPE (256U) /* BIT_9 */
#define O_U_WEAR (512U) /* BIT_10 */
#define O_U_EXTRA (1024U) /* BIT_11 */
#define O_U_WEIGHT (2048U) /* BIT_12 */
#define O_U_COST (4096U) /* BIT_13 */
#define O_U_BV1 (8192U) /* BIT_14 */
#define O_U_BV2 (16384U) /* BIT_15 */
#define O_U_AFFS (32768U) /* BIT_16 */
#define O_U_TRAP (65536U) /* BIT_17 */
#define O_U_COND (131072U) /* BIT_18 */
#define O_U_ANTI (262144U) /* BIT_19 */
#define O_U_EXTRA2 (524288U) /* BIT_20 */
#define O_U_TIMER (1048576U) /* BIT_21 */
#define O_U_ANTI2 (2097152U) /* BIT_22 */
#define O_U_EDESC (4194304U) /* BIT_23 */
#define O_U_MATERIAL (8388608U) /* BIT_24 */
#define O_U_SPACE (16777216U) /* BIT_25 */
#define O_U_VAL4 (33554432U) /* BIT_26 */
#define O_U_VAL5 (67108864U) /* BIT_27 */
#define O_U_VAL6 (134217728U) /* BIT_28 */
#define O_U_VAL7 (268435456U) /* BIT_29 */
#define O_U_BV3 (536870912U) /* BIT_30 */
#define O_U_BV4 (1073741824U) /* BIT_31 */
#define O_U_BV5 (2147483648U) /* BIT_32 */
/* Maximum file size */
#define SAV_MAXSIZE (240 * 1024) /* 240 KB */
#define MAX_RECURSION 50 /* Max item container depth */
/***************************************************************************
* Global Variables
***************************************************************************/
static int verbose_mode = 0;
static int dry_run_mode = 0;
static int backup_mode = 0;
/* Statistics */
static struct {
int status_bytes_read;
int status_bytes_written;
int skills_converted;
int witness_records;
int affects_converted;
int items_converted;
} stats;
/***************************************************************************
* File Header Structure
***************************************************************************/
typedef struct {
uint8_t save_version;
uint8_t short_size;
uint8_t int_size;
uint8_t long_size;
uint8_t rent_type;
uint32_t skill_offset;
uint32_t witness_offset;
uint32_t affect_offset;
uint32_t item_offset;
uint32_t size_offset;
uint32_t act3_surname;
uint32_t start_room;
uint64_t save_time;
} pfile_header_t;
/***************************************************************************
* Endian Conversion Functions
***************************************************************************/
/* Read big-endian 16-bit value and advance pointer */
static uint16_t read_be16(unsigned char **buf) {
uint16_t val = ((uint16_t)(*buf)[0] << 8) | (uint16_t)(*buf)[1];
*buf += 2;
return val;
}
/* Read big-endian 32-bit value and advance pointer */
static uint32_t read_be32(unsigned char **buf) {
uint32_t val = ((uint32_t)(*buf)[0] << 24) |
((uint32_t)(*buf)[1] << 16) |
((uint32_t)(*buf)[2] << 8) |
(uint32_t)(*buf)[3];
*buf += 4;
return val;
}
/* Read big-endian 64-bit value and advance pointer
*
* CRITICAL FIX: Old ADD_LONG macro used htonl() which is 32-bit only!
* Even when long_size=8, old files only have 4 bytes of real data (from htonl),
* followed by 4 bytes of padding/garbage.
*
* So we ALWAYS read just the first 4 bytes as the actual value.
*/
static uint64_t read_be64(unsigned char **buf, int size) {
uint64_t val;
if (size == 8) {
/* Old ADD_LONG used htonl() (32-bit), so only first 4 bytes are valid */
val = read_be32(buf); /* Read the 4-byte big-endian value */
*buf += 4; /* Skip the 4 bytes of padding/garbage */
} else {
/* 32-bit LONG - read 4 bytes */
val = read_be32(buf);
}
return val;
}
/* Write little-endian 16-bit value and advance pointer */
static void write_le16(unsigned char **buf, uint16_t val) {
(*buf)[0] = val & 0xFF;
(*buf)[1] = (val >> 8) & 0xFF;
*buf += 2;
}
/* Write little-endian 32-bit value and advance pointer */
static void write_le32(unsigned char **buf, uint32_t val) {
(*buf)[0] = val & 0xFF;
(*buf)[1] = (val >> 8) & 0xFF;
(*buf)[2] = (val >> 16) & 0xFF;
(*buf)[3] = (val >> 24) & 0xFF;
*buf += 4;
}
/* Write little-endian 64-bit value and advance pointer */
static void write_le64(unsigned char **buf, uint64_t val) {
(*buf)[0] = val & 0xFF;
(*buf)[1] = (val >> 8) & 0xFF;
(*buf)[2] = (val >> 16) & 0xFF;
(*buf)[3] = (val >> 24) & 0xFF;
(*buf)[4] = (val >> 32) & 0xFF;
(*buf)[5] = (val >> 40) & 0xFF;
(*buf)[6] = (val >> 48) & 0xFF;
(*buf)[7] = (val >> 56) & 0xFF;
*buf += 8;
}
/***************************************************************************
* String Conversion Functions
***************************************************************************/
/* Read string: big-endian length prefix + string data */
static char* read_string(unsigned char **buf) {
uint16_t len = read_be16(buf);
if (len == 0) {
return NULL;
}
char *str = (char*)malloc(len + 1);
if (!str) {
fprintf(stderr, "ERROR: Failed to allocate memory for string\n");
exit(1);
}
memcpy(str, *buf, len);
str[len] = '\0';
*buf += len;
return str;
}
/* Write string: little-endian length prefix + string data */
static void write_string(unsigned char **out, const char *str) {
if (!str) {
write_le16(out, 0);
return;
}
uint16_t len = strlen(str);
write_le16(out, len);
memcpy(*out, str, len);
*out += len;
}
/***************************************************************************
* Header Parsing and Conversion
***************************************************************************/
static int parse_header(unsigned char *buf, size_t file_size, pfile_header_t *header) {
unsigned char *ptr = buf;
/* Parse fixed header fields */
header->save_version = *ptr++;
header->short_size = *ptr++;
header->int_size = *ptr++;
header->long_size = *ptr++;
header->rent_type = *ptr++;
/* Parse big-endian offsets */
header->skill_offset = read_be32(&ptr);
header->witness_offset = read_be32(&ptr);
header->affect_offset = read_be32(&ptr);
header->item_offset = read_be32(&ptr);
header->size_offset = read_be32(&ptr);
header->act3_surname = read_be32(&ptr);
header->start_room = read_be32(&ptr);
header->save_time = read_be64(&ptr, header->long_size);
/* Validate header */
if (header->save_version != SAV_SAVEVERS) {
fprintf(stderr, "ERROR: Invalid save version %d (expected %d)\n",
header->save_version, SAV_SAVEVERS);
return 0;
}
if (header->short_size != 2 || header->int_size != 4 ||
(header->long_size != 4 && header->long_size != 8)) {
fprintf(stderr, "ERROR: Invalid type sizes (short=%d, int=%d, long=%d)\n",
header->short_size, header->int_size, header->long_size);
return 0;
}
/* Validate offsets are within file */
if (header->skill_offset >= file_size || header->witness_offset >= file_size ||
header->affect_offset >= file_size || header->item_offset >= file_size ||
header->size_offset > file_size) {
fprintf(stderr, "ERROR: Invalid section offsets\n");
return 0;
}
/* Verify section version bytes */
if (buf[41] != SAV_STATVERS) {
fprintf(stderr, "ERROR: Invalid status version %d at offset 41\n", buf[41]);
return 0;
}
if (buf[header->skill_offset] != SAV_SKILLVERS) {
fprintf(stderr, "ERROR: Invalid skill version at offset %u\n", header->skill_offset);
return 0;
}
if (buf[header->witness_offset] != SAV_WTNSVERS) {
fprintf(stderr, "ERROR: Invalid witness version at offset %u\n", header->witness_offset);
return 0;
}
if (buf[header->affect_offset] != SAV_AFFVERS) {
fprintf(stderr, "ERROR: Invalid affect version at offset %u\n", header->affect_offset);
return 0;
}
if (buf[header->item_offset] != SAV_ITEMVERS) {
fprintf(stderr, "ERROR: Invalid item version at offset %u\n", header->item_offset);
return 0;
}
return 1;
}
static void write_header(unsigned char **out, const pfile_header_t *header) {
/* Write fixed header fields */
*(*out)++ = header->save_version;
*(*out)++ = header->short_size;
*(*out)++ = header->int_size;
*(*out)++ = header->long_size;
*(*out)++ = header->rent_type;
/* Write little-endian offsets (these will be updated after conversion) */
write_le32(out, header->skill_offset);
write_le32(out, header->witness_offset);
write_le32(out, header->affect_offset);
write_le32(out, header->item_offset);
write_le32(out, header->size_offset);
write_le32(out, header->act3_surname);
write_le32(out, header->start_room);
write_le64(out, header->save_time);
}
static void print_header(const pfile_header_t *header) {
if (!verbose_mode) return;
printf("\n=== File Header ===\n");
printf("Save version: %d\n", header->save_version);
printf("Type sizes: short=%d, int=%d, long=%d\n",
header->short_size, header->int_size, header->long_size);
printf("Rent type: %d\n", header->rent_type);
printf("Section offsets:\n");
printf(" Status: 41 (implicit)\n");
printf(" Skills: %u\n", header->skill_offset);
printf(" Witness: %u\n", header->witness_offset);
printf(" Affects: %u\n", header->affect_offset);
printf(" Items: %u\n", header->item_offset);
printf(" EOF: %u\n", header->size_offset);
/* Handle timestamp quirk */
uint64_t timestamp = header->save_time;
if (header->long_size == 8 && timestamp > 0x100000000ULL) {
timestamp = timestamp >> 32; /* Use high 32 bits */
}
time_t t = (time_t)timestamp;
printf("Save time: %s", ctime(&t));
}
/***************************************************************************
* Forward Declarations
***************************************************************************/
static int convert_status_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header);
static int convert_skills_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header);
static int convert_witness_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header);
static int convert_affects_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header);
static int convert_items_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header);
/***************************************************************************
* Main Conversion Function
***************************************************************************/
static int convert_pfile(unsigned char *input_buf, size_t input_size,
unsigned char **output_buf, size_t *output_size) {
pfile_header_t header;
unsigned char *in, *out, *out_start;
extern unsigned char *input_end;
/* Set input buffer boundary */
input_end = input_buf + input_size;
/* Parse and validate header */
if (!parse_header(input_buf, input_size, &header)) {
return 0;
}
print_header(&header);
/* Allocate output buffer (input size + 40 bytes for pc_timer expansion) */
size_t max_output_size = input_size + 100; /* Extra padding for safety */
*output_buf = (unsigned char*)malloc(max_output_size);
if (!*output_buf) {
fprintf(stderr, "ERROR: Failed to allocate output buffer\n");
return 0;
}
out_start = *output_buf;
out = out_start;
/* Write header (we'll update offsets later) */
write_header(&out, &header);
/* Convert status section */
in = input_buf + 41; /* Status starts after header */
if (!convert_status_section(&in, &out, &header)) {
free(*output_buf);
return 0;
}
/* Update skill_offset in header */
uint32_t new_skill_offset = out - out_start;
/* Convert skills section */
in = input_buf + header.skill_offset;
if (!convert_skills_section(&in, &out, &header)) {
free(*output_buf);
return 0;
}
/* Update witness_offset in header */
uint32_t new_witness_offset = out - out_start;
/* Convert witness section */
in = input_buf + header.witness_offset;
if (!convert_witness_section(&in, &out, &header)) {
free(*output_buf);
return 0;
}
/* Update affect_offset in header */
uint32_t new_affect_offset = out - out_start;
/* Convert affects section */
in = input_buf + header.affect_offset;
if (!convert_affects_section(&in, &out, &header)) {
free(*output_buf);
return 0;
}
/* Update item_offset in header */
uint32_t new_item_offset = out - out_start;
/* Convert items section */
in = input_buf + header.item_offset;
if (!convert_items_section(&in, &out, &header)) {
free(*output_buf);
return 0;
}
/* Update size_offset in header */
uint32_t new_size_offset = out - out_start;
/* Update all offsets in the header */
unsigned char *header_ptr = out_start + 5; /* Skip to first offset */
write_le32(&header_ptr, new_skill_offset);
write_le32(&header_ptr, new_witness_offset);
write_le32(&header_ptr, new_affect_offset);
write_le32(&header_ptr, new_item_offset);
write_le32(&header_ptr, new_size_offset);
*output_size = new_size_offset;
if (verbose_mode) {
printf("\n=== Conversion Summary ===\n");
printf("Input size: %zu bytes\n", input_size);
printf("Output size: %zu bytes\n", *output_size);
printf("Size change: %+zd bytes\n", (ssize_t)(*output_size - input_size));
printf("\n=== Section Offsets ===\n");
printf("Status: 41\n");
printf("Skills: %u (was %u)\n", new_skill_offset, header.skill_offset);
printf("Witness: %u (was %u)\n", new_witness_offset, header.witness_offset);
printf("Affects: %u (was %u)\n", new_affect_offset, header.affect_offset);
printf("Items: %u (was %u)\n", new_item_offset, header.item_offset);
printf("EOF: %u (was %u)\n", new_size_offset, header.size_offset);
}
return 1;
}
/***************************************************************************
* Status Section Conversion
***************************************************************************/
static int convert_status_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header) {
unsigned char *in_start = *in;
unsigned char *out_start = *out;
uint8_t version;
char *str;
int i;
if (verbose_mode) {
printf("\n=== Converting Status Section ===\n");
}
/* Version byte */
version = *(*in)++;
if (version != SAV_STATVERS) {
fprintf(stderr, "ERROR: Invalid status version %d\n", version);
return 0;
}
*(*out)++ = version;
/* Player name (STRING) */
str = read_string(in);
write_string(out, str);
if (verbose_mode && str) {
printf("Player name: %s\n", str);
}
free(str);
/* pid (INT) */
uint32_t pid = read_be32(in);
write_le32(out, pid);
/* screen_length (BYTE) */
*(*out)++ = *(*in)++;
/* Strings: password, short_descr, long_descr, description, title */
for (i = 0; i < 5; i++) {
str = read_string(in);
write_string(out, str);
free(str);
}
/* m_class, secondary_class (INT, INT) */
uint32_t m_class = read_be32(in);
uint32_t secondary_class = read_be32(in);
write_le32(out, m_class);
write_le32(out, secondary_class);
/* spec, race, racewar, level, sex (5 BYTEs) */
for (i = 0; i < 5; i++) {
*(*out)++ = *(*in)++;
}
if (verbose_mode) {
printf("Class: %u, Level: %u, Race: %u\n", m_class, (*in)[-2], (*in)[-4]);
}
/* weight, height (SHORT, SHORT) */
uint16_t weight = read_be16(in);
uint16_t height = read_be16(in);
write_le16(out, weight);
write_le16(out, height);
/* size (BYTE) */
*(*out)++ = *(*in)++;
/* home, birthplace, orig_birthplace (INT, INT, INT) */
for (i = 0; i < 3; i++) {
uint32_t val = read_be32(in);
write_le32(out, val);
}
/* birth_time (LONG) */
uint64_t birth_time = read_be64(in, header->long_size);
write_le64(out, birth_time);
/* played_time (INT) */
uint32_t played_time = read_be32(in);
write_le32(out, played_time);
/* saved_time (LONG) */
uint64_t saved_time = read_be64(in, header->long_size);
write_le64(out, saved_time);
/* perm_aging (SHORT) */
uint16_t perm_aging = read_be16(in);
write_le16(out, perm_aging);
/* undead_spell_slots (BYTE[13]) */
for (i = 0; i < MAX_CIRCLE + 1; i++) {
*(*out)++ = *(*in)++;
}
/* last_level (INT, always 0) */
uint32_t last_level = read_be32(in);
write_le32(out, last_level);
/* CRITICAL: pc_timer array - read as INT (old format), write as LONG (new format) */
/* This is where the 40-byte expansion occurs: 10 timers × 4 bytes growth = 40 bytes */
for (i = 0; i < NUMB_PC_TIMERS; i++) {
uint32_t timer_val = read_be32(in); /* Read as INT (4 bytes) */
write_le64(out, (uint64_t)timer_val); /* Write as LONG (8 bytes) */
}
/* tongue_count (SHORT) */
uint16_t tongue_count = read_be16(in);
write_le16(out, tongue_count);
/* languages (BYTE[29]) */
for (i = 0; i < MAX_TONGUE; i++) {
*(*out)++ = *(*in)++;
}
/* intro_count (SHORT) */
uint16_t intro_count = read_be16(in);
write_le16(out, intro_count);
/* introd_list and introd_times arrays */
for (i = 0; i < MAX_INTRO; i++) {
uint32_t introd_id = read_be32(in);
uint64_t introd_time = read_be64(in, header->long_size);
write_le32(out, introd_id);
write_le64(out, introd_time);
}
/* learned_forged_list (INT[1000]) */
for (i = 0; i < MAX_FORGE_ITEMS; i++) {
uint32_t forged_item = read_be32(in);
write_le32(out, forged_item);
}
/* Base stats (BYTE[10]) */
for (i = 0; i < 10; i++) {
*(*out)++ = *(*in)++;
}
/* mana, base_mana, damage_taken (SHORT, SHORT, SHORT) */
for (i = 0; i < 3; i++) {
uint16_t val = read_be16(in);
write_le16(out, val);
}
/* spells_memmed (BYTE) */
*(*out)++ = *(*in)++;
/* base_hit, vitality, base_vitality (SHORT, SHORT, SHORT) */
for (i = 0; i < 3; i++) {
uint16_t val = read_be16(in);
write_le16(out, val);
}
/* Money: copper, silver, gold, platinum (INT, INT, INT, INT) */
/* experience, padding, epics, epic_skill_points, skillpoints (INT × 5) */
/* spell_bind_used, act, act2, vote, alignment, padding (INT × 6) */
for (i = 0; i < 15; i++) {
uint32_t val = read_be32(in);
write_le32(out, val);
}
/* prestige, guild_id (SHORT, SHORT) */
for (i = 0; i < 2; i++) {
uint16_t val = read_be16(in);
write_le16(out, val);
}
/* guild_status (INT) */
uint32_t guild_status = read_be32(in);
write_le32(out, guild_status);
/* time_left_guild (LONG) */
uint64_t time_left_guild = read_be64(in, header->long_size);
write_le64(out, time_left_guild);
/* nb_left_guild (BYTE) */
*(*out)++ = *(*in)++;
/* time_unspecced, frags, oldfrags (LONG, LONG, LONG) */
for (i = 0; i < 3; i++) {
uint64_t val = read_be64(in, header->long_size);
write_le64(out, val);
}
/* numb_gcmd (INT) */
uint32_t numb_gcmd = read_be32(in);
write_le32(out, numb_gcmd);
/* gcmd_arr (INT[numb_gcmd]) */
for (i = 0; i < (int)numb_gcmd; i++) {
uint32_t gcmd = read_be32(in);
write_le32(out, gcmd);
}
/* conditions (BYTE[5]) */
for (i = 0; i < MAX_COND; i++) {
*(*out)++ = *(*in)++;
}
/* Poof strings (4 STRINGs) */
for (i = 0; i < 4; i++) {
str = read_string(in);
write_string(out, str);
free(str);
}
/* echo_toggle (BYTE) */
*(*out)++ = *(*in)++;
/* prompt (SHORT) */
uint16_t prompt = read_be16(in);
write_le16(out, prompt);
/* wiz_invis, law_flags (LONG, LONG) */
for (i = 0; i < 2; i++) {
uint64_t val = read_be64(in, header->long_size);
write_le64(out, val);
}
/* wimpy, aggressive (SHORT, SHORT) */
for (i = 0; i < 2; i++) {
uint16_t val = read_be16(in);
write_le16(out, val);
}
/* highest_level (BYTE) */
*(*out)++ = *(*in)++;
/* Bank balance: copper, silver, gold, platinum (INT × 4) */
for (i = 0; i < 4; i++) {
uint32_t val = read_be32(in);
write_le32(out, val);
}
/* numb_deaths (LONG) */
uint64_t numb_deaths = read_be64(in, header->long_size);
write_le64(out, numb_deaths);
/* Quest data (14 INT fields) */
for (i = 0; i < 14; i++) {
uint32_t val = read_be32(in);
write_le32(out, val);
}
stats.status_bytes_read = *in - in_start;
stats.status_bytes_written = *out - out_start;
if (verbose_mode) {
printf("Status section: %d bytes read, %d bytes written (+%d expansion)\n",
stats.status_bytes_read, stats.status_bytes_written,
stats.status_bytes_written - stats.status_bytes_read);
}
return 1;
}
/***************************************************************************
* Skills Section Conversion
***************************************************************************/
static int convert_skills_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header) {
uint8_t version;
int i;
if (verbose_mode) {
printf("\n=== Converting Skills Section ===\n");
}
/* Version byte */
version = *(*in)++;
if (version != SAV_SKILLVERS) {
fprintf(stderr, "ERROR: Invalid skills version %d\n", version);
return 0;
}
*(*out)++ = version;
/* num_skills (INT) */
uint32_t num_skills = read_be32(in);
write_le32(out, num_skills);
if (num_skills != MAX_SKILLS) {
fprintf(stderr, "WARNING: num_skills=%u (expected %d)\n", num_skills, MAX_SKILLS);
}
/* Skill data (learned, taught, padding) × num_skills */
for (i = 0; i < (int)num_skills; i++) {
*(*out)++ = *(*in)++; /* learned */
*(*out)++ = *(*in)++; /* taught */
*(*out)++ = *(*in)++; /* padding */
}
/* Padding INT and SHORT */
uint32_t pad_int = read_be32(in);
uint16_t pad_short = read_be16(in);
write_le32(out, pad_int);
write_le16(out, pad_short);
stats.skills_converted = num_skills;
if (verbose_mode) {
printf("Skills converted: %u\n", num_skills);
}
return 1;
}
/***************************************************************************
* Witness Section Conversion
***************************************************************************/
static int convert_witness_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header) {
uint8_t version;
char *str;
int i;
if (verbose_mode) {
printf("\n=== Converting Witness Section ===\n");
}
/* Version byte */
version = *(*in)++;
if (version != SAV_WTNSVERS) {
fprintf(stderr, "ERROR: Invalid witness version %d\n", version);
return 0;
}
*(*out)++ = version;
/* count (INT) */
uint32_t count = read_be32(in);
write_le32(out, count);
/* Witness records */
for (i = 0; i < (int)count; i++) {
/* attacker (STRING) */
str = read_string(in);
write_string(out, str);
free(str);
/* victim (STRING) */
str = read_string(in);
write_string(out, str);
free(str);
/* time (LONG) */
uint64_t time = read_be64(in, header->long_size);
write_le64(out, time);
/* crime, room (INT, INT) */
uint32_t crime = read_be32(in);
uint32_t room = read_be32(in);
write_le32(out, crime);
write_le32(out, room);
}
stats.witness_records = count;
if (verbose_mode) {
printf("Witness records converted: %u\n", count);
}
return 1;
}
/***************************************************************************
* Affects Section Conversion
***************************************************************************/
static int convert_affects_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header) {
uint8_t version;
char *str;
int i, j;
if (verbose_mode) {
printf("\n=== Converting Affects Section ===\n");
}
/* Version byte */
version = *(*in)++;
if (version != SAV_AFFVERS) {
fprintf(stderr, "ERROR: Invalid affects version %d\n", version);
return 0;
}
*(*out)++ = version;
/* count (SHORT) */
uint16_t count = read_be16(in);
write_le16(out, count);
/* Affect records */
for (i = 0; i < count; i++) {
/* custom_messages (BYTE) */
uint8_t custom_messages = *(*in)++;
*(*out)++ = custom_messages;
/* Conditional wear-off strings */
if (custom_messages & 1) {
str = read_string(in);
write_string(out, str);
free(str);
}
if (custom_messages & 2) {
str = read_string(in);
write_string(out, str);
free(str);
}
/* type (SHORT) */
uint16_t type = read_be16(in);
write_le16(out, type);
/* duration (INT) */
uint32_t duration = read_be32(in);
write_le32(out, duration);
/* flags (SHORT) */
uint16_t flags = read_be16(in);
write_le16(out, flags);
/* modifier (INT) */
uint32_t modifier = read_be32(in);
write_le32(out, modifier);
/* location (BYTE) */
*(*out)++ = *(*in)++;
/* 6 bitvectors (5 data + 1 padding) */
for (j = 0; j < 6; j++) {
uint64_t bv = read_be64(in, header->long_size);
write_le64(out, bv);
}
}
stats.affects_converted = count;
if (verbose_mode) {
printf("Affects converted: %u\n", count);
}
return 1;
}
/***************************************************************************
* Items Section Conversion
***************************************************************************/
static int convert_item(unsigned char **in, unsigned char **out,
const pfile_header_t *header, int depth);
static int convert_items_section(unsigned char **in, unsigned char **out,
const pfile_header_t *header) {
uint8_t version;
extern unsigned char *input_end; /* Set by convert_pfile */
if (verbose_mode) {
printf("\n=== Converting Items Section ===\n");
}
/* Version byte */
version = *(*in)++;
if (version != SAV_ITEMVERS) {
fprintf(stderr, "ERROR: Invalid items version %d\n", version);
return 0;
}
*(*out)++ = version;
/* total_item_count (INT) */
uint32_t total_count = read_be32(in);
write_le32(out, total_count);
if (verbose_mode) {
printf("Total items: %u\n", total_count);
}
/* Convert items recursively */
int item_count = 0;
uint8_t peek_flag;
while (1) {
/* Peek at next flag before converting */
peek_flag = **in;
if (!convert_item(in, out, header, 0)) {
return 0;
}
/* If we just processed an EOL at top level, we're done */
if (peek_flag == O_F_EOL) {
break; /* End of top-level items */
}
item_count++;
}
stats.items_converted = item_count;
if (verbose_mode) {
printf("Items converted: %d\n", item_count);
}
return 1;
}
unsigned char *input_end = NULL; /* Buffer boundary check */
static int convert_item(unsigned char **in, unsigned char **out,
const pfile_header_t *header, int depth) {
char *str;
int i;