11590Srgrimes/*-
21590Srgrimes * Copyright (c) 2014 Michihiro NAKAJIMA
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151590Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161590Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171590Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
181590Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191590Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201590Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211590Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221590Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231590Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241590Srgrimes */
251590Srgrimes
261590Srgrimes#include "archive_platform.h"
271590Srgrimes__FBSDID("$FreeBSD$");
281590Srgrimes
291590Srgrimes#ifdef HAVE_ERRNO_H
301590Srgrimes#include <errno.h>
311590Srgrimes#endif
321590Srgrimes#include "archive_write_private.h"
331590Srgrimes
341590Srgrimesint
351590Srgrimesarchive_write_set_passphrase(struct archive *_a, const char *p)
361590Srgrimes{
371590Srgrimes	struct archive_write *a = (struct archive_write *)_a;
381590Srgrimes
391590Srgrimes	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
401590Srgrimes		"archive_write_set_passphrase");
411590Srgrimes
421590Srgrimes	if (p == NULL || p[0] == '\0') {
431590Srgrimes		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
441590Srgrimes		    "Empty passphrase is unacceptable");
451590Srgrimes		return (ARCHIVE_FAILED);
461590Srgrimes	}
471590Srgrimes	free(a->passphrase);
481590Srgrimes	a->passphrase = strdup(p);
491590Srgrimes	if (a->passphrase == NULL) {
501590Srgrimes		archive_set_error(&a->archive, ENOMEM,
511590Srgrimes		    "Can't allocate data for passphrase");
521590Srgrimes		return (ARCHIVE_FATAL);
531590Srgrimes	}
541590Srgrimes	return (ARCHIVE_OK);
551590Srgrimes}
561590Srgrimes
571590Srgrimes
581590Srgrimesint
591590Srgrimesarchive_write_set_passphrase_callback(struct archive *_a, void *client_data,
601590Srgrimes    archive_passphrase_callback *cb)
611590Srgrimes{
621590Srgrimes	struct archive_write *a = (struct archive_write *)_a;
631590Srgrimes
641590Srgrimes	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
651590Srgrimes		"archive_write_set_passphrase_callback");
661590Srgrimes
671590Srgrimes	a->passphrase_callback = cb;
681590Srgrimes	a->passphrase_client_data = client_data;
691590Srgrimes	return (ARCHIVE_OK);
701590Srgrimes}
711590Srgrimes
721590Srgrimes
731590Srgrimesconst 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