libarchive / libarchive

Multi-format archive and compression library
http://www.libarchive.org
Other
3.05k stars 772 forks source link

Incorrect passphrase for Zip archive #1755

Open ikrivosheev opened 2 years ago

ikrivosheev commented 2 years ago

Hello! I try open zip archive with password in Chinese, but i get an error when reading entry.

Password: 夜亱线强爲為为 File: clean_one_layer_password_in_utf8.zip OS: linux libarchive: 3.6.1 LANG: en_US.utf8

Simple code to reproduce:

#include <archive.h>
#include <archive_entry.h>
#include <stdio.h>

#define CHECK_AND_EXIT(result, msg)                                            \
  {                                                                            \
    if (result) {                                                              \
      printf(msg);                                                             \
      return 1;                                                                \
    }                                                                          \
  }

int main(int argc, char **argv) {
  int r;
  char buff[8192];
  ssize_t len;
  FILE *out;
  struct archive *ina;
  struct archive_entry *entry;
  char *input, *passphrase;

  input = argv[1];
  passphrase = argv[2];

  CHECK_AND_EXIT((ina = archive_read_new()) == NULL,
                 "Cannot create archive reader");
  CHECK_AND_EXIT(archive_read_support_filter_all(ina) != ARCHIVE_OK,
                 "Cannot enable decompression");
  CHECK_AND_EXIT(archive_read_support_format_all(ina) != ARCHIVE_OK,
                 "Cannot enable read formats")
  CHECK_AND_EXIT(archive_read_add_passphrase(ina, passphrase) != ARCHIVE_OK,
                 "Cannot add passphrase");
  CHECK_AND_EXIT(archive_read_open_filename(ina, input, 10240) != ARCHIVE_OK,
                 "Cannot open archive");
  CHECK_AND_EXIT((out = fopen(argv[3], "wb")) == NULL,
                 "Cannot open output file");

  while ((r = archive_read_next_header(ina, &entry)) == ARCHIVE_OK) {
    printf("%s: ", archive_entry_pathname(entry));
    /* Skip anything that isn't a regular file. */
    if (!S_ISREG(archive_entry_mode(entry))) {
      printf("skipped\n");
      continue;
    }
    if (archive_entry_size(entry) > 0) {
      do {

        len = archive_read_data(ina, buff, sizeof(buff));
        if (len == 0) {
          printf("copied\n");
          break;
        }
        if (len < 0) {
          printf("Error reading input archive: retcode=%zi string=%s\n", len,
                 archive_error_string(ina));
          break;
        }
        fprintf(out, buff, len);
      } while (1);
    }
  }

  CHECK_AND_EXIT(r != ARCHIVE_EOF, "Error reading archive");
  /* Close the archives.  */
  CHECK_AND_EXIT(archive_read_free(ina) != ARCHIVE_OK,
                 "Error closing input archive");
  return 0;
}
AngelineNinu commented 11 months ago

HI, I'm also trying for Japanese password, Was this issue resolved?

ikrivosheev commented 5 months ago

@AngelineNinu hello, nope(