archive_write_set_passphrase.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include "archive_write_private.h"
  31. int
  32. archive_write_set_passphrase(struct archive *_a, const char *p)
  33. {
  34. struct archive_write *a = (struct archive_write *)_a;
  35. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
  36. "archive_write_set_passphrase");
  37. if (p == NULL || p[0] == '\0') {
  38. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  39. "Empty passphrase is unacceptable");
  40. return (ARCHIVE_FAILED);
  41. }
  42. free(a->passphrase);
  43. a->passphrase = strdup(p);
  44. if (a->passphrase == NULL) {
  45. archive_set_error(&a->archive, ENOMEM,
  46. "Can't allocate data for passphrase");
  47. return (ARCHIVE_FATAL);
  48. }
  49. return (ARCHIVE_OK);
  50. }
  51. int
  52. archive_write_set_passphrase_callback(struct archive *_a, void *client_data,
  53. archive_passphrase_callback *cb)
  54. {
  55. struct archive_write *a = (struct archive_write *)_a;
  56. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
  57. "archive_write_set_passphrase_callback");
  58. a->passphrase_callback = cb;
  59. a->passphrase_client_data = client_data;
  60. return (ARCHIVE_OK);
  61. }
  62. const char *
  63. __archive_write_get_passphrase(struct archive_write *a)
  64. {
  65. if (a->passphrase != NULL)
  66. return (a->passphrase);
  67. if (a->passphrase_callback != NULL) {
  68. const char *p;
  69. p = a->passphrase_callback(&a->archive,
  70. a->passphrase_client_data);
  71. if (p != NULL) {
  72. a->passphrase = strdup(p);
  73. if (a->passphrase == NULL) {
  74. archive_set_error(&a->archive, ENOMEM,
  75. "Can't allocate data for passphrase");
  76. return (NULL);
  77. }
  78. return (a->passphrase);
  79. }
  80. }
  81. return (NULL);
  82. }