Skip to content

Commit

Permalink
file rewad and write
Browse files Browse the repository at this point in the history
  • Loading branch information
k-okawa committed Jul 5, 2021
1 parent 53ca804 commit 00409cf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ project(Steganography)

set(CMAKE_CXX_STANDARD 17)

add_executable(Steganography main.cpp src/utils/argparse.h src/steganography/ISteganography.h src/steganography/JpgStegano.h src/steganography/JpgStegano.cpp)
add_executable(Steganography main.cpp src/utils/argparse.h src/steganography/ISteganography.h src/steganography/JpgStegano.h src/steganography/JpgStegano.cpp src/utils/fileutil.h)
6 changes: 5 additions & 1 deletion src/steganography/JpgStegano.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
// Created by kyohei.okawa on 2021/07/05.
//

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "./JpgStegano.h"
#include "../utils/fileutil.h"

void JpgStegano::Insert(std::string imagePath, std::string str) {

std::vector<std::byte> data = FileUtil::Read(imagePath);
FileUtil::Write(data, imagePath);
}

std::vector<std::byte> JpgStegano::Load(std::string imagePath) {
Expand Down
55 changes: 55 additions & 0 deletions src/utils/fileutil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Created by kyohei.okawa on 2021/07/05.
//

#ifndef STEGANOGRAPHY_FILEUTIL_H
#define STEGANOGRAPHY_FILEUTIL_H

#include <stdio.h>
#include <fstream>
#include <iostream>

namespace FileUtil {

std::vector<std::byte> Read(std::string path) {
std::ifstream ifs(path, std::ios::binary);
if (ifs.fail()) {
std::cerr << "Failed to open file" << std::endl;
return std::vector<std::byte>();
}


ifs.seekg(0, std::ios::end);
long long int size = ifs.tellg();
ifs.seekg(0);

char *data = new char[size];
ifs.read(data, size);
std::vector<std::byte> ret;
for (int i = 0; i < size; i++) {
ret.push_back((std::byte) data[i]);
}
delete[] data;

ifs.close();
return ret;
}

void Write(std::vector<std::byte> data, std::string path) {
std::ofstream fout(path, std::ios::out | std::ios::binary | std::ios::trunc);

if (fout.fail()) {
std::cerr << "Failed to open file" << std::endl;
return;
}

for(int i = 0; i < data.size(); i++) {
fout.write((char *)&data[i], sizeof(std::byte));
}

fout.close();
}

}

#endif //STEGANOGRAPHY_FILEUTIL_H

0 comments on commit 00409cf

Please sign in to comment.