SfxDump.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "SfxDump.h"
  2. #include "SfxDumpStructures.h"
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define SFX_DUMP_SOUND_COUNT 750
  8. static int read_exact(FILE* file, void* buffer, size_t size){
  9. return fread(buffer, 1, size, file) == size;
  10. }
  11. static int write_exact(FILE* file, const void* buffer, size_t size){
  12. return fwrite(buffer, 1, size, file) == size;
  13. }
  14. static int convert_sound(
  15. FILE* fmt_file,
  16. FILE* data_file,
  17. const FfWav_header* header,
  18. const char* target_dir,
  19. int sound_index
  20. ){
  21. Fmt_chunk format = {0};
  22. Loop_chunk loop = {0};
  23. Riff_header riff = {0};
  24. Data_chunk* data_chunk = NULL;
  25. FILE* output_file = NULL;
  26. char* output_path = NULL;
  27. int result = 1;
  28. size_t dir_length = strlen(target_dir);
  29. int add_separator = dir_length > 0
  30. && target_dir[dir_length - 1] != '/'
  31. && target_dir[dir_length - 1] != '\\';
  32. size_t path_capacity;
  33. uint64_t riff_size;
  34. if (dir_length > SIZE_MAX - 16 - (size_t)add_separator) {
  35. fprintf(stderr, "SfxDump: output directory path is too long.\n");
  36. return 1;
  37. }
  38. path_capacity = dir_length + (size_t)add_separator + 16;
  39. output_path = malloc(path_capacity);
  40. if (!output_path) {
  41. fprintf(stderr, "SfxDump: unable to allocate output path.\n");
  42. return 1;
  43. }
  44. if (snprintf(
  45. output_path, path_capacity, "%s%s%d.wav", target_dir,
  46. add_separator ? "/" : "", sound_index
  47. ) >= (int)path_capacity){
  48. fprintf(stderr, "SfxDump: output path is too long.\n");
  49. goto cleanup;
  50. }
  51. memcpy(format.id, "fmt ", sizeof format.id);
  52. format.size = sizeof format.adpcm;
  53. if (!read_exact(fmt_file, &format.adpcm, sizeof format.adpcm)){
  54. fprintf(stderr, "SfxDump: truncated format record for sound %d.\n", sound_index);
  55. goto cleanup;
  56. }
  57. memcpy(loop.id, "fflp", sizeof loop.id);
  58. loop.size = sizeof loop.start + sizeof loop.end;
  59. loop.start = header->start;
  60. loop.end = header->end;
  61. if ((uint64_t)header->len > (uint64_t)SIZE_MAX - sizeof *data_chunk){
  62. fprintf(stderr, "SfxDump: sound %d is too large.\n", sound_index);
  63. goto cleanup;
  64. }
  65. data_chunk = malloc(sizeof *data_chunk + (size_t)header->len);
  66. if (!data_chunk){
  67. fprintf(stderr, "SfxDump: unable to allocate sound %d.\n", sound_index);
  68. goto cleanup;
  69. }
  70. memcpy(data_chunk->id, "data", sizeof data_chunk->id);
  71. data_chunk->size = header->len;
  72. if (!read_exact(data_file, data_chunk->data, header->len)){
  73. fprintf(stderr, "SfxDump: truncated audio data for sound %d.\n", sound_index);
  74. goto cleanup;
  75. }
  76. memcpy(riff.id, "RIFF", sizeof riff.id);
  77. memcpy(riff.format, "WAVE", sizeof riff.format);
  78. riff_size = sizeof riff.format + sizeof format + sizeof *data_chunk + header->len;
  79. if (header->loop) riff_size += sizeof loop;
  80. if (riff_size > UINT32_MAX){
  81. fprintf(stderr, "SfxDump: WAV file for sound %d is too large.\n", sound_index);
  82. goto cleanup;
  83. }
  84. riff.size = (uint32_t)riff_size;
  85. output_file = fopen(output_path, "wb");
  86. if (!output_file){
  87. fprintf(stderr, "SfxDump: unable to create %s.\n", output_path);
  88. goto cleanup;
  89. }
  90. if (!write_exact(output_file, &riff, sizeof riff)
  91. || !write_exact(output_file, &format, sizeof format)
  92. || !write_exact(output_file, data_chunk, sizeof *data_chunk + header->len)
  93. || (header->loop && !write_exact(output_file, &loop, sizeof loop))){
  94. fprintf(stderr, "SfxDump: failed writing %s.\n", output_path);
  95. goto cleanup;
  96. }
  97. if (fclose(output_file) != 0){
  98. output_file = NULL;
  99. fprintf(stderr, "SfxDump: failed closing %s.\n", output_path);
  100. goto cleanup;
  101. }
  102. output_file = NULL;
  103. result = 0;
  104. cleanup:
  105. if (output_file) fclose(output_file);
  106. free(data_chunk);
  107. free(output_path);
  108. return result;
  109. }
  110. int SfxDumpConvert(const char* fmt_path, const char* data_path, const char* target_dir){
  111. FILE* fmt_file;
  112. FILE* data_file;
  113. int result = 0;
  114. if (!fmt_path || !data_path || !target_dir || target_dir[0] == '\0'){
  115. fprintf(stderr, "SfxDump: invalid conversion path.\n");
  116. return 1;
  117. }
  118. fmt_file = fopen(fmt_path, "rb");
  119. if (!fmt_file){
  120. fprintf(stderr, "SfxDump: unable to open %s.\n", fmt_path);
  121. return 1;
  122. }
  123. data_file = fopen(data_path, "rb");
  124. if (!data_file){
  125. fprintf(stderr, "SfxDump: unable to open %s.\n", data_path);
  126. fclose(fmt_file);
  127. return 1;
  128. }
  129. for (int sound_index = 0; sound_index < SFX_DUMP_SOUND_COUNT; ++sound_index){
  130. FfWav_header header = {0};
  131. size_t header_size = fread(&header, 1, sizeof header, fmt_file);
  132. if (header_size == 0 && feof(fmt_file)) break;
  133. if (header_size != sizeof header){
  134. fprintf(stderr, "SfxDump: truncated format table at sound %d.\n", sound_index);
  135. result = 1;
  136. break;
  137. }
  138. if (!header.len){
  139. if (fseek(fmt_file, (long)sizeof(WAVEFORMATEX), SEEK_CUR) != 0){
  140. fprintf(stderr, "SfxDump: truncated empty format record at sound %d.\n", sound_index);
  141. result = 1;
  142. break;
  143. }
  144. continue;
  145. }
  146. if (convert_sound(fmt_file, data_file, &header, target_dir, sound_index) != 0){
  147. result = 1;
  148. break;
  149. }
  150. }
  151. if (fclose(fmt_file) != 0) result = 1;
  152. if (fclose(data_file) != 0) result = 1;
  153. return result;
  154. }