Skip to content

Commit

Permalink
chore: podofo
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Jun 24, 2023
1 parent 7a58084 commit 97cf490
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 107 deletions.
4 changes: 2 additions & 2 deletions libs/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ libxml2_dep = dependency('libxml-2.0', required: false)
xml2_dep = dependency('xml2', required: false)
zlib_dep = dependency('zlib')

inc_so = include_directories('pkcs11/src/.', 'shared/src/', '/usr/include/podofo-0.9')
inc_so = include_directories('pkcs11/src/.', 'shared/src/')

inc_a = include_directories('sign-sdk/include', 'sign-sdk/src', 'shared/src/', '/usr/include/podofo-0.9')
inc_a = include_directories('sign-sdk/include', 'sign-sdk/src', 'shared/src/')

cie_pkcs11_sources = [
'shared/src/Util/log.cpp',
Expand Down
21 changes: 10 additions & 11 deletions libs/sign-sdk/include/PdfSignatureGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

#ifndef _PDFSIGNATUREGENERATOR_H_
#define _PDFSIGNATUREGENERATOR_H_
#include <podofo/base/PdfWriter.h>
#include <podofo/doc/PdfMemDocument.h>
#include <podofo/doc/PdfSignOutputDevice.h>
#include <podofo/doc/PdfSignatureField.h>

#include <podofo/podofo.h>

#include <string_view>

#include "Util/UUCByteArray.h"

using namespace PoDoFo;
Expand Down Expand Up @@ -52,11 +51,11 @@ class PdfSignatureGenerator {

void GetBufferForSignature(UUCByteArray& toSign);

void SetSignature(const char* signature, int len);
void SetSignature(const char* signature, size_t len);

void GetSignedPdf(UUCByteArray& signature);

void AddFont(const char* szFontName, const char* szFontPath);
void AddFont(const std::string_view& szFontName, const char* szFontPath);

const double getWidth(int pageIndex);

Expand All @@ -69,15 +68,15 @@ class PdfSignatureGenerator {

PdfWriter* m_pPdfWriter;

PdfSignatureField* m_pSignatureField;
PdfField* m_pSignatureField;

PdfSignOutputDevice* m_pSignOutputDevice;
OutputStreamDevice* m_pSignOutputDevice;

PdfOutputDevice* m_pFinalOutDevice;
OutputStreamDevice* m_pFinalOutDevice;

char* m_pMainDocbuffer;
const char* m_pMainDocbuffer;

char* m_pSignDocbuffer;
const char* m_pSignDocbuffer;

const double lastSignatureY(int left, int bottom);

Expand Down
4 changes: 1 addition & 3 deletions libs/sign-sdk/include/PdfVerifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
#ifndef _PDFVERIFIER_H_
#define _PDFVERIFIER_H_

#include <podofo/doc/PdfSignOutputDevice.h>
#include <podofo/doc/PdfSignatureField.h>
#include <podofo/podofo.h>

#include "Util/UUCByteArray.h"
#include "Sign/disigonsdk.h"
#include "Util/UUCByteArray.h"

using namespace PoDoFo;
using namespace std;
Expand Down
148 changes: 72 additions & 76 deletions libs/sign-sdk/src/PdfSignatureGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

#include "PdfSignatureGenerator.h"
#include <podofo/main/PdfCommon.h>

#include <cmath>

#include "PdfVerifier.h"
#include "UUCLogger.h"
Expand All @@ -33,7 +36,6 @@ PdfSignatureGenerator::PdfSignatureGenerator()
m_pFinalOutDevice(NULL),
m_pMainDocbuffer(NULL),
m_pSignDocbuffer(NULL) {
PoDoFo::PdfError::EnableLogging(false);
}

PdfSignatureGenerator::~PdfSignatureGenerator() {
Expand All @@ -54,12 +56,13 @@ int PdfSignatureGenerator::Load(const char* pdf, int len) {
if (m_pPdfMemDocument) delete m_pPdfMemDocument;

try {
printf("PDF LENGTH");
printf("%i", len);
printf("STOP");
printf("PDF LENGTH\n");
printf("%i\n", len);
printf("STOP\n");
PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::Debug);

m_pPdfMemDocument = new PdfMemDocument();
m_pPdfMemDocument->Load(pdf);
m_pPdfMemDocument->Load(pdf, "");
printf("OK m_pPdfMemDocument");
int nSigns = PDFVerifier::GetNumberOfSignatures(m_pPdfMemDocument);
printf("OK nSigns: %d", nSigns);
Expand All @@ -77,17 +80,14 @@ int PdfSignatureGenerator::Load(const char* pdf, int len) {
}
}

void PdfSignatureGenerator::AddFont(const char* szFontName,
void PdfSignatureGenerator::AddFont(const std::string_view& szFontName,
const char* szFontPath) {
// printf(szFontName);
// printf(szFontPath);

m_pPdfDocument->PoDoFo::PdfDocument::CreateFont(
szFontName, false, PdfEncodingFactory::GlobalWinAnsiEncodingInstance(),
PdfFontCache::eFontCreationFlags_AutoSelectBase14, true);
m_pPdfDocument->PoDoFo::PdfDocument::CreateFont(
szFontName, true, PdfEncodingFactory::GlobalWinAnsiEncodingInstance(),
PdfFontCache::eFontCreationFlags_AutoSelectBase14, true);
m_pPdfDocument->PoDoFo::PdfDocument::GetFonts().GetOrCreateFont(szFontName,
false);
m_pPdfDocument->PoDoFo::PdfDocument::GetFonts().GetOrCreateFont(szFontName,
true);
}

void PdfSignatureGenerator::InitSignature(
Expand Down Expand Up @@ -128,22 +128,23 @@ void PdfSignatureGenerator::InitSignature(

if (m_pSignatureField) delete m_pSignatureField;

PdfPage* pPage = m_pPdfMemDocument->GetPage(pageIndex);
PdfRect cropBox = pPage->GetCropBox();
auto& pPage = m_pPdfMemDocument->GetPages().GetPageAt(pageIndex);

float left0 = left * cropBox.GetWidth();
float bottom0 = cropBox.GetHeight() - (bottom * cropBox.GetHeight());
Rect cropBox = pPage.GetCropBox();

float width0 = width * cropBox.GetWidth();
float height0 = height * cropBox.GetHeight();
float left0 = left * cropBox.Width;
float bottom0 = cropBox.Height - (bottom * cropBox.Height);

float width0 = width * cropBox.Width;
float height0 = height * cropBox.Height;

printf("pdf rect: %f, %f, %f, %f\n", left0, bottom0, width0, height0);

PdfRect rect(left0, bottom0, width0, height0);
Rect rect(left0, bottom0, width0, height0);

LOG_DBG((0, "InitSignature", "PdfSignatureField"));

m_pSignatureField = new PdfSignatureField(pPage, rect, m_pPdfMemDocument);
auto m_pSignatureField = &pPage.CreateField<PdfSignature>(szName, rect);

LOG_DBG((0, "InitSignature", "PdfSignatureField OK"));

Expand All @@ -168,31 +169,22 @@ void PdfSignatureGenerator::InitSignature(

LOG_DBG((0, "InitSignature", "Date OK"));

m_pSignOutputDevice->PdfSignOutputDevice::SetSignatureSize(SIGNATURE_SIZE);
// FIXME
// m_pSignOutputDevice->PdfSignOutputDevice::SetSignatureSize(SIGNATURE_SIZE);

LOG_DBG((0, "InitSignature", "SIGNATURE_SIZE OK"));

// if (width * height > 0) {
// try {
// m_pSignatureField->SetAppearance(szImagePath, szDescription);
// LOG_DBG((0, "InitSignature", "SetAppearance OK"));
// } catch (PdfError& error) {
// LOG_ERR((0, "InitSignature", "SetAppearance error: %s, %s",
// PdfError::ErrorMessage(error.GetError()), error.what()));
// } catch (PdfError* perror) {
// LOG_ERR((0, "InitSignature", "SetAppearance error2: %s, %s",
// PdfError::ErrorMessage(perror->GetError()), perror->what()));
// } catch (std::exception& ex) {
// LOG_ERR(
// (0, "InitSignature", "SetAppearance std exception, %s",
// ex.what()));
// } catch (std::exception* pex) {
// LOG_ERR((0, "InitSignature", "SetAppearance std exception2, %s",
// pex->what()));
// } catch (...) {
// LOG_ERR((0, "InitSignature", "SetAppearance unknown error"));
// }
// }
if (width * height > 0) {
Rect annotSize(0.0, 0.0, rect.Width, rect.Height);
auto sigXObject = m_pPdfDocument->CreateXObjectForm(annotSize);
try {
m_pSignatureField->SetAppearanceStream(*sigXObject);
LOG_DBG((0, "InitSignature", "SetAppearance OK"));
} catch (...) {
LOG_ERR((0, "InitSignature", "SetAppearance error: %s, %s",
PdfError::ErrorMessage(error.GetError()), error.what()));
}
}

// if (szGraphometricData && szGraphometricData[0])
// m_pSignatureField->SetGraphometricData(
Expand All @@ -206,18 +198,21 @@ void PdfSignatureGenerator::InitSignature(
int fulllen = m_actualLen * 2 + SIGNATURE_SIZE * 2;

int mainDoclen = 0;
charbuff outputBuffer;
m_pMainDocbuffer = NULL;
while (!m_pMainDocbuffer) {

while (sizeof(m_pMainDocbuffer) != 0) {
try {
LOG_DBG((0, "InitSignature", "fulllen %d", fulllen));
m_pMainDocbuffer = new char[fulllen];
PdfOutputDevice pdfOutDevice(m_pMainDocbuffer, fulllen);
m_pPdfMemDocument->Write(&pdfOutDevice);
auto input = std::make_shared<SpanStreamDevice>(m_pMainDocbuffer);
BufferStreamDevice pdfOutDevice(outputBuffer);
input->CopyTo(pdfOutDevice);
mainDoclen = pdfOutDevice.GetLength();
} catch (::PoDoFo::PdfError err) {
if (m_pMainDocbuffer) {
} catch (...) {
if (!iszero(m_pMainDocbuffer)) {
// FIXME
delete m_pMainDocbuffer;
m_pMainDocbuffer = NULL;
m_pMainDocbuffer = "";
}

LOG_DBG((0, "PdfError", "what %s", err.what()));
Expand All @@ -232,24 +227,26 @@ void PdfSignatureGenerator::InitSignature(

LOG_DBG((0, "InitSignature", "m_pSignDocbuffer %d", fulllen));

m_pFinalOutDevice = new PdfOutputDevice(m_pSignDocbuffer, fulllen);
m_pSignOutputDevice = new PdfSignOutputDevice(m_pFinalOutDevice);
auto input = std::make_shared<SpanStreamDevice>(m_pSignDocbuffer);
BufferStreamDevice m_pFinalOutDevice(outputBuffer);
input->CopyTo(m_pFinalOutDevice);

LOG_DBG((0, "InitSignature", "buffers OK %d", fulllen));

// imposta la firma
m_pSignOutputDevice->SetSignatureSize(SIGNATURE_SIZE);

LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SIGNATURE_SIZE));
// m_pSignOutputDevice->SetSignatureSize(SIGNATURE_SIZE);

// LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SIGNATURE_SIZE));

// Scrive il documento reale
m_pSignOutputDevice->Write(m_pMainDocbuffer, mainDoclen);

LOG_DBG((0, "InitSignature", "Write OK %d", mainDoclen));

m_pSignOutputDevice->AdjustByteRange();
// m_pSignOutputDevice->AdjustByteRange();

LOG_DBG((0, "InitSignature", "AdjustByteRange OK"));
// LOG_DBG((0, "InitSignature", "AdjustByteRange OK"));
}

void PdfSignatureGenerator::GetBufferForSignature(UUCByteArray& toSign) {
Expand All @@ -260,54 +257,52 @@ void PdfSignatureGenerator::GetBufferForSignature(UUCByteArray& toSign) {

m_pSignOutputDevice->Seek(0);

int nRead = m_pSignOutputDevice->ReadForSignature(buffer, len);
if (nRead == -1) throw nRead;
// int nRead = m_pSignOutputDevice->ReadForSignature(buffer, len);
// if (nRead == -1) throw nRead;

toSign.append((BYTE*)buffer, nRead);
toSign.append((BYTE*)buffer, sizeof(buffer));

delete[] buffer;
}

void PdfSignatureGenerator::SetSignature(const char* signature, int len) {
PdfData signatureData(signature, len);
m_pSignOutputDevice->SetSignature(signatureData);
void PdfSignatureGenerator::SetSignature(const char* signature, size_t len) {
m_pSignOutputDevice->Write(signature, len);
}

void PdfSignatureGenerator::GetSignedPdf(UUCByteArray& signedPdf) {
int finalLength = m_pSignOutputDevice->GetLength();
char* szSignedPdf = new char[finalLength];

m_pSignOutputDevice->Seek(0);
int nRead = m_pSignOutputDevice->Read(szSignedPdf, finalLength);

signedPdf.append((BYTE*)szSignedPdf, nRead);
signedPdf.append((BYTE*)szSignedPdf, sizeof(szSignedPdf));

delete[] szSignedPdf;
}

const double PdfSignatureGenerator::getWidth(int pageIndex) {
if (m_pPdfMemDocument) {
PdfPage* pPage = m_pPdfMemDocument->GetPage(pageIndex);
return pPage->GetPageSize().GetWidth();
auto& pPage = m_pPdfMemDocument->GetPages().GetPageAt(pageIndex);
return pPage.GetMediaBox(false).Width;
}
return 0;
}

const double PdfSignatureGenerator::getHeight(int pageIndex) {
if (m_pPdfMemDocument) {
PdfPage* pPage = m_pPdfMemDocument->GetPage(pageIndex);
return pPage->GetPageSize().GetHeight();
auto& pPage = m_pPdfMemDocument->GetPages().GetPageAt(pageIndex);
return pPage.GetMediaBox(false).Height;
}
return 0;
}

const double PdfSignatureGenerator::lastSignatureY(int left, int bottom) {
if (!m_pPdfMemDocument) return -1;
/// Find the document catalog dictionary
const PdfObject* const trailer = m_pPdfMemDocument->GetTrailer();
if (!trailer->IsDictionary()) return -1;
PdfObject trailer = m_pPdfMemDocument->GetTrailer().GetObject();
if (!trailer.IsDictionary()) return -1;
const PdfObject* const catalogRef =
trailer->GetDictionary().GetKey(PdfName("Root"));
trailer.GetDictionary().GetKey(PdfName("Root"));
if (catalogRef == 0 || !catalogRef->IsReference())
return -2; // throw std::invalid_argument("Invalid /Root entry");
const PdfObject* const catalog =
Expand All @@ -321,8 +316,8 @@ const double PdfSignatureGenerator::lastSignatureY(int left, int bottom) {
catalog->GetDictionary().GetKey(PdfName("AcroForm"));
if (acroFormValue == 0) return bottom;
if (acroFormValue->IsReference())
acroFormValue = m_pPdfMemDocument->GetObjects().GetObject(
acroFormValue->GetReference());
acroFormValue =
m_pPdfMemDocument->GetObjects().GetObject(acroFormValue->GetReference());

if (!acroFormValue->IsDictionary()) return bottom;

Expand All @@ -331,8 +326,8 @@ const double PdfSignatureGenerator::lastSignatureY(int left, int bottom) {
if (fieldsValue == 0) return bottom;

if (fieldsValue->IsReference())
fieldsValue = m_pPdfMemDocument->GetObjects().GetObject(
acroFormValue->GetReference());
fieldsValue =
m_pPdfMemDocument->GetObjects().GetObject(acroFormValue->GetReference());

if (!fieldsValue->IsArray()) return bottom;

Expand All @@ -353,7 +348,7 @@ const double PdfSignatureGenerator::lastSignatureY(int left, int bottom) {
return bottom;
}
PdfArray rectArray = keyRect->GetArray();
PdfRect rect;
Rect rect;
rect.FromArray(rectArray);

if (rect.GetLeft() == left) {
Expand Down Expand Up @@ -389,4 +384,5 @@ bool PdfSignatureGenerator::IsSignatureField(const PdfMemDocument* pDoc,
return true;
else
return false;
return true;
}
Loading

0 comments on commit 97cf490

Please sign in to comment.