~ubuntu-branches/ubuntu/breezy/koffice/breezy-security

« back to all changes in this revision

Viewing changes to lib/kofficecore/koGlobal.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-10-11 14:49:50 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051011144950-lwpngbifzp8nk0ds
Tags: 1:1.4.1-0ubuntu7
* SECURITY UPDATE: fix heap based buffer overflow in the RTF importer of KWord
* Opening specially crafted RTF files in KWord can cause
  execution of abitrary code.
* Add kubuntu_01_rtfimport_heap_overflow.diff
* References:
  CAN-2005-2971
  CESA-2005-005
  http://www.koffice.org/security/advisory-20051011-1.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* This file is part of the KDE project
2
 
   Copyright (C) 2001 David Faure <faure@kde.org>
 
2
   Copyright (C) 2001-2005 David Faure <faure@kde.org>
3
3
   Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
4
4
 
5
5
   This library is free software; you can redistribute it and/or
21
21
#include "config.h"
22
22
#include <koGlobal.h>
23
23
#include <kdebug.h>
24
 
#include <klocale.h>
25
 
#include <kprinter.h>
26
24
#include <qfont.h>
27
25
#include <qfontinfo.h>
28
26
#include <kglobalsettings.h>
29
27
#include <kglobal.h>
 
28
#include <klocale.h>
30
29
#include <ksimpleconfig.h>
31
30
#include <kstandarddirs.h>
32
31
#include <kstaticdeleter.h>
34
33
#include <kiconloader.h>
35
34
#include <kstandarddirs.h>
36
35
 
37
 
struct PageFormatInfo
38
 
{
39
 
    KoFormat format;
40
 
    KPrinter::PageSize kprinter;
41
 
    const char* shortName; // Short name
42
 
    const char* descriptiveName; // Full name, which will be translated
43
 
    double width; // in mm
44
 
    double height; // in mm
45
 
};
46
 
 
47
 
// NOTES:
48
 
// - the width and height of non-ISO formats are rounded
49
 
// - the comments "should be..." indicates the exact values if the inch sizes would be multiplied by 25.4 mm/inch
50
 
 
51
 
const PageFormatInfo pageFormatInfo[]=
52
 
{
53
 
    { PG_DIN_A3,        KPrinter::A3,           "A3",           I18N_NOOP("ISO A3"),       297.0,  420.0 },
54
 
    { PG_DIN_A4,        KPrinter::A4,           "A4",           I18N_NOOP("ISO A4"),       210.0,  297.0 },
55
 
    { PG_DIN_A5,        KPrinter::A5,           "A5",           I18N_NOOP("ISO A5"),       148.0,  210.0 },
56
 
    { PG_US_LETTER,     KPrinter::Letter,       "Letter",       I18N_NOOP("US Letter"),    216.0,  279.0 }, // should be 215.9 mm x 279.4 mm
57
 
    { PG_US_LEGAL,      KPrinter::Legal,        "Legal",        I18N_NOOP("US Legal"),     216.0,  356.0 }, // should be 215.9 mm x 335.6 mm
58
 
    { PG_SCREEN,        KPrinter::A4,           "Screen",       I18N_NOOP("Screen"), PG_A4_HEIGHT, PG_A4_WIDTH }, // Custom, so fall back to A4
59
 
    { PG_CUSTOM,        KPrinter::A4,           "Custom",       I18N_NOOP("Custom"), PG_A4_WIDTH, PG_A4_HEIGHT }, // Custom, so fall back to A4
60
 
    { PG_DIN_B5,        KPrinter::B5,           "B5",           I18N_NOOP("ISO B5"),       182.0,  257.0 },
61
 
    { PG_US_EXECUTIVE,  KPrinter::Executive,    "Executive",    I18N_NOOP("US Executive"), 191.0,  254.0 }, // should be 190.5 mm x 254.0 mm
62
 
    { PG_DIN_A0,        KPrinter::A0,           "A0",           I18N_NOOP("ISO A0"),       841.0, 1189.0 },
63
 
    { PG_DIN_A1,        KPrinter::A1,           "A1",           I18N_NOOP("ISO A1"),       594.0,  841.0 },
64
 
    { PG_DIN_A2,        KPrinter::A2,           "A2",           I18N_NOOP("ISO A2"),       420.0,  594.0 },
65
 
    { PG_DIN_A6,        KPrinter::A6,           "A6",           I18N_NOOP("ISO A6"),       105.0,  148.0 },
66
 
    { PG_DIN_A7,        KPrinter::A7,           "A7",           I18N_NOOP("ISO A7"),        74.0,  105.0 },
67
 
    { PG_DIN_A8,        KPrinter::A8,           "A8",           I18N_NOOP("ISO A8"),        52.0,   74.0 },
68
 
    { PG_DIN_A9,        KPrinter::A9,           "A9",           I18N_NOOP("ISO A9"),        37.0,   52.0 },
69
 
    { PG_DIN_B0,        KPrinter::B0,           "B0",           I18N_NOOP("ISO B0"),      1030.0, 1456.0 },
70
 
    { PG_DIN_B1,        KPrinter::B1,           "B1",           I18N_NOOP("ISO B1"),       728.0, 1030.0 },
71
 
    { PG_DIN_B10,       KPrinter::B10,          "B10",          I18N_NOOP("ISO B10"),       32.0,   45.0 },
72
 
    { PG_DIN_B2,        KPrinter::B2,           "B2",           I18N_NOOP("ISO B2"),       515.0,  728.0 },
73
 
    { PG_DIN_B3,        KPrinter::B3,           "B3",           I18N_NOOP("ISO B3"),       364.0,  515.0 },
74
 
    { PG_DIN_B4,        KPrinter::B4,           "B4",           I18N_NOOP("ISO B4"),       257.0,  364.0 },
75
 
    { PG_DIN_B6,        KPrinter::B6,           "B6",           I18N_NOOP("ISO B6"),       128.0,  182.0 },
76
 
    { PG_ISO_C5,        KPrinter::C5E,          "C5",           I18N_NOOP("ISO C5"),       163.0,  229.0 }, // Some sources tells: 162 mm x 228 mm
77
 
    { PG_US_COMM10,     KPrinter::Comm10E,      "Comm10",       I18N_NOOP("US Common 10"), 105.0,  241.0 }, // should be 104.775 mm x 241.3 mm
78
 
    { PG_ISO_DL,        KPrinter::DLE,          "DL",           I18N_NOOP("ISO DL"),       110.0,  220.0 },
79
 
    { PG_US_FOLIO,      KPrinter::Folio,        "Folio",        I18N_NOOP("US Folio"),     210.0,  330.0 }, // should be 209.54 mm x 330.2 mm
80
 
    { PG_US_LEDGER,     KPrinter::Ledger,       "Ledger",       I18N_NOOP("US Ledger"),    432.0,  279.0 }, // should be 431.8 mm x 297.4 mm
81
 
    { PG_US_TABLOID,    KPrinter::Tabloid,      "Tabloid",      I18N_NOOP("US Tabloid"),   279.0,  432.0 }  // should be 297.4 mm x 431.8 mm
82
 
};
83
 
 
84
 
int KoPageFormat::printerPageSize( KoFormat format )
85
 
{
86
 
    if ( format == PG_SCREEN )
87
 
    {
88
 
            kdWarning() << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE." << endl;
89
 
            return KPrinter::A4;
90
 
    }
91
 
    else if ( format == PG_CUSTOM )
92
 
    {
93
 
            kdWarning() << "The used page layout (CUSTOM) is not supported by KPrinter. Printing in A4." << endl;
94
 
            return KPrinter::A4;
95
 
    }
96
 
    else if ( format <= PG_LAST_FORMAT )
97
 
        return pageFormatInfo[ format ].kprinter;
98
 
    else
99
 
        return KPrinter::A4;
100
 
}
101
 
 
102
 
double KoPageFormat::width( KoFormat format, KoOrientation orientation )
103
 
{
104
 
    if ( orientation == PG_LANDSCAPE )
105
 
        return height( format, PG_PORTRAIT );
106
 
    if ( format <= PG_LAST_FORMAT )
107
 
        return pageFormatInfo[ format ].width;
108
 
    return PG_A4_WIDTH;   // should never happen
109
 
}
110
 
 
111
 
double KoPageFormat::height( KoFormat format, KoOrientation orientation )
112
 
{
113
 
    if ( orientation == PG_LANDSCAPE )
114
 
        return width( format, PG_PORTRAIT );
115
 
    if ( format <= PG_LAST_FORMAT )
116
 
        return pageFormatInfo[ format ].height;
117
 
    return PG_A4_HEIGHT;
118
 
}
119
 
 
120
 
KoFormat KoPageFormat::guessFormat( double width, double height )
121
 
{
122
 
    for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
123
 
    {
124
 
        // We have some tolerance. 1pt is a third of a mm, this is
125
 
        // barely noticeable for a page size.
126
 
        if ( i != PG_CUSTOM
127
 
             && kAbs( width - pageFormatInfo[i].width ) < 1.0
128
 
             && kAbs( height - pageFormatInfo[i].height ) < 1.0 )
129
 
            return static_cast<KoFormat>(i);
130
 
    }
131
 
    return PG_CUSTOM;
132
 
}
133
 
 
134
 
QString KoPageFormat::formatString( KoFormat format )
135
 
{
136
 
    if ( format <= PG_LAST_FORMAT )
137
 
        return QString::fromLatin1( pageFormatInfo[ format ].shortName );
138
 
    return QString::fromLatin1( "A4" );
139
 
}
140
 
 
141
 
KoFormat KoPageFormat::formatFromString( const QString & string )
142
 
{
143
 
    for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
144
 
    {
145
 
        if (string == QString::fromLatin1( pageFormatInfo[ i ].shortName ))
146
 
            return pageFormatInfo[ i ].format;
147
 
    }
148
 
    // We do not know the format name, so we have a custom format
149
 
    return PG_CUSTOM;
150
 
}
151
 
 
152
 
QString KoPageFormat::name( KoFormat format )
153
 
{
154
 
    if ( format <= PG_LAST_FORMAT )
155
 
        return i18n( pageFormatInfo[ format ].descriptiveName );
156
 
    return i18n( pageFormatInfo[ PG_DIN_A4 ].descriptiveName );
157
 
}
158
 
 
159
 
QStringList KoPageFormat::allFormats()
160
 
{
161
 
    QStringList lst;
162
 
    for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
163
 
    {
164
 
        lst << i18n( pageFormatInfo[ i ].descriptiveName );
165
 
    }
166
 
    return lst;
167
 
}
168
36
 
169
37
KoGlobal* KoGlobal::s_global = 0L;
170
38
static KStaticDeleter<KoGlobal> sdg;
189
57
 
190
58
    // Tell the iconloader about share/apps/koffice/icons
191
59
    KGlobal::iconLoader()->addAppDir("koffice");
 
60
 
 
61
    // Another way to get the DPI of the display would be QPaintDeviceMetrics,
 
62
    // but we have no widget here (and moving this to KoView wouldn't allow
 
63
    // using this from the document easily).
 
64
#ifdef Q_WS_X11
 
65
    m_dpiX = QPaintDevice::x11AppDpiX();
 
66
    m_dpiY = QPaintDevice::x11AppDpiY();
 
67
#else
 
68
    m_dpiX = 75;
 
69
    m_dpiY = 75;
 
70
#endif
192
71
}
193
72
 
194
73
KoGlobal::~KoGlobal()
213
92
    return font;
214
93
}
215
94
 
216
 
QStringList KoGlobal::_listTagOfLanguages()
 
95
QStringList KoGlobal::_listOfLanguageTags()
217
96
{
218
 
    if ( m_languageTag.isEmpty() )
 
97
    if ( m_langMap.isEmpty() )
219
98
        createListOfLanguages();
220
 
    return m_languageTag;
 
99
    return m_langMap.values();
221
100
}
222
101
 
223
102
QStringList KoGlobal::_listOfLanguages()
224
103
{
225
 
    if ( m_languageList.empty() )
 
104
    if ( m_langMap.empty() )
226
105
        createListOfLanguages();
227
 
    return m_languageList;
 
106
    return m_langMap.keys();
228
107
}
229
108
 
230
109
void KoGlobal::createListOfLanguages()
231
110
{
232
 
    QStringList alllang = KGlobal::dirs()->findAllResources("locale",
 
111
    KConfig config( "all_languages", true, false, "locale" );
 
112
    // Note that we could also use KLocale::allLanguagesTwoAlpha
 
113
 
 
114
    QMap<QString, bool> seenLanguages;
 
115
    const QStringList langlist = config.groupList();
 
116
    for ( QStringList::ConstIterator itall = langlist.begin();
 
117
          itall != langlist.end(); ++itall )
 
118
    {
 
119
        const QString tag = *itall;
 
120
        config.setGroup( tag );
 
121
        const QString name = config.readEntry("Name", tag);
 
122
        // e.g. name is "French" and tag is "fr"
 
123
 
 
124
        // The QMap does the sorting on the display-name, so that
 
125
        // comboboxes are sorted.
 
126
        m_langMap.insert( name, tag );
 
127
 
 
128
        seenLanguages.insert( tag, true );
 
129
    }
 
130
 
 
131
    // Also take a look at the installed translations.
 
132
    // Many of them are already in all_languages but all_languages doesn't
 
133
    // currently have en_GB or en_US etc.
 
134
 
 
135
    const QStringList translationList = KGlobal::dirs()->findAllResources("locale",
233
136
                                                            QString::fromLatin1("*/entry.desktop"));
234
 
    QStringList langlist=alllang;
235
 
    for ( QStringList::ConstIterator it = langlist.begin();
236
 
          it != langlist.end(); ++it )
 
137
    for ( QStringList::ConstIterator it = translationList.begin();
 
138
          it != translationList.end(); ++it )
237
139
    {
238
 
        KSimpleConfig entry(*it);
239
 
        entry.setGroup("KCM Locale");
240
 
        QString name = entry.readEntry("Name",
241
 
                                       KGlobal::locale()->translate("without name"));
242
 
 
 
140
        // Extract the language tag from the directory name
243
141
        QString tag = *it;
244
142
        int index = tag.findRev('/');
245
143
        tag = tag.left(index);
246
144
        index = tag.findRev('/');
247
145
        tag = tag.mid(index+1);
248
 
        m_languageList.append(name);
249
 
        m_languageTag.append(tag);
 
146
 
 
147
        if ( seenLanguages.find( tag ) == seenLanguages.end() ) {
 
148
            KSimpleConfig entry(*it);
 
149
            entry.setGroup("KCM Locale");
 
150
 
 
151
            const QString name = entry.readEntry("Name", tag);
 
152
            // e.g. name is "US English" and tag is "en_US"
 
153
            m_langMap.insert( name, tag );
 
154
 
 
155
            // enable this if writing a third way of finding languages below
 
156
            //seenLanguages.insert( tag, true );
 
157
        }
 
158
 
250
159
    }
251
160
 
 
161
    // #### We also might not have an entry for a language where spellchecking is supported,
 
162
    //      but no KDE translation is available, like fr_CA.
 
163
    // How to add them?
252
164
}
253
165
 
254
166
QString KoGlobal::tagOfLanguage( const QString & _lang)
255
167
{
256
 
    // Should use iterator...
257
 
    int pos = self()->m_languageList.findIndex( _lang );
258
 
    if ( pos != -1)
259
 
    {
260
 
        return self()->m_languageTag[ pos ];
261
 
    }
 
168
    const LanguageMap& map = self()->m_langMap;
 
169
    QMap<QString,QString>::ConstIterator it = map.find( _lang );
 
170
    if ( it != map.end() )
 
171
        return *it;
262
172
    return QString::null;
263
173
}
264
174
 
265
 
int KoGlobal::languageIndexFromTag( const QString &_lang )
 
175
QString KoGlobal::languageFromTag( const QString &langTag )
266
176
{
267
 
    return self()->m_languageTag.findIndex( _lang );
268
 
}
 
177
    const LanguageMap& map = self()->m_langMap;
 
178
    QMap<QString,QString>::ConstIterator it = map.begin();
 
179
    const QMap<QString,QString>::ConstIterator end = map.end();
 
180
    for ( ; it != end; ++it )
 
181
        if ( it.data() == langTag )
 
182
            return it.key();
269
183
 
270
 
QString KoGlobal::languageFromTag( const QString &_lang )
271
 
{
272
 
    // should use iterator
273
 
    int pos = self()->m_languageTag.findIndex( _lang );
274
 
    if ( pos != -1)
275
 
        return self()->m_languageList[ pos ];
276
 
    else
277
 
        return QString::null;
 
184
    // Language code not found. Better return the code (tag) than nothing.
 
185
    return langTag;
278
186
}
279
187
 
280
188
KConfig* KoGlobal::_kofficeConfig()
284
192
    }
285
193
    return m_kofficeConfig;
286
194
}
 
195
 
 
196
void KoGlobal::setDPI( int x, int y )
 
197
{
 
198
    //kdDebug() << k_funcinfo << x << "," << y << endl;
 
199
    KoGlobal* s = self();
 
200
    s->m_dpiX = x;
 
201
    s->m_dpiY = y;
 
202
}