1*84d9c625SLionel Sambuc /* $NetBSD: util2.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */ 2*84d9c625SLionel Sambuc #include "config.h" 3*84d9c625SLionel Sambuc 4*84d9c625SLionel Sambuc #include <sys/types.h> 5*84d9c625SLionel Sambuc #include <sys/queue.h> 6*84d9c625SLionel Sambuc #include <sys/time.h> 7*84d9c625SLionel Sambuc 8*84d9c625SLionel Sambuc #include <bitstring.h> 9*84d9c625SLionel Sambuc #include <errno.h> 10*84d9c625SLionel Sambuc #include <limits.h> 11*84d9c625SLionel Sambuc #include <stdio.h> 12*84d9c625SLionel Sambuc #include <stdlib.h> 13*84d9c625SLionel Sambuc #include <string.h> 14*84d9c625SLionel Sambuc #include <unistd.h> 15*84d9c625SLionel Sambuc 16*84d9c625SLionel Sambuc #include "multibyte.h" 17*84d9c625SLionel Sambuc 18*84d9c625SLionel Sambuc int ucs2utf8(const CHAR_T * src,size_t len,char * dst)19*84d9c625SLionel Sambucucs2utf8(const CHAR_T *src, size_t len, char *dst) 20*84d9c625SLionel Sambuc { 21*84d9c625SLionel Sambuc int i, j; 22*84d9c625SLionel Sambuc 23*84d9c625SLionel Sambuc for (i = 0, j = 0; i < len; ++i) { 24*84d9c625SLionel Sambuc if (src[i] < 0x80) 25*84d9c625SLionel Sambuc dst[j++] = src[i]; 26*84d9c625SLionel Sambuc else if (src[i] < 0x800) { 27*84d9c625SLionel Sambuc dst[j++] = (src[i] >> 6) | 0xc0; 28*84d9c625SLionel Sambuc dst[j++] = (src[i] & 0x3f) | 0x80; 29*84d9c625SLionel Sambuc } else { 30*84d9c625SLionel Sambuc dst[j++] = (src[i] >> 12) | 0xe0; 31*84d9c625SLionel Sambuc dst[j++] = ((src[i] >> 6) & 0x3f) | 0x80; 32*84d9c625SLionel Sambuc dst[j++] = (src[i] & 0x3f) | 0x80; 33*84d9c625SLionel Sambuc } 34*84d9c625SLionel Sambuc } 35*84d9c625SLionel Sambuc 36*84d9c625SLionel Sambuc return j; 37*84d9c625SLionel Sambuc } 38