Beispiel #1
0
/**
 * Convert ASCII encoding back to data
 * out_size must match exactly the size of the data before it was encoded.
 *
 * @param enc the encoding
 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
 * @param out location where to store the decoded data
 * @param out_size size of the output buffer @a out
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
 */
int
GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
			       void *out, size_t out_size)
{
  unsigned int rpos;
  unsigned int wpos;
  unsigned int bits;
  unsigned int vbit;
  int ret;
  int shift;
  unsigned char *uout;
  unsigned int encoded_len = out_size * 8;

  if (0 == enclen)
  {
    if (0 == out_size)
      return GNUNET_OK;
    return GNUNET_SYSERR;
  }
  uout = out;
  wpos = out_size;
  rpos = enclen;
  if ((encoded_len % 5) > 0)
  {
    vbit = encoded_len % 5; /* padding! */
    shift = 5 - vbit;
    bits = (ret = getValue__ (enc[--rpos])) >> (5 - (encoded_len % 5));
  }
Beispiel #2
0
/**
 * Convert ASCII encoding back to data
 * out_size must match exactly the size of the data before it was encoded.
 *
 * @param enc the encoding
 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
 * @param out location where to store the decoded data
 * @param out_size sizeof the output buffer
 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
 */
int
GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
                              unsigned char *out, size_t out_size)
{
  unsigned int rpos;
  unsigned int wpos;
  unsigned int bits;
  unsigned int vbit;
  int ret;
  int shift;
  int encoded_len = out_size * 8;
  if (encoded_len % 5 > 0)
  {
    vbit = encoded_len % 5; /* padding! */
    shift = 5 - vbit;
  }
  else
  {
    vbit = 0;
    shift = 0;
  }
  if ((encoded_len + shift) / 5 != enclen)
    return GNUNET_SYSERR;

  wpos = out_size;
  rpos = enclen;
  bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
  if (-1 == ret)
    return GNUNET_SYSERR;
  while (wpos > 0)
  {
    GNUNET_assert (rpos > 0);
    bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
    if (-1 == ret)
      return GNUNET_SYSERR;
    vbit += 5;
    if (vbit >= 8)
    {
      out[--wpos] = (unsigned char) bits;
      bits >>= 8;
      vbit -= 8;
    }
  }