clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name rscodec.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/runner/work/LibreCAD/LibreCAD/libraries/libdxfrw -fcoverage-compilation-dir=/home/runner/work/LibreCAD/LibreCAD/libraries/libdxfrw -resource-dir /usr/lib/llvm-18/lib/clang/18 -D _REENTRANT -D MUPARSER_STATIC -D QT_NO_DEBUG -I . -I ../../../Qt/6.9.0/gcc_64/mkspecs/linux-g++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=gnu++1z -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/runner/work/LibreCAD/LibreCAD/out/2026-08-04-154929-5069-1 -x c++ src/intern/rscodec.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | #include "rscodec.h" |
| 29 | #include <new> // std::nothrow |
| 30 | #include <fstream> |
| 31 | |
| 32 | RScodec::RScodec(unsigned int pp, int mm, int tt) { |
| 33 | this->mm = mm; |
| 34 | this->tt = tt; |
| 35 | nn = (1<<mm) -1; |
| 1 | Assuming right operand of bit shift is non-negative but less than 32 | |
|
| 36 | kk = nn -(tt*2); |
| 37 | isOk = true; |
| 38 | |
| 39 | alpha_to = new (std::nothrow) int[nn+1]; |
| 40 | index_of = new (std::nothrow) unsigned int[nn+1]; |
| 2 | | Storing uninitialized value | |
|
| 41 | gg = new (std::nothrow) int[nn-kk+1]; |
| 42 | |
| 43 | RSgenerate_gf(pp) ; |
| 44 | |
| 45 | RSgen_poly() ; |
| 3 | | Calling 'RScodec::RSgen_poly' | |
|
| 46 | |
| 47 | |
| 48 | const int bb = nn - kk; |
| 49 | recd = new (std::nothrow) int[nn]; |
| 50 | elp = new int*[bb + 2]; |
| 51 | for (int i = 0; i < bb + 2; ++i) { |
| 52 | elp[i] = new int[bb]; |
| 53 | } |
| 54 | d = new int[bb + 2]; |
| 55 | l = new int[bb + 2]; |
| 56 | u_lu = new int[bb + 2]; |
| 57 | s = new int[bb + 1]; |
| 58 | root = new int[tt]; |
| 59 | loc = new int[tt]; |
| 60 | z = new int[tt + 1]; |
| 61 | err = new int[nn]; |
| 62 | reg = new int[tt + 1]; |
| 63 | } |
| 64 | |
| 65 | RScodec::~RScodec() { |
| 66 | delete[] alpha_to; |
| 67 | delete[] index_of; |
| 68 | delete[] gg; |
| 69 | |
| 70 | delete[] recd; |
| 71 | for (int i = 0; i < nn - kk + 2; ++i) { |
| 72 | delete[] elp[i]; |
| 73 | } |
| 74 | delete[] elp; |
| 75 | delete[] d; |
| 76 | delete[] l; |
| 77 | delete[] u_lu; |
| 78 | delete[] s; |
| 79 | delete[] root; |
| 80 | delete[] loc; |
| 81 | delete[] z; |
| 82 | delete[] err; |
| 83 | delete[] reg; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | void RScodec::RSgenerate_gf(unsigned int pp) const { |
| 93 | int i, mask ; |
| 94 | int pb; |
| 95 | |
| 96 | mask = 1 ; |
| 97 | alpha_to[mm] = 0 ; |
| 98 | for (i=0; i<mm; i++) { |
| 99 | alpha_to[i] = mask ; |
| 100 | index_of[alpha_to[i]] = i ; |
| 101 | pb = (pp >>(mm-1-i)) & 1; |
| 102 | if (pb!=0) { |
| 103 | alpha_to[mm] ^= mask; |
| 104 | } |
| 105 | mask <<= 1 ; |
| 106 | } |
| 107 | index_of[alpha_to[mm]] = mm ; |
| 108 | mask >>= 1 ; |
| 109 | for (i=mm+1; i<nn; i++) { |
| 110 | if (alpha_to[i-1] >= mask) { |
| 111 | alpha_to[i] = alpha_to[mm] ^ ((alpha_to[i-1]^mask)<<1) ; |
| 112 | } else { |
| 113 | alpha_to[i] = alpha_to[i-1]<<1 ; |
| 114 | } |
| 115 | index_of[alpha_to[i]] = i ; |
| 116 | } |
| 117 | index_of[0] = -1 ; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | void RScodec::RSgen_poly() { |
| 125 | int i,j ; |
| 126 | int tmp; |
| 127 | int bb = nn-kk; |
| 128 | |
| 129 | gg[0] = 2 ; |
| |
| 130 | gg[1] = 1 ; |
| 131 | for (i=2; i<=bb; i++) { |
| |
| 6 | | Loop condition is true. Entering loop body | |
|
| 132 | gg[i] = 1 ; |
| 133 | for (j=i-1; j>0; j--) { |
| 7 | | Loop condition is true. Entering loop body | |
|
| 10 | | Loop condition is false. Execution continues on line 143 | |
|
| 134 | if (gg[j] != 0) { |
| 135 | if (gg[j]<0 || gg[j]>nn) { isOk=false; return; } |
| |
| 136 | tmp = (index_of[gg[j]]+i)%nn; |
| 137 | if (tmp<0) { isOk=false; return; } |
| |
| 138 | gg[j] = gg[j-1]^ alpha_to[tmp] ; |
| 139 | } else { |
| 140 | gg[j] = gg[j-1] ; |
| 141 | } |
| 142 | } |
| 143 | gg[0] = alpha_to[(index_of[gg[0]]+i)%nn] ; |
| 11 | | The left operand of '+' is a garbage value due to array index out of bounds |
|
| 144 | } |
| 145 | |
| 146 | for (i=0; i<=bb; i++) { |
| 147 | gg[i] = index_of[gg[i]] ; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int RScodec::calcDecode(unsigned char* data, int* recd, int** elp, int* d, int* l, int* u_lu, int* s, int* root, int* loc, int* z, int* err, int* reg, int bb) const { |
| 152 | if (!isOk) { |
| 153 | return -1; |
| 154 | } |
| 155 | int count = 0; |
| 156 | int syn_error = 0; |
| 157 | int i, j, u, q; |
| 158 | |
| 159 | |
| 160 | |
| 161 | for (int i = 0, j = bb; i<kk; i++, j++) { |
| 162 | recd[j] = index_of[data[j]]; |
| 163 | } |
| 164 | for (int i = kk, j = 0; i<nn; i++, j++) { |
| 165 | recd[j] = index_of[data[j]]; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | for (i = 1; i <= bb; i++) { |
| 170 | s[i] = 0; |
| 171 | for (j = 0; j<nn; j++) { |
| 172 | if (recd[j] != -1) { |
| 173 | s[i] ^= alpha_to[(recd[j] + i*j) % nn]; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (s[i] != 0) { |
| 178 | syn_error = 1; |
| 179 | } |
| 180 | s[i] = index_of[s[i]]; |
| 181 | } |
| 182 | |
| 183 | if (!syn_error) { |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | d[0] = 0; |
| 198 | d[1] = s[1]; |
| 199 | elp[0][0] = 0; |
| 200 | elp[1][0] = 1; |
| 201 | for (i = 1; i<bb; i++) { |
| 202 | elp[0][i] = -1; |
| 203 | elp[1][i] = 0; |
| 204 | } |
| 205 | l[0] = 0; |
| 206 | l[1] = 0; |
| 207 | u_lu[0] = -1; |
| 208 | u_lu[1] = 0; |
| 209 | u = 0; |
| 210 | |
| 211 | do { |
| 212 | u++; |
| 213 | if (d[u] == -1) { |
| 214 | l[u + 1] = l[u]; |
| 215 | for (i = 0; i <= l[u]; i++) { |
| 216 | elp[u + 1][i] = elp[u][i]; |
| 217 | elp[u][i] = index_of[elp[u][i]]; |
| 218 | } |
| 219 | } |
| 220 | else { |
| 221 | |
| 222 | q = u - 1; |
| 223 | while ((d[q] == -1) && (q>0)) { |
| 224 | q--; |
| 225 | } |
| 226 | |
| 227 | if (q>0) { |
| 228 | j = q; |
| 229 | do { |
| 230 | j--; |
| 231 | if ((d[j] != -1) && (u_lu[q]<u_lu[j])) { |
| 232 | q = j; |
| 233 | } |
| 234 | } while (j>0); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | |
| 239 | if (l[u]>l[q] + u - q) { |
| 240 | l[u + 1] = l[u]; |
| 241 | } |
| 242 | else { |
| 243 | l[u + 1] = l[q] + u - q; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | for (i = 0; i<bb; i++) { |
| 248 | elp[u + 1][i] = 0; |
| 249 | } |
| 250 | for (i = 0; i <= l[q]; i++){ |
| 251 | if (elp[q][i] != -1) { |
| 252 | elp[u + 1][i + u - q] = alpha_to[(d[u] + nn - d[q] + elp[q][i]) % nn]; |
| 253 | } |
| 254 | } |
| 255 | for (i = 0; i <= l[u]; i++) { |
| 256 | elp[u + 1][i] ^= elp[u][i]; |
| 257 | elp[u][i] = index_of[elp[u][i]]; |
| 258 | } |
| 259 | } |
| 260 | u_lu[u + 1] = u - l[u + 1]; |
| 261 | |
| 262 | |
| 263 | if (u<bb){ |
| 264 | if (s[u + 1] != -1) { |
| 265 | d[u + 1] = alpha_to[s[u + 1]]; |
| 266 | } |
| 267 | else { |
| 268 | d[u + 1] = 0; |
| 269 | } |
| 270 | for (i = 1; i <= l[u + 1]; i++){ |
| 271 | if ((s[u + 1 - i] != -1) && (elp[u + 1][i] != 0)) { |
| 272 | d[u + 1] ^= alpha_to[(s[u + 1 - i] + index_of[elp[u + 1][i]]) % nn]; |
| 273 | } |
| 274 | } |
| 275 | d[u + 1] = index_of[d[u + 1]]; |
| 276 | } |
| 277 | } while ((u<bb) && (l[u + 1] <= tt)); |
| 278 | |
| 279 | u++; |
| 280 | if (l[u]>tt) { |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | for (i = 0; i <= l[u]; i++) { |
| 287 | elp[u][i] = index_of[elp[u][i]]; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | for (i = 1; i <= l[u]; i++) { |
| 292 | reg[i] = elp[u][i]; |
| 293 | } |
| 294 | count = 0; |
| 295 | for (i = 1; i <= nn; i++) { |
| 296 | q = 1; |
| 297 | for (j = 1; j <= l[u]; j++) { |
| 298 | if (reg[j] != -1) { |
| 299 | reg[j] = (reg[j] + j) % nn; |
| 300 | q ^= alpha_to[reg[j]]; |
| 301 | } |
| 302 | } |
| 303 | if (!q) { |
| 304 | root[count] = i; |
| 305 | loc[count] = nn - i; |
| 306 | count++; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (count != l[u]) { |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | for (i = 1; i <= l[u]; i++) { |
| 317 | if ((s[i] != -1) && (elp[u][i] != -1)) { |
| 318 | z[i] = alpha_to[s[i]] ^ alpha_to[elp[u][i]]; |
| 319 | } |
| 320 | else if ((s[i] != -1) && (elp[u][i] == -1)) { |
| 321 | z[i] = alpha_to[s[i]]; |
| 322 | } |
| 323 | else if ((s[i] == -1) && (elp[u][i] != -1)) { |
| 324 | z[i] = alpha_to[elp[u][i]]; |
| 325 | } |
| 326 | else { |
| 327 | z[i] = 0; |
| 328 | } |
| 329 | for (j = 1; j<i; j++) { |
| 330 | if ((s[j] != -1) && (elp[u][i - j] != -1)) { |
| 331 | z[i] ^= alpha_to[(elp[u][i - j] + s[j]) % nn]; |
| 332 | } |
| 333 | } |
| 334 | z[i] = index_of[z[i]]; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | for (i = 0; i<nn; i++) { |
| 339 | err[i] = 0; |
| 340 | } |
| 341 | for (i = 0; i<l[u]; i++) { |
| 342 | err[loc[i]] = 1; |
| 343 | for (j = 1; j <= l[u]; j++) { |
| 344 | if (z[j] != -1) { |
| 345 | err[loc[i]] ^= alpha_to[(z[j] + j*root[i]) % nn]; |
| 346 | } |
| 347 | } |
| 348 | if (err[loc[i]] != 0) { |
| 349 | err[loc[i]] = index_of[err[loc[i]]]; |
| 350 | q = 0; |
| 351 | for (j = 0; j<l[u]; j++) { |
| 352 | if (j != i) { |
| 353 | q += index_of[1 ^ alpha_to[(loc[j] + root[i]) % nn]]; |
| 354 | } |
| 355 | } |
| 356 | q = q % nn; |
| 357 | err[loc[i]] = alpha_to[(err[loc[i]] - q + nn) % nn]; |
| 358 | data[loc[i]] ^= err[loc[i]]; |
| 359 | } |
| 360 | } |
| 361 | return count; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | bool RScodec::encode(unsigned char *data, unsigned char *parity) const { |
| 371 | if (!isOk) { |
| 372 | return false; |
| 373 | } |
| 374 | int i,j ; |
| 375 | int feedback ; |
| 376 | unsigned char *idata = data; |
| 377 | unsigned char *bd = parity; |
| 378 | int bb = nn-kk; |
| 379 | |
| 380 | for (i=0; i<bb; i++) { |
| 381 | bd[i] = 0 ; |
| 382 | } |
| 383 | for (i=kk-1; i>=0; i--) { |
| 384 | feedback = index_of[idata[i]^bd[bb-1]] ; |
| 385 | if (feedback != -1) { |
| 386 | for (j=bb-1; j>0; j--) { |
| 387 | if (gg[j] != -1) { |
| 388 | bd[j] = bd[j-1]^alpha_to[(gg[j]+feedback)%nn] ; |
| 389 | } |
| 390 | else { |
| 391 | bd[j] = bd[j-1] ; |
| 392 | } |
| 393 | } |
| 394 | bd[0] = alpha_to[(gg[0]+feedback)%nn] ; |
| 395 | } else { |
| 396 | for (j=bb-1; j>0; j--) { |
| 397 | bd[j] = bd[j-1] ; |
| 398 | } |
| 399 | bd[0] = 0 ; |
| 400 | } |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | int RScodec::decode(unsigned char *data) { |
| 426 | if (!isOk) { |
| 427 | return -1; |
| 428 | } |
| 429 | const int bb = nn-kk; |
| 430 | |
| 431 | |
| 432 | |
| 433 | return calcDecode(data, recd, elp, d, l, u_lu, s, root, loc, z, err, reg, bb); |
| 434 | } |