MCPcopy Create free account
hub / github.com/antirez/ds4 / dist_json_utf8_valid

Function dist_json_utf8_valid

ds4_distributed.c:2795–2821  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2793}
2794
2795static bool dist_json_utf8_valid(const char *s, size_t n) {
2796 for (size_t i = 0; i < n;) {
2797 unsigned char c = (unsigned char)s[i];
2798 if (c < 0x80) {
2799 i++;
2800 continue;
2801 }
2802 int need = 0;
2803 if ((c & 0xe0) == 0xc0) need = 2;
2804 else if ((c & 0xf0) == 0xe0) need = 3;
2805 else if ((c & 0xf8) == 0xf0) need = 4;
2806 else return false;
2807 if (i + (size_t)need > n) return false;
2808 unsigned char c1 = (unsigned char)s[i + 1u];
2809 if ((c1 & 0xc0) != 0x80) return false;
2810 if (need == 2 && c < 0xc2) return false;
2811 if (need == 3 && c == 0xe0 && c1 < 0xa0) return false;
2812 if (need == 3 && c == 0xed && c1 >= 0xa0) return false;
2813 if (need == 4 && c == 0xf0 && c1 < 0x90) return false;
2814 if (need == 4 && c == 0xf4 && c1 >= 0x90) return false;
2815 for (int j = 2; j < need; j++) {
2816 if ((((unsigned char)s[i + (size_t)j]) & 0xc0) != 0x80) return false;
2817 }
2818 i += (size_t)need;
2819 }
2820 return true;
2821}
2822
2823static void dist_json_write_string(FILE *fp, const char *s, size_t n) {
2824 const bool valid_utf8 = dist_json_utf8_valid(s, n);

Callers 1

dist_json_write_stringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected