validate wav file size with wav header

Hello i have problem of validate wav file size with wav header

https://imgur.com/p92l39p

suppose riff_size should match with filelength , but the value in not tally

also is there a faster way to convert little endian to big endian in 16 bits, with not allow using other lib, except iostream , fstream and cstring

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
#include <iostream>
#include <fstream>
#include <cstring>

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

struct WaveHeader {
 char riff_label[4]; // (offset 0) = {‘R’, ‘I’, ‘F’, ‘F’}
 unsigned riff_size; // (offset 4) = 36 + data_size
 char file_tag[4]; // (offset 8) = {‘W’, ‘A’, ‘V’, ‘E’}
 char fmt_label[4]; // (offset 12) = {‘f’, ‘m’,’t’, ’ ‘}
 unsigned fmt_size; // (offset 16) = 16
 unsigned short audio_format; // (offset 20) = 1
 unsigned short channel_count; // (offset 22) = 1 or 2
 unsigned sampling_rate; // (offset 24) = <anything>
 unsigned bytes_per_second; // (offset 28) = <ignore>
 unsigned short bytes_per_sample; // (offset 32) = <ignore>
 unsigned short bits_per_sample; // (offset 34) = 16
 char data_label[4]; // (offset 36) = {‘d’, ‘a’, ‘t’, ‘a’}
 unsigned data_size; // (offset 40) = <# of bytes of data>
};

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

void RunReserveProgram(const char * input , const char * output)
{
  FILE *wavFile=fopen(input, "rb");
  if (wavFile) 
  {
    WaveHeader wavHeader;
    int headerSize = sizeof(wavHeader), filelength = 0;;
    fread(&wavHeader,headerSize,1,wavFile);

    filelength = filesize(input);
    
    std::cout <<wavHeader.data_size <<" " << filelength << std::endl;
    
    
    if(!(wavHeader.file_tag[0] =='W' && wavHeader.file_tag[1] =='A' 
      && wavHeader.file_tag[2] =='V' && wavHeader.file_tag[3] =='E' 
      && wavHeader.riff_size == filelength ))
    {
       fclose(wavFile);
       std::cout << "bad wave file" << std::endl;
       exit(EXIT_FAILURE);
    }
    
    fclose(wavFile);
  }
  else
  {
    std::cout << "unable to open file \'" << input << "\'" << std::endl;
    exit(EXIT_FAILURE);
  }
}

void PrintUsage()
{
  std::cout << "Usage:" << std::endl;
  std::cout << "\treverse <wave in> <wave out> " << std::endl;
  std::cout << "where:" << std::endl;
  std::cout << "\t<wave in> -- input wave file " << std::endl;
  std::cout << "\t<wave out>-- output wave file" << std::endl;
  exit(EXIT_SUCCESS);
}

void ErrorMsg()
{
  std::cout << "incorrect number of arguments "<< std::endl;
  exit(EXIT_FAILURE);
}

int main(int argc, char** argv)
{
  if(argc == 4)
  {
    RunReserveProgram(argv[2],argv[3]);
  }
  else if(argc == 2)
  {
    PrintUsage();
  }
  else
  {
     ErrorMsg();
  }
}


here the source file, include the wav file i trying to read
Last edited on
The size of the file is supposed to be riff_size + 8.
thank it though my doubt of why missing 8 byte
The other 8 bytes are sizeof(riff_label) + sizeof(riff_size).
The first thing I would suggest is commit to using either C or C++.
A casual mix-and-match approach is just a train wreck waiting for happen.
For example, you have both C and C++ file streams open on the same file at the same time.

> also is there a faster way to convert little endian to big endian in 16 bits,
What's faster than 1-line of code containing only 3 operations?

> with not allow using other lib, except iostream , fstream and cstring
No, it means you have to do this for yourself.

Write your own little functions in the style of these, if you need to.
https://linux.die.net/man/3/endian
https://linux.die.net/man/3/htons

Topic archived. No new replies allowed.