| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#define KATE_INTERNAL |
| 11 |
#include "kate_internal.h" |
| 12 |
|
| 13 |
#ifdef HAVE_STRING_H |
| 14 |
#include <string.h> |
| 15 |
#endif |
| 16 |
#include "kate/kate.h" |
| 17 |
|
| 18 |
/* #define ENABLE_CODE_POINTS_ABOVE_0x10ffff */ |
| 19 |
|
| 20 |
inline int kate_is_valid_code_point(int c) |
| 21 |
{ |
| 22 |
/* surrogate range is invalid */ |
| 23 |
if (c>=0xd800 && c<=0xdfff) return 0; /* UTF-16 surrogates */ |
| 24 |
if (c>=0xfdd0 && c<=0xfdef) return 0; |
| 25 |
if ((c&0xfffe)==0xfffe) return 0; /* the two last code points of a plane are invalid */ |
| 26 |
|
| 27 |
#ifdef ENABLE_CODE_POINTS_ABOVE_0x10ffff |
| 28 |
return c>=0 && c<=0x7fffffff; |
| 29 |
#else |
| 30 |
return c>=0 && c<=0x10ffff; |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
static inline int get_bytes_for_code_point(int c) __attribute__((const)); |
| 35 |
static inline int get_bytes_for_code_point(int c) |
| 36 |
{ |
| 37 |
if (!kate_is_valid_code_point(c)) return -1; |
| 38 |
if (c<=0x7f) return 1; |
| 39 |
if (c<=0x7ff) return 2; |
| 40 |
if (c<=0xffff) return 3; |
| 41 |
#ifdef ENABLE_CODE_POINTS_ABOVE_0x10ffff |
| 42 |
if (c<=0x001fffff) return 4; |
| 43 |
if (c<=0x03ffffff) return 5; |
| 44 |
if (c<=0x7fffffff) return 6; |
| 45 |
#else |
| 46 |
if (c<=0x10ffff) return 4; |
| 47 |
#endif |
| 48 |
return -1; |
| 49 |
} |
| 50 |
|
| 51 |
static int kate_text_utf8_read(const char *s,int *cp) |
| 52 |
{ |
| 53 |
int c; |
| 54 |
|
| 55 |
if (!s) return KATE_E_INVALID_PARAMETER; |
| 56 |
|
| 57 |
c=0; |
| 58 |
|
| 59 |
if (((*s)&0x80)==0) { |
| 60 |
/* 1 byte */ |
| 61 |
c=*s; |
| 62 |
|
| 63 |
*cp=c; |
| 64 |
return 1; |
| 65 |
} |
| 66 |
else if (((*s)&0xe0)==0xc0) { |
| 67 |
/* 2 bytes */ |
| 68 |
c|=(((*s)&0x1f)<<6); |
| 69 |
s++; |
| 70 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 71 |
c|=((*s)&0x3f); |
| 72 |
if (c<=0x7f) return KATE_E_TEXT; |
| 73 |
|
| 74 |
*cp=c; |
| 75 |
return 2; |
| 76 |
} |
| 77 |
else if (((*s)&0xf0)==0xe0) { |
| 78 |
/* 3 bytes */ |
| 79 |
c|=(((*s)&0xf)<<12); |
| 80 |
s++; |
| 81 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 82 |
c|=(((*s)&0x3f)<<6); |
| 83 |
s++; |
| 84 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 85 |
c|=(((*s)&0x3f)); |
| 86 |
if (c<=0x7ff) return KATE_E_TEXT; |
| 87 |
|
| 88 |
*cp=c; |
| 89 |
return 3; |
| 90 |
} |
| 91 |
else if (((*s)&0xf8)==0xf0) { |
| 92 |
/* 4 bytes */ |
| 93 |
c|=(((*s)&0x7)<<18); |
| 94 |
s++; |
| 95 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 96 |
c|=(((*s)&0x3f)<<12); |
| 97 |
s++; |
| 98 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 99 |
c|=(((*s)&0x3f)<<6); |
| 100 |
s++; |
| 101 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 102 |
c|=(((*s)&0x3f)); |
| 103 |
if (c<=0xffff) return KATE_E_TEXT; |
| 104 |
|
| 105 |
*cp=c; |
| 106 |
return 4; |
| 107 |
} |
| 108 |
#ifdef ENABLE_CODE_POINTS_ABOVE_0x10ffff |
| 109 |
/* 5 and 6 bytes are for unicode code points not yet assigned */ |
| 110 |
else if (((*s)&0xfc)==0xf8) { |
| 111 |
/* 5 bytes */ |
| 112 |
c|=(((*s)&0x3)<<24); |
| 113 |
s++; |
| 114 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 115 |
c|=(((*s)&0x3f)<<18); |
| 116 |
s++; |
| 117 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 118 |
c|=(((*s)&0x3f)<<12); |
| 119 |
s++; |
| 120 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 121 |
c|=(((*s)&0x3f)<<6); |
| 122 |
s++; |
| 123 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 124 |
c|=(((*s)&0x3f)); |
| 125 |
if (c<=0x001fffff) return KATE_E_TEXT; |
| 126 |
|
| 127 |
*cp=c; |
| 128 |
return 5; |
| 129 |
} |
| 130 |
else if (((*s)&0xfe)==0xfc) { |
| 131 |
/* 6 bytes */ |
| 132 |
c|=(((*s)&0x1)<<30); |
| 133 |
s++; |
| 134 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 135 |
c|=(((*s)&0x3f)<<24); |
| 136 |
s++; |
| 137 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 138 |
c|=(((*s)&0x3f)<<18); |
| 139 |
s++; |
| 140 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 141 |
c|=(((*s)&0x3f)<<12); |
| 142 |
s++; |
| 143 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 144 |
c|=(((*s)&0x3f)<<6); |
| 145 |
s++; |
| 146 |
if (((*s)&0xc0)!=0x80) return KATE_E_TEXT; |
| 147 |
c|=(((*s)&0x3f)); |
| 148 |
if (c<=0x03ffffff) return KATE_E_TEXT; |
| 149 |
|
| 150 |
*cp=c; |
| 151 |
return 6; |
| 152 |
} |
| 153 |
#endif |
| 154 |
|
| 155 |
return KATE_E_TEXT; |
| 156 |
} |
| 157 |
|
| 158 |
static int kate_text_utf8_write(char *s,int cp) |
| 159 |
{ |
| 160 |
int bytes; |
| 161 |
|
| 162 |
if (!s) return KATE_E_INVALID_PARAMETER; |
| 163 |
if (!kate_is_valid_code_point(cp)) return KATE_E_INVALID_PARAMETER; |
| 164 |
|
| 165 |
bytes=get_bytes_for_code_point(cp); |
| 166 |
if (bytes<=0) return KATE_E_INVALID_PARAMETER; |
| 167 |
|
| 168 |
switch (bytes) { |
| 169 |
case 1: |
| 170 |
*s++=cp; |
| 171 |
break; |
| 172 |
case 2: |
| 173 |
*s++=0xc0 | (cp>>6); |
| 174 |
*s++=0x80 | (cp&0x3f); |
| 175 |
break; |
| 176 |
case 3: |
| 177 |
*s++=0xe0 | (cp>>12); |
| 178 |
*s++=0x80 | ((cp>>6)&0x3f); |
| 179 |
*s++=0x80 | (cp&0x3f); |
| 180 |
break; |
| 181 |
case 4: |
| 182 |
*s++=0xf0 | (cp>>18); |
| 183 |
*s++=0x80 | ((cp>>12)&0x3f); |
| 184 |
*s++=0x80 | ((cp>>6)&0x3f); |
| 185 |
*s++=0x80 | (cp&0x3f); |
| 186 |
break; |
| 187 |
#ifdef ENABLE_CODE_POINTS_ABOVE_0x10ffff |
| 188 |
case 5: |
| 189 |
*s++=0xf8 | (cp>>24); |
| 190 |
*s++=0x80 | ((cp>>18)&0x3f); |
| 191 |
*s++=0x80 | ((cp>>12)&0x3f); |
| 192 |
*s++=0x80 | ((cp>>6)&0x3f); |
| 193 |
*s++=0x80 | (cp&0x3f); |
| 194 |
break; |
| 195 |
case 6: |
| 196 |
*s++=0xfc | (cp>>30); |
| 197 |
*s++=0x80 | ((cp>>24)&0x3f); |
| 198 |
*s++=0x80 | ((cp>>18)&0x3f); |
| 199 |
*s++=0x80 | ((cp>>12)&0x3f); |
| 200 |
*s++=0x80 | ((cp>>6)&0x3f); |
| 201 |
*s++=0x80 | (cp&0x3f); |
| 202 |
break; |
| 203 |
#endif |
| 204 |
default: |
| 205 |
return KATE_E_INVALID_PARAMETER; |
| 206 |
} |
| 207 |
return bytes; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
\ingroup text |
| 212 |
Reads a code point from the string, advancing the text pointer to the next one. |
| 213 |
\param text_encoding the character encoding the text is coded in |
| 214 |
\param text a pointer to the text pointer, to be advanced to the next character on success |
| 215 |
\param len0 a pointer to the length of the buffer, including any terminating NUL, to be decreased by the number of bytes that text is advanced on success |
| 216 |
\returns >=0 success, the unicode code point read |
| 217 |
\returns KATE_E_* error |
| 218 |
*/ |
| 219 |
int kate_text_get_character(kate_text_encoding text_encoding,const char ** const text,size_t *len0) |
| 220 |
{ |
| 221 |
const char *new_text; |
| 222 |
int c,ret; |
| 223 |
size_t bytes; |
| 224 |
|
| 225 |
if (!text || !len0) return KATE_E_INVALID_PARAMETER; |
| 226 |
|
| 227 |
switch (text_encoding) { |
| 228 |
case kate_utf8: |
| 229 |
new_text=*text; |
| 230 |
ret=kate_text_utf8_read(new_text,&c); |
| 231 |
if (ret<0) return ret; |
| 232 |
bytes=ret; |
| 233 |
if (bytes>*len0) return KATE_E_TEXT; |
| 234 |
*len0-=bytes; |
| 235 |
*text+=bytes; |
| 236 |
return c; |
| 237 |
default: |
| 238 |
return KATE_E_INVALID_PARAMETER; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
\ingroup text |
| 244 |
Writes a code point to the given string, advancing the text pointer to the next byte. |
| 245 |
\param text_encoding the character encoding the text is coded in |
| 246 |
\param c the unicode code point to write to the string |
| 247 |
\param text a pointer to the text pointer, to be advanced to the next character on success |
| 248 |
\param len0 a pointer to the length of the buffer, including any terminating NUL, to be decreased by the number of bytes that text is advanced on success |
| 249 |
\returns >=0 success, the number of bytes used to write this code point |
| 250 |
\returns KATE_E_* error |
| 251 |
*/ |
| 252 |
int kate_text_set_character(kate_text_encoding text_encoding,int c,char ** const text,size_t *len0) |
| 253 |
{ |
| 254 |
char tmp[8]={0}; |
| 255 |
size_t bytes; |
| 256 |
int ret; |
| 257 |
|
| 258 |
if (!text || !len0) return KATE_E_INVALID_PARAMETER; |
| 259 |
|
| 260 |
switch (text_encoding) { |
| 261 |
case kate_utf8: |
| 262 |
ret=kate_text_utf8_write(tmp,c); |
| 263 |
if (ret<0) return ret; |
| 264 |
bytes=ret; |
| 265 |
if (bytes>*len0) return KATE_E_TEXT; |
| 266 |
memcpy(*text,tmp,bytes); |
| 267 |
*text+=bytes; |
| 268 |
*len0-=bytes; |
| 269 |
return bytes; |
| 270 |
default: |
| 271 |
return KATE_E_INVALID_PARAMETER; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
\ingroup text |
| 277 |
Removes markup from the given text. |
| 278 |
\param text_encoding the character encoding the text is coded in |
| 279 |
\param text the text to remove markup from |
| 280 |
\param len0 the length in bytes of the text, including any terminating NUL - will be set to the length of the text with markup removed, including any terminating NUL |
| 281 |
\returns 0 success |
| 282 |
\returns KATE_E_* error |
| 283 |
*/ |
| 284 |
int kate_text_remove_markup(kate_text_encoding text_encoding,char *text,size_t *len0) |
| 285 |
{ |
| 286 |
char *r=text,*w=text; |
| 287 |
int in_tag=0; |
| 288 |
size_t n; |
| 289 |
|
| 290 |
if (!text || !len0) return KATE_E_INVALID_PARAMETER; |
| 291 |
|
| 292 |
switch (text_encoding) { |
| 293 |
case kate_utf8: |
| 294 |
while (*r && (size_t)(r-text)<*len0) { |
| 295 |
int ret,c; |
| 296 |
ret=kate_text_utf8_read(r,&c); |
| 297 |
if (ret<0) return ret; |
| 298 |
r+=ret; |
| 299 |
if (r>text+*len0) { |
| 300 |
/* we went over the limit, we probably read dummy data, discard */ |
| 301 |
break; |
| 302 |
} |
| 303 |
if (c=='<') { |
| 304 |
in_tag++; |
| 305 |
/* add a newline if we just removed a br */ |
| 306 |
if (*len0>=3 && !strncmp(r,"br>",3)) { |
| 307 |
ret=kate_text_utf8_write(w,'\n'); |
| 308 |
if (ret<0) return ret; |
| 309 |
w+=ret; |
| 310 |
} |
| 311 |
} |
| 312 |
if (!in_tag) { |
| 313 |
ret=kate_text_utf8_write(w,c); |
| 314 |
if (ret<0) return ret; |
| 315 |
w+=ret; |
| 316 |
} |
| 317 |
if (c=='>') { |
| 318 |
in_tag--; |
| 319 |
} |
| 320 |
} |
| 321 |
/* zero all bytes we removed */ |
| 322 |
for (n=0;n<*len0-(w-text);++n) w[n]=0; |
| 323 |
/* adjust len0 to new size */ |
| 324 |
*len0=w-text; |
| 325 |
break; |
| 326 |
default: |
| 327 |
return KATE_E_INVALID_PARAMETER; |
| 328 |
} |
| 329 |
|
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
\ingroup text |
| 335 |
Validates text for the given character encoding, flagging partial sequences and invalid sequences. |
| 336 |
\param text_encoding the character encoding the text is coded in |
| 337 |
\param text the text to validate (may include embedded NULs) |
| 338 |
\param len0 the length in bytes of the text, including any terminating NUL |
| 339 |
\returns 0 success |
| 340 |
\returns KATE_E_* error |
| 341 |
*/ |
| 342 |
int kate_text_validate(kate_text_encoding text_encoding,const char *text,size_t len0) |
| 343 |
{ |
| 344 |
if (!text) return KATE_E_INVALID_PARAMETER; |
| 345 |
|
| 346 |
switch (text_encoding) { |
| 347 |
case kate_utf8: |
| 348 |
while (len0>0) { |
| 349 |
int ret,c; |
| 350 |
ret=kate_text_utf8_read(text,&c); |
| 351 |
if (ret<0) return ret; |
| 352 |
if (!kate_is_valid_code_point(c)) return KATE_E_TEXT; |
| 353 |
if ((size_t)ret>len0) { |
| 354 |
/* we went over the limit, we probably read dummy data, discard */ |
| 355 |
return KATE_E_TEXT; |
| 356 |
} |
| 357 |
text+=ret; |
| 358 |
len0-=ret; |
| 359 |
} |
| 360 |
break; |
| 361 |
default: |
| 362 |
return KATE_E_INVALID_PARAMETER; |
| 363 |
} |
| 364 |
|
| 365 |
return 0; |
| 366 |
} |
| 367 |
|