~ubuntu-branches/debian/lenny/exiv2/lenny

« back to all changes in this revision

Viewing changes to src/exifdata-test.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-06-21 08:23:53 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080621082353-b1n4w08trwfwbfl4
Tags: 0.17.1-1
* New upstream release
  - Library transition cleared on debian-release/ d-d-a
* Version 0.17 also fixes:
  - CVE-2008-2696: DoS via metadata in images (Closes: #486328)
  - crashes when fed with wrong file (Closes: #485670)
* Urgency medium for CVE fix
* debian/patches/gcc4.3.diff unecessary for gcc-4.3
* Add /usr/share/bug/exiv2/presubj message for reportbug(1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ***************************************************************** -*- C++ -*-
2
 
/*
3
 
  Abstract : ExifData assignment and copy construction unit tests
4
 
 
5
 
  File     : exifdata-test.cpp
6
 
  Version  : $Rev: 631 $
7
 
  Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
8
 
  History  : 20-Feb-05, ahu: created
9
 
 
10
 
 */
11
 
// *****************************************************************************
12
 
// included header files
13
 
#include "image.hpp"
14
 
#include "exif.hpp"
15
 
#include <iostream>
16
 
#include <iomanip>
17
 
#include <string>
18
 
#include <cassert>
19
 
 
20
 
void write(const std::string& file, Exiv2::ExifData& ed);
21
 
void print(const std::string& file);
22
 
 
23
 
// *****************************************************************************
24
 
// Main
25
 
int main(int argc, char* const argv[])
26
 
{
27
 
try {
28
 
    if (argc != 2) {
29
 
        std::cout << "Usage: " << argv[0] << " file\n";
30
 
        return 1;
31
 
    }
32
 
    std::string file(argv[1]);
33
 
 
34
 
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
35
 
    assert (image.get() != 0);
36
 
    image->readMetadata();
37
 
 
38
 
    Exiv2::ExifData &ed = image->exifData();
39
 
    if (ed.empty()) {
40
 
        std::string error = file + ": No Exif data found in the file";
41
 
        throw Exiv2::Error(1, error);
42
 
    }
43
 
 
44
 
    std::cout << "Copy construction, non-intrusive changes\n";
45
 
    Exiv2::ExifData ed1(ed);
46
 
    ed1["Exif.Image.DateTime"] = "Sunday, 11am";
47
 
    ed1["Exif.Image.Orientation"] = uint16_t(2);
48
 
    ed1["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am";
49
 
    ed1["Exif.Photo.MeteringMode"] = uint16_t(1);
50
 
    ed1["Exif.Iop.InteroperabilityIndex"] = "123";
51
 
//    ed1["Exif.Thumbnail.Orientation"] = uint16_t(2);
52
 
    write(file, ed1);
53
 
    print(file);
54
 
    std::cout << "----------------------------------------------\n";
55
 
 
56
 
    std::cout << "Copy construction, intrusive changes\n";
57
 
    Exiv2::ExifData ed2(ed);
58
 
    ed2["Exif.Image.DateTime"] = "Sunday, 11am and ten minutes";
59
 
    ed2["Exif.Image.Orientation"] = "2 3 4 5";
60
 
    ed2["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am and ten minutes";
61
 
    ed2["Exif.Photo.MeteringMode"] = "1 2 3 4 5 6";
62
 
    ed2["Exif.Iop.InteroperabilityIndex"] = "1234";
63
 
    ed2["Exif.Thumbnail.Orientation"] = "2 3 4 5 6";
64
 
    write(file, ed2);
65
 
    print(file);
66
 
    std::cout << "----------------------------------------------\n";
67
 
 
68
 
    std::cout << "Assignment, non-intrusive changes\n";
69
 
    Exiv2::ExifData ed3;
70
 
    ed3["Exif.Iop.InteroperabilityVersion"] = "Test 6 Iop tag";
71
 
    ed3["Exif.Thumbnail.Artist"] = "Test 6 Ifd1 tag";
72
 
    ed3 = ed;
73
 
    ed3["Exif.Image.DateTime"] = "Sunday, 11am";
74
 
    ed3["Exif.Image.Orientation"] = uint16_t(2);
75
 
    ed3["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am";
76
 
    ed3["Exif.Photo.MeteringMode"] = uint16_t(1);
77
 
    ed3["Exif.Iop.InteroperabilityIndex"] = "123";
78
 
//    ed3["Exif.Thumbnail.Orientation"] = uint16_t(2);
79
 
    write(file, ed3);
80
 
    print(file);
81
 
    std::cout << "----------------------------------------------\n";
82
 
 
83
 
    std::cout << "Assignment, intrusive changes\n";
84
 
    Exiv2::ExifData ed4;
85
 
    ed4["Exif.Iop.InteroperabilityVersion"] = "Test 6 Iop tag";
86
 
    ed4["Exif.Thumbnail.Artist"] = "Test 6 Ifd1 tag";
87
 
    ed4 = ed;
88
 
    ed4["Exif.Image.DateTime"] = "Sunday, 11am and ten minutes";
89
 
    ed4["Exif.Image.Orientation"] = "2 3 4 5";
90
 
    ed4["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am and ten minutes";
91
 
    ed4["Exif.Photo.MeteringMode"] = uint16_t(1);
92
 
    ed4["Exif.Iop.InteroperabilityIndex"] = "123";
93
 
    ed4["Exif.Thumbnail.Orientation"] = uint16_t(2);
94
 
    write(file, ed4);
95
 
    print(file);
96
 
 
97
 
    return 0;
98
 
}
99
 
catch (Exiv2::AnyError& e) {
100
 
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
101
 
    return -1;
102
 
}
103
 
}
104
 
 
105
 
void write(const std::string& file, Exiv2::ExifData& ed)
106
 
{
107
 
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
108
 
    assert (image.get() != 0);
109
 
    image->setExifData(ed);
110
 
    image->writeMetadata();
111
 
}
112
 
 
113
 
void print(const std::string& file)
114
 
{
115
 
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
116
 
    assert (image.get() != 0);
117
 
    image->readMetadata();
118
 
 
119
 
    Exiv2::ExifData &ed = image->exifData();
120
 
    Exiv2::ExifData::const_iterator end = ed.end();
121
 
    for (Exiv2::ExifData::const_iterator i = ed.begin(); i != end; ++i) {
122
 
        std::cout << std::setw(45) << std::setfill(' ') << std::left
123
 
                  << i->key() << " "
124
 
                  << "0x" << std::setw(4) << std::setfill('0') << std::right
125
 
                  << std::hex << i->tag() << " "
126
 
                  << std::setw(12) << std::setfill(' ') << std::left
127
 
                  << i->ifdName() << " "
128
 
                  << std::setw(9) << std::setfill(' ') << std::left
129
 
                  << i->typeName() << " "
130
 
                  << std::dec << std::setw(3)
131
 
                  << std::setfill(' ') << std::right
132
 
                  << i->count() << " "
133
 
                  << std::dec << i->value()
134
 
                  << "\n";
135
 
    }
136
 
}