Commit a9df3f32 authored by Marco Pesenti Gritti's avatar Marco Pesenti Gritti

2005-09-16 Marco Pesenti Gritti <mpg@redhat.com>

        * goo/Makefile.am:
        * poppler/DCTStream.h:
        * poppler/Decrypt.cc:
        * poppler/Decrypt.h:
        * poppler/FlateStream.h:
        * poppler/GlobalParams.cc:
        * poppler/GlobalParams.h:
        * poppler/Makefile.am:
        * poppler/PDFDoc.cc:
        * poppler/PDFDoc.h:
        * poppler/Parser.cc:
        * poppler/Parser.h:
        * poppler/Stream.cc:
        * poppler/Stream.h:
        * poppler/XRef.cc:
        * poppler/XRef.h:
        * poppler/poppler-config.h.in:

        Merge security plugins support from xpdf 3.01
parent a777e5c2
2005-09-16 Marco Pesenti Gritti <mpg@redhat.com>
* goo/Makefile.am:
* poppler/DCTStream.h:
* poppler/Decrypt.cc:
* poppler/Decrypt.h:
* poppler/FlateStream.h:
* poppler/GlobalParams.cc:
* poppler/GlobalParams.h:
* poppler/Makefile.am:
* poppler/PDFDoc.cc:
* poppler/PDFDoc.h:
* poppler/Parser.cc:
* poppler/Parser.h:
* poppler/Stream.cc:
* poppler/Stream.h:
* poppler/XRef.cc:
* poppler/XRef.h:
* poppler/poppler-config.h.in:
Merge security plugins support from xpdf 3.01
2005-09-16 Marco Pesenti Gritti <mpg@redhat.com>
* configure.ac:
......@@ -6,6 +28,8 @@
* splash/SplashFTFont.cc:
* splash/SplashMath.h:
* splash/SplashTypes.h:
* goo/FixedPoint.cc:
* goo/FixedPoint.h:
Merge support for fixed point
......
//========================================================================
//
// FixedPoint.cc
//
// Fixed point type, with C++ operators.
//
// Copyright 2004 Glyph & Cog, LLC
//
//========================================================================
#include <config.h>
#if USE_FIXEDPOINT
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include "FixedPoint.h"
FixedPoint FixedPoint::sqrt(FixedPoint x) {
FixedPoint y0, y1, z;
if (x.val <= 0) {
y1.val = 0;
} else {
y1.val = x.val >> 1;
do {
y0.val = y1.val;
z = x / y0;
y1.val = (y0.val + z.val) >> 1;
} while (::abs(y0.val - y1.val) > 1);
}
return y1;
}
//~ this is not very accurate
FixedPoint FixedPoint::pow(FixedPoint x, FixedPoint y) {
FixedPoint t, t2, lnx0, lnx, z0, z;
int d, i;
if (y.val <= 0) {
z.val = 0;
} else {
// y * ln(x)
t = (x - 1) / (x + 1);
t2 = t * t;
d = 1;
lnx = 0;
do {
lnx0 = lnx;
lnx += t / d;
t *= t2;
d += 2;
} while (::abs(lnx.val - lnx0.val) > 2);
lnx.val <<= 1;
t = y * lnx;
// exp(y * ln(x))
t2 = t;
d = 1;
i = 1;
z = 1;
do {
z0 = z;
z += t2 / d;
t2 *= t;
++i;
d *= i;
} while (::abs(z.val - z0.val) > 2 && d < (1 << fixptShift));
}
return z;
}
int FixedPoint::mul(int x, int y) {
#if 1 //~tmp
return ((FixPtInt64)x * y) >> fixptShift;
#else
int ah0, ah, bh, al, bl;
ah0 = x & fixptMaskH;
ah = x >> fixptShift;
al = x - ah0;
bh = y >> fixptShift;
bl = y - (bh << fixptShift);
return ah0 * bh + ah * bl + al * bh + ((al * bl) >> fixptShift);
#endif
}
int FixedPoint::div(int x, int y) {
#if 1 //~tmp
return ((FixPtInt64)x << fixptShift) / y;
#else
#endif
}
#endif // USE_FIXEDPOINT
//========================================================================
//
// FixedPoint.h
//
// Fixed point type, with C++ operators.
//
// Copyright 2004 Glyph & Cog, LLC
//
//========================================================================
#ifndef FIXEDPOINT_H
#define FIXEDPOINT_H
#include <aconf.h>
#if USE_FIXEDPOINT
#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif
#include <stdio.h>
#include <stdlib.h>
#define fixptShift 16
#define fixptMaskL ((1 << fixptShift) - 1)
#define fixptMaskH (~fixptMaskL)
typedef long long FixPtInt64;
class FixedPoint {
public:
FixedPoint() { val = 0; }
FixedPoint(const FixedPoint &x) { val = x.val; }
FixedPoint(double x) { val = (int)(x * (1 << fixptShift) + 0.5); }
FixedPoint(int x) { val = x << fixptShift; }
FixedPoint(long x) { val = x << fixptShift; }
operator float()
{ return (float) val * ((float)1 / (float)(1 << fixptShift)); }
operator double()
{ return (double) val * (1.0 / (double)(1 << fixptShift)); }
operator int()
{ return val >> fixptShift; }
int getRaw() { return val; }
FixedPoint operator =(FixedPoint x) { val = x.val; return *this; }
int operator ==(FixedPoint x) { return val == x.val; }
int operator ==(double x) { return *this == (FixedPoint)x; }
int operator ==(int x) { return *this == (FixedPoint)x; }
int operator ==(long x) { return *this == (FixedPoint)x; }
int operator !=(FixedPoint x) { return val != x.val; }
int operator !=(double x) { return *this != (FixedPoint)x; }
int operator !=(int x) { return *this != (FixedPoint)x; }
int operator !=(long x) { return *this != (FixedPoint)x; }
int operator <(FixedPoint x) { return val < x.val; }
int operator <(double x) { return *this < (FixedPoint)x; }
int operator <(int x) { return *this < (FixedPoint)x; }
int operator <(long x) { return *this < (FixedPoint)x; }
int operator <=(FixedPoint x) { return val <= x.val; }
int operator <=(double x) { return *this <= (FixedPoint)x; }
int operator <=(int x) { return *this <= (FixedPoint)x; }
int operator <=(long x) { return *this <= (FixedPoint)x; }
int operator >(FixedPoint x) { return val > x.val; }
int operator >(double x) { return *this > (FixedPoint)x; }
int operator >(int x) { return *this > (FixedPoint)x; }
int operator >(long x) { return *this > (FixedPoint)x; }
int operator >=(FixedPoint x) { return val >= x.val; }
int operator >=(double x) { return *this >= (FixedPoint)x; }
int operator >=(int x) { return *this >= (FixedPoint)x; }
int operator >=(long x) { return *this >= (FixedPoint)x; }
FixedPoint operator -() { return make(-val); }
FixedPoint operator +(FixedPoint x) { return make(val + x.val); }
FixedPoint operator +(double x) { return *this + (FixedPoint)x; }
FixedPoint operator +(int x) { return *this + (FixedPoint)x; }
FixedPoint operator +(long x) { return *this + (FixedPoint)x; }
FixedPoint operator +=(FixedPoint x) { val = val + x.val; return *this; }
FixedPoint operator +=(double x) { return *this += (FixedPoint)x; }
FixedPoint operator +=(int x) { return *this += (FixedPoint)x; }
FixedPoint operator +=(long x) { return *this += (FixedPoint)x; }
FixedPoint operator -(FixedPoint x) { return make(val - x.val); }
FixedPoint operator -(double x) { return *this - (FixedPoint)x; }
FixedPoint operator -(int x) { return *this - (FixedPoint)x; }
FixedPoint operator -(long x) { return *this - (FixedPoint)x; }
FixedPoint operator -=(FixedPoint x) { val = val - x.val; return *this; }
FixedPoint operator -=(double x) { return *this -= (FixedPoint)x; }
FixedPoint operator -=(int x) { return *this -= (FixedPoint)x; }
FixedPoint operator -=(long x) { return *this -= (FixedPoint)x; }
FixedPoint operator *(FixedPoint x) { return make(mul(val, x.val)); }
FixedPoint operator *(double x) { return *this * (FixedPoint)x; }
FixedPoint operator *(int x) { return *this * (FixedPoint)x; }
FixedPoint operator *(long x) { return *this * (FixedPoint)x; }
FixedPoint operator *=(FixedPoint x) { val = mul(val, x.val); return *this; }
FixedPoint operator *=(double x) { return *this *= (FixedPoint)x; }
FixedPoint operator *=(int x) { return *this *= (FixedPoint)x; }
FixedPoint operator *=(long x) { return *this *= (FixedPoint)x; }
FixedPoint operator /(FixedPoint x) { return make(div(val, x.val)); }
FixedPoint operator /(double x) { return *this / (FixedPoint)x; }
FixedPoint operator /(int x) { return *this / (FixedPoint)x; }
FixedPoint operator /(long x) { return *this / (FixedPoint)x; }
FixedPoint operator /=(FixedPoint x) { val = div(val, x.val); return *this; }
FixedPoint operator /=(double x) { return *this /= (FixedPoint)x; }
FixedPoint operator /=(int x) { return *this /= (FixedPoint)x; }
FixedPoint operator /=(long x) { return *this /= (FixedPoint)x; }
static FixedPoint abs(FixedPoint x) { return make(::abs(x.val)); }
static int floor(FixedPoint x) { return x.val >> fixptShift; }
static int ceil(FixedPoint x)
{ return (x.val & fixptMaskL) ? ((x.val >> fixptShift) + 1)
: (x.val >> fixptShift); }
static int round(FixedPoint x)
{ return (x.val + (1 << (fixptShift - 1))) >> fixptShift; }
static FixedPoint sqrt(FixedPoint x);
static FixedPoint pow(FixedPoint x, FixedPoint y);
private:
static FixedPoint make(int valA) { FixedPoint x; x.val = valA; return x; }
static int mul(int x, int y);
static int div(int x, int y);
int val; // 16.16 fixed point
};
#endif // USE_FIXEDPOINT
#endif
......@@ -10,7 +10,8 @@ poppler_goo_include_HEADERS = \
gmem.h \
gtypes.h \
gmem.h \
gfile.h
gfile.h \
FixedPoint.h
libgoo_la_SOURCES = \
gfile.cc \
......@@ -19,4 +20,5 @@ libgoo_la_SOURCES = \
GooList.cc \
GooTimer.cc \
GooString.cc \
gmem.c
gmem.c \
FixedPoint.cc
......@@ -32,9 +32,7 @@
#include "poppler-config.h"
#include "Error.h"
#include "Object.h"
#ifndef NO_DECRYPTION
#include "Decrypt.h"
#endif
#include "Stream.h"
extern "C" {
......
......@@ -66,7 +66,8 @@ GBool Decrypt::makeFileKey(int encVersion, int encRevision, int keyLength,
GooString *ownerKey, GooString *userKey,
int permissions, GooString *fileID,
GooString *ownerPassword, GooString *userPassword,
Guchar *fileKey, GBool *ownerPasswordOk) {
Guchar *fileKey, GBool encryptMetadata,
GBool *ownerPasswordOk) {
Guchar test[32], test2[32];
GooString *userPassword2;
Guchar fState[256];
......@@ -111,7 +112,8 @@ GBool Decrypt::makeFileKey(int encVersion, int encRevision, int keyLength,
}
userPassword2 = new GooString((char *)test2, 32);
if (makeFileKey2(encVersion, encRevision, keyLength, ownerKey, userKey,
permissions, fileID, userPassword2, fileKey)) {
permissions, fileID, userPassword2, fileKey,
encryptMetadata)) {
*ownerPasswordOk = gTrue;
delete userPassword2;
return gTrue;
......@@ -121,13 +123,15 @@ GBool Decrypt::makeFileKey(int encVersion, int encRevision, int keyLength,
// try using the supplied user password
return makeFileKey2(encVersion, encRevision, keyLength, ownerKey, userKey,
permissions, fileID, userPassword, fileKey);
permissions, fileID, userPassword, fileKey,
encryptMetadata);
}
GBool Decrypt::makeFileKey2(int encVersion, int encRevision, int keyLength,
GooString *ownerKey, GooString *userKey,
int permissions, GooString *fileID,
GooString *userPassword, Guchar *fileKey) {
GooString *userPassword, Guchar *fileKey,
GBool encryptMetadata) {
Guchar *buf;
Guchar test[32];
Guchar fState[256];
......@@ -137,7 +141,7 @@ GBool Decrypt::makeFileKey2(int encVersion, int encRevision, int keyLength,
GBool ok;
// generate file key
buf = (Guchar *)gmalloc(68 + fileID->getLength());
buf = (Guchar *)gmalloc(72 + fileID->getLength());
if (userPassword) {
len = userPassword->getLength();
if (len < 32) {
......@@ -155,7 +159,14 @@ GBool Decrypt::makeFileKey2(int encVersion, int encRevision, int keyLength,
buf[66] = (permissions >> 16) & 0xff;
buf[67] = (permissions >> 24) & 0xff;
memcpy(buf + 68, fileID->getCString(), fileID->getLength());
md5(buf, 68 + fileID->getLength(), fileKey);
len = 68 + fileID->getLength();
if (!encryptMetadata) {
buf[len++] = 0xff;
buf[len++] = 0xff;
buf[len++] = 0xff;
buf[len++] = 0xff;
}
md5(buf, len, fileKey);
if (encRevision == 3) {
for (i = 0; i < 50; ++i) {
md5(fileKey, keyLength, fileKey);
......
......@@ -41,14 +41,16 @@ public:
GooString *ownerKey, GooString *userKey,
int permissions, GooString *fileID,
GooString *ownerPassword, GooString *userPassword,
Guchar *fileKey, GBool *ownerPasswordOk);
Guchar *fileKey, GBool encryptMetadata,
GBool *ownerPasswordOk);
private:
static GBool makeFileKey2(int encVersion, int encRevision, int keyLength,
GooString *ownerKey, GooString *userKey,
int permissions, GooString *fileID,
GooString *userPassword, Guchar *fileKey);
GooString *userPassword, Guchar *fileKey,
GBool encryptMetadata);
int objKeyLength;
Guchar objKey[21];
......
......@@ -32,9 +32,7 @@
#include "poppler-config.h"
#include "Error.h"
#include "Object.h"
#ifndef NO_DECRYPTION
#include "Decrypt.h"
#endif
#include "Stream.h"
extern "C" {
......
......@@ -15,6 +15,14 @@
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#ifdef ENABLE_PLUGINS
# ifndef WIN32
# include <dlfcn.h>
# endif
#endif
#ifdef WIN32
# include <shlobj.h>
#endif
#if HAVE_PAPER_H
#include <paper.h>
#endif
......@@ -31,6 +39,9 @@
#include "CMap.h"
#include "BuiltinFontTables.h"
#include "FontEncodingTables.h"
#ifdef ENABLE_PLUGINS
# include "XpdfPluginAPI.h"
#endif
#include "GlobalParams.h"
#include "GfxFont.h"
......@@ -58,6 +69,12 @@
#include "UnicodeMapTables.h"
#include "UTF8.h"
#ifdef ENABLE_PLUGINS
# ifdef WIN32
extern XpdfPluginVecTable xpdfPluginVecTable;
# endif
#endif
//------------------------------------------------------------------------
#define cidToUnicodeCacheSize 4
......@@ -121,6 +138,148 @@ PSFontParam::~PSFontParam() {
}
}
#ifdef ENABLE_PLUGINS
//------------------------------------------------------------------------
// Plugin
//------------------------------------------------------------------------
class Plugin {
public:
static Plugin *load(char *type, char *name);
~Plugin();
private:
#ifdef WIN32
Plugin(HMODULE libA);
HMODULE lib;
#else
Plugin(void *dlA);
void *dl;
#endif
};
Plugin *Plugin::load(char *type, char *name) {
GString *path;
Plugin *plugin;
XpdfPluginVecTable *vt;
XpdfBool (*xpdfInitPlugin)(void);
#ifdef WIN32
HMODULE libA;
#else
void *dlA;
#endif
path = globalParams->getBaseDir();
appendToPath(path, "plugins");
appendToPath(path, type);
appendToPath(path, name);
#ifdef WIN32
path->append(".dll");
if (!(libA = LoadLibrary(path->getCString()))) {
error(-1, "Failed to load plugin '%s'",
path->getCString());
goto err1;
}
if (!(vt = (XpdfPluginVecTable *)
GetProcAddress(libA, "xpdfPluginVecTable"))) {
error(-1, "Failed to find xpdfPluginVecTable in plugin '%s'",
path->getCString());
goto err2;
}
#else
//~ need to deal with other extensions here
path->append(".so");
if (!(dlA = dlopen(path->getCString(), RTLD_NOW))) {
error(-1, "Failed to load plugin '%s': %s",
path->getCString(), dlerror());
goto err1;
}
if (!(vt = (XpdfPluginVecTable *)dlsym(dlA, "xpdfPluginVecTable"))) {
error(-1, "Failed to find xpdfPluginVecTable in plugin '%s'",
path->getCString());
goto err2;
}
#endif
if (vt->version != xpdfPluginVecTable.version) {
error(-1, "Plugin '%s' is wrong version", path->getCString());
goto err2;
}
memcpy(vt, &xpdfPluginVecTable, sizeof(xpdfPluginVecTable));
#ifdef WIN32
if (!(xpdfInitPlugin = (XpdfBool (*)(void))
GetProcAddress(libA, "xpdfInitPlugin"))) {
error(-1, "Failed to find xpdfInitPlugin in plugin '%s'",
path->getCString());
goto err2;
}
#else
if (!(xpdfInitPlugin = (XpdfBool (*)(void))dlsym(dlA, "xpdfInitPlugin"))) {
error(-1, "Failed to find xpdfInitPlugin in plugin '%s'",
path->getCString());
goto err2;
}
#endif
if (!(*xpdfInitPlugin)()) {
error(-1, "Initialization of plugin '%s' failed",
path->getCString());
goto err2;
}
#ifdef WIN32
plugin = new Plugin(libA);
#else
plugin = new Plugin(dlA);
#endif
delete path;
return plugin;
err2:
#ifdef WIN32
FreeLibrary(libA);
#else
dlclose(dlA);
#endif
err1:
delete path;
return NULL;
}
#ifdef WIN32
Plugin::Plugin(HMODULE libA) {
lib = libA;
}
#else
Plugin::Plugin(void *dlA) {
dl = dlA;
}
#endif
Plugin::~Plugin() {
void (*xpdfFreePlugin)(void);
#ifdef WIN32
if ((xpdfFreePlugin = (void (*)(void))
GetProcAddress(lib, "xpdfFreePlugin"))) {
(*xpdfFreePlugin)();
}
FreeLibrary(lib);
#else
if ((xpdfFreePlugin = (void (*)(void))dlsym(dl, "xpdfFreePlugin"))) {
(*xpdfFreePlugin)();
}
dlclose(dl);
#endif
}
#endif // ENABLE_PLUGINS
//------------------------------------------------------------------------
// parsing
//------------------------------------------------------------------------
......@@ -224,6 +383,11 @@ GlobalParams::GlobalParams(char *cfgFileName) {
unicodeMapCache = new UnicodeMapCache();
cMapCache = new CMapCache();
#ifdef ENABLE_PLUGINS
plugins = new GList();
securityHandlers = new GList();
#endif
// set up the initial nameToUnicode table
for (i = 0; nameToUnicodeTab[i].name; ++i) {
nameToUnicode->add(nameToUnicodeTab[i].name, nameToUnicodeTab[i].u);
......@@ -816,6 +980,11 @@ GlobalParams::~GlobalParams() {
delete unicodeMapCache;
delete cMapCache;
#ifdef ENABLE_PLUGINS
delete securityHandlers;
deleteGList(plugins, Plugin);
#endif
#if MULTITHREADED
gDestroyMutex(&mutex);
gDestroyMutex(&unicodeMapCacheMutex);
......@@ -1741,3 +1910,63 @@ void GlobalParams::setErrQuiet(GBool errQuietA) {
errQuiet = errQuietA;
unlockGlobalParams;
}
void GlobalParams::addSecurityHandler(XpdfSecurityHandler *handler) {
#ifdef ENABLE_PLUGINS
lockGlobalParams;
securityHandlers->append(handler);
unlockGlobalParams;
#endif
}
XpdfSecurityHandler *GlobalParams::getSecurityHandler(char *name) {
#ifdef ENABLE_PLUGINS
XpdfSecurityHandler *hdlr;
int i;
lockGlobalParams;
for (i = 0; i < securityHandlers->getLength(); ++i) {
hdlr = (XpdfSecurityHandler *)securityHandlers->get(i);
if (!stricmp(hdlr->name, name)) {
unlockGlobalParams;
return hdlr;
}
}
unlockGlobalParams;
if (!loadPlugin("security", name)) {
return NULL;
}
lockGlobalParams;
for (i = 0; i < securityHandlers->getLength(); ++i) {
hdlr = (XpdfSecurityHandler *)securityHandlers->get(i);
if (!strcmp(hdlr->name, name)) {
unlockGlobalParams;
return hdlr;
}
}
unlockGlobalParams;
#endif
return NULL;
}
#ifdef ENABLE_PLUGINS
//------------------------------------------------------------------------
// plugins
//------------------------------------------------------------------------
GBool GlobalParams::loadPlugin(char *type, char *name) {
Plugin *plugin;
if (!(plugin = Plugin::load(type, name))) {
return gFalse;
}
lockGlobalParams;
plugins->append(plugin);
unlockGlobalParams;
return gTrue;
}
#endif // ENABLE_PLUGINS
......@@ -33,6 +33,7 @@ class UnicodeMap;
class UnicodeMapCache;
class CMap;
class CMapCache;
struct XpdfSecurityHandler;
class GlobalParams;
class GfxFont;