43
43
#if BSG_KSJSONCODEC_UseKSLogger
44
44
#include "BSG_KSLogger.h"
45
45
#else
46
- #define BSG_KSLOG_DEBUG (FMT , ...)
46
+ #define BSG_KSLOG_ERROR (FMT , ...)
47
47
#endif
48
48
49
+
49
50
/** The work buffer size to use when escaping string values.
50
51
* There's little reason to change this since nothing ever gets truncated.
51
52
*/
@@ -125,7 +126,7 @@ int bsg_ksjsoncodec_i_appendEscapedString(
125
126
src ++ ) {
126
127
* dst ++ = * src ;
127
128
}
128
-
129
+
129
130
// Deal with complicated case (if any)
130
131
for (; src < srcEnd ; src ++ ) {
131
132
switch (* src ) {
@@ -155,12 +156,23 @@ int bsg_ksjsoncodec_i_appendEscapedString(
155
156
* dst ++ = 't' ;
156
157
break ;
157
158
default :
158
- unlikely_if ((unsigned char )* src < ' ' ) {
159
- BSG_KSLOG_DEBUG ("Invalid character 0x%02x in string: %s" , * src ,
160
- string );
161
- return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
159
+
160
+ // escape control chars (U+0000 - U+001F)
161
+ // see https://www.ietf.org/rfc/rfc4627.txt
162
+
163
+ if ((unsigned char )* src < ' ' ) {
164
+ unsigned int last = * src % 16 ;
165
+ unsigned int first = (* src - last ) / 16 ;
166
+
167
+ * dst ++ = '\\' ;
168
+ * dst ++ = 'u' ;
169
+ * dst ++ = '0' ;
170
+ * dst ++ = '0' ;
171
+ * dst ++ = bsg_g_hexNybbles [first ];
172
+ * dst ++ = bsg_g_hexNybbles [last ];
173
+ } else {
174
+ * dst ++ = * src ;
162
175
}
163
- * dst ++ = * src ;
164
176
}
165
177
}
166
178
size_t encLength = (size_t )(dst - workBuffer );
@@ -252,7 +264,7 @@ int bsg_ksjsonbeginElement(BSG_KSJSONEncodeContext *const context,
252
264
// Add a name field if we're in an object.
253
265
if (context -> isObject [context -> containerLevel ]) {
254
266
unlikely_if (name == NULL ) {
255
- BSG_KSLOG_DEBUG ("Name was null inside an object" );
267
+ BSG_KSLOG_ERROR ("Name was null inside an object" );
256
268
return BSG_KSJSON_ERROR_INVALID_DATA ;
257
269
}
258
270
unlikely_if ((result = bsg_ksjsoncodec_i_addQuotedEscapedString (
@@ -323,7 +335,7 @@ int bsg_ksjsonaddJSONElement(BSG_KSJSONEncodeContext *const context,
323
335
idx ++ ;
324
336
}
325
337
unlikely_if (idx >= length ) {
326
- BSG_KSLOG_DEBUG ("JSON element contained no JSON data: %s" , element );
338
+ BSG_KSLOG_ERROR ("JSON element contained no JSON data: %s" , element );
327
339
return BSG_KSJSON_ERROR_INVALID_DATA ;
328
340
}
329
341
switch (element [idx ]) {
@@ -346,7 +358,7 @@ int bsg_ksjsonaddJSONElement(BSG_KSJSONEncodeContext *const context,
346
358
case '9' :
347
359
break ;
348
360
default :
349
- BSG_KSLOG_DEBUG ("Invalid character '%c' in: " , element [idx ], element );
361
+ BSG_KSLOG_ERROR ("Invalid character '%c' in: " , element [idx ], element );
350
362
return BSG_KSJSON_ERROR_INVALID_DATA ;
351
363
}
352
364
@@ -656,14 +668,14 @@ int bsg_ksjsoncodec_i_writeUTF8(unsigned int character, char **dst) {
656
668
}
657
669
658
670
// If we get here, the character cannot be converted to valid UTF-8.
659
- BSG_KSLOG_DEBUG ("Invalid unicode: 0x%04x" , character );
671
+ BSG_KSLOG_ERROR ("Invalid unicode: 0x%04x" , character );
660
672
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
661
673
}
662
674
663
675
int bsg_ksjsoncodec_i_decodeString (const char * * ptr , const char * const end ,
664
676
char * * dstString ) {
665
677
unlikely_if (* * ptr != '\"' ) {
666
- BSG_KSLOG_DEBUG ("Expected '\"' but got '%c'" , * * ptr );
678
+ BSG_KSLOG_ERROR ("Expected '\"' but got '%c'" , * * ptr );
667
679
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
668
680
}
669
681
@@ -677,7 +689,7 @@ int bsg_ksjsoncodec_i_decodeString(const char **ptr, const char *const end,
677
689
}
678
690
}
679
691
unlikely_if (src >= end ) {
680
- BSG_KSLOG_DEBUG ("Premature end of data" );
692
+ BSG_KSLOG_ERROR ("Premature end of data" );
681
693
return BSG_KSJSON_ERROR_INCOMPLETE ;
682
694
}
683
695
const char * const srcEnd = src ;
@@ -729,7 +741,7 @@ int bsg_ksjsoncodec_i_decodeString(const char **ptr, const char *const end,
729
741
continue ;
730
742
case 'u' : {
731
743
unlikely_if (src + 5 > srcEnd ) {
732
- BSG_KSLOG_DEBUG ("Premature end of data" );
744
+ BSG_KSLOG_ERROR ("Premature end of data" );
733
745
result = BSG_KSJSON_ERROR_INCOMPLETE ;
734
746
goto failed ;
735
747
}
@@ -738,15 +750,15 @@ int bsg_ksjsoncodec_i_decodeString(const char **ptr, const char *const end,
738
750
bsg_g_hexConversion [src [3 ]] << 4 |
739
751
bsg_g_hexConversion [src [4 ]];
740
752
unlikely_if (accum > 0xffff ) {
741
- BSG_KSLOG_DEBUG ("Invalid unicode sequence: %c%c%c%c" ,
753
+ BSG_KSLOG_ERROR ("Invalid unicode sequence: %c%c%c%c" ,
742
754
src [1 ], src [2 ], src [3 ], src [4 ]);
743
755
result = BSG_KSJSON_ERROR_INVALID_CHARACTER ;
744
756
goto failed ;
745
757
}
746
758
747
759
// UTF-16 Trail surrogate on its own.
748
760
unlikely_if (accum >= 0xdc00 && accum <= 0xdfff ) {
749
- BSG_KSLOG_DEBUG ("Unexpected trail surrogate: 0x%04x" ,
761
+ BSG_KSLOG_ERROR ("Unexpected trail surrogate: 0x%04x" ,
750
762
accum );
751
763
result = BSG_KSJSON_ERROR_INVALID_CHARACTER ;
752
764
goto failed ;
@@ -756,12 +768,12 @@ int bsg_ksjsoncodec_i_decodeString(const char **ptr, const char *const end,
756
768
unlikely_if (accum >= 0xd800 && accum <= 0xdbff ) {
757
769
// Fetch trail surrogate.
758
770
unlikely_if (src + 11 > srcEnd ) {
759
- BSG_KSLOG_DEBUG ("Premature end of data" );
771
+ BSG_KSLOG_ERROR ("Premature end of data" );
760
772
result = BSG_KSJSON_ERROR_INCOMPLETE ;
761
773
goto failed ;
762
774
}
763
775
unlikely_if (src [5 ] != '\\' || src [6 ] != 'u' ) {
764
- BSG_KSLOG_DEBUG ("Expected \"\\u\" but got: \"%c%c\"" ,
776
+ BSG_KSLOG_ERROR ("Expected \"\\u\" but got: \"%c%c\"" ,
765
777
src [5 ], src [6 ]);
766
778
result = BSG_KSJSON_ERROR_INVALID_CHARACTER ;
767
779
goto failed ;
@@ -772,7 +784,7 @@ int bsg_ksjsoncodec_i_decodeString(const char **ptr, const char *const end,
772
784
bsg_g_hexConversion [src [3 ]] << 4 |
773
785
bsg_g_hexConversion [src [4 ]];
774
786
unlikely_if (accum2 < 0xdc00 || accum2 > 0xdfff ) {
775
- BSG_KSLOG_DEBUG ("Invalid trail surrogate: 0x%04x" ,
787
+ BSG_KSLOG_ERROR ("Invalid trail surrogate: 0x%04x" ,
776
788
accum2 );
777
789
result = BSG_KSJSON_ERROR_INVALID_CHARACTER ;
778
790
goto failed ;
@@ -787,7 +799,7 @@ int bsg_ksjsoncodec_i_decodeString(const char **ptr, const char *const end,
787
799
continue ;
788
800
}
789
801
default :
790
- BSG_KSLOG_DEBUG ("Invalid control character '%c'" , * src );
802
+ BSG_KSLOG_ERROR ("Invalid control character '%c'" , * src );
791
803
result = BSG_KSJSON_ERROR_INVALID_CHARACTER ;
792
804
goto failed ;
793
805
}
@@ -811,7 +823,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
811
823
void * const userData ) {
812
824
skipWhitespace (ptr , end );
813
825
unlikely_if (* ptr >= end ) {
814
- BSG_KSLOG_DEBUG ("Premature end of data" );
826
+ BSG_KSLOG_ERROR ("Premature end of data" );
815
827
return BSG_KSJSON_ERROR_INCOMPLETE ;
816
828
}
817
829
@@ -837,7 +849,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
837
849
unlikely_if (* ptr >= end ) { break ; }
838
850
likely_if (* * ptr == ',' ) { (* ptr )++ ; }
839
851
}
840
- BSG_KSLOG_DEBUG ("Premature end of data" );
852
+ BSG_KSLOG_ERROR ("Premature end of data" );
841
853
return BSG_KSJSON_ERROR_INCOMPLETE ;
842
854
}
843
855
case '{' : {
@@ -861,7 +873,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
861
873
}
862
874
unlikely_if (* * ptr != ':' ) {
863
875
free (key );
864
- BSG_KSLOG_DEBUG ("Expected ':' but got '%c'" , * * ptr );
876
+ BSG_KSLOG_ERROR ("Expected ':' but got '%c'" , * * ptr );
865
877
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
866
878
}
867
879
(* ptr )++ ;
@@ -874,7 +886,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
874
886
unlikely_if (* ptr >= end ) { break ; }
875
887
likely_if (* * ptr == ',' ) { (* ptr )++ ; }
876
888
}
877
- BSG_KSLOG_DEBUG ("Premature end of data" );
889
+ BSG_KSLOG_ERROR ("Premature end of data" );
878
890
return BSG_KSJSON_ERROR_INCOMPLETE ;
879
891
}
880
892
case '\"' : {
@@ -887,12 +899,12 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
887
899
}
888
900
case 'f' : {
889
901
unlikely_if (end - * ptr < 5 ) {
890
- BSG_KSLOG_DEBUG ("Premature end of data" );
902
+ BSG_KSLOG_ERROR ("Premature end of data" );
891
903
return BSG_KSJSON_ERROR_INCOMPLETE ;
892
904
}
893
905
unlikely_if (!((* ptr )[1 ] == 'a' && (* ptr )[2 ] == 'l' &&
894
906
(* ptr )[3 ] == 's' && (* ptr )[4 ] == 'e' )) {
895
- BSG_KSLOG_DEBUG ("Expected \"false\" but got \"f%c%c%c%c\"" ,
907
+ BSG_KSLOG_ERROR ("Expected \"false\" but got \"f%c%c%c%c\"" ,
896
908
(* ptr )[1 ], (* ptr )[2 ], (* ptr )[3 ], (* ptr )[4 ]);
897
909
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
898
910
}
@@ -901,12 +913,12 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
901
913
}
902
914
case 't' : {
903
915
unlikely_if (end - * ptr < 4 ) {
904
- BSG_KSLOG_DEBUG ("Premature end of data" );
916
+ BSG_KSLOG_ERROR ("Premature end of data" );
905
917
return BSG_KSJSON_ERROR_INCOMPLETE ;
906
918
}
907
919
unlikely_if (
908
920
!((* ptr )[1 ] == 'r' && (* ptr )[2 ] == 'u' && (* ptr )[3 ] == 'e' )) {
909
- BSG_KSLOG_DEBUG ("Expected \"true\" but got \"t%c%c%c\"" , (* ptr )[1 ],
921
+ BSG_KSLOG_ERROR ("Expected \"true\" but got \"t%c%c%c\"" , (* ptr )[1 ],
910
922
(* ptr )[2 ], (* ptr )[3 ]);
911
923
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
912
924
}
@@ -915,12 +927,12 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
915
927
}
916
928
case 'n' : {
917
929
unlikely_if (end - * ptr < 4 ) {
918
- BSG_KSLOG_DEBUG ("Premature end of data" );
930
+ BSG_KSLOG_ERROR ("Premature end of data" );
919
931
return BSG_KSJSON_ERROR_INCOMPLETE ;
920
932
}
921
933
unlikely_if (
922
934
!((* ptr )[1 ] == 'u' && (* ptr )[2 ] == 'l' && (* ptr )[3 ] == 'l' )) {
923
- BSG_KSLOG_DEBUG ("Expected \"null\" but got \"n%c%c%c\"" , (* ptr )[1 ],
935
+ BSG_KSLOG_ERROR ("Expected \"null\" but got \"n%c%c%c\"" , (* ptr )[1 ],
924
936
(* ptr )[2 ], (* ptr )[3 ]);
925
937
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
926
938
}
@@ -931,7 +943,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
931
943
sign = -1 ;
932
944
(* ptr )++ ;
933
945
unlikely_if (!isdigit (* * ptr )) {
934
- BSG_KSLOG_DEBUG ("Not a digit: '%c'" , * * ptr );
946
+ BSG_KSLOG_ERROR ("Not a digit: '%c'" , * * ptr );
935
947
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
936
948
}
937
949
// Fall through
@@ -958,7 +970,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
958
970
}
959
971
960
972
unlikely_if (* ptr >= end ) {
961
- BSG_KSLOG_DEBUG ("Premature end of data" );
973
+ BSG_KSLOG_ERROR ("Premature end of data" );
962
974
return BSG_KSJSON_ERROR_INCOMPLETE ;
963
975
}
964
976
@@ -972,7 +984,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
972
984
}
973
985
974
986
unlikely_if (* ptr >= end ) {
975
- BSG_KSLOG_DEBUG ("Premature end of data" );
987
+ BSG_KSLOG_ERROR ("Premature end of data" );
976
988
return BSG_KSJSON_ERROR_INCOMPLETE ;
977
989
}
978
990
@@ -993,7 +1005,7 @@ int bsg_ksjsoncodec_i_decodeElement(const char **ptr, const char *const end,
993
1005
return callbacks -> onFloatingPointElement (name , value , userData );
994
1006
}
995
1007
}
996
- BSG_KSLOG_DEBUG ("Invalid character '%c'" , * * ptr );
1008
+ BSG_KSLOG_ERROR ("Invalid character '%c'" , * * ptr );
997
1009
return BSG_KSJSON_ERROR_INVALID_CHARACTER ;
998
1010
}
999
1011
0 commit comments