Path: blob/master/compat/jansson/utf.c
1295 views
/*1* Copyright (c) 2009-2013 Petri Lehtinen <[email protected]>2*3* Jansson is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include <string.h>8#include "utf.h"910int utf8_encode(int32_t codepoint, char *buffer, int *size)11{12if(codepoint < 0)13return -1;14else if(codepoint < 0x80)15{16buffer[0] = (char)codepoint;17*size = 1;18}19else if(codepoint < 0x800)20{21buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);22buffer[1] = 0x80 + ((codepoint & 0x03F));23*size = 2;24}25else if(codepoint < 0x10000)26{27buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);28buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);29buffer[2] = 0x80 + ((codepoint & 0x003F));30*size = 3;31}32else if(codepoint <= 0x10FFFF)33{34buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);35buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);36buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);37buffer[3] = 0x80 + ((codepoint & 0x00003F));38*size = 4;39}40else41return -1;4243return 0;44}4546int utf8_check_first(char byte)47{48unsigned char u = (unsigned char)byte;4950if(u < 0x80)51return 1;5253if(0x80 <= u && u <= 0xBF) {54/* second, third or fourth byte of a multi-byte55sequence, i.e. a "continuation byte" */56return 0;57}58else if(u == 0xC0 || u == 0xC1) {59/* overlong encoding of an ASCII byte */60return 0;61}62else if(0xC2 <= u && u <= 0xDF) {63/* 2-byte sequence */64return 2;65}6667else if(0xE0 <= u && u <= 0xEF) {68/* 3-byte sequence */69return 3;70}71else if(0xF0 <= u && u <= 0xF4) {72/* 4-byte sequence */73return 4;74}75else { /* u >= 0xF5 */76/* Restricted (start of 4-, 5- or 6-byte sequence) or invalid77UTF-8 */78return 0;79}80}8182int utf8_check_full(const char *buffer, int size, int32_t *codepoint)83{84int i;85int32_t value = 0;86unsigned char u = (unsigned char)buffer[0];8788if(size == 2)89{90value = u & 0x1F;91}92else if(size == 3)93{94value = u & 0xF;95}96else if(size == 4)97{98value = u & 0x7;99}100else101return 0;102103for(i = 1; i < size; i++)104{105u = (unsigned char)buffer[i];106107if(u < 0x80 || u > 0xBF) {108/* not a continuation byte */109return 0;110}111112value = (value << 6) + (u & 0x3F);113}114115if(value > 0x10FFFF) {116/* not in Unicode range */117return 0;118}119120else if(0xD800 <= value && value <= 0xDFFF) {121/* invalid code point (UTF-16 surrogate halves) */122return 0;123}124125else if((size == 2 && value < 0x80) ||126(size == 3 && value < 0x800) ||127(size == 4 && value < 0x10000)) {128/* overlong encoding */129return 0;130}131132if(codepoint)133*codepoint = value;134135return 1;136}137138const char *utf8_iterate(const char *buffer, int32_t *codepoint)139{140int count;141int32_t value;142143if(!*buffer)144return buffer;145146count = utf8_check_first(buffer[0]);147if(count <= 0)148return NULL;149150if(count == 1)151value = (unsigned char)buffer[0];152else153{154if(!utf8_check_full(buffer, count, &value))155return NULL;156}157158if(codepoint)159*codepoint = value;160161return buffer + count;162}163164int utf8_check_string(const char *string, int length)165{166int i;167168if(length == -1)169length = strlen(string);170171for(i = 0; i < length; i++)172{173int count = utf8_check_first(string[i]);174if(count == 0)175return 0;176else if(count > 1)177{178if(i + count > length)179return 0;180181if(!utf8_check_full(&string[i], count, NULL))182return 0;183184i += count - 1;185}186}187188return 1;189}190191192