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