Example #1
0
wxString &
strip(wxString &s,
      bool newlines) {
  int i, len;
  const wxChar *c;

  c = s.c_str();
  i = 0;
  if (newlines)
    while ((c[i] != 0) && (isblanktab(c[i]) || iscr(c[i])))
      i++;
  else
    while ((c[i] != 0) && isblanktab(c[i]))
      i++;

  if (i > 0)
    s.Remove(0, i);

  c = s.c_str();
  len = s.length();
  i = 0;

  if (newlines)
    while ((i < len) && (isblanktab(c[len - i - 1]) || iscr(c[len - i - 1])))
      i++;
  else
    while ((i < len) && isblanktab(c[len - i - 1]))
      i++;

  if (i > 0)
    s.Remove(len - i, i);

  return s;
}
Example #2
0
void
strip_back(std::string &s,
           bool newlines) {
  const char *c = s.c_str();
  int len       = s.length();
  int i         = 0;

  if (newlines)
    while ((i < len) && (isblanktab(c[len - i - 1]) || iscr(c[len - i - 1])))
      ++i;
  else
    while ((i < len) && isblanktab(c[len - i - 1]))
      ++i;

  if (i > 0)
    s.erase(len - i, i);
}
Example #3
0
void
strip(std::string &s,
      bool newlines) {
  const char *c = s.c_str();
  int i         = 0;

  if (newlines)
    while ((c[i] != 0) && (isblanktab(c[i]) || iscr(c[i])))
      i++;
  else
    while ((c[i] != 0) && isblanktab(c[i]))
      i++;

  if (i > 0)
    s.erase(0, i);

  strip_back(s, newlines);
}
Example #4
0
bitvalue_c::bitvalue_c(std::string s,
                       unsigned int allowed_bitlength) {
  if ((allowed_bitlength != 0) && ((allowed_bitlength % 8) != 0))
    throw mtx::invalid_parameter_x();

  unsigned int len = s.size();
  ba::to_lower(s);
  std::string s2;

  unsigned int i;
  for (i = 0; i < len; i++) {
    // Space or tab?
    if (isblanktab(s[i]))
      continue;

    // Skip hyphens and curly braces. Makes copy & paste a bit easier.
    if ((s[i] == '-') || (s[i] == '{') || (s[i] == '}'))
      continue;

    // Space or tab followed by "0x"? Then skip it.
    if (s.substr(i, 2) == "0x") {
      i++;
      continue;
    }

    // Invalid character?
    if (!ishexdigit(s[i]))
      throw mtx::bitvalue_parser_x(boost::format(Y("Not a hex digit at position %1%")) % i);

    // Input too long?
    if ((allowed_bitlength > 0) && ((s2.length() * 4) >= allowed_bitlength))
      throw mtx::bitvalue_parser_x(boost::format(Y("Input too long: %1% > %2%")) % (s2.length() * 4) % allowed_bitlength);

    // Store the value.
    s2 += s[i];
  }

  // Is half a byte or more missing?
  len = s2.length();
  if (((len % 2) != 0)
      ||
      ((allowed_bitlength != 0) && ((len * 4) < allowed_bitlength)))
    throw mtx::bitvalue_parser_x(Y("Missing one hex digit"));

  m_value = memory_c::alloc(len / 2);

  unsigned char *buffer = m_value->get_buffer();
  for (i = 0; i < len; i += 2)
    buffer[i / 2] = hextodec(s2[i]) << 4 | hextodec(s2[i + 1]);
}
Example #5
0
std::string &
shrink_whitespace(std::string &s) {
  size_t i                     = 0;
  bool previous_was_whitespace = false;
  while (s.length() > i) {
    if (!isblanktab(s[i])) {
      previous_was_whitespace = false;
      ++i;
      continue;
    }
    if (previous_was_whitespace)
      s.erase(i, 1);
    else {
      previous_was_whitespace = true;
      ++i;
    }
  }

  return s;
}
Example #6
0
int
base64_decode(const std::string &src,
              unsigned char *dst) {
  unsigned int pos     = 0;
  unsigned int dst_pos = 0;
  unsigned int pad     = 0;
  while (pos < src.size()) {
    unsigned char in[4];
    unsigned int in_pos = 0;

    while ((src.size() > pos) && (4 > in_pos)) {
      unsigned char c = (unsigned char)src[pos];
      ++pos;

      if ((('A' <= c) && ('Z' >= c)) || (('a' <= c) && ('z' >= 'c')) || (('0' <= c) && ('9' >= c)) || ('+' == c) || ('/' == c)) {
        in[in_pos] = c;
        ++in_pos;

      } else if (c == '=')
        pad++;

      else if (!isblanktab(c) && !iscr(c))
        return -1;
    }

    unsigned int values_idx;
    unsigned char values[4];
    for (values_idx = 0; values_idx < in_pos; values_idx++) {
      values[values_idx] =
          (('A' <= in[values_idx]) && ('Z' >= in[values_idx])) ? in[values_idx] - 'A'
        : (('a' <= in[values_idx]) && ('z' >= in[values_idx])) ? in[values_idx] - 'a' + 26
        : (('0' <= in[values_idx]) && ('9' >= in[values_idx])) ? in[values_idx] - '0' + 52
        :  ('+' == in[values_idx])                             ?  62
        :  ('/' == in[values_idx])                             ?  63
        :                                                        255;

      if (255 == values[values_idx])
        throw mtx::base64::invalid_data_x();
    }

    unsigned char mid[6];
    mid[0] = values[0] << 2;
    mid[1] = values[1] >> 4;
    mid[2] = values[1] << 4;
    mid[3] = values[2] >> 2;
    mid[4] = values[2] << 6;
    mid[5] = values[3];

    dst[dst_pos] = mid[0] | mid[1];
    dst_pos++;
    if (1 >= pad) {
      dst[dst_pos] = mid[2] | mid[3];
      dst_pos++;
      if (0 == pad) {
        dst[dst_pos] = mid[4] | mid[5];
        dst_pos++;
      }
    }

    if (0 != pad)
      return dst_pos;
  }

  return dst_pos;
}
Example #7
0
std::string
base64_decode(std::string const &src) {
  auto pos = 0u;
  auto pad = 0u;
  auto dst = std::string{};

  while (pos < src.size()) {
    unsigned char in[4];
    unsigned int in_pos = 0;

    while ((src.size() > pos) && (4 > in_pos)) {
      unsigned char c = (unsigned char)src[pos];
      ++pos;

      if ((('A' <= c) && ('Z' >= c)) || (('a' <= c) && ('z' >= 'c')) || (('0' <= c) && ('9' >= c)) || ('+' == c) || ('/' == c)) {
        in[in_pos] = c;
        ++in_pos;

      } else if (c == '=')
        pad++;

      else if (!isblanktab(c) && !iscr(c))
        throw mtx::base64::invalid_data_x{};
    }

    unsigned int values_idx;
    unsigned char values[4];
    memset(values, 0, 4);
    for (values_idx = 0; values_idx < in_pos; values_idx++) {
      values[values_idx] =
          (('A' <= in[values_idx]) && ('Z' >= in[values_idx])) ? in[values_idx] - 'A'
        : (('a' <= in[values_idx]) && ('z' >= in[values_idx])) ? in[values_idx] - 'a' + 26
        : (('0' <= in[values_idx]) && ('9' >= in[values_idx])) ? in[values_idx] - '0' + 52
        :  ('+' == in[values_idx])                             ?  62
        :  ('/' == in[values_idx])                             ?  63
        :                                                        255;

      if (255 == values[values_idx])
        throw mtx::base64::invalid_data_x{};
    }

    unsigned char mid[6];
    mid[0] = values[0] << 2;
    mid[1] = values[1] >> 4;
    mid[2] = values[1] << 4;
    mid[3] = values[2] >> 2;
    mid[4] = values[2] << 6;
    mid[5] = values[3];

    dst += mid[0] | mid[1];
    if (1 >= pad) {
      dst += mid[2] | mid[3];
      if (0 == pad)
        dst += mid[4] | mid[5];
    }

    if (0 != pad)
      break;
  }

  return dst;
}