view mcabber/src/utf8.c @ 256:7d1a4aff62d4

# HG changeset patch # User mikael@frmp8452 # Node ID 9a74425dc51f6d0551fa2b5851ee381c63d7bb4e # Parent e8f9abe7dc3fbc03c95cacdd7a6c760d87664ea1 Cosmetic change Fix case in MCabber... diff -r e8f9abe7dc3f -r 9a74425dc51f mcabber/AUTHORS
author mikael@frmp8452
date Thu, 30 Jun 2005 23:07:20 +0100
parents 18a03a69f5e4
children 1bc374915787 33b8e801ffa6
line wrap: on
line source

#include <stdlib.h>
#include <string.h>

#include "utf8.h"


/* Desc: convert UTF8 -> ASCII
 * 
 * In  : UTF8 string
 * Out : ASCII string
 *
 * Note: it is up to the caller to free the returned string
 */
char *utf8_decode(const char *src)
{
  char *ret = calloc(1, strlen(src) + 1);
  unsigned char *aux = (unsigned char*)ret;

  while (*src) {
    unsigned char lead = *src++;
    if ((lead & 0xe0) == 0xc0) {
      unsigned char ch2 = *src++;
      *aux = ((lead & 0x1f) << 6) | (ch2 & 0x3f);
    } else {
      *aux = lead;
    }
    aux++;
  }

  return ret;
}


/* Desc: convert ASCII -> UTF8
 * 
 * In  : ASCII string
 * Out : UTF8 string
 *
 * Note: it is up to the caller to free the returned string
 */
char *utf8_encode(const char *src)
{
  char *ret = calloc(1, (strlen(src) * 2) + 1);
  unsigned char *aux = (unsigned char*)ret;

  while (*src) {
    unsigned char ch = *src++;
    if (ch < 0x80U) {
      *aux++ = ch;
    } else {  /* if (ch < 0x800U) { */
      *aux++ = 0xc0 | (ch >> 6);
      *aux++ = 0x80 | (ch & 0x3f);
    }
  }

  return ret;
}