archive_write_set_passphrase.c revision 302408
1/*-
2 * Copyright (c) 2014 Michihiro NAKAJIMA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "archive_platform.h"
27__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_write_set_passphrase.c 299529 2016-05-12 10:16:16Z mm $");
28
29#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
32#include "archive_write_private.h"
33
34int
35archive_write_set_passphrase(struct archive *_a, const char *p)
36{
37	struct archive_write *a = (struct archive_write *)_a;
38
39	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
40		"archive_write_set_passphrase");
41
42	if (p == NULL || p[0] == '\0') {
43		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
44		    "Empty passphrase is unacceptable");
45		return (ARCHIVE_FAILED);
46	}
47	free(a->passphrase);
48	a->passphrase = strdup(p);
49	if (a->passphrase == NULL) {
50		archive_set_error(&a->archive, ENOMEM,
51		    "Can't allocate data for passphrase");
52		return (ARCHIVE_FATAL);
53	}
54	return (ARCHIVE_OK);
55}
56
57
58int
59archive_write_set_passphrase_callback(struct archive *_a, void *client_data,
60    archive_passphrase_callback *cb)
61{
62	struct archive_write *a = (struct archive_write *)_a;
63
64	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
65		"archive_write_set_passphrase_callback");
66
67	a->passphrase_callback = cb;
68	a->passphrase_client_data = client_data;
69	return (ARCHIVE_OK);
70}
71
72
73const char *
74__archive_write_get_passphrase(struct archive_write *a)
75{
76
77	if (a->passphrase != NULL)
78		return (a->passphrase);
79
80	if (a->passphrase_callback != NULL) {
81		const char *p;
82		p = a->passphrase_callback(&a->archive,
83		    a->passphrase_client_data);
84		if (p != NULL) {
85			a->passphrase = strdup(p);
86			if (a->passphrase == NULL) {
87				archive_set_error(&a->archive, ENOMEM,
88				    "Can't allocate data for passphrase");
89				return (NULL);
90			}
91			return (a->passphrase);
92		}
93	}
94	return (NULL);
95}
96