/*
 * utf8toucs2
 *
 * $Id: utf8toucs2.c,v 1.2 2002/05/19 19:11:47 patrice Exp $
 *
 * PostgreSQL user function to convert a utf8 char to a ucs2 int.
 *
 * 2002-05-19 Patrice Hédé <patrice.hede@islande.org>
 *   Initial Release
 *
 * Function created with :
 *   CREATE FUNCTION utf8toucs2 ( text )
 *   RETURNS int AS '/absolute/path/to/utf8toucs2.so'
 *   LANGUAGE 'c';
 *
 * typical use :
 *   SELECT utf8toucs2( "é" );
 */

#include "postgres.h"
#include "fmgr.h"
#include <string.h>

/*
 * int4 utf8toucs2(text)
 */

PG_FUNCTION_INFO_V1(utf8toucs2);

Datum utf8toucs2(PG_FUNCTION_ARGS) {
  text *src_string = PG_GETARG_TEXT_P(0);
  int4 ucs2 = 0;
  unsigned char *src = (unsigned char *)(src_string->vl_dat);

  if(!src_string) {
    PG_RETURN_INT32(-1);
  }

  if(*src < 0x80) {
    PG_RETURN_INT32(*src);
  }
  else if(*src <= 0xc1) {
    PG_RETURN_INT32(-1);
  }
  else if(*src <= 0xdf) {
    if((src_string->vl_len >= 2) &&
       (src[1] >= 0x80) && (src[1] <= 0xbf)) {
      ucs2=((src[0] & 0x1f) << 6) | (src[1] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  else if(*src == 0xe0) {
    if((src_string->vl_len >= 3) &&
       (src[1] >= 0xa0) && (src[1] <= 0xbf) &&
       (src[2] >= 0x80) && (src[2] <= 0xbf))  {
      ucs2=((src[0] & 0x0f) << 12) | ((src[1] & 0x3f) << 6)  | (src[2] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  else if(*src == 0xed) {
    if((src_string->vl_len >= 3) &&
       (src[1] >= 0x80) && (src[1] <= 0x9f) &&
       (src[2] >= 0x80) && (src[2] <= 0xbf))  {
      ucs2=((src[0] & 0x0f) << 12) | ((src[1] & 0x3f) << 6)  | (src[2] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  else if(*src <= 0xef) {
    if((src_string->vl_len >= 3) &&
       (src[1] >= 0x80) && (src[1] <= 0xbf) &&
       (src[2] >= 0x80) && (src[2] <= 0xbf))  {
      ucs2=((src[0] & 0x0f) << 12) | ((src[1] & 0x3f) << 6)  | (src[2] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  else if(*src == 0xf0) {
    if((src_string->vl_len >= 4) &&
       (src[1] >= 0x90) && (src[2] <= 0xbf) &&
       (src[2] >= 0x80) && (src[2] <= 0xbf) &&
       (src[3] >= 0x80) && (src[3] <= 0xbf)) {
      ucs2=((src[0] & 0x07) << 18) | ((src[1] & 0x3f) << 12) |
           ((src[2] & 0x3f) <<  6) | (src[3] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  else if(*src <= 0xf3) {
    if((src_string->vl_len >= 4) &&
       (src[1] >= 0x80) && (src[2] <= 0xbf) &&
       (src[2] >= 0x80) && (src[2] <= 0xbf) &&
       (src[3] >= 0x80) && (src[3] <= 0xbf)) {
      ucs2=((src[0] & 0x07) << 18) | ((src[1] & 0x3f) << 12) |
           ((src[2] & 0x3f) <<  6) | (src[3] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  else if(*src == 0xf4) {
    if((src_string->vl_len >= 4) &&
       (src[1] >= 0x80) && (src[2] <= 0x8f) &&
       (src[2] >= 0x80) && (src[2] <= 0xbf) &&
       (src[3] >= 0x80) && (src[3] <= 0xbf)) {
      ucs2=((src[0] & 0x07) << 18) | ((src[1] & 0x3f) << 12) |
           ((src[2] & 0x3f) <<  6) | (src[3] & 0x3f);
      PG_RETURN_INT32(ucs2);
    }
    else {
      PG_RETURN_INT32(-1);
    }
  }
  PG_RETURN_INT32(-1);
}
