omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / reservoir / res_mat.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note Pure seeded OM-2 MATLAB MAT-file version 5 (little-endian) reader. |
| 5 | note Replaces tools/reservoir/res_mat.{c,h}. |
| 6 | note Concatenated after src/reservoir/reservoir.om and tools/reservoir/res_inflate.om. |
| 7 | |
| 8 | const RES_MAT_OK int 0 |
| 9 | const RES_MAT_ERR_IO int -1 |
| 10 | const RES_MAT_ERR_FORMAT int -2 |
| 11 | const RES_MAT_ERR_INFLATE int -3 |
| 12 | const RES_MAT_ERR_NOMEM int -4 |
| 13 | const RES_MAT_ERR_DIGEST int -5 |
| 14 | const RES_MAT_ERR_NOTFOUND int -6 |
| 15 | const RES_MAT_UNSUPPORTED int -7 |
| 16 | const RES_MAT_ERR_SHAPE int -8 |
| 17 | |
| 18 | const RES_MX_CELL ulong 1 |
| 19 | const RES_MX_STRUCT ulong 2 |
| 20 | const RES_MX_OBJECT ulong 3 |
| 21 | const RES_MX_CHAR ulong 4 |
| 22 | const RES_MX_SPARSE ulong 5 |
| 23 | const RES_MX_DOUBLE ulong 6 |
| 24 | const RES_MX_SINGLE ulong 7 |
| 25 | const RES_MX_INT8 ulong 8 |
| 26 | const RES_MX_UINT8 ulong 9 |
| 27 | const RES_MX_INT16 ulong 10 |
| 28 | const RES_MX_UINT16 ulong 11 |
| 29 | const RES_MX_INT32 ulong 12 |
| 30 | const RES_MX_UINT32 ulong 13 |
| 31 | const RES_MX_INT64 ulong 14 |
| 32 | const RES_MX_UINT64 ulong 15 |
| 33 | const RES_MX_OPAQUE ulong 17 |
| 34 | |
| 35 | const RES_MI_INT8 ulong 1 |
| 36 | const RES_MI_UINT8 ulong 2 |
| 37 | const RES_MI_INT16 ulong 3 |
| 38 | const RES_MI_UINT16 ulong 4 |
| 39 | const RES_MI_INT32 ulong 5 |
| 40 | const RES_MI_UINT32 ulong 6 |
| 41 | const RES_MI_SINGLE ulong 7 |
| 42 | const RES_MI_DOUBLE ulong 9 |
| 43 | const RES_MI_INT64 ulong 12 |
| 44 | const RES_MI_UINT64 ulong 13 |
| 45 | const RES_MI_MATRIX ulong 14 |
| 46 | const RES_MI_COMPRESSED ulong 15 |
| 47 | |
| 48 | const RES_MAT_MAX_VARS ulong 64 |
| 49 | const RES_MAT_MAX_DIMS ulong 8 |
| 50 | const RES_MAT_MAX_NAME ulong 64 |
| 51 | const RES_MAT_MAX_DEPTH ulong 4 |
| 52 | const RES_MAT_META_STRIDE ulong 16 |
| 53 | note ResMatFile layout (98 ulongs): |
| 54 | note [0]=file_bytes [1]=var_count [2..65]=var_idx[0..63] [66..97]=sha256 bytes(32) |
| 55 | const RES_MAT_FILE_WORDS ulong 98 |
| 56 | |
| 57 | text mat_magic "MATLAB 5.0 MAT-file" |
| 58 | |
| 59 | note Global pools for parsed MAT arrays and numeric data |
| 60 | global mat_re f64 * |
| 61 | global mat_re_used ulong 1 |
| 62 | global mat_re_cap ulong 1 |
| 63 | |
| 64 | note Each array descriptor is 16 ulongs: |
| 65 | note [0]=cls [1]=is_complex [2]=is_logical [3]=has_data [4]=ndims [5]=numel |
| 66 | note [6]=re_off [7]=cell_off [8..15]=dims[0..7] |
| 67 | global mat_meta ulong * |
| 68 | global mat_meta_used ulong 1 |
| 69 | global mat_meta_cap ulong 1 |
| 70 | |
| 71 | note 64 bytes per array descriptor: [0]=name_len, [1..63]=name bytes |
| 72 | global mat_name uchar * |
| 73 | global mat_name_cap ulong 1 |
| 74 | |
| 75 | note Source file bytes buffer (src_id = 0); inf_out is src_id = 1 |
| 76 | global mat_src uchar * |
| 77 | global mat_src_cap ulong 1 |
| 78 | |
| 79 | fn res_mat_reset_pool to void |
| 80 | put mat_re_used 0 0 |
| 81 | put mat_meta_used 0 0 |
| 82 | end |
| 83 | |
| 84 | fn mat_ensure_src need ulong to int |
| 85 | cap = get mat_src_cap 0 |
| 86 | if le need cap |
| 87 | ret RES_MAT_OK |
| 88 | end |
| 89 | ncap = ulong 65536 |
| 90 | if gt cap 0 |
| 91 | ncap = cap |
| 92 | end |
| 93 | while lt ncap need bound 64 |
| 94 | ncap = mul ncap 2 |
| 95 | end |
| 96 | e = enlarge mat_src ncap |
| 97 | if lt e 0 |
| 98 | ret RES_MAT_ERR_NOMEM |
| 99 | end |
| 100 | put mat_src_cap 0 ncap |
| 101 | ret RES_MAT_OK |
| 102 | end |
| 103 | |
| 104 | fn mat_alloc_re n ulong out_off edit ulong 1 to int |
| 105 | used = get mat_re_used 0 |
| 106 | need = add used n |
| 107 | cap = get mat_re_cap 0 |
| 108 | if gt need cap |
| 109 | ncap = ulong 65536 |
| 110 | if gt cap 0 |
| 111 | ncap = cap |
| 112 | end |
| 113 | while lt ncap need bound 64 |
| 114 | ncap = mul ncap 2 |
| 115 | end |
| 116 | e = enlarge mat_re ncap |
| 117 | if lt e 0 |
| 118 | ret RES_MAT_ERR_NOMEM |
| 119 | end |
| 120 | put mat_re_cap 0 ncap |
| 121 | end |
| 122 | put out_off 0 used |
| 123 | put mat_re_used 0 need |
| 124 | ret RES_MAT_OK |
| 125 | end |
| 126 | |
| 127 | fn mat_alloc_arrs count ulong out_idx edit ulong 1 to int |
| 128 | used = get mat_meta_used 0 |
| 129 | need = add used count |
| 130 | cap = get mat_meta_cap 0 |
| 131 | if gt need cap |
| 132 | ncap = ulong 256 |
| 133 | if gt cap 0 |
| 134 | ncap = cap |
| 135 | end |
| 136 | while lt ncap need bound 64 |
| 137 | ncap = mul ncap 2 |
| 138 | end |
| 139 | e = enlarge mat_meta mul ncap RES_MAT_META_STRIDE |
| 140 | if lt e 0 |
| 141 | ret RES_MAT_ERR_NOMEM |
| 142 | end |
| 143 | e = enlarge mat_name mul ncap RES_MAT_MAX_NAME |
| 144 | if lt e 0 |
| 145 | ret RES_MAT_ERR_NOMEM |
| 146 | end |
| 147 | put mat_meta_cap 0 ncap |
| 148 | put mat_name_cap 0 mul ncap RES_MAT_MAX_NAME |
| 149 | end |
| 150 | for i ulong mul count RES_MAT_META_STRIDE bound 1000000 |
| 151 | put mat_meta add mul used RES_MAT_META_STRIDE i 0 |
| 152 | end |
| 153 | for i ulong mul count RES_MAT_MAX_NAME bound 1000000 |
| 154 | put mat_name add mul used RES_MAT_MAX_NAME i 0 |
| 155 | end |
| 156 | put out_idx 0 used |
| 157 | put mat_meta_used 0 need |
| 158 | ret RES_MAT_OK |
| 159 | end |
| 160 | |
| 161 | fn mat_byte src uint off ulong to uchar |
| 162 | if eq src 0 |
| 163 | ret get mat_src off |
| 164 | end |
| 165 | ret get inf_out off |
| 166 | end |
| 167 | |
| 168 | fn mat_rd32 src uint off ulong to uint |
| 169 | b0 = uint mat_byte src off |
| 170 | b1 = shl uint mat_byte src add off 1 8 |
| 171 | b2 = shl uint mat_byte src add off 2 16 |
| 172 | b3 = shl uint mat_byte src add off 3 24 |
| 173 | ret or or or b0 b1 b2 b3 |
| 174 | end |
| 175 | |
| 176 | fn mat_rd64 src uint off ulong to ulong |
| 177 | lo = ulong mat_rd32 src off |
| 178 | hi = ulong mat_rd32 src add off 4 |
| 179 | ret or shl hi 32 lo |
| 180 | end |
| 181 | |
| 182 | note tag_out: [0]=type [1]=nbytes [2]=data_off [3]=next_off |
| 183 | fn mat_read_tag src uint base ulong n ulong pos ulong align bool tag_out edit ulong 4 to int |
| 184 | if or gt pos n lt sub n pos 8 |
| 185 | ret RES_MAT_ERR_FORMAT |
| 186 | end |
| 187 | w0 = mat_rd32 src add base pos |
| 188 | hi = shr w0 16 |
| 189 | if ne hi 0 |
| 190 | if gt hi 4 |
| 191 | ret RES_MAT_ERR_FORMAT |
| 192 | end |
| 193 | put tag_out 0 ulong and w0 0xffff |
| 194 | put tag_out 1 ulong hi |
| 195 | put tag_out 2 add pos 4 |
| 196 | put tag_out 3 add pos 8 |
| 197 | ret RES_MAT_OK |
| 198 | end |
| 199 | nbytes = ulong mat_rd32 src add base add pos 4 |
| 200 | doff = add pos 8 |
| 201 | if lt sub n doff nbytes |
| 202 | ret RES_MAT_ERR_FORMAT |
| 203 | end |
| 204 | eend = add doff nbytes |
| 205 | if align |
| 206 | rem8 = and eend 7 |
| 207 | if ne rem8 0 |
| 208 | eend = add eend sub 8 rem8 |
| 209 | end |
| 210 | if gt eend n |
| 211 | eend = n |
| 212 | end |
| 213 | end |
| 214 | put tag_out 0 ulong w0 |
| 215 | put tag_out 1 nbytes |
| 216 | put tag_out 2 doff |
| 217 | put tag_out 3 eend |
| 218 | ret RES_MAT_OK |
| 219 | end |
| 220 | |
| 221 | fn mat_type_size mtype ulong to ulong |
| 222 | if or eq mtype RES_MI_INT8 eq mtype RES_MI_UINT8 |
| 223 | ret 1 |
| 224 | end |
| 225 | if or eq mtype RES_MI_INT16 eq mtype RES_MI_UINT16 |
| 226 | ret 2 |
| 227 | end |
| 228 | if or eq mtype RES_MI_INT32 or eq mtype RES_MI_UINT32 eq mtype RES_MI_SINGLE |
| 229 | ret 4 |
| 230 | end |
| 231 | if or eq mtype RES_MI_DOUBLE or eq mtype RES_MI_INT64 eq mtype RES_MI_UINT64 |
| 232 | ret 8 |
| 233 | end |
| 234 | ret 0 |
| 235 | end |
| 236 | |
| 237 | fn mat_conv_one mtype ulong src uint off ulong out_v edit f64 1 to int |
| 238 | if eq mtype RES_MI_DOUBLE |
| 239 | put out_v 0 bitcast f64 mat_rd64 src off |
| 240 | ret RES_MAT_OK |
| 241 | end |
| 242 | if eq mtype RES_MI_SINGLE |
| 243 | put out_v 0 f64 bitcast f32 mat_rd32 src off |
| 244 | ret RES_MAT_OK |
| 245 | end |
| 246 | if eq mtype RES_MI_UINT8 |
| 247 | put out_v 0 f64 mat_byte src off |
| 248 | ret RES_MAT_OK |
| 249 | end |
| 250 | if eq mtype RES_MI_INT8 |
| 251 | v = int mat_byte src off |
| 252 | if ge v 128 |
| 253 | v = sub v 256 |
| 254 | end |
| 255 | put out_v 0 f64 v |
| 256 | ret RES_MAT_OK |
| 257 | end |
| 258 | if eq mtype RES_MI_UINT16 |
| 259 | v = or ulong mat_byte src off shl ulong mat_byte src add off 1 8 |
| 260 | put out_v 0 f64 v |
| 261 | ret RES_MAT_OK |
| 262 | end |
| 263 | if eq mtype RES_MI_INT16 |
| 264 | v = int or ulong mat_byte src off shl ulong mat_byte src add off 1 8 |
| 265 | if ge v 32768 |
| 266 | v = sub v 65536 |
| 267 | end |
| 268 | put out_v 0 f64 v |
| 269 | ret RES_MAT_OK |
| 270 | end |
| 271 | if eq mtype RES_MI_UINT32 |
| 272 | put out_v 0 f64 mat_rd32 src off |
| 273 | ret RES_MAT_OK |
| 274 | end |
| 275 | if eq mtype RES_MI_INT32 |
| 276 | u = ulong mat_rd32 src off |
| 277 | v = long u |
| 278 | if ge u 2147483648 |
| 279 | v = sub v 4294967296 |
| 280 | end |
| 281 | put out_v 0 f64 v |
| 282 | ret RES_MAT_OK |
| 283 | end |
| 284 | if eq mtype RES_MI_UINT64 |
| 285 | u = mat_rd64 src off |
| 286 | if gt u 9007199254740992 |
| 287 | ret RES_MAT_UNSUPPORTED |
| 288 | end |
| 289 | put out_v 0 f64 u |
| 290 | ret RES_MAT_OK |
| 291 | end |
| 292 | if eq mtype RES_MI_INT64 |
| 293 | u = mat_rd64 src off |
| 294 | v = bitcast long u |
| 295 | if or gt v 9007199254740992 lt v -9007199254740992 |
| 296 | ret RES_MAT_UNSUPPORTED |
| 297 | end |
| 298 | put out_v 0 f64 v |
| 299 | ret RES_MAT_OK |
| 300 | end |
| 301 | ret RES_MAT_ERR_FORMAT |
| 302 | end |
| 303 | |
| 304 | fn mat_convert mtype ulong src uint off ulong numel ulong re_off ulong to int |
| 305 | sz = mat_type_size mtype |
| 306 | if eq sz 0 |
| 307 | ret RES_MAT_ERR_FORMAT |
| 308 | end |
| 309 | fv = new f64 1 0.0f64 |
| 310 | for i ulong numel bound 50000000 |
| 311 | e = mat_conv_one mtype src add off mul i sz fv |
| 312 | if ne e RES_MAT_OK |
| 313 | ret e |
| 314 | end |
| 315 | put mat_re add re_off i get fv 0 |
| 316 | end |
| 317 | ret RES_MAT_OK |
| 318 | end |
| 319 | |
| 320 | fn mat_set_name aidx ulong src uint off ulong nbytes ulong to void |
| 321 | nl = nbytes |
| 322 | if gt nl 63 |
| 323 | nl = 63 |
| 324 | end |
| 325 | nb = mul aidx RES_MAT_MAX_NAME |
| 326 | put mat_name nb uchar and nl 0xff |
| 327 | for i ulong nl bound 64 |
| 328 | put mat_name add nb add i 1 mat_byte src add off i |
| 329 | end |
| 330 | end |
| 331 | |
| 332 | fn mat_parse_dims src uint base ulong nbytes ulong mb ulong to int |
| 333 | ndims = div nbytes 4 |
| 334 | if or lt ndims 2 gt ndims RES_MAT_MAX_DIMS |
| 335 | ret RES_MAT_ERR_FORMAT |
| 336 | end |
| 337 | put mat_meta add mb 4 ndims |
| 338 | numel = ulong 1 |
| 339 | for i ulong ndims bound 8 |
| 340 | du = mat_rd32 src add base mul i 4 |
| 341 | if ge du 0x80000000 |
| 342 | ret RES_MAT_ERR_FORMAT |
| 343 | end |
| 344 | d = ulong du |
| 345 | put mat_meta add mb add 8 i d |
| 346 | if ne d 0 |
| 347 | if gt numel div 2147483648 d |
| 348 | ret RES_MAT_ERR_FORMAT |
| 349 | end |
| 350 | end |
| 351 | numel = mul numel d |
| 352 | end |
| 353 | put mat_meta add mb 5 numel |
| 354 | ret RES_MAT_OK |
| 355 | end |
| 356 | |
| 357 | fn mat_parse_numeric src uint base ulong n ulong pos ulong mb ulong to int |
| 358 | if ne get mat_meta add mb 1 0 |
| 359 | ret RES_MAT_OK |
| 360 | end |
| 361 | tag = new ulong 4 0 |
| 362 | e = mat_read_tag src base n pos true tag |
| 363 | if ne e RES_MAT_OK |
| 364 | ret e |
| 365 | end |
| 366 | numel = get mat_meta add mb 5 |
| 367 | sz = mat_type_size get tag 0 |
| 368 | if or eq sz 0 ne get tag 1 mul numel sz |
| 369 | ret RES_MAT_ERR_FORMAT |
| 370 | end |
| 371 | roff = new ulong 1 0 |
| 372 | e = mat_alloc_re numel roff |
| 373 | if ne e RES_MAT_OK |
| 374 | ret e |
| 375 | end |
| 376 | e = mat_convert get tag 0 src add base get tag 2 numel get roff 0 |
| 377 | if ne e RES_MAT_OK |
| 378 | ret e |
| 379 | end |
| 380 | put mat_meta add mb 6 get roff 0 |
| 381 | put mat_meta add mb 3 1 |
| 382 | ret RES_MAT_OK |
| 383 | end |
| 384 | |
| 385 | fn mat_parse_flags src uint base ulong n ulong mb ulong tag edit ulong 4 to int |
| 386 | e = mat_read_tag src base n 0 true tag |
| 387 | if ne e RES_MAT_OK |
| 388 | ret e |
| 389 | end |
| 390 | if or ne get tag 0 RES_MI_UINT32 ne get tag 1 8 |
| 391 | ret RES_MAT_ERR_FORMAT |
| 392 | end |
| 393 | flags = mat_rd32 src add base get tag 2 |
| 394 | put mat_meta mb ulong and flags 0xff |
| 395 | if ne and flags 0x800 0 |
| 396 | put mat_meta add mb 1 1 |
| 397 | end |
| 398 | if ne and flags 0x200 0 |
| 399 | put mat_meta add mb 2 1 |
| 400 | end |
| 401 | ret RES_MAT_OK |
| 402 | end |
| 403 | |
| 404 | fn mat_parse_header src uint base ulong n ulong aidx ulong pos_out edit ulong 1 to int |
| 405 | mb = mul aidx RES_MAT_META_STRIDE |
| 406 | tag = new ulong 4 0 |
| 407 | e = mat_parse_flags src base n mb tag |
| 408 | if ne e RES_MAT_OK |
| 409 | ret e |
| 410 | end |
| 411 | pos = get tag 3 |
| 412 | if eq get mat_meta mb RES_MX_OPAQUE |
| 413 | e = mat_read_tag src base n pos true tag |
| 414 | if ne e RES_MAT_OK |
| 415 | ret e |
| 416 | end |
| 417 | if ne get tag 0 RES_MI_INT8 |
| 418 | ret RES_MAT_ERR_FORMAT |
| 419 | end |
| 420 | pos = add base get tag 2 |
| 421 | mat_set_name aidx src pos get tag 1 |
| 422 | put pos_out 0 0 |
| 423 | ret RES_MAT_OK |
| 424 | end |
| 425 | e = mat_read_tag src base n pos true tag |
| 426 | if ne e RES_MAT_OK |
| 427 | ret e |
| 428 | end |
| 429 | if ne get tag 0 RES_MI_INT32 |
| 430 | ret RES_MAT_ERR_FORMAT |
| 431 | end |
| 432 | if ne rem get tag 1 4 0 |
| 433 | ret RES_MAT_ERR_FORMAT |
| 434 | end |
| 435 | pos = add base get tag 2 |
| 436 | e = mat_parse_dims src pos get tag 1 mb |
| 437 | if ne e RES_MAT_OK |
| 438 | ret e |
| 439 | end |
| 440 | pos = get tag 3 |
| 441 | e = mat_read_tag src base n pos true tag |
| 442 | if ne e RES_MAT_OK |
| 443 | ret e |
| 444 | end |
| 445 | if ne get tag 0 RES_MI_INT8 |
| 446 | ret RES_MAT_ERR_FORMAT |
| 447 | end |
| 448 | pos = add base get tag 2 |
| 449 | mat_set_name aidx src pos get tag 1 |
| 450 | put pos_out 0 get tag 3 |
| 451 | ret RES_MAT_OK |
| 452 | end |
| 453 | |
| 454 | fn mat_parse_cell_elem src uint base ulong n ulong pos ulong dst_idx ulong depth ulong to long |
| 455 | tag = new ulong 4 0 |
| 456 | e = mat_read_tag src base n pos true tag |
| 457 | if ne e RES_MAT_OK |
| 458 | ret long e |
| 459 | end |
| 460 | if ne get tag 0 RES_MI_MATRIX |
| 461 | ret long RES_MAT_ERR_FORMAT |
| 462 | end |
| 463 | e = mat_parse_matrix src add base get tag 2 get tag 1 dst_idx depth |
| 464 | if ne e RES_MAT_OK |
| 465 | ret long e |
| 466 | end |
| 467 | ret long get tag 3 |
| 468 | end |
| 469 | |
| 470 | fn mat_parse_cell src uint base ulong n ulong mb ulong depth ulong to int |
| 471 | numel = get mat_meta add mb 5 |
| 472 | pos = get mat_meta add mb 7 |
| 473 | if or gt pos n gt numel div sub n pos 8 |
| 474 | ret RES_MAT_ERR_FORMAT |
| 475 | end |
| 476 | coff = new ulong 1 0 |
| 477 | e = mat_alloc_arrs numel coff |
| 478 | if ne e RES_MAT_OK |
| 479 | ret e |
| 480 | end |
| 481 | cbase = get coff 0 |
| 482 | put mat_meta add mb 7 cbase |
| 483 | for i ulong numel bound 1000000 |
| 484 | npos = mat_parse_cell_elem src base n pos add cbase i add depth 1 |
| 485 | if lt npos 0 |
| 486 | ret int npos |
| 487 | end |
| 488 | pos = ulong npos |
| 489 | end |
| 490 | put mat_meta add mb 3 1 |
| 491 | ret RES_MAT_OK |
| 492 | end |
| 493 | |
| 494 | fn mat_parse_matrix src uint base ulong n ulong aidx ulong depth ulong to int |
| 495 | mb = mul aidx RES_MAT_META_STRIDE |
| 496 | if eq n 0 |
| 497 | put mat_meta mb RES_MX_DOUBLE |
| 498 | put mat_meta add mb 4 2 |
| 499 | put mat_meta add mb 3 1 |
| 500 | ret RES_MAT_OK |
| 501 | end |
| 502 | if gt depth RES_MAT_MAX_DEPTH |
| 503 | ret RES_MAT_UNSUPPORTED |
| 504 | end |
| 505 | pout = new ulong 1 0 |
| 506 | e = mat_parse_header src base n aidx pout |
| 507 | if ne e RES_MAT_OK |
| 508 | ret e |
| 509 | end |
| 510 | cls = get mat_meta mb |
| 511 | if eq cls RES_MX_OPAQUE |
| 512 | ret RES_MAT_OK |
| 513 | end |
| 514 | pos = get pout 0 |
| 515 | if and ge cls RES_MX_DOUBLE le cls RES_MX_UINT64 |
| 516 | ret mat_parse_numeric src base n pos mb |
| 517 | end |
| 518 | if eq cls RES_MX_CELL |
| 519 | put mat_meta add mb 7 pos |
| 520 | ret mat_parse_cell src base n mb depth |
| 521 | end |
| 522 | ret RES_MAT_OK |
| 523 | end |
| 524 | |
| 525 | fn mat_add_element mf edit ulong 98 src uint base ulong n ulong to int |
| 526 | tag = new ulong 4 0 |
| 527 | e = mat_read_tag src base n 0 true tag |
| 528 | if ne e RES_MAT_OK |
| 529 | ret e |
| 530 | end |
| 531 | if ne get tag 0 RES_MI_MATRIX |
| 532 | ret RES_MAT_ERR_FORMAT |
| 533 | end |
| 534 | vc = get mf 1 |
| 535 | if ge vc RES_MAT_MAX_VARS |
| 536 | ret RES_MAT_UNSUPPORTED |
| 537 | end |
| 538 | aoff = new ulong 1 0 |
| 539 | e = mat_alloc_arrs 1 aoff |
| 540 | if ne e RES_MAT_OK |
| 541 | ret e |
| 542 | end |
| 543 | aidx = get aoff 0 |
| 544 | put mf add 2 vc aidx |
| 545 | put mf 1 add vc 1 |
| 546 | ret mat_parse_matrix src add base get tag 2 get tag 1 aidx 0 |
| 547 | end |
| 548 | |
| 549 | note Inflate directly from global mat_src[in_off .. in_off + in_len) |
| 550 | fn inf_getbits_src need ulong val_out edit uint 1 to int |
| 551 | if eq need 0 |
| 552 | put val_out 0 0 |
| 553 | ret RES_INF_OK |
| 554 | end |
| 555 | v = get inf_st 3 |
| 556 | bc = get inf_st 4 |
| 557 | pos = get inf_st 2 |
| 558 | iend = get inf_st 1 |
| 559 | while lt bc need bound 8 |
| 560 | if ge pos iend |
| 561 | ret RES_INF_ERR_TRUNC |
| 562 | end |
| 563 | b = ulong get mat_src pos |
| 564 | v = or v shl b bc |
| 565 | pos = add pos 1 |
| 566 | bc = add bc 8 |
| 567 | end |
| 568 | mask = sub shl ulong 1 need 1 |
| 569 | put val_out 0 uint and v mask |
| 570 | put inf_st 2 pos |
| 571 | put inf_st 3 shr v need |
| 572 | put inf_st 4 sub bc need |
| 573 | ret RES_INF_OK |
| 574 | end |
| 575 | |
| 576 | fn inf_stored_src to int |
| 577 | put inf_st 3 0 |
| 578 | put inf_st 4 0 |
| 579 | pos = get inf_st 2 |
| 580 | iend = get inf_st 1 |
| 581 | if lt sub iend pos 4 |
| 582 | ret RES_INF_ERR_TRUNC |
| 583 | end |
| 584 | blen = or ulong get mat_src pos shl ulong get mat_src add pos 1 8 |
| 585 | nlen = or ulong get mat_src add pos 2 shl ulong get mat_src add pos 3 8 |
| 586 | if ne blen and xor nlen 0xffff 0xffff |
| 587 | ret RES_INF_ERR_BLOCK |
| 588 | end |
| 589 | pos = add pos 4 |
| 590 | if lt sub iend pos blen |
| 591 | ret RES_INF_ERR_TRUNC |
| 592 | end |
| 593 | for i ulong blen bound 65536 |
| 594 | e = inf_put get mat_src add pos i |
| 595 | if ne e RES_INF_OK |
| 596 | ret e |
| 597 | end |
| 598 | end |
| 599 | put inf_st 2 add pos blen |
| 600 | ret RES_INF_OK |
| 601 | end |
| 602 | |
| 603 | fn inf_decode_lc_src use_fix bool sym_out edit int 1 to int |
| 604 | code = int 0 |
| 605 | first = int 0 |
| 606 | index = int 0 |
| 607 | bv = new uint 1 0 |
| 608 | for l ulong 15 bound 15 |
| 609 | e = inf_getbits_src 1 bv |
| 610 | if ne e RES_INF_OK |
| 611 | ret e |
| 612 | end |
| 613 | code = or code int get bv 0 |
| 614 | cnt = get inf_lc_cnt add l 1 |
| 615 | if use_fix |
| 616 | cnt = get inf_fix_lc_cnt add l 1 |
| 617 | end |
| 618 | if lt sub code cnt first |
| 619 | si = ulong add index sub code first |
| 620 | if use_fix |
| 621 | put sym_out 0 get inf_fix_lc_sym si |
| 622 | else |
| 623 | put sym_out 0 get inf_lc_sym si |
| 624 | end |
| 625 | ret RES_INF_OK |
| 626 | end |
| 627 | index = add index cnt |
| 628 | first = shl add first cnt 1 |
| 629 | code = shl code 1 |
| 630 | end |
| 631 | ret RES_INF_ERR_CODES |
| 632 | end |
| 633 | |
| 634 | fn inf_decode_dc_src use_fix bool sym_out edit int 1 to int |
| 635 | code = int 0 |
| 636 | first = int 0 |
| 637 | index = int 0 |
| 638 | bv = new uint 1 0 |
| 639 | for l ulong 15 bound 15 |
| 640 | e = inf_getbits_src 1 bv |
| 641 | if ne e RES_INF_OK |
| 642 | ret e |
| 643 | end |
| 644 | code = or code int get bv 0 |
| 645 | cnt = get inf_dc_cnt add l 1 |
| 646 | if use_fix |
| 647 | cnt = get inf_fix_dc_cnt add l 1 |
| 648 | end |
| 649 | if lt sub code cnt first |
| 650 | si = ulong add index sub code first |
| 651 | if use_fix |
| 652 | put sym_out 0 get inf_fix_dc_sym si |
| 653 | else |
| 654 | put sym_out 0 get inf_dc_sym si |
| 655 | end |
| 656 | ret RES_INF_OK |
| 657 | end |
| 658 | index = add index cnt |
| 659 | first = shl add first cnt 1 |
| 660 | code = shl code 1 |
| 661 | end |
| 662 | ret RES_INF_ERR_CODES |
| 663 | end |
| 664 | |
| 665 | fn inf_codes_src use_fix bool to int |
| 666 | sv = new int 1 0 |
| 667 | xv = new uint 1 0 |
| 668 | while true bound 500000000 |
| 669 | e = inf_decode_lc_src use_fix sv |
| 670 | if ne e RES_INF_OK |
| 671 | ret e |
| 672 | end |
| 673 | sym = get sv 0 |
| 674 | if lt sym 256 |
| 675 | e = inf_put uchar and sym 0xff |
| 676 | if ne e RES_INF_OK |
| 677 | ret e |
| 678 | end |
| 679 | continue |
| 680 | end |
| 681 | if eq sym 256 |
| 682 | ret RES_INF_OK |
| 683 | end |
| 684 | sym = sub sym 257 |
| 685 | if or lt sym 0 ge sym 29 |
| 686 | ret RES_INF_ERR_SYMBOL |
| 687 | end |
| 688 | e = inf_getbits_src inf_lext ulong sym xv |
| 689 | if ne e RES_INF_OK |
| 690 | ret e |
| 691 | end |
| 692 | mlen = add inf_lbase ulong sym ulong get xv 0 |
| 693 | e = inf_decode_dc_src use_fix sv |
| 694 | if ne e RES_INF_OK |
| 695 | ret e |
| 696 | end |
| 697 | dsym = get sv 0 |
| 698 | if or lt dsym 0 ge dsym 30 |
| 699 | ret RES_INF_ERR_SYMBOL |
| 700 | end |
| 701 | e = inf_getbits_src inf_dext ulong dsym xv |
| 702 | if ne e RES_INF_OK |
| 703 | ret e |
| 704 | end |
| 705 | dist = add inf_dbase ulong dsym ulong get xv 0 |
| 706 | e = inf_copy_match mlen dist |
| 707 | if ne e RES_INF_OK |
| 708 | ret e |
| 709 | end |
| 710 | end |
| 711 | ret RES_INF_ERR_BLOCK |
| 712 | end |
| 713 | |
| 714 | fn inf_dynamic_read_lens_src total ulong to int |
| 715 | idx = ulong 0 |
| 716 | sv = new int 1 0 |
| 717 | xv = new uint 1 0 |
| 718 | while lt idx total bound 400 |
| 719 | e = inf_decode_lc_src false sv |
| 720 | if ne e RES_INF_OK |
| 721 | ret e |
| 722 | end |
| 723 | sym = get sv 0 |
| 724 | if lt sym 16 |
| 725 | put inf_lengths idx sym |
| 726 | idx = add idx 1 |
| 727 | continue |
| 728 | end |
| 729 | lv = int 0 |
| 730 | rep = ulong 0 |
| 731 | if eq sym 16 |
| 732 | if eq idx 0 |
| 733 | ret RES_INF_ERR_CODES |
| 734 | end |
| 735 | lv = get inf_lengths sub idx 1 |
| 736 | e = inf_getbits_src 2 xv |
| 737 | if ne e RES_INF_OK |
| 738 | ret e |
| 739 | end |
| 740 | rep = add 3 ulong get xv 0 |
| 741 | else |
| 742 | if eq sym 17 |
| 743 | e = inf_getbits_src 3 xv |
| 744 | if ne e RES_INF_OK |
| 745 | ret e |
| 746 | end |
| 747 | rep = add 3 ulong get xv 0 |
| 748 | else |
| 749 | e = inf_getbits_src 7 xv |
| 750 | if ne e RES_INF_OK |
| 751 | ret e |
| 752 | end |
| 753 | rep = add 11 ulong get xv 0 |
| 754 | end |
| 755 | end |
| 756 | if gt add idx rep total |
| 757 | ret RES_INF_ERR_CODES |
| 758 | end |
| 759 | for r ulong rep bound 140 |
| 760 | put inf_lengths idx lv |
| 761 | idx = add idx 1 |
| 762 | end |
| 763 | end |
| 764 | ret RES_INF_OK |
| 765 | end |
| 766 | |
| 767 | fn inf_dynamic_src to int |
| 768 | xv = new uint 1 0 |
| 769 | e = inf_getbits_src 5 xv |
| 770 | if ne e RES_INF_OK |
| 771 | ret e |
| 772 | end |
| 773 | nlen = add ulong get xv 0 257 |
| 774 | e = inf_getbits_src 5 xv |
| 775 | if ne e RES_INF_OK |
| 776 | ret e |
| 777 | end |
| 778 | ndist = add ulong get xv 0 1 |
| 779 | e = inf_getbits_src 4 xv |
| 780 | if ne e RES_INF_OK |
| 781 | ret e |
| 782 | end |
| 783 | ncode = add ulong get xv 0 4 |
| 784 | if or gt nlen 286 gt ndist 30 |
| 785 | ret RES_INF_ERR_CODES |
| 786 | end |
| 787 | for i ulong 19 bound 19 |
| 788 | put inf_lengths i 0 |
| 789 | end |
| 790 | for i ulong ncode bound 19 |
| 791 | e = inf_getbits_src 3 xv |
| 792 | if ne e RES_INF_OK |
| 793 | ret e |
| 794 | end |
| 795 | put inf_lengths inf_order i int get xv 0 |
| 796 | end |
| 797 | if ne inf_construct_lc 0 19 0 |
| 798 | ret RES_INF_ERR_CODES |
| 799 | end |
| 800 | e = inf_dynamic_read_lens_src add nlen ndist |
| 801 | if ne e RES_INF_OK |
| 802 | ret e |
| 803 | end |
| 804 | if eq get inf_lengths 256 0 |
| 805 | ret RES_INF_ERR_CODES |
| 806 | end |
| 807 | e = inf_construct_lc 0 nlen |
| 808 | if ne e 0 |
| 809 | if or lt e 0 ne int nlen add get inf_lc_cnt 0 get inf_lc_cnt 1 |
| 810 | ret RES_INF_ERR_CODES |
| 811 | end |
| 812 | end |
| 813 | e = inf_construct_dc nlen ndist |
| 814 | if ne e 0 |
| 815 | if or lt e 0 ne int ndist add get inf_dc_cnt 0 get inf_dc_cnt 1 |
| 816 | ret RES_INF_ERR_CODES |
| 817 | end |
| 818 | end |
| 819 | ret inf_codes_src false |
| 820 | end |
| 821 | |
| 822 | fn inf_check_adler_src in_off ulong to int |
| 823 | pos = get inf_st 2 |
| 824 | iend = get inf_st 1 |
| 825 | if lt sub iend pos 4 |
| 826 | ret RES_INF_ERR_TRUNC |
| 827 | end |
| 828 | want = shl uint get mat_src pos 24 |
| 829 | want = or want shl uint get mat_src add pos 1 16 |
| 830 | want = or want shl uint get mat_src add pos 2 8 |
| 831 | want = or want uint get mat_src add pos 3 |
| 832 | got = res_adler32_inf_out get inf_out_len 0 |
| 833 | if ne got want |
| 834 | ret RES_INF_ERR_ADLER |
| 835 | end |
| 836 | put inf_consumed 0 sub add pos 4 in_off |
| 837 | ret RES_INF_OK |
| 838 | end |
| 839 | |
| 840 | fn res_zlib_inflate_src in_off ulong in_len ulong to int |
| 841 | put inf_out_len 0 0 |
| 842 | put inf_consumed 0 0 |
| 843 | if lt in_len 2 |
| 844 | ret RES_INF_ERR_TRUNC |
| 845 | end |
| 846 | cmf = uint get mat_src in_off |
| 847 | flg = uint get mat_src add in_off 1 |
| 848 | if or ne and cmf 0x0f 8 gt shr cmf 4 7 |
| 849 | ret RES_INF_ERR_HEADER |
| 850 | end |
| 851 | if ne rem or shl cmf 8 flg 31 0 |
| 852 | ret RES_INF_ERR_HEADER |
| 853 | end |
| 854 | if ne and flg 0x20 0 |
| 855 | ret RES_INF_ERR_HEADER |
| 856 | end |
| 857 | put inf_st 0 in_off |
| 858 | put inf_st 1 add in_off in_len |
| 859 | put inf_st 2 add in_off 2 |
| 860 | put inf_st 3 0 |
| 861 | put inf_st 4 0 |
| 862 | last = uint 0 |
| 863 | btype = new uint 1 0 |
| 864 | lv = new uint 1 0 |
| 865 | while eq last 0 bound 1000000 |
| 866 | e = inf_getbits_src 1 lv |
| 867 | if ne e RES_INF_OK |
| 868 | ret e |
| 869 | end |
| 870 | last = get lv 0 |
| 871 | e = inf_getbits_src 2 btype |
| 872 | if ne e RES_INF_OK |
| 873 | ret e |
| 874 | end |
| 875 | bt = get btype 0 |
| 876 | if eq bt 0 |
| 877 | e = inf_stored_src |
| 878 | else |
| 879 | if eq bt 1 |
| 880 | inf_build_fixed |
| 881 | e = inf_codes_src true |
| 882 | else |
| 883 | if eq bt 2 |
| 884 | e = inf_dynamic_src |
| 885 | else |
| 886 | e = RES_INF_ERR_BLOCK |
| 887 | end |
| 888 | end |
| 889 | end |
| 890 | if ne e RES_INF_OK |
| 891 | ret e |
| 892 | end |
| 893 | end |
| 894 | put inf_st 3 0 |
| 895 | put inf_st 4 0 |
| 896 | ret inf_check_adler_src in_off |
| 897 | end |
| 898 | |
| 899 | fn res_mat_parse_src n ulong mf edit ulong 98 to int |
| 900 | put mf 1 0 |
| 901 | if lt n 128 |
| 902 | ret RES_MAT_ERR_FORMAT |
| 903 | end |
| 904 | if or ne get mat_src 126 uchar 73 ne get mat_src 127 uchar 77 |
| 905 | ret RES_MAT_UNSUPPORTED |
| 906 | end |
| 907 | if or ne get mat_src 124 uchar 0 ne get mat_src 125 uchar 1 |
| 908 | ret RES_MAT_ERR_FORMAT |
| 909 | end |
| 910 | for i ulong 19 bound 19 |
| 911 | if ne get mat_src i get mat_magic i |
| 912 | ret RES_MAT_ERR_FORMAT |
| 913 | end |
| 914 | end |
| 915 | pos = ulong 128 |
| 916 | tag = new ulong 4 0 |
| 917 | while lt pos n bound 100000 |
| 918 | e = mat_read_tag 0 0 n pos false tag |
| 919 | if ne e RES_MAT_OK |
| 920 | ret e |
| 921 | end |
| 922 | tp = get tag 0 |
| 923 | if eq tp RES_MI_COMPRESSED |
| 924 | z = res_zlib_inflate_src get tag 2 get tag 1 |
| 925 | if ne z RES_INF_OK |
| 926 | ret RES_MAT_ERR_INFLATE |
| 927 | end |
| 928 | if ne get inf_consumed 0 get tag 1 |
| 929 | ret RES_MAT_ERR_FORMAT |
| 930 | end |
| 931 | e = mat_add_element mf 1 0 get inf_out_len 0 |
| 932 | if ne e RES_MAT_OK |
| 933 | ret e |
| 934 | end |
| 935 | pos = add get tag 2 get tag 1 |
| 936 | else |
| 937 | if eq tp RES_MI_MATRIX |
| 938 | e = mat_add_element mf 0 pos sub n pos |
| 939 | if ne e RES_MAT_OK |
| 940 | ret e |
| 941 | end |
| 942 | eend = add get tag 2 get tag 1 |
| 943 | rem8 = and eend 7 |
| 944 | if ne rem8 0 |
| 945 | eend = add eend sub 8 rem8 |
| 946 | end |
| 947 | pos = eend |
| 948 | else |
| 949 | ret RES_MAT_ERR_FORMAT |
| 950 | end |
| 951 | end |
| 952 | end |
| 953 | ret RES_MAT_OK |
| 954 | end |
| 955 | |
| 956 | fn res_mat_parse bytes view uchar * n ulong mf edit ulong 98 to int |
| 957 | e = mat_ensure_src n |
| 958 | if ne e RES_MAT_OK |
| 959 | ret e |
| 960 | end |
| 961 | for i ulong n bound 200000000 |
| 962 | put mat_src i get bytes i |
| 963 | end |
| 964 | put mf 0 n |
| 965 | ret res_mat_parse_src n mf |
| 966 | end |
| 967 | |
| 968 | fn mat_copy_src_chunk pos ulong k ulong chunk edit uchar 4096 to void |
| 969 | for i ulong k bound 4096 |
| 970 | put chunk i get mat_src add pos i |
| 971 | end |
| 972 | end |
| 973 | |
| 974 | fn mat_hash_src n ulong out_d edit uchar 32 to void |
| 975 | ctx = new uint 74 0 |
| 976 | res_sha_init ctx |
| 977 | chunk = new uchar 4096 0 |
| 978 | pos = ulong 0 |
| 979 | while lt pos n bound 100000 |
| 980 | k = sub n pos |
| 981 | if gt k 4096 |
| 982 | k = 4096 |
| 983 | end |
| 984 | mat_copy_src_chunk pos k chunk |
| 985 | res_sha_update ctx part chunk 0 k |
| 986 | pos = add pos k |
| 987 | end |
| 988 | res_sha_final ctx out_d |
| 989 | end |
| 990 | |
| 991 | fn mat_copy_chunk_to_src total ulong nru ulong chunk view uchar 65536 to void |
| 992 | for i ulong nru bound 65536 |
| 993 | put mat_src add total i get chunk i |
| 994 | end |
| 995 | end |
| 996 | |
| 997 | fn mat_read_file_to_src path view uchar * total_out edit ulong 1 to int |
| 998 | fd = fopen path 0 len path 0 0 |
| 999 | if lt fd 0 |
| 1000 | ret RES_MAT_ERR_IO |
| 1001 | end |
| 1002 | total = ulong 0 |
| 1003 | chunk = new uchar 65536 0 |
| 1004 | while true bound 100000 |
| 1005 | nr = read int fd chunk 0 65536 |
| 1006 | if lt nr 0 |
| 1007 | _fc = fclose int fd |
| 1008 | ret RES_MAT_ERR_IO |
| 1009 | end |
| 1010 | if eq nr 0 |
| 1011 | break |
| 1012 | end |
| 1013 | nru = ulong nr |
| 1014 | e = mat_ensure_src add total nru |
| 1015 | if ne e RES_MAT_OK |
| 1016 | _fc = fclose int fd |
| 1017 | ret e |
| 1018 | end |
| 1019 | mat_copy_chunk_to_src total nru view chunk |
| 1020 | total = add total nru |
| 1021 | end |
| 1022 | _fc = fclose int fd |
| 1023 | put total_out 0 total |
| 1024 | ret RES_MAT_OK |
| 1025 | end |
| 1026 | |
| 1027 | fn res_mat_load path view uchar * want_sha view uchar 32 check_sha bool mf edit ulong 98 to int |
| 1028 | for i ulong RES_MAT_FILE_WORDS bound 98 |
| 1029 | put mf i 0 |
| 1030 | end |
| 1031 | tout = new ulong 1 0 |
| 1032 | e = mat_read_file_to_src path tout |
| 1033 | if ne e RES_MAT_OK |
| 1034 | ret e |
| 1035 | end |
| 1036 | total = get tout 0 |
| 1037 | put mf 0 total |
| 1038 | dig = new uchar 32 0 |
| 1039 | mat_hash_src total dig |
| 1040 | for i ulong 32 bound 32 |
| 1041 | put mf add 66 i ulong get dig i |
| 1042 | end |
| 1043 | if check_sha |
| 1044 | for i ulong 32 bound 32 |
| 1045 | if ne get dig i get want_sha i |
| 1046 | ret RES_MAT_ERR_DIGEST |
| 1047 | end |
| 1048 | end |
| 1049 | end |
| 1050 | ret res_mat_parse_src total mf |
| 1051 | end |
| 1052 | |
| 1053 | fn mat_name_eq aidx ulong name view uchar * to bool |
| 1054 | nb = mul aidx RES_MAT_MAX_NAME |
| 1055 | nlen = len name |
| 1056 | if ne ulong get mat_name nb nlen |
| 1057 | ret false |
| 1058 | end |
| 1059 | for k ulong nlen bound 64 |
| 1060 | if ne get mat_name add nb add k 1 get name k |
| 1061 | ret false |
| 1062 | end |
| 1063 | end |
| 1064 | ret true |
| 1065 | end |
| 1066 | |
| 1067 | note Returns array index (>= 0) or -1 if not found. |
| 1068 | fn res_mat_find mf view ulong 98 name view uchar * to long |
| 1069 | vc = get mf 1 |
| 1070 | for i ulong vc bound 64 |
| 1071 | aidx = get mf add 2 i |
| 1072 | if mat_name_eq aidx name |
| 1073 | ret long aidx |
| 1074 | end |
| 1075 | end |
| 1076 | ret -1 |
| 1077 | end |
| 1078 | |
| 1079 | fn res_mat_cell_at cidx long sub_arr view ulong * cnt ulong to long |
| 1080 | if lt cidx 0 |
| 1081 | ret -1 |
| 1082 | end |
| 1083 | mb = mul ulong cidx RES_MAT_META_STRIDE |
| 1084 | if or ne get mat_meta mb RES_MX_CELL eq get mat_meta add mb 3 0 |
| 1085 | ret -1 |
| 1086 | end |
| 1087 | if ne cnt get mat_meta add mb 4 |
| 1088 | ret -1 |
| 1089 | end |
| 1090 | idx = ulong 0 |
| 1091 | stride = ulong 1 |
| 1092 | for i ulong cnt bound 8 |
| 1093 | d = get mat_meta add mb add 8 i |
| 1094 | si = get sub_arr i |
| 1095 | if ge si d |
| 1096 | ret -1 |
| 1097 | end |
| 1098 | idx = add idx mul si stride |
| 1099 | stride = mul stride d |
| 1100 | end |
| 1101 | ret long add get mat_meta add mb 7 idx |
| 1102 | end |
| 1103 | |
| 1104 | fn res_mat_values aidx long want_numel ulong out_off edit ulong 1 to int |
| 1105 | if lt aidx 0 |
| 1106 | ret RES_MAT_ERR_NOTFOUND |
| 1107 | end |
| 1108 | mb = mul ulong aidx RES_MAT_META_STRIDE |
| 1109 | cls = get mat_meta mb |
| 1110 | if or lt cls RES_MX_DOUBLE or gt cls RES_MX_UINT64 eq get mat_meta add mb 3 0 |
| 1111 | ret RES_MAT_UNSUPPORTED |
| 1112 | end |
| 1113 | if and ne want_numel 0 ne get mat_meta add mb 5 want_numel |
| 1114 | ret RES_MAT_ERR_SHAPE |
| 1115 | end |
| 1116 | put out_off 0 get mat_meta add mb 6 |
| 1117 | ret RES_MAT_OK |
| 1118 | end |