@@ -57,37 +57,23 @@ STATIC void get_word(int n, const mchar_t **pos, const mchar_t **end) {
57
57
* end = * pos + len ;
58
58
}
59
59
60
- STATIC int put_utf8 (char * buf , int u ) {
60
+ STATIC void put_utf8 (vstr_t * vstr , int u ) {
61
61
if (u >= translation_offstart ) {
62
62
u += translation_offset ;
63
63
}
64
- if (u <= 0x7f ) {
65
- * buf = u ;
66
- return 1 ;
67
- } else if (word_start <= u && u <= word_end ) {
64
+ if (word_start <= u && u <= word_end ) {
68
65
uint n = (u - word_start );
69
66
const mchar_t * pos , * end ;
70
67
get_word (n , & pos , & end );
71
- int ret = 0 ;
72
68
// note that at present, entries in the words table are
73
69
// guaranteed not to represent words themselves, so this adds
74
70
// at most 1 level of recursive call
75
71
for (; pos < end ; pos ++ ) {
76
- int len = put_utf8 (buf , * pos );
77
- buf += len ;
78
- ret += len ;
72
+ put_utf8 (vstr , * pos );
79
73
}
80
- return ret ;
81
- } else if (u <= 0x07ff ) {
82
- * buf ++ = 0b11000000 | (u >> 6 );
83
- * buf = 0b10000000 | (u & 0b00111111 );
84
- return 2 ;
85
- } else { // u <= 0xffff
86
- * buf ++ = 0b11100000 | (u >> 12 );
87
- * buf ++ = 0b10000000 | ((u >> 6 ) & 0b00111111 );
88
- * buf = 0b10000000 | (u & 0b00111111 );
89
- return 3 ;
74
+ return ;
90
75
}
76
+ vstr_add_char (vstr , u );
91
77
}
92
78
93
79
uint16_t decompress_length (mp_rom_error_text_t compressed ) {
@@ -123,15 +109,15 @@ static int get_nbits(bitstream_state_t *st, int n) {
123
109
return r ;
124
110
}
125
111
126
- char * decompress (mp_rom_error_text_t compressed , char * decompressed ) {
112
+ static void decompress_vstr (mp_rom_error_text_t compressed , vstr_t * decompressed ) {
127
113
bitstream_state_t b = {
128
114
.ptr = & (compressed -> data ) + (compress_max_length_bits >> 3 ),
129
115
.bit = 1 << (7 - ((compress_max_length_bits ) & 0x7 )),
130
116
};
131
- uint16_t length = decompress_length (compressed );
132
117
118
+ size_t alloc = decompressed -> alloc - 1 ;
133
119
// Stop one early because the last byte is always NULL.
134
- for (uint16_t i = 0 ; i < length - 1 ;) {
120
+ for (; decompressed -> len < alloc ;) {
135
121
uint32_t bits = 0 ;
136
122
uint8_t bit_length = 0 ;
137
123
uint32_t max_code = lengths [0 ];
@@ -148,16 +134,19 @@ char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
148
134
int v = values [searched_length + bits - max_code ];
149
135
if (v == 1 ) {
150
136
qstr q = get_nbits (& b , translation_qstr_bits ) + 1 ; // honestly no idea why "+1"...
151
- for (const char * qc = qstr_str (q ); * qc ;) {
152
- decompressed [i ++ ] = * qc ++ ;
153
- }
137
+ vstr_add_str (decompressed , qstr_str (q ));
154
138
} else {
155
- i += put_utf8 (decompressed + i , v );
139
+ put_utf8 (decompressed , v );
156
140
}
157
141
}
142
+ }
158
143
159
- decompressed [length - 1 ] = '\0' ;
160
- return decompressed ;
144
+
145
+ char * decompress (mp_rom_error_text_t compressed , char * decompressed ) {
146
+ vstr_t vstr ;
147
+ vstr_init_fixed_buf (& vstr , decompress_length (compressed ), decompressed );
148
+ decompress_vstr (compressed , & vstr );
149
+ return vstr_null_terminated_str (& vstr );
161
150
}
162
151
163
152
#if CIRCUITPY_TRANSLATE_OBJECT == 1
0 commit comments