Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.1 kB
4
Indexable
Never
#include <bits/stdc++.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
extern int errno;

using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()

char *filenames[10];
int filenames_sz = 0;
int shift = 4uLL;

void fetch_filenames(const char *prog_name) {
  size_t v1;             // rax
  size_t sz;             // [rsp+10h] [rbp-30h]
  DIR *dir;              // [rsp+18h] [rbp-28h]
  size_t filename_sz;    // [rsp+20h] [rbp-20h]
  struct dirent *entry;  // [rsp+30h] [rbp-10h]
  char *filename;        // [rsp+38h] [rbp-8h]

  dir = opendir(".");
  if (!dir) {
    perror("Error opening directory");
    exit(-1);
  }

  sz = 0LL;
  while (1) {
    entry = readdir(dir);
    if (!entry || sz > 0xFE) break;
    if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..") &&
        strcmp(entry->d_name, prog_name + 2) &&
        !strncmp(entry->d_name, "ch4ll3ng_", 9uLL)) {
      filename_sz = strlen(entry->d_name) + 1;
      filename = (char *)malloc(filename_sz);
      strncpy(filename, entry->d_name, filename_sz);
      v1 = sz++;
      filenames[v1] = filename;
    }
  }
  filenames_sz = sz;
  closedir(dir);
}

// Modified function to get seed of a valid .png file
int64_t generate_rng_seed() {
  int fd;           // [rsp+Ch] [rbp-14h]
  int64_t seed[2];  // [rsp+10h] [rbp-10h] BYREF
  int idx = 0;
  string seedProvider = "seedprovider.png";
  cout << "seed file: " << seedProvider << endl;
  fd = open(seedProvider.c_str(), 0);
  if (fd == -1) {
    perror("Error opening file");
    exit(-1);
  }
  read(fd, seed, 8uLL);
  // seed[0] must always be 727905341920923785
  cout << "SEED: " << seed[0] << endl;
  return seed[0];
}
size_t get_file_sz(const char *filename) {
  struct stat st;
  stat(filename, &st);
  return st.st_size;
}
void read_file(const char *filename, char *buf, size_t sz) {
  int fd;  // [rsp+2Ch] [rbp-4h]
  fd = open(filename, 2);
  string x(filename);
  lseek(fd, shift, 0);
  read(fd, buf, sz);
  close(fd);
}
char *append_ext(const char *filename) {
  size_t v1;           // rax
  size_t file_sz;      // [rsp+10h] [rbp-10h]
  char *new_filename;  // [rsp+18h] [rbp-8h]

  v1 = strlen(filename);
  file_sz = v1 + 1;
  new_filename = (char *)malloc(v1 + 6);
  strncpy(new_filename, filename, file_sz);
  strcat(new_filename, ".png");
  return new_filename;
}
void enc_data(char *data, size_t sz) {
  size_t i;  // [rsp+18h] [rbp-8h]

  for (i = 0LL; i < sz; ++i) data[i] ^= rand() % 256;
}
void create_enc_file(const char *filename, char *data, size_t sz,
                     unsigned int id) {
  unsigned int ida;       // [rsp+4h] [rbp-2Ch] BYREF
  size_t sza;             // [rsp+8h] [rbp-28h]
  char *dataa;            // [rsp+10h] [rbp-20h]
  const char *filenamea;  // [rsp+18h] [rbp-18h]
  int fd;                 // [rsp+2Ch] [rbp-4h]

  filenamea = filename;
  dataa = data;
  sza = sz;
  ida = id;

  fd = open(filename, O_RDWR | O_CREAT, 384LL);
  if (fd == -1) {
    perror("open");
    exit(-1);
  }
  // Commenting out the next line, to only use the data.
  // write(fd, &ida, 4uLL);
  write(fd, dataa, sza);
  close(fd);
}
void enc_file(const char *filename, unsigned int id) {
  size_t file_sz;      // [rsp+18h] [rbp-18h]
  char *buf;           // [rsp+20h] [rbp-10h]
  char *enc_filename;  // [rsp+28h] [rbp-8h]
  file_sz = get_file_sz(filename) - shift;
  buf = (char *)malloc(file_sz);
  read_file(filename, buf, file_sz);

  // adding an extra out_ prefix.
  std::string tmp(filename);
  string out = "out_" + tmp;
  filename = out.c_str();

  enc_filename = append_ext(filename);
  enc_data(buf, file_sz);
  create_enc_file(enc_filename, buf, file_sz, id);
  free(enc_filename);
  free(buf);
}

int main(int argc, const char **argv) {
  fetch_filenames(*argv);
  unsigned int rng_seed;  // eax
  rng_seed = generate_rng_seed();
  srand(rng_seed);
  for (int i = 0LL; i < filenames_sz; ++i) enc_file(filenames[i], i);
}
Leave a Comment