trngaje / SDL_drastic

Simple Directmedia Layer
https://libsdl.org
zlib License
0 stars 0 forks source link

hook spu_load_fake_microphone_data #7

Open trngaje opened 1 month ago

trngaje commented 1 month ago

아래 루틴을 통해 wav 파일의 format을 이해하고 샘플 microphone.wav를 생성한다.

void spu_load_fake_microphone_data(long param_1,char *param_2)

{
  FILE *__stream;
  ulong uVar1;
  size_t sVar2;
  void *__ptr;
  undefined8 uVar3;
  long lVar4;
  int local_30;
  uint local_2c;
  int local_28;
  int local_24;
  int local_20;
  int local_18;
  undefined4 local_14;
  short local_a;
  long local_8;

  local_8 = ___stack_chk_guard;
  __stream = fopen(param_2,"rb");
  if (__stream == (FILE *)0x0) {
    uVar3 = 0xffffffff;
  }
  else {
    fseek(__stream,0,2);
    uVar1 = ftell(__stream);
    fseek(__stream,0,0);
    sVar2 = fread(&local_28,4,3,__stream);
    if (sVar2 == 3) {
      if (local_28 == 0x46464952) {
        if (local_24 == (int)uVar1 + -8) {
          if (local_20 == 0x45564157) {
            do {
              sVar2 = fread(&local_30,4,2,__stream);
              if (sVar2 != 2) {
                puts(" ERROR: Incomplete sub-chunk header.");
                goto LAB_0007f438;
              }
              if (local_30 == 0x20746d66) {
                if (local_2c != 0x10) {
                  puts(" ERROR: WAV fmt chunk is incorrectly sized.");
                  goto LAB_0007f438;
                }
                sVar2 = fread(&local_18,4,4,__stream);
                if (sVar2 != 4) {
                  puts(" ERROR: Could not read fmt data from WAV file.");
                  goto LAB_0007f438;
                }
                if (local_18 != 0x10001) {
                  __printf_chk(1," ERROR: WAV must be uncompressed PCM and mono (is %08x).\n");
                  goto LAB_0007f438;
                }
                if (local_a != 0x10) {
                  puts(" ERROR: WAV must be 16 bits per sample.");
                  goto LAB_0007f438;
                }
              }
              else {
                if (local_30 == 0x61746164) {
                  uVar1 = (ulong)local_2c;
                  __ptr = malloc(uVar1);
                  *(void **)(param_1 + 0x40d30) = __ptr;
                  sVar2 = fread(__ptr,uVar1,1,__stream);
                  if (sVar2 == 1) {
                    uVar3 = 0;
                    *(undefined4 *)(param_1 + 0x40d38) = local_14;
                    *(uint *)(param_1 + 0x40d3c) = local_2c >> 1;
                    goto LAB_0007f384;
                  }
                  puts(" ERROR: Could not read WAV data.");
                  free(*(void **)(param_1 + 0x40d30));
                  *(undefined8 *)(param_1 + 0x40d30) = 0;
                  goto LAB_0007f438;
                }
                fseek(__stream,(ulong)local_2c,1);
              }
              lVar4 = ftell(__stream);
            } while (lVar4 < (long)(uVar1 & 0xffffffff));
            puts("ERROR: Did not find data subchunk in WAV file.");
          }
          else {
            puts(" ERROR: WAV file does not have correct Format string.");
          }
        }
        else {
          __printf_chk(1," ERROR: WAV file does not have correct ChunkSize (%d should be %d)\n");
        }
      }
      else {
        puts(" ERROR: WAV file does not have correct ChunkID string.");
      }
    }
    else {
      __printf_chk(1," ERROR: Could not read WAV header from %s.\n",param_2);
    }
LAB_0007f438:
    fclose(__stream);
    uVar3 = 0xffffffff;
  }
LAB_0007f384:
  if (local_8 == ___stack_chk_guard) {
    return;
  }
                    /* WARNING: Subroutine does not return */
  __stack_chk_fail(uVar3);
}
trngaje commented 1 month ago
unsigned long spu_load_fake_microphone_data(long param_1,char *param_2)

{
  FILE *__stream;
  ulong uVar1;
  size_t sVar2;
  void *__ptr;
  unsigned long uVar3;
  long lVar4;
  int local_30;
  uint local_2c;
  int local_28; // fread로 읽혀지는 버퍼 4byte x 3
  int local_24;
  int local_20;
  int local_18;

  undefined4 local_14; // sample rate
  short local_a;
  long local_8;

  //local_8 = ___stack_chk_guard;
  __stream = fopen(param_2,"rb");
  if (__stream == (FILE *)0x0) {
    uVar3 = 0xffffffff;
  }
  else {
    fseek(__stream,0,2); // int fseek(FILE* stream, long int offset, int origin);
    uVar1 = ftell(__stream);
    fseek(__stream,0,0);
    sVar2 = fread(&local_28,4,3,__stream); // size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
    if (sVar2 == 3) {
      header 정보
      // https://crystalcube.co.kr/123

      if (local_28 == 0x46464952) { // "RIFF"
        if (local_24 == (int)uVar1 + -8) {
          if (local_20 == 0x45564157) { // "WAVE"
            do {
              sVar2 = fread(&local_30,4,2,__stream);
              if (sVar2 != 2) {
                puts(" ERROR: Incomplete sub-chunk header.");
                goto LAB_0007f438;
              }
              if (local_30 == 0x20746d66) { // "fmt "
                if (local_2c != 0x10) {
                  puts(" ERROR: WAV fmt chunk is incorrectly sized.");
                  goto LAB_0007f438;
                }
                sVar2 = fread(&local_18,4,4,__stream);
                if (sVar2 != 4) {
                  puts(" ERROR: Could not read fmt data from WAV file.");
                  goto LAB_0007f438;
                }
                if (local_18 != 0x10001) { // audio format (2byte) | numchannels (2byte)
                  __printf_chk(1," ERROR: WAV must be uncompressed PCM and mono (is %08x).\n");
                  goto LAB_0007f438;
                }
                if (local_a != 0x10) { // bit per sample (2byte)
                  puts(" ERROR: WAV must be 16 bits per sample.");
                  goto LAB_0007f438;
                }
              }
              else {
                if (local_30 == 0x61746164) { "data"
                  uVar1 = (ulong)local_2c; // Chunk Size
                  __ptr = malloc(uVar1);
                  *(void **)(param_1 + 0x40d30) = __ptr;
                  sVar2 = fread(__ptr,uVar1,1,__stream);
                  if (sVar2 == 1) {
                    uVar3 = 0;
                    *(undefined4 *)(param_1 + 0x40d38) = local_14;
                    *(uint *)(param_1 + 0x40d3c) = local_2c >> 1;
                    goto LAB_0007f384;
                  }
                  puts(" ERROR: Could not read WAV data.");
                  free(*(void **)(param_1 + 0x40d30));
                  *(undefined8 *)(param_1 + 0x40d30) = 0;
                  goto LAB_0007f438;
                }
                fseek(__stream,(ulong)local_2c,1);
              }
              lVar4 = ftell(__stream);
            } while (lVar4 < (long)(uVar1 & 0xffffffff));
            puts("ERROR: Did not find data subchunk in WAV file.");
          }
          else {
            puts(" ERROR: WAV file does not have correct Format string.");
          }
        }
        else {
          __printf_chk(1," ERROR: WAV file does not have correct ChunkSize (%d should be %d)\n");
        }
      }
      else {
        puts(" ERROR: WAV file does not have correct ChunkID string.");
      }
    }
    else {
      __printf_chk(1," ERROR: Could not read WAV header from %s.\n",param_2);
    }
LAB_0007f438:
    fclose(__stream);
    uVar3 = 0xffffffff;
  }
LAB_0007f384:
  //if (local_8 == ___stack_chk_guard) {
    return uVar3;
  }
                    /* WARNING: Subroutine does not return */
  //__stack_chk_fail(uVar3);
}