Esempio n. 1
0
gchar *base64_encode (guint len, const gchar *str, gboolean split_lines)
{
  guint i;
  GString *tmp;

  /* TODO: calculate requisite output string length and allocate that big a
   * GString */
  tmp = g_string_new ("");

  for (i = 0; i < len; i += 3)
    {
      guint c1, c2, c3, c4;

      if (split_lines && i > 0 && (i * 4) % 76 == 0)
          g_string_append_c (tmp, '\n');

      switch (i + 3 - len)
        {
        case 1:
          c1 = encoding[GET_6_BITS_0 (str + i)];
          c2 = encoding[GET_6_BITS_1 (str + i)];
          /* can't use GET_6_BITS_2 because str[i+2] is out of range */
          c3 = encoding[(str[i + 1] & 0x0f) << 2];
          c4 = '=';
          break;
        case 2:
          c1 = encoding[GET_6_BITS_0 (str + i)];
          /* can't use GET_6_BITS_1 because str[i+1] is out of range */
          c2 = encoding[(str[i] & 0x03) << 4];
          c3 = '=';
          c4 = '=';
          break;
        default:
          c1 = encoding[GET_6_BITS_0 (str + i)];
          c2 = encoding[GET_6_BITS_1 (str + i)];
          c3 = encoding[GET_6_BITS_2 (str + i)];
          c4 = encoding[GET_6_BITS_3 (str + i)];
        }

      g_string_append_printf (tmp, "%c%c%c%c", c1, c2, c3, c4);
    }

  return g_string_free (tmp, FALSE);
}
Esempio n. 2
0
gchar *_lm_base64_encode (const gchar *txt, gsize n)
{
  guint i;
  guint len;
  GString *tmp;
  GString *str = g_string_new_len (txt, n);

  len = str->len;
  /* TODO: calculate requisite output string length and allocate that big a
   * GString */
  tmp = g_string_new ("");

  for (i = 0; i < len; i += 3)
    {
      guint c1, c2, c3, c4;

      switch (i + 3 - len)
        {
        case 1:
          c1 = encoding[GET_6_BITS_0 (str->str + i)];
          c2 = encoding[GET_6_BITS_1 (str->str + i)];
          c3 = encoding[GET_6_BITS_2 (str->str + i)];
          c4 = '=';
          break;
        case 2:
          c1 = encoding[GET_6_BITS_0 (str->str + i)];
          c2 = encoding[GET_6_BITS_1 (str->str + i)];
          c3 = '=';
          c4 = '=';
          break;
        default:
          c1 = encoding[GET_6_BITS_0 (str->str + i)];
          c2 = encoding[GET_6_BITS_1 (str->str + i)];
          c3 = encoding[GET_6_BITS_2 (str->str + i)];
          c4 = encoding[GET_6_BITS_3 (str->str + i)];
        }

      g_string_append_printf (tmp, "%c%c%c%c", c1, c2, c3, c4);
    }

  return g_string_free (tmp, FALSE);
}