1299425Smm/*-
2299425Smm * Copyright (c) 2014 Michihiro NAKAJIMA
3299425Smm * All rights reserved.
4299425Smm *
5299425Smm * Redistribution and use in source and binary forms, with or without
6299425Smm * modification, are permitted provided that the following conditions
7299425Smm * are met:
8299425Smm * 1. Redistributions of source code must retain the above copyright
9299425Smm *    notice, this list of conditions and the following disclaimer.
10299425Smm * 2. Redistributions in binary form must reproduce the above copyright
11299425Smm *    notice, this list of conditions and the following disclaimer in the
12299425Smm *    documentation and/or other materials provided with the distribution.
13299425Smm *
14299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24299425Smm */
25299425Smm
26299425Smm#include "archive_platform.h"
27299425Smm__FBSDID("$FreeBSD$");
28299425Smm
29299425Smm#ifdef HAVE_ERRNO_H
30299425Smm#include <errno.h>
31299425Smm#endif
32299425Smm#include "archive_write_private.h"
33299425Smm
34299425Smmint
35299425Smmarchive_write_set_passphrase(struct archive *_a, const char *p)
36299425Smm{
37299425Smm	struct archive_write *a = (struct archive_write *)_a;
38299425Smm
39299425Smm	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
40299425Smm		"archive_write_set_passphrase");
41299425Smm
42299425Smm	if (p == NULL || p[0] == '\0') {
43299425Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
44299425Smm		    "Empty passphrase is unacceptable");
45299425Smm		return (ARCHIVE_FAILED);
46299425Smm	}
47299425Smm	free(a->passphrase);
48299425Smm	a->passphrase = strdup(p);
49299425Smm	if (a->passphrase == NULL) {
50299425Smm		archive_set_error(&a->archive, ENOMEM,
51299425Smm		    "Can't allocate data for passphrase");
52299425Smm		return (ARCHIVE_FATAL);
53299425Smm	}
54299425Smm	return (ARCHIVE_OK);
55299425Smm}
56299425Smm
57299425Smm
58299425Smmint
59299425Smmarchive_write_set_passphrase_callback(struct archive *_a, void *client_data,
60299425Smm    archive_passphrase_callback *cb)
61299425Smm{
62299425Smm	struct archive_write *a = (struct archive_write *)_a;
63299425Smm
64299425Smm	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
65299425Smm		"archive_write_set_passphrase_callback");
66299425Smm
67299425Smm	a->passphrase_callback = cb;
68299425Smm	a->passphrase_client_data = client_data;
69299425Smm	return (ARCHIVE_OK);
70299425Smm}
71299425Smm
72299425Smm
73299425Smmconst char *
74299425Smm__archive_write_get_passphrase(struct archive_write *a)
75299425Smm{
76299425Smm
77299425Smm	if (a->passphrase != NULL)
78299425Smm		return (a->passphrase);
79299425Smm
80299425Smm	if (a->passphrase_callback != NULL) {
81299425Smm		const char *p;
82299425Smm		p = a->passphrase_callback(&a->archive,
83299425Smm		    a->passphrase_client_data);
84299425Smm		if (p != NULL) {
85299425Smm			a->passphrase = strdup(p);
86299425Smm			if (a->passphrase == NULL) {
87299425Smm				archive_set_error(&a->archive, ENOMEM,
88299425Smm				    "Can't allocate data for passphrase");
89299425Smm				return (NULL);
90299425Smm			}
91299425Smm			return (a->passphrase);
92299425Smm		}
93299425Smm	}
94299425Smm	return (NULL);
95299425Smm}
96