Spaces:
Running
Running
File size: 11,347 Bytes
5cee033 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
/* poppler-attachment.cc: glib wrapper for poppler
* Copyright (C) 2006, Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <cerrno>
#include <goo/gfile.h>
#include "poppler.h"
#include "poppler-private.h"
#include <new>
/**
* SECTION:poppler-attachment
* @short_description: Attachments
* @title: PopplerAttachment
*/
/* FIXME: We need to add gettext support sometime */
#define _(x) (x)
struct PopplerAttachmentPrivate
{
Object obj_stream {};
GDateTime *mtime;
GDateTime *ctime;
};
static void poppler_attachment_finalize(GObject *obj);
G_DEFINE_TYPE_WITH_PRIVATE(PopplerAttachment, poppler_attachment, G_TYPE_OBJECT)
#define GET_PRIVATE(obj) ((PopplerAttachmentPrivate *)poppler_attachment_get_instance_private(obj))
static void poppler_attachment_init(PopplerAttachment *attachment)
{
void *place;
place = GET_PRIVATE(attachment);
new (place) PopplerAttachmentPrivate();
}
static void poppler_attachment_class_init(PopplerAttachmentClass *klass)
{
G_OBJECT_CLASS(klass)->finalize = poppler_attachment_finalize;
}
static void poppler_attachment_finalize(GObject *obj)
{
PopplerAttachment *attachment;
PopplerAttachmentPrivate *priv;
attachment = (PopplerAttachment *)obj;
priv = GET_PRIVATE(attachment);
if (attachment->name) {
g_free(attachment->name);
}
attachment->name = nullptr;
if (attachment->description) {
g_free(attachment->description);
}
attachment->description = nullptr;
if (attachment->checksum) {
g_string_free(attachment->checksum, TRUE);
}
attachment->checksum = nullptr;
g_clear_pointer(&priv->mtime, g_date_time_unref);
g_clear_pointer(&priv->ctime, g_date_time_unref);
priv->~PopplerAttachmentPrivate();
G_OBJECT_CLASS(poppler_attachment_parent_class)->finalize(obj);
}
/* Public functions */
PopplerAttachment *_poppler_attachment_new(FileSpec *emb_file)
{
PopplerAttachment *attachment;
PopplerAttachmentPrivate *priv;
EmbFile *embFile;
g_assert(emb_file != nullptr);
attachment = (PopplerAttachment *)g_object_new(POPPLER_TYPE_ATTACHMENT, nullptr);
priv = GET_PRIVATE(attachment);
if (emb_file->getFileName()) {
attachment->name = _poppler_goo_string_to_utf8(emb_file->getFileName());
}
if (emb_file->getDescription()) {
attachment->description = _poppler_goo_string_to_utf8(emb_file->getDescription());
}
embFile = emb_file->getEmbeddedFile();
if (embFile != nullptr && embFile->streamObject()->isStream()) {
attachment->size = embFile->size();
if (embFile->createDate()) {
priv->ctime = _poppler_convert_pdf_date_to_date_time(embFile->createDate());
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* This will overflow on dates from after 2038. This field is
* deprecated, only kept for backward compatibility. */
attachment->ctime = (GTime)g_date_time_to_unix(priv->ctime);
G_GNUC_END_IGNORE_DEPRECATIONS
}
if (embFile->modDate()) {
priv->mtime = _poppler_convert_pdf_date_to_date_time(embFile->modDate());
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* This will overflow on dates from after 2038. This field is
* deprecated, only kept for backward compatibility. */
attachment->mtime = (GTime)g_date_time_to_unix(priv->mtime);
G_GNUC_END_IGNORE_DEPRECATIONS
}
if (embFile->checksum() && embFile->checksum()->getLength() > 0) {
attachment->checksum = g_string_new_len(embFile->checksum()->c_str(), embFile->checksum()->getLength());
}
priv->obj_stream = embFile->streamObject()->copy();
} else {
g_warning("Missing stream object for embedded file");
g_clear_object(&attachment);
}
return attachment;
}
/**
* poppler_attachment_get_checksum:
* @attachment: a #PopplerAttachment
*
* Returns: The attachment's checksum.
*
* Since: 20.09.0
*/
const GString *poppler_attachment_get_checksum(PopplerAttachment *attachment)
{
return attachment->checksum;
}
/**
* poppler_attachment_get_ctime:
* @attachment: a #PopplerAttachment
*
* Returns: (transfer none) (nullable): The attachment's creation date and time
* as a #GDateTime, or %NULL if the creation date and time is not available.
*
* Since: 20.09.0
*/
GDateTime *poppler_attachment_get_ctime(PopplerAttachment *attachment)
{
return GET_PRIVATE(attachment)->ctime;
}
/**
* poppler_attachment_get_description:
* @attachment: a #PopplerAttachment
*
* Returns: The attachment's descriptive text.
*
* Since: 20.09.0
*/
const gchar *poppler_attachment_get_description(PopplerAttachment *attachment)
{
return attachment->description;
}
/**
* poppler_attachment_get_mtime:
* @attachment: a #PopplerAttachment
*
* Returns: (transfer none) (nullable): The attachment's modification date and
* time as a #GDateTime, or %NULL if the modification date and time is not
* available.
*
* Since: 20.09.0
*/
GDateTime *poppler_attachment_get_mtime(PopplerAttachment *attachment)
{
return GET_PRIVATE(attachment)->mtime;
}
/**
* poppler_attachment_get_name:
* @attachment: a #PopplerAttachment
*
* Returns: The attachment's name.
*
* Since: 20.09.0
*/
const gchar *poppler_attachment_get_name(PopplerAttachment *attachment)
{
return attachment->name;
}
/**
* poppler_attachment_get_size:
* @attachment: a #PopplerAttachment
*
* Returns: The attachment's size.
*
* Since: 20.09.0
*/
gsize poppler_attachment_get_size(PopplerAttachment *attachment)
{
return attachment->size;
}
static gboolean save_helper(const gchar *buf, gsize count, gpointer data, GError **error)
{
FILE *f = (FILE *)data;
gsize n;
n = fwrite(buf, 1, count, f);
if (n != count) {
int errsv = errno;
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv), _("Error writing to image file: %s"), g_strerror(errsv));
return FALSE;
}
return TRUE;
}
/**
* poppler_attachment_save:
* @attachment: A #PopplerAttachment.
* @filename: name of file to save
* @error: (allow-none): return location for error, or %NULL.
*
* Saves @attachment to a file indicated by @filename. If @error is set, %FALSE
* will be returned. Possible errors include those in the #G_FILE_ERROR domain
* and whatever the save function generates.
*
* Return value: %TRUE, if the file successfully saved
**/
gboolean poppler_attachment_save(PopplerAttachment *attachment, const char *filename, GError **error)
{
gboolean result;
FILE *f;
g_return_val_if_fail(POPPLER_IS_ATTACHMENT(attachment), FALSE);
f = openFile(filename, "wb");
if (f == nullptr) {
gchar *display_name = g_filename_display_name(filename);
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), _("Failed to open '%s' for writing: %s"), display_name, g_strerror(errno));
g_free(display_name);
return FALSE;
}
result = poppler_attachment_save_to_callback(attachment, save_helper, f, error);
if (fclose(f) < 0) {
gchar *display_name = g_filename_display_name(filename);
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), _("Failed to close '%s', all data may not have been saved: %s"), display_name, g_strerror(errno));
g_free(display_name);
return FALSE;
}
return result;
}
#ifndef G_OS_WIN32
/**
* poppler_attachment_save_to_fd:
* @attachment: A #PopplerAttachment.
* @fd: a valid file descriptor open for writing
* @error: (allow-none): return location for error, or %NULL.
*
* Saves @attachment to a file referred to by @fd. If @error is set, %FALSE
* will be returned. Possible errors include those in the #G_FILE_ERROR domain
* and whatever the save function generates.
* Note that this function takes ownership of @fd; you must not operate on it
* again, nor close it.
*
* Return value: %TRUE, if the file successfully saved
*
* Since: 21.12.0
**/
gboolean poppler_attachment_save_to_fd(PopplerAttachment *attachment, int fd, GError **error)
{
gboolean result;
FILE *f;
g_return_val_if_fail(POPPLER_IS_ATTACHMENT(attachment), FALSE);
g_return_val_if_fail(fd != -1, FALSE);
g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
f = fdopen(fd, "wb");
if (f == nullptr) {
int errsv = errno;
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv), _("Failed to open FD %d for writing: %s"), fd, g_strerror(errsv));
close(fd);
return FALSE;
}
result = poppler_attachment_save_to_callback(attachment, save_helper, f, error);
if (fclose(f) < 0) {
int errsv = errno;
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv), _("Failed to close FD %d, all data may not have been saved: %s"), fd, g_strerror(errsv));
return FALSE;
}
return result;
}
#endif /* !G_OS_WIN32 */
#define BUF_SIZE 1024
/**
* poppler_attachment_save_to_callback:
* @attachment: A #PopplerAttachment.
* @save_func: (scope call): a function that is called to save each block of data that the save routine generates.
* @user_data: user data to pass to the save function.
* @error: (allow-none): return location for error, or %NULL.
*
* Saves @attachment by feeding the produced data to @save_func. Can be used
* when you want to store the attachment to something other than a file, such as
* an in-memory buffer or a socket. If @error is set, %FALSE will be
* returned. Possible errors include those in the #G_FILE_ERROR domain and
* whatever the save function generates.
*
* Return value: %TRUE, if the save successfully completed
**/
gboolean poppler_attachment_save_to_callback(PopplerAttachment *attachment, PopplerAttachmentSaveFunc save_func, gpointer user_data, GError **error)
{
PopplerAttachmentPrivate *priv;
Stream *stream;
gchar buf[BUF_SIZE];
int i;
gboolean eof_reached = FALSE;
g_return_val_if_fail(POPPLER_IS_ATTACHMENT(attachment), FALSE);
priv = GET_PRIVATE(attachment);
stream = priv->obj_stream.getStream();
stream->reset();
do {
int data;
for (i = 0; i < BUF_SIZE; i++) {
data = stream->getChar();
if (data == EOF) {
eof_reached = TRUE;
break;
}
buf[i] = data;
}
if (i > 0) {
if (!(save_func)(buf, i, user_data, error)) {
return FALSE;
}
}
} while (!eof_reached);
return TRUE;
}
|