GrpcPrint/PrintS/job/JobContent.cpp
2024-03-26 10:33:00 +08:00

174 lines
5.3 KiB
C++

#include "JobContent.h"
JobContent::JobContent()
:m_container_content(NULL)
{
m_Parser = XML_ParserCreate(NULL);
XML_SetElementHandler(m_Parser, StartElement, EndElement);
XML_SetCharacterDataHandler(m_Parser, DataHandler);
}
JobContent::~JobContent()
{
if (m_container_content)delete m_container_content;
}
bool JobContent::LoadFile(string strpath)
{
string filepath = strpath + "/Content.xml";
FILE* file;
struct stat st;
if (!fopen_s(&file, filepath.c_str(), "rb") == 0) {
return false;
}
bool rel = true;
m_file_path = strpath;
if (m_container_content)delete m_container_content;
m_container_content = NULL;
m_container_content = new ContainerContent;
XML_SetUserData(m_Parser, m_container_content);
fstat(_fileno(file), &st);
char* readBuffer = new char[st.st_size];
size_t rsize = fread(readBuffer, 1, st.st_size, file);
if (XML_Parse(m_Parser, readBuffer, rsize, true) == XML_STATUS_ERROR) {
rel = false;
}
fclose(file);
delete[] readBuffer;
XML_ParserFree(m_Parser);
return rel;
}
string JobContent::GetMetadataFileName()
{
if (m_container_content==NULL)return "";
return m_container_content->container_files->metadata_file->file_name;
}
string JobContent::GetBinaryFileName(vector<BPBinary::BFileInfo>& bfiles)
{
if (m_container_content==NULL)return "";
size_t ss = m_file_path.find_last_of("\\");
string path = m_file_path.substr(0, ss);
vector<BinaryFile*> binaryfiles = m_container_content->container_files->binary_files;
for (size_t i = 0; i < binaryfiles.size(); ++i) {
BPBinary::BFileInfo bfi;
bfi.file_name = binaryfiles[i]->file_name;
if (binaryfiles[i]->is_outside_container) {
bfi.file_path = path + "\\" + binaryfiles[i]->file_name;
}
else bfi.file_path = m_file_path + "\\" + binaryfiles[i]->file_name;
bfiles.push_back(bfi);
}
return path+"\\";
}
void XMLCALL JobContent::StartElement(void *userData, const XML_Char *name, const XML_Char **attrs) {
ContainerContent* cc = (ContainerContent*)userData;
string strname(name);
if (strcmp(name, "Version") == 0) {
for (int i = 0; attrs[i] != 0; i += 2)
{
if (strcmp(attrs[i], "Major") == 0 && attrs[i + 1] != 0) {
cc->version->major = stoi(string(attrs[i + 1]));
}
else if (strcmp(attrs[i], "Minor") == 0 && attrs[i + 1] != 0) {
cc->version->minor = stoi(string(attrs[i + 1]));
}
else if (strcmp(attrs[i], "Revision") == 0 && attrs[i + 1] != 0) {
cc->version->revision = stoi(string(attrs[i + 1]));
}
}
}
else if (strcmp(name, "EncryptionStrategy") == 0) {
EncryptionStrategy* es = new EncryptionStrategy();
for (int i = 0; attrs[i] != 0; i += 2)
{
if (strcmp(attrs[i], "Id") == 0 && attrs[i + 1] != 0) {
es->id = string(attrs[i + 1]);
}
else if (strcmp(attrs[i], "Method") == 0 && attrs[i + 1] != 0) {
es->method = string(attrs[i + 1]);
}
}
cc->encryption_strategies[es->id] = es;
cc->parseAssist.lastEncryptionStrategy = es;
}
else if (strcmp(name, "EncryptedContent") == 0) {
EncryptedContent* ec = new EncryptedContent();
for (int i = 0; attrs[i] != 0; i += 2)
{
if (strcmp(attrs[i], "EncryptionScheme") == 0 && attrs[i + 1] != 0) {
ec->encryption_scheme = string(attrs[i + 1]);
}
}
if (cc->parseAssist.lastEncryptionStrategy) {
cc->parseAssist.lastEncryptionStrategy->session_key->encrypted_contents_map[ec->encryption_scheme] = ec;
}
cc->parseAssist.lastEncryptedContent = ec;
}
else if (strcmp(name, "MetadataFile") == 0) {
for (int i = 0; attrs[i] != 0; i += 2)
{
if (strcmp(attrs[i], "FileName") == 0 && attrs[i + 1] != 0) {
cc->container_files->metadata_file->file_name= string(attrs[i + 1]);
}
else if (strcmp(attrs[i], "EncryptionStrategyRef") == 0 && attrs[i + 1] != 0) {
cc->container_files->metadata_file->encryption_strategy_ref = string(attrs[i + 1]);
}
}
}
else if (strcmp(name, "BinaryFile") == 0) {
BinaryFile* bf = new BinaryFile();
for (int i = 0; attrs[i] != 0; i += 2)
{
if (strcmp(attrs[i], "FileName") == 0 && attrs[i + 1] != 0) {
bf->file_name = string(attrs[i + 1]);
}
else if (strcmp(attrs[i], "EncryptionStrategyRef") == 0 && attrs[i + 1] != 0) {
bf->encryption_strategy_ref = string(attrs[i + 1]);
}
else if (strcmp(attrs[i], "IsOutsideContainer") == 0 && attrs[i + 1] != 0){
bf->is_outside_container = (strcmp(attrs[i + 1], "false") == 0) ? false : true;
}
}
cc->container_files->binary_files.push_back(bf);
}
cc->parseAssist.depth++;
cc->parseAssist.depthMap[cc->parseAssist.depth] = strname;
}
void XMLCALL JobContent::EndElement(void *userData, const XML_Char *name)
{
ContainerContent* cc = (ContainerContent*)userData;
if (strcmp(name, "InitializationVector") == 0) {
string str = cc->parseAssist.depthMap[cc->parseAssist.depth - 1];
if (str=="EncryptedContent") {
cc->parseAssist.lastEncryptedContent->initialization_vector = cc->parseAssist.tempValue;
}
else if (str=="EncryptionStrategy") {
cc->parseAssist.lastEncryptionStrategy->initialization_vector = cc->parseAssist.tempValue;
}
}
else if (strcmp(name, "CipherText") == 0) {
cc->parseAssist.lastEncryptedContent->cipher_text = cc->parseAssist.tempValue;
}
cc->parseAssist.depth--;
}
void XMLCALL JobContent::DataHandler(void *userData, const XML_Char *s, int len)
{
ContainerContent* cc = (ContainerContent*)userData;
char buffer[512];
memset(buffer, '\0', 512);
memcpy(buffer, s, len);
cc->parseAssist.tempValue = string(buffer);
}