comparison src/name/blackcap/exifwasher/exiv2/native.cpp @ 2:efd9fe2d70d7

Rationalize exceptions, code whitelist, add command-line tool.
author David Barts <n5jrn@me.com>
date Wed, 01 Apr 2020 14:23:54 -0700
parents 42277ce58ace
children a59d84674fb0
comparison
equal deleted inserted replaced
1:42277ce58ace 2:efd9fe2d70d7
1 #include <jni.h> 1 #include <jni.h>
2 #include <exiv2/exiv2.hpp> 2 #include <exiv2/exiv2.hpp>
3 #include <exception>
3 #include <iostream> 4 #include <iostream>
4 #include <iomanip> 5 #include <iomanip>
5 #include <cassert> 6 #include <cassert>
6 7
7 /* Functions for class name_blackcap_exifwasher_exiv2_Image */ 8 /* Functions for class name_blackcap_exifwasher_exiv2_Image */
28 (JNIEnv *jEnv, jobject jThis, jstring path) { 29 (JNIEnv *jEnv, jobject jThis, jstring path) {
29 const char *cPath = jEnv->GetStringUTFChars(path, NULL); 30 const char *cPath = jEnv->GetStringUTFChars(path, NULL);
30 jlong ret = 0; 31 jlong ret = 0;
31 try { 32 try {
32 ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release()); 33 ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release());
33 } catch (...) { 34 } catch (std::exception& e) {
34 jEnv->ExceptionClear(); 35 jEnv->ExceptionClear();
35 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 36 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
36 const char *pfx = "unable to open "; 37 jEnv->ThrowNew(ex, e.what());
37 char *message = (char *) malloc(strlen(cPath) + strlen(pfx) + 1);
38 strcpy(message, pfx);
39 strcat(message, cPath);
40 jEnv->ThrowNew(ex, message);
41 free(message);
42 } 38 }
43 jEnv->ReleaseStringUTFChars(path, cPath); 39 jEnv->ReleaseStringUTFChars(path, cPath);
44 return ret; 40 return ret;
45 } 41 }
46 42
53 (JNIEnv *jEnv, jobject jThis) { 49 (JNIEnv *jEnv, jobject jThis) {
54 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis)); 50 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
55 if (jEnv->ExceptionCheck()) return; 51 if (jEnv->ExceptionCheck()) return;
56 try { 52 try {
57 image->writeMetadata(); 53 image->writeMetadata();
58 } catch (...) { 54 } catch (std::exception& e) {
59 jEnv->ExceptionClear(); 55 jEnv->ExceptionClear();
60 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 56 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
61 jEnv->ThrowNew(ex, "unable to write metadata"); 57 jEnv->ThrowNew(ex, e.what());
62 } 58 }
63 } 59 }
64 60
65 /* 61 /*
66 * Class: name_blackcap_exifwasher_exiv2_Image 62 * Class: name_blackcap_exifwasher_exiv2_Image
71 (JNIEnv *jEnv, jobject jThis) { 67 (JNIEnv *jEnv, jobject jThis) {
72 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis)); 68 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
73 if (jEnv->ExceptionCheck()) return 0; 69 if (jEnv->ExceptionCheck()) return 0;
74 try { 70 try {
75 image->readMetadata(); 71 image->readMetadata();
76 } catch (...) { 72 } catch (std::exception& e) {
77 jEnv->ExceptionClear(); 73 jEnv->ExceptionClear();
78 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 74 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
79 jEnv->ThrowNew(ex, "unable to read metadata"); 75 jEnv->ThrowNew(ex, e.what());
80 return 0; 76 return 0;
81 } 77 }
82 return reinterpret_cast<jlong> (&(image->exifData())); 78 return reinterpret_cast<jlong> (&(image->exifData()));
83 } 79 }
84 80
116 if (jEnv->ExceptionCheck()) return; 112 if (jEnv->ExceptionCheck()) return;
117 const char *cKey = jEnv->GetStringUTFChars(key, NULL); 113 const char *cKey = jEnv->GetStringUTFChars(key, NULL);
118 Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey))); 114 Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
119 try { 115 try {
120 data->erase(found); 116 data->erase(found);
121 } catch (...) { 117 } catch (std::exception& e) {
122 jEnv->ExceptionClear(); 118 jEnv->ExceptionClear();
123 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 119 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
124 const char *pfx = "unable to delete "; 120 jEnv->ThrowNew(ex, e.what());
125 char *message = (char *) malloc(strlen(cKey) + strlen(pfx) + 1);
126 strcpy(message, pfx);
127 strcat(message, cKey);
128 jEnv->ThrowNew(ex, message);
129 free(message);
130 } 121 }
131 jEnv->ReleaseStringUTFChars(key, cKey); 122 jEnv->ReleaseStringUTFChars(key, cKey);
132 } 123 }
133 124
134 /* 125 /*