1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "netcdf.h"
#include "ncutf8.h"
#include "utf8proc.h"
/* Provide a wrapper around whatever utf8 library we use. */
/*
* Check validity of a UTF8 encoded null-terminated byte string.
* Return codes:
* NC_NOERR -- string is valid utf8
* NC_ENOMEM -- out of memory
* NC_EINVAL -- invalid argument or internal error
* NC_EBADNAME-- not valid utf8
*/
int nc_utf8_validate(const unsigned char* name)
{
int ncstat = NC_NOERR;
const nc_utf8proc_uint8_t *str;
nc_utf8proc_ssize_t nchars = -1;
nc_utf8proc_int32_t codepoint;
nc_utf8proc_ssize_t count;
str = (const nc_utf8proc_uint8_t*)name;
while(*str) {
count = nc_utf8proc_iterate(str,nchars,&codepoint);
if(count < 0) {
switch (count) {
case UTF8PROC_ERROR_NOMEM:
case UTF8PROC_ERROR_OVERFLOW:
ncstat = NC_ENOMEM;
break;
case UTF8PROC_ERROR_INVALIDOPTS:
ncstat = NC_EINVAL;
break;
case UTF8PROC_ERROR_INVALIDUTF8:
case UTF8PROC_ERROR_NOTASSIGNED:
default:
ncstat = NC_EBADNAME;
break;
}
goto done;
} else { /* move to next char */
str += count;
}
}
done:
return ncstat;
}
/*
* Returns a pointer to newly allocated memory of a
* normalized version of the null-terminated string 'str'.
* Normalized string is returned in normalp argument;
* caller must free.
* Return codes:
* NC_NOERR -- success
* NC_ENOMEM -- out of memory
* NC_EINVAL -- illegal argument or internal error
* NC_EBADNAME -- other failure
*/
int
nc_utf8_normalize(const unsigned char* utf8, unsigned char** normalp)
{
int ncstat = NC_NOERR;
const nc_utf8proc_uint8_t* str = (const nc_utf8proc_uint8_t*)utf8;
nc_utf8proc_uint8_t* retval = NULL;
nc_utf8proc_ssize_t count;
count = nc_utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE | UTF8PROC_COMPOSE);
if(count < 0) {/* error */
switch (count) {
case UTF8PROC_ERROR_NOMEM:
case UTF8PROC_ERROR_OVERFLOW:
ncstat = NC_ENOMEM;
break;
case UTF8PROC_ERROR_INVALIDOPTS:
ncstat = NC_EINVAL;
break;
case UTF8PROC_ERROR_INVALIDUTF8:
case UTF8PROC_ERROR_NOTASSIGNED:
default:
ncstat = NC_EBADNAME;
break;
}
goto done;
} else
if(normalp) *normalp = (unsigned char*)retval;
done:
return ncstat;
}
/*
* Convert a normalized utf8 string to utf16. This is approximate
* because it just does the truncation version of conversion for
* each 32-bit codepoint to get the corresponding utf16.
* Return codes:
* NC_NOERR -- success
* NC_ENOMEM -- out of memory
* NC_EINVAL -- invalid argument or internal error
* NC_EBADNAME-- not valid utf16
*/
int nc_utf8_to_utf16(const unsigned char* s8, unsigned short** utf16p, size_t* len16p)
{
int ncstat = NC_NOERR;
const nc_utf8proc_uint8_t *str;
nc_utf8proc_ssize_t nchars = -1;
nc_utf8proc_int32_t codepoint;
nc_utf8proc_ssize_t count;
size_t len8, len16;
unsigned short* utf16;
unsigned short* p16;
len8 = strlen((char*)s8);
utf16 = (unsigned short*)malloc(sizeof(unsigned short)*(len8+1));
if(utf16 == NULL) {
ncstat = NC_ENOMEM;
goto done;
}
str = (const nc_utf8proc_uint8_t*)s8;
/* Walk the string and convert each codepoint */
p16 = utf16;
len16 = 0;
while(*str) {
count = nc_utf8proc_iterate(str,nchars,&codepoint);
if(count < 0) {
switch (count) {
case UTF8PROC_ERROR_NOMEM:
case UTF8PROC_ERROR_OVERFLOW:
ncstat = NC_ENOMEM;
break;
case UTF8PROC_ERROR_INVALIDOPTS:
ncstat = NC_EINVAL;
break;
case UTF8PROC_ERROR_INVALIDUTF8:
case UTF8PROC_ERROR_NOTASSIGNED:
default:
ncstat = NC_EBADNAME;
break;
}
goto done;
} else { /* move to next char */
/* Complain if top 16 bits not zero */
if((codepoint & 0xFFFF0000) != 0) {
ncstat = NC_EBADNAME;
goto done;
}
/* Truncate codepoint to 16 bits and store */
*p16++ = (unsigned short)(codepoint & 0x0000FFFF);
str += count;
len16++;
}
}
*p16++ = (unsigned short)0;
if(utf16p)
*utf16p = utf16;
else
free(utf16);
if(len16p) *len16p = len16;
done:
if(ncstat) free(utf16);
return ncstat;
}
|