general.c (628B)
1 #include <assert.h> 2 3 /* nombre d'octets en trop */ 4 int 5 utf8_trop(char *base, char *fin) 6 { 7 int i, n; 8 unsigned char c; 9 10 assert(base); 11 assert(fin); 12 assert(base <= fin); 13 14 if (((unsigned char) *base >> 6) == 0x2) 15 return -1; 16 17 for (i = 1; ((unsigned char) *fin >> 6) == 0x2; fin--) 18 i++; 19 c = *fin; 20 if (c <= 0x7F) /* un octet */ 21 n = 1; 22 else if (c >= 0xF0) /* quatre octets */ 23 n = 4; 24 else if (c >= 0xE0) /* trois octets */ 25 n = 3; 26 else if (c >= 0xC2) /* deux octets */ 27 n = 2; 28 else /* séquence invalide */ 29 n = 0;; 30 31 if (i < n) 32 return i; 33 else if (i == n) 34 return 0; 35 else 36 return n - i; 37 }