@@ -34,7 +34,9 @@ static inline bool encode(std::string &value) {
3434 bool changed = false ;
3535 std::string::size_type count = 0 ;
3636 auto bytes_left = input_len;
37- unsigned char unicode[8 ];
37+ char unicode[8 ] = {0 }; /* 5 or 6 bytes for OLD standard unicode character and 1 byte for null terminator */
38+ /* 4 bytes are enough for standard UTF8 + '\0' */
39+ /* result can be only signed char array, so no need to cast later */
3840
3941 /* RFC3629 states that UTF-8 are encoded using sequences of 1 to 4 octets. */
4042 /* Max size per character should fit in 4 bytes */
@@ -81,10 +83,7 @@ static inline bool encode(std::string &value) {
8183 d = ((c & 0x1F ) << 6 ) | (*(utf + 1 ) & 0x3F );
8284 *data++ = ' %' ;
8385 *data++ = ' u' ;
84- snprintf (reinterpret_cast <char *>(unicode),
85- sizeof (reinterpret_cast <char *>(unicode)),
86- " %x" , d);
87- length = strlen (reinterpret_cast <char *>(unicode));
86+ length = snprintf (unicode, sizeof (unicode), " %x" , d);
8887
8988 switch (length) {
9089 case 1 :
@@ -104,7 +103,7 @@ static inline bool encode(std::string &value) {
104103 break ;
105104 }
106105
107- for (std::string::size_type j = 0 ; j < length; j++) {
106+ for (int j = 0 ; j < length; j++) {
108107 *data++ = unicode[j];
109108 }
110109
@@ -133,10 +132,7 @@ static inline bool encode(std::string &value) {
133132 | (*(utf + 2 ) & 0x3F );
134133 *data++ = ' %' ;
135134 *data++ = ' u' ;
136- snprintf (reinterpret_cast <char *>(unicode),
137- sizeof (reinterpret_cast <char *>(unicode)),
138- " %x" , d);
139- length = strlen (reinterpret_cast <char *>(unicode));
135+ length = snprintf (unicode, sizeof (unicode), " %x" , d);
140136
141137 switch (length) {
142138 case 1 :
@@ -156,7 +152,7 @@ static inline bool encode(std::string &value) {
156152 break ;
157153 }
158154
159- for (std::string::size_type j = 0 ; j < length; j++) {
155+ for (int j = 0 ; j < length; j++) {
160156 *data++ = unicode[j];
161157 }
162158
@@ -195,10 +191,7 @@ static inline bool encode(std::string &value) {
195191 | (*(utf + 3 ) & 0x3F );
196192 *data++ = ' %' ;
197193 *data++ = ' u' ;
198- snprintf (reinterpret_cast <char *>(unicode),
199- sizeof (reinterpret_cast <char *>(unicode)),
200- " %x" , d);
201- length = strlen (reinterpret_cast <char *>(unicode));
194+ length = snprintf (unicode, sizeof (unicode), " %x" , d);
202195
203196 switch (length) {
204197 case 1 :
@@ -218,7 +211,7 @@ static inline bool encode(std::string &value) {
218211 break ;
219212 }
220213
221- for (std::string::size_type j = 0 ; j < length; j++) {
214+ for (int j = 0 ; j < length; j++) {
222215 *data++ = unicode[j];
223216 }
224217
0 commit comments