<commit_before>/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef __cplusplus
#include <stdint.h>
#include <stdbool.h>
#else
#include <proton/type_compat.h>
#endif
#include <ctype.h>
#include <string.h>
#include <proton/error.h>
#include <proton/util.h>
#include <proton/types.h>
#include "util.h"

ssize_t pn_quote_data(char *dst, size_t capacity, const char *src, size_t size)
{
  int idx = 0;
  for (unsigned i = 0; i < size; i++)
  {
    uint8_t c = src[i];
    if (isprint(c)) {
      if (idx < (int) (capacity - 1)) {
        dst[idx++] = c;
      } else {
        if (idx > 0) {
          dst[idx - 1] = '\0';
        }
        return PN_OVERFLOW;
      }
    } else {
      if (idx < (int) (capacity - 4)) {
        idx += sprintf(dst + idx, "\\x%.2x", c);
      } else {
        if (idx > 0) {
          dst[idx - 1] = '\0';
        }
        return PN_OVERFLOW;
      }
    }
  }

  dst[idx] = '\0';
  return idx;
}

int pn_quote(pn_string_t *dst, const char *src, size_t size)
{
  while (true) {
    size_t str_size = pn_string_size(dst);
    char *str = pn_string_buffer(dst) + str_size;
    size_t capacity = pn_string_capacity(dst) - str_size;
    ssize_t ssize = pn_quote_data(str, capacity, src, size);
    if (ssize == PN_OVERFLOW) {
      int err = pn_string_grow(dst, (str_size + capacity) ? 2*(str_size + capacity) : 16);
      if (err) return err;
    } else if (ssize >= 0) {
      return pn_string_resize(dst, str_size + ssize);
    } else {
      return ssize;
    }
  }
}

void pn_fprint_data(FILE *stream, const char *bytes, size_t size)
{
  char buf[256];
  ssize_t n = pn_quote_data(buf, 256, bytes, size);
  if (n >= 0) {
    fputs(buf, stream);
  } else {
    if (n == PN_OVERFLOW) {
      fputs(buf, stream);
      fputs("... (truncated)", stream);
    }
    else
      fprintf(stderr, "pn_quote_data: %s\n", pn_code(n));
  }
}

void pn_print_data(const char *bytes, size_t size)
{
  pn_fprint_data(stdout, bytes, size);
}

// Parse URL syntax:
// [ <scheme> :// ] [ <user> [ : <password> ] @ ] <host> [ : <port> ] [ / <path> ]
// <user>, <password>, <host>, <port> cannot contain any of '@', ':', '/'
// <path> can contain any character
void pni_parse_url(char *url, char **scheme, char **user, char **pass, char **host, char **port, char **path)
{
  if (url) {
    char *scheme_end = strstr(url, "://");
    if (scheme_end) {
      *scheme_end = '\0';
      *scheme = url;
      url = scheme_end + 3;
    }

    char *at = strchr(url, '@');
    if (at) {
      *at = '\0';
      char *up = url;
      *user = up;
      url = at + 1;
      char *colon = strchr(up, ':');
      if (colon) {
        *colon = '\0';
        *pass = colon + 1;
      }
    }

    char *slash = strchr(url, '/');
    if (slash) {
      *slash = '\0';
      *host = url;
      url = slash + 1;
      *path = url;
    } else {
      *host = url;
    }

    char *colon = strchr(*host, ':');
    if (colon) {
      *colon = '\0';
      *port = colon + 1;
    }
  }
}

void pn_vfatal(const char *fmt, va_list ap)
{
  vfprintf(stderr, fmt, ap);
  abort();
}

void pn_fatal(const char *fmt, ...)<commit_msg>PROTON-506: Fix C address URL parser to correctly identify the host component if there is an '@' character in the path component.<commit_after>/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef __cplusplus
#include <stdint.h>
#include <stdbool.h>
#else
#include <proton/type_compat.h>
#endif
#include <ctype.h>
#include <string.h>
#include <proton/error.h>
#include <proton/util.h>
#include <proton/types.h>
#include "util.h"

ssize_t pn_quote_data(char *dst, size_t capacity, const char *src, size_t size)
{
  int idx = 0;
  for (unsigned i = 0; i < size; i++)
  {
    uint8_t c = src[i];
    if (isprint(c)) {
      if (idx < (int) (capacity - 1)) {
        dst[idx++] = c;
      } else {
        if (idx > 0) {
          dst[idx - 1] = '\0';
        }
        return PN_OVERFLOW;
      }
    } else {
      if (idx < (int) (capacity - 4)) {
        idx += sprintf(dst + idx, "\\x%.2x", c);
      } else {
        if (idx > 0) {
          dst[idx - 1] = '\0';
        }
        return PN_OVERFLOW;
      }
    }
  }

  dst[idx] = '\0';
  return idx;
}

int pn_quote(pn_string_t *dst, const char *src, size_t size)
{
  while (true) {
    size_t str_size = pn_string_size(dst);
    char *str = pn_string_buffer(dst) + str_size;
    size_t capacity = pn_string_capacity(dst) - str_size;
    ssize_t ssize = pn_quote_data(str, capacity, src, size);
    if (ssize == PN_OVERFLOW) {
      int err = pn_string_grow(dst, (str_size + capacity) ? 2*(str_size + capacity) : 16);
      if (err) return err;
    } else if (ssize >= 0) {
      return pn_string_resize(dst, str_size + ssize);
    } else {
      return ssize;
    }
  }
}

void pn_fprint_data(FILE *stream, const char *bytes, size_t size)
{
  char buf[256];
  ssize_t n = pn_quote_data(buf, 256, bytes, size);
  if (n >= 0) {
    fputs(buf, stream);
  } else {
    if (n == PN_OVERFLOW) {
      fputs(buf, stream);
      fputs("... (truncated)", stream);
    }
    else
      fprintf(stderr, "pn_quote_data: %s\n", pn_code(n));
  }
}

void pn_print_data(const char *bytes, size_t size)
{
  pn_fprint_data(stdout, bytes, size);
}

// Parse URL syntax:
// [ <scheme> :// ] [ <user> [ : <password> ] @ ] <host> [ : <port> ] [ / <path> ]
// <user>, <password>, <host>, <port> cannot contain any of '@', ':', '/'
// <path> can contain any character
void pni_parse_url(char *url, char **scheme, char **user, char **pass, char **host, char **port, char **path)
{
  if (!url) return;

  char *scheme_end = strstr(url, "://");
  if (scheme_end) {
    *scheme_end = '\0';
    *scheme = url;
    url = scheme_end + 3;
  }

  char *slash = strchr(url, '/');
  if (slash) {
    *slash = '\0';
    *path = slash + 1;
  }

  char *at = strchr(url, '@');
  if (at) {
    *at = '\0';
    char *up = url;
    *user = up;
    url = at + 1;
    char *colon = strchr(up, ':');
    if (colon) {
      *colon = '\0';
      *pass = colon + 1;
    }
  }

  *host = url;

  char *colon = strchr(*host, ':');
  if (colon) {
    *colon = '\0';
    *port = colon + 1;
  }

}

void pn_vfatal(const char *fmt, va_list ap)
{
  vfprintf(stderr, fmt, ap);
  abort();
}

void pn_fatal(const char *fmt, ...)
{'fraction_non_alphanumeric': 0.15497922699232028, 'fraction_numerical': 0.009945864283016493, 'mean_word_length': 2.6191343963553533, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 57, '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': '6468987', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2879, 'n_words': 862}