mpi_print (estream_t fp, gcry_mpi_t a, int mode)
{
  int n=0;

  if (!a)
    return es_fprintf (fp, "[MPI_NULL]");
  if (!mode)
    {
      unsigned int n1;
      n1 = gcry_mpi_get_nbits(a);
      n += es_fprintf (fp, "[%u bits]", n1);
    }
  else if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
    {
      unsigned int nbits;
      unsigned char *p = gcry_mpi_get_opaque (a, &nbits);
      if (!p)
        n += es_fprintf (fp, "[invalid opaque value]");
      else
        {
          nbits = (nbits + 7)/8;
          for (; nbits; nbits--, p++)
            n += es_fprintf (fp, "%02X", *p);
        }
    }
  else
    {
      unsigned char *buffer;

      if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, a))
        BUG ();
      es_fputs (buffer, fp);
      n += strlen (buffer);
      gcry_free (buffer);
    }
  return n;
}
mpi_print (estream_t fp, gcry_mpi_t a, int mode)
{
  int n=0;

  if (!a)
    return es_fprintf (fp, "[MPI_NULL]");
  if (!mode)
    {
      unsigned int n1;
      n1 = gcry_mpi_get_nbits(a);
      n += es_fprintf (fp, "[%u bits]", n1);
    }
  else if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
    {
      unsigned int nbits;
      unsigned char *p = gcry_mpi_get_opaque (a, &nbits);
      if (!p)
        n += es_fprintf (fp, "[invalid opaque value]");
      else
        {
          nbits = (nbits + 7)/8;
          for (; nbits; nbits--, p++)
            n += es_fprintf (fp, "%02X", *p);
        }
    }
  else
    {
      unsigned char *buffer;

      if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, a))
        BUG ();
      es_fputs (buffer, fp);
      n += strlen (buffer);
      gcry_free (buffer);
    }
  return n;
}