fork download
  1. /******************************************************************************
  2.  
  3.   Online C++ Compiler.
  4.   Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <map>
  13.  
  14. int char2int(char input){
  15. if(input >= '0' && input <= '9')
  16. return input - '0';
  17. if(input >= 'A' && input <= 'F')
  18. return input - 'A' + 10;
  19. if(input >= 'a' && input <= 'f')
  20. return input - 'a' + 10;
  21. return -1;
  22. }
  23.  
  24. void hex2bin(const char* src, char* target){
  25. while(*src && src[1]){
  26. *(target++) = char2int(*src)*16 + char2int(src[1]);
  27. src += 2;
  28. }
  29. }
  30.  
  31. bool mct2binary(const std::string& inputFile, const std::string& outputFile){
  32. std::ifstream in(inputFile.c_str());
  33. std::ofstream out(outputFile.c_str(), std::ios::binary);
  34. if(in && out){
  35. std::string line;
  36. char buffer[16];
  37. while(!in.eof()){
  38. std::getline(in, line);
  39. if(line[0]!='+' && line!=""){
  40. hex2bin(line.c_str(), buffer);
  41. out.write(buffer, 16);
  42. }
  43. }
  44. return true;
  45. }
  46. return false;
  47. }
  48.  
  49. bool binary2mct(const std::string& inputFile, const std::string& outputFile){
  50. std::ifstream in(inputFile.c_str(), std::ios::binary);
  51. std::ofstream out(outputFile.c_str());
  52. if(in && out){
  53. char buffer[16];
  54. for(int i=0 ; i<16 ; i++){
  55. out << "+Sector: " << std::dec << i << std::endl;
  56. for(int j=0 ; j<4 ; j++){
  57. in.read(buffer, 16);
  58. for(int k=0 ; k<16 ; k++){
  59. out << std::setfill('0') << std::setw(2) << std::hex << (int) (unsigned char) buffer[k];
  60. }
  61. out << std::endl;
  62. }
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68.  
  69. std::map<std::string, std::string> getArgs(int argc, char* argv[]){
  70. std::map<std::string, std::string> args;
  71. for(int i=1 ; i<argc ; i++){
  72. if(i+1<argc && argv[i+1][0]!='-'){
  73. args[argv[i]] = argv[i+1];
  74. } else{
  75. args[argv[i]] = "";
  76. }
  77. }
  78. return args;
  79. }
  80.  
  81. bool has(const std::map<std::string, std::string>& args, const std::string& flag){
  82. return args.find(flag) != args.end();
  83. }
  84.  
  85. void printHelp(){
  86. std::cout << "Converts Mifare Classic Tool dumps into LibNfc dumps and vice-versa." << std::endl << std::endl;
  87. std::cout << "PARAMETERS :" << std::endl;
  88. std::cout << "\t-f \t:\tfile to convert" << std::endl;
  89. std::cout << "\t-bin \t:\tconvert to binary format (libnfc)" << std::endl;
  90. std::cout << "\t-mct \t:\tconvert to Mifare Classic Tool format (Android App)" << std::endl;
  91. std::cout << "OPTIONS :" << std::endl;
  92. std::cout << "\t-o \t:\toutput file - default to ($input)_{bin|mct}" << std::endl;
  93. }
  94.  
  95. int main(int argc, char* argv[]){
  96. std::map<std::string, std::string> args = getArgs(argc, argv);
  97. if(has(args, "-f") && has(args, "-bin")){
  98. std::string inputFile = args["-f"];
  99. std::string outputFile = has(args, "-o") ? args["-o"] : inputFile+"_bin";
  100. bool done = mct2binary(inputFile, outputFile);
  101. std::cout << (done ? "Success." : "Error. Could not read input file.") << std::endl;
  102. }
  103. else if(has(args, "-f") && has(args, "-mct")){
  104. std::string inputFile = args["-f"];
  105. std::string outputFile = has(args, "-o") ? args["-o"] : inputFile+"_mct";
  106. bool done = binary2mct(inputFile, outputFile);
  107. std::cout << (done ? "Success." : "Error. Could not read input file.") << std::endl;
  108. }
  109. else{
  110. printHelp();
  111. }
  112. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Converts Mifare Classic Tool dumps into LibNfc dumps and vice-versa.

PARAMETERS :
	-f 	:	file to convert
	-bin 	:	convert to binary format (libnfc)
	-mct 	:	convert to Mifare Classic Tool format (Android App)
OPTIONS :
	-o 	:	output file - default to ($input)_{bin|mct}