Coverage Report

Created: 2025-11-09 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/libcommon/utf8_common.c
Line
Count
Source
1
/*
2
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
3
4
All rights reserved. This program and the accompanying materials
5
are made available under the terms of the Eclipse Public License 2.0
6
and Eclipse Distribution License v1.0 which accompany this distribution.
7
8
The Eclipse Public License is available at
9
   https://www.eclipse.org/legal/epl-2.0/
10
and the Eclipse Distribution License is available at
11
  http://www.eclipse.org/org/documents/edl-v10.php.
12
13
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14
15
Contributors:
16
   Roger Light - initial implementation.
17
*/
18
19
#include "config.h"
20
21
#include <stdio.h>
22
#include "mosquitto.h"
23
24
25
BROKER_EXPORT int mosquitto_validate_utf8(const char *str, int len)
26
0
{
27
0
  int i;
28
0
  int j;
29
0
  int codelen;
30
0
  int codepoint;
31
0
  const unsigned char *ustr = (const unsigned char *)str;
32
33
0
  if(!str){
34
0
    return MOSQ_ERR_INVAL;
35
0
  }
36
0
  if(len < 0 || len > 65536){
37
0
    return MOSQ_ERR_INVAL;
38
0
  }
39
40
0
  for(i=0; i<len; i++){
41
0
    if(ustr[i] == 0){
42
0
      return MOSQ_ERR_MALFORMED_UTF8;
43
0
    }else if(ustr[i] <= 0x7f){
44
0
      codelen = 1;
45
0
      codepoint = ustr[i];
46
0
    }else if((ustr[i] & 0xE0) == 0xC0){
47
      /* 110xxxxx - 2 byte sequence */
48
0
      if(ustr[i] == 0xC0 || ustr[i] == 0xC1){
49
        /* Invalid bytes */
50
0
        return MOSQ_ERR_MALFORMED_UTF8;
51
0
      }
52
0
      codelen = 2;
53
0
      codepoint = (ustr[i] & 0x1F);
54
0
    }else if((ustr[i] & 0xF0) == 0xE0){
55
      /* 1110xxxx - 3 byte sequence */
56
0
      codelen = 3;
57
0
      codepoint = (ustr[i] & 0x0F);
58
0
    }else if((ustr[i] & 0xF8) == 0xF0){
59
      /* 11110xxx - 4 byte sequence */
60
0
      if(ustr[i] > 0xF4){
61
        /* Invalid, this would produce values > 0x10FFFF. */
62
0
        return MOSQ_ERR_MALFORMED_UTF8;
63
0
      }
64
0
      codelen = 4;
65
0
      codepoint = (ustr[i] & 0x07);
66
0
    }else{
67
      /* Unexpected continuation byte. */
68
0
      return MOSQ_ERR_MALFORMED_UTF8;
69
0
    }
70
71
    /* Reconstruct full code point */
72
0
    if(i >= len-codelen+1){
73
      /* Not enough data */
74
0
      return MOSQ_ERR_MALFORMED_UTF8;
75
0
    }
76
0
    for(j=0; j<codelen-1; j++){
77
0
      if((ustr[++i] & 0xC0) != 0x80){
78
        /* Not a continuation byte */
79
0
        return MOSQ_ERR_MALFORMED_UTF8;
80
0
      }
81
0
      codepoint = (codepoint<<6) | (ustr[i] & 0x3F);
82
0
    }
83
84
    /* Check for UTF-16 high/low surrogates */
85
0
    if(codepoint >= 0xD800 && codepoint <= 0xDFFF){
86
0
      return MOSQ_ERR_MALFORMED_UTF8;
87
0
    }
88
89
    /* Check for overlong or out of range encodings */
90
    /* Checking codelen == 2 isn't necessary here, because it is already
91
     * covered above in the C0 and C1 checks.
92
     * if(codelen == 2 && codepoint < 0x0080){
93
     *   return MOSQ_ERR_MALFORMED_UTF8;
94
     * }else
95
    */
96
0
    if(codelen == 3 && codepoint < 0x0800){
97
0
      return MOSQ_ERR_MALFORMED_UTF8;
98
0
    }else if(codelen == 4 && (codepoint < 0x10000 || codepoint > 0x10FFFF)){
99
0
      return MOSQ_ERR_MALFORMED_UTF8;
100
0
    }
101
102
    /* Check for non-characters */
103
0
    if(codepoint >= 0xFDD0 && codepoint <= 0xFDEF){
104
0
      return MOSQ_ERR_MALFORMED_UTF8;
105
0
    }
106
0
    if((codepoint & 0xFFFF) == 0xFFFE || (codepoint & 0xFFFF) == 0xFFFF){
107
0
      return MOSQ_ERR_MALFORMED_UTF8;
108
0
    }
109
    /* Check for control characters */
110
0
    if(codepoint <= 0x001F || (codepoint >= 0x007F && codepoint <= 0x009F)){
111
0
      return MOSQ_ERR_MALFORMED_UTF8;
112
0
    }
113
0
  }
114
0
  return MOSQ_ERR_SUCCESS;
115
0
}