<commit_before>/*
  Copyright (C) 2013-2015 Yubico AB

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include "u2f-host.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#ifdef _WIN32
  #include <windows.h>
#else
  #include <signal.h>
  #include <poll.h>
#endif

#define TIMEOUT 60

typedef struct {
  int op;
  char *domain;
  char **challenges;
} OP;

static OP eof_op = {'e'};

static int
read_n_bytes(char *buf, long len) {
  while (len > 0) {
    long r = read(0, buf, len);
    if (!r)
      return 0;
    buf += r;
    len -= r;
  }
  return 1;
}

static OP *
read_action(int timeout) {
#ifdef _WIN32
  HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  DWORD available;

  PeekNamedPipe(hIn, NULL, 0, NULL, &available, NULL);
  if (available == 0)
    return NULL;
#else
  struct pollfd pfd[] = {{0, POLLIN, 0}};
  poll(pfd, 1, timeout);
  if ((pfd[0].revents & (POLLIN | POLLERR | POLLHUP)) == 0)
    return NULL;
#endif
  char op;

  if (read(0, &op, 1) == 0)
    return &eof_op;

  if (op == 'r' || op == 's') {
    char input_len_buf[13];
    long challenges_lengths[16];
    long challenges_num, challenges_buf_len = 0, domain_len;

    if (!read_n_bytes(input_len_buf, 8))
      return &eof_op;

    input_len_buf[8] = '\0';
    challenges_num = strtol(input_len_buf + 4, NULL, 16);
    input_len_buf[4] = '\0';
    domain_len = strtol(input_len_buf, NULL, 16);

    for (int i = 0; i < challenges_num; i++) {
      if (!read_n_bytes(input_len_buf, 4))
        return &eof_op;
      challenges_lengths[i] = strtol(input_len_buf, NULL, 16);
      challenges_buf_len += challenges_lengths[i];
    }

    OP *buf = malloc(domain_len + (sizeof(char *) * (challenges_num + 1)) +
                     challenges_buf_len + challenges_num + 1 + sizeof(OP));
    if (!buf)
      return &eof_op;

    buf->op = op;
    buf->challenges = (char **) ((char *) buf) + sizeof(OP);
    buf->domain = ((char *) buf) + sizeof(OP) + (sizeof(char *) * (challenges_num + 1));
    buf->domain[domain_len] = '\0';

    if (!read_n_bytes(buf->domain, domain_len)) {
      free(buf);
      return &eof_op;
    }

    char *challenge = buf->domain + domain_len;
    for (int i = 0; i < challenges_num; i++) {<commit_msg>Fix some pointer calculation<commit_after>/*
  Copyright (C) 2013-2015 Yubico AB

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include "u2f-host.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#ifdef _WIN32
#include <windows.h>
#else

#include <signal.h>
#include <poll.h>

#endif

#define TIMEOUT 60

typedef struct {
  int op;
  char *domain;
  char **challenges;
} OP;

static OP eof_op = {'e'};

static int
read_n_bytes(char *buf, long len) {
  while (len > 0) {
    long r = read(0, buf, len);
    if (!r)
      return 0;
    buf += r;
    len -= r;
  }
  return 1;
}

static OP *
read_action(int timeout) {
#ifdef _WIN32
  HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  DWORD available;

  PeekNamedPipe(hIn, NULL, 0, NULL, &available, NULL);
  if (available == 0)
    return NULL;
#else
  struct pollfd pfd[] = {{0, POLLIN, 0}};
  poll(pfd, 1, timeout);
  if ((pfd[0].revents & (POLLIN | POLLERR | POLLHUP)) == 0)
    return NULL;
#endif
  char op;

  if (read(0, &op, 1) == 0)
    return &eof_op;

  if (op == 'r' || op == 's') {
    char input_len_buf[9];
    long challenges_lengths[16];
    long challenges_num, challenges_buf_len = 0, domain_len;

    if (!read_n_bytes(input_len_buf, 8))
      return &eof_op;

    input_len_buf[8] = '\0';
    challenges_num = strtol(input_len_buf + 4, NULL, 16);
    input_len_buf[4] = '\0';
    domain_len = strtol(input_len_buf, NULL, 16);

    for (int i = 0; i < challenges_num; i++) {
      if (!read_n_bytes(input_len_buf, 4))
        return &eof_op;
      challenges_lengths[i] = strtol(input_len_buf, NULL, 16);
      challenges_buf_len += challenges_lengths[i];
    }

    OP *buf = malloc(domain_len + (sizeof(char *) * (challenges_num + 1)) +
                     challenges_buf_len + challenges_num + 1 + sizeof(OP));
    if (!buf)
      return &eof_op;

    buf->op = op;
    buf->challenges = (char **) (buf + 1);
    buf->domain = (char *) (buf->challenges + challenges_num + 1);
    buf->domain[domain_len] = '\0';

    if (!read_n_bytes(buf->domain, domain_len)) {
      free(buf);
      return &eof_op;
    }

    char *challenge = buf->domain + domain_len + 1;
    for (int i = 0; i < challenges_num; i++) {
{'fraction_non_alphanumeric': 0.1335521769177609, 'fraction_numerical': 0.01883206634416033, 'mean_word_length': 3.1888567293777133, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4386135', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2059, 'n_words': 641}