Guest User

LSB Extraction - C++

a guest
Mar 6th, 2010
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <sys/stat.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdio.h>
  5. using namespace std;
  6. // Developed by: M. Robertson
  7. //Code to derive the Least Significant Bit from a 16bit audio file.
  8. // An example of this code is as follows:
  9. //sokalli2 thaipos: if you have say an array of 8 bytes and a variable bits then
  10. // for (int i=0; i<8; ++i) bits = (bits << 1) | (bytes[i] & 1);
  11. int main ()
  12. {
  13. int dataStart = 44; //This is the 'start' of the data.
  14. // If your header length is 44 bytes, this should be 44,
  15. // due to index start of 0.
  16. int dataEnd = 3000000; //Put this to a particularly high number
  17. //if you don't feel like finding out the filesize.
  18. int byteLength = 0;
  19. char bits;
  20. //Make SURE this file is placed wherever the code is.
  21. fstream myFile("dinosaur_noise.wav", ios::in | ios::out | ios::binary);
  22. ofstream saveFile("dinoNoise_LSB");
  23. char ch;
  24. cout << "Least Significant Bit extractor for 16bit audio files.\n";
  25.  
  26. //TODO: need to put in an if statement to make sure the file exists.
  27.  
  28. //This loop goes through the entire file / dataSet as designated above.
  29. for(int i = dataStart; i < dataEnd; i++){
  30. myFile.seekg(i);//This forces the index of the currently viewed byte
  31. myFile.get(ch); //This pulls the currently viewed byte into the ch variable
  32.  
  33. //From x to the 16th byte, we pull the last bit out of every other byte,
  34. // starting from the x to the 16th byte; skipping
  35. if((i-dataStart+1)%16 == 0){
  36. //cout << endl;
  37. //The byte doesn't necessarily need to be reset,
  38. // but is good practice to prevent strange data occurances.
  39. bits = 0x00;
  40. byteLength++; //Keep a count for how many times we delve in to this.
  41. // This will count the number of bytes total in the program.
  42. for(int j = 15; j >= 0; j--){
  43. j--;
  44. myFile.seekg(i-j);
  45. myFile.get(ch);
  46. //cout << ch << "--"; //Some debug; don't show unless
  47. //cout << (ch&0x01) << endl; //you're only pulling a couple of bytes.
  48. //saveFile << ch; //Also for debugging purposes.
  49.  
  50. //The magic bitwise:
  51. // bits << 1 : Every time a bit is added, move all the bits over 1.
  52. // ch & 1 : Grab the last bit from the char byte.
  53. // | (or) after bit shift and grabbing the last bit adds in a single bit
  54. // to the end of the byte total.
  55. bits = (bits << 1) | (ch & 1);
  56. }
  57. //Save our totaled byte to our file.
  58. saveFile << bits;
  59. }
  60.  
  61. //cout << (ch);
  62.  
  63. //This is to allow a larger number for the for loop than the file.
  64. // If we reach the end of the file, simply break out.
  65. if(myFile.eof()){
  66. cout << ch;
  67. break;
  68. }
  69. }
  70. cout << "\nSuccess. The File is " << byteLength << " bytes long.";
  71. saveFile.close();
  72. myFile.close();
  73. char anything;
  74. cin >> anything;
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment