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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
|
/* $OpenBSD: softmagic.c,v 1.16 2010/01/17 20:36:21 chl Exp $ */
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
* maintained 1995-present by Christos Zoulas and others.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* softmagic - interpret variable magic from MAGIC
*/
#include <r_userconf.h>
#if !USE_LIB_MAGIC
#include "file.h"
#include "r_regex.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
static int match(RMagic *, struct r_magic *, ut32,
const ut8 *, size_t, int);
static int mget(RMagic *, const ut8 *,
struct r_magic *, size_t, unsigned int);
static int magiccheck(RMagic *, struct r_magic *);
static st32 mprint(RMagic *, struct r_magic *);
static void mdebug(ut32, const char *, size_t);
static int mcopy(RMagic *, union VALUETYPE *, int, int,
const ut8 *, ut32, size_t, size_t);
static int mconvert(RMagic *, struct r_magic *);
static int print_sep(RMagic *, int);
static void cvt_8(union VALUETYPE *, const struct r_magic *);
static void cvt_16(union VALUETYPE *, const struct r_magic *);
static void cvt_32(union VALUETYPE *, const struct r_magic *);
static void cvt_64(union VALUETYPE *, const struct r_magic *);
/*
* Macro to give description string according to whether we want plain
* text or MIME type
*/
#define R_MAGIC_DESC ((ms->flags & R_MAGIC_MIME) ? m->mimetype : m->desc)
/*
* softmagic - lookup one file in parsed, in-memory copy of database
* Passed the name and FILE * of one file to be typed.
*/
/*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
int file_softmagic(RMagic *ms, const ut8 *buf, size_t nbytes, int mode) {
struct mlist *ml;
int rv;
for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode)) != 0)
return rv;
return 0;
}
/*
* Go through the whole list, stopping if you find a match. Process all
* the continuations of that match before returning.
*
* We support multi-level continuations:
*
* At any time when processing a successful top-level match, there is a
* current continuation level; it represents the level of the last
* successfully matched continuation.
*
* Continuations above that level are skipped as, if we see one, it
* means that the continuation that controls them - i.e, the
* lower-level continuation preceding them - failed to match.
*
* Continuations below that level are processed as, if we see one,
* it means we've finished processing or skipping higher-level
* continuations under the control of a successful or unsuccessful
* lower-level continuation, and are now seeing the next lower-level
* continuation and should process it. The current continuation
* level reverts to the level of the one we're seeing.
*
* Continuations at the current level are processed as, if we see
* one, there's no lower-level continuation that may have failed.
*
* If a continuation matches, we bump the current continuation level
* so that higher-level continuations are processed.
*/
static int match(RMagic *ms, struct r_magic *magic, ut32 nmagic, const ut8 *s, size_t nbytes, int mode) {
ut32 magindex = 0;
unsigned int cont_level = 0;
int need_separator = 0;
int returnval = 0; /* if a match is found it is set to 1*/
int firstline = 1; /* a flag to print X\n X\n- X */
int printed_something = 0;
if (file_check_mem (ms, cont_level) == -1)
return -1;
for (magindex = 0; magindex < nmagic; magindex++) {
int flush;
struct r_magic *m = &magic[magindex];
if ((m->flag & BINTEST) != mode) {
/* Skip sub-tests */
while (magic[magindex + 1].cont_level != 0 &&
++magindex < nmagic)
continue;
continue; /* Skip to next top-level test*/
}
ms->offset = m->offset;
ms->line = m->lineno;
/* if main entry matches, print it... */
flush = !mget(ms, s, m, nbytes, cont_level);
if (flush) {
if (m->reln == '!')
flush = 0;
} else {
int ret = magiccheck (ms, m);
if (ret == -1) return -1;
if (!ret) flush++;
}
if (flush) {
/*
* main entry didn't match,
* flush its continuations
*/
while (magindex < nmagic - 1 && magic[magindex + 1].cont_level)
magindex++;
continue;
}
/*
* If we are going to print something, we'll need to print
* a blank before we print something else.
*/
if (*R_MAGIC_DESC) {
need_separator = 1;
printed_something = 1;
if (print_sep(ms, firstline) == -1)
return -1;
}
if ((ms->c.li[cont_level].off = mprint(ms, m)) == -1)
return -1;
/* and any continuations that match */
if (file_check_mem(ms, ++cont_level) == -1)
return -1;
while (magic[magindex+1].cont_level != 0 && ++magindex < nmagic) {
m = &magic[magindex];
ms->line = m->lineno; /* for messages */
if (cont_level < m->cont_level)
continue;
if (cont_level > m->cont_level) {
/*
* We're at the end of the level
* "cont_level" continuations.
*/
cont_level = m->cont_level;
}
ms->offset = m->offset;
if (m->flag & OFFADD)
ms->offset += ms->c.li[cont_level - 1].off;
if (m->cond == COND_ELSE || m->cond == COND_ELIF) {
if (ms->c.li[cont_level].last_match == 1)
continue;
}
flush = !mget(ms, s, m, nbytes, cont_level);
if (flush && m->reln != '!')
continue;
switch (flush ? 1 : magiccheck(ms, m)) {
case -1:
return -1;
case 0:
ms->c.li[cont_level].last_match = 0;
break;
default:
ms->c.li[cont_level].last_match = 1;
if (m->type != FILE_DEFAULT)
ms->c.li[cont_level].got_match = 1;
else if (ms->c.li[cont_level].got_match) {
ms->c.li[cont_level].got_match = 0;
break;
}
/*
* If we are going to print something,
* make sure that we have a separator first.
*/
if (*R_MAGIC_DESC) {
printed_something = 1;
if (print_sep(ms, firstline) == -1)
return -1;
}
/*
* This continuation matched. Print
* its message, with a blank before it
* if the previous item printed and
* this item isn't empty.
*/
/* space if previous printed */
if (need_separator
&& ((m->flag & NOSPACE) == 0)
&& *R_MAGIC_DESC) {
if (file_printf (ms, " ") == -1)
return -1;
need_separator = 0;
}
if ((ms->c.li[cont_level].off = mprint(ms, m)) == -1)
return -1;
if (*R_MAGIC_DESC)
need_separator = 1;
/*
* If we see any continuations
* at a higher level,
* process them.
*/
if (file_check_mem(ms, ++cont_level) == -1)
return -1;
break;
}
}
if (printed_something) {
firstline = 0;
returnval = 1;
}
if ((ms->flags & R_MAGIC_CONTINUE) == 0 && printed_something)
return 1; /* don't keep searching */
}
return returnval; /* This is hit if -k is set or there is no match */
}
static int check_fmt(RMagic *ms, struct r_magic *m) {
RRegex rx;
int rc;
if (strchr (R_MAGIC_DESC, '%') == NULL)
return 0;
rc = r_regex_comp (&rx, "%[-0-9\\.]*s", R_REGEX_EXTENDED|R_REGEX_NOSUB);
if (rc) {
char errmsg[512];
r_regex_error (rc, &rx, errmsg, sizeof (errmsg));
file_magerror (ms, "regex error %d, (%s)", rc, errmsg);
return -1;
} else {
rc = r_regex_exec (&rx, R_MAGIC_DESC, 0, 0, 0);
r_regex_fini (&rx);
return !rc;
}
}
char * strdupn(const char *str, size_t n) {
size_t len;
char *copy;
for (len = 0; len < n && str[len]; len++)
continue;
if ((copy = malloc (len + 1)) == NULL)
return NULL;
(void)memcpy (copy, str, len);
copy[len] = '\0';
return copy;
}
static st32 mprint(RMagic *ms, struct r_magic *m) {
ut64 v;
float vf;
double vd;
ut64 t = 0;
char *buf;
union VALUETYPE *p = &ms->ms_value;
switch (m->type) {
case FILE_BYTE:
v = file_signextend(ms, m, (ut64)p->b);
switch (check_fmt(ms, m)) {
case -1:
return -1;
case 1:
buf = malloc (2);
if (snprintf (buf, 2, "%c", (ut8)v)<0) {
return -1;
free (buf);
}
if (file_printf (ms, R_MAGIC_DESC, buf) == -1)
free (buf);
return -1;
break;
default:
if (file_printf(ms, R_MAGIC_DESC, (ut8) v) == -1)
return -1;
break;
}
t = ms->offset + sizeof(char);
break;
case FILE_SHORT:
case FILE_BESHORT:
case FILE_LESHORT:
v = file_signextend (ms, m, (ut64)p->h);
switch (check_fmt (ms, m)) {
case -1:
return -1;
case 1:
buf = malloc (32);
if (snprintf (buf, 32, "%hu", (unsigned short)v) < 0) {
free (buf);
return -1;
}
if (file_printf(ms, R_MAGIC_DESC, buf) == -1) {
free (buf);
return -1;
}
break;
default:
if (file_printf(ms, R_MAGIC_DESC, (unsigned short) v) == -1)
return -1;
break;
}
t = ms->offset + sizeof(short);
break;
case FILE_LONG:
case FILE_BELONG:
case FILE_LELONG:
case FILE_MELONG:
v = file_signextend(ms, m, (ut64)p->l);
switch (check_fmt(ms, m)) {
case -1:
return -1;
case 1:
buf = malloc (32);
if (snprintf (buf, 32, "%u", (ut32)v) < 0) {
free (buf);
return -1;
}
if (file_printf(ms, R_MAGIC_DESC, buf) == -1) {
free (buf);
return -1;
}
break;
default:
if (file_printf(ms, R_MAGIC_DESC, (ut32) v) == -1)
return -1;
break;
}
t = ms->offset + sizeof(st32);
break;
case FILE_QUAD:
case FILE_BEQUAD:
case FILE_LEQUAD:
v = file_signextend(ms, m, p->q);
if (file_printf(ms, R_MAGIC_DESC, (ut64) v) == -1)
return -1;
t = ms->offset + sizeof(ut64);
break;
case FILE_STRING:
case FILE_PSTRING:
case FILE_BESTRING16:
case FILE_LESTRING16:
if (m->reln == '=' || m->reln == '!') {
if (file_printf (ms, R_MAGIC_DESC, m->value.s) == -1)
return -1;
t = ms->offset + m->vallen;
}
else {
if (*m->value.s == '\0')
p->s[strcspn (p->s, "\n")] = '\0';
if (file_printf (ms, R_MAGIC_DESC, p->s) == -1)
return -1;
t = ms->offset + strlen (p->s);
if (m->type == FILE_PSTRING)
t++;
}
break;
case FILE_DATE:
case FILE_BEDATE:
case FILE_LEDATE:
case FILE_MEDATE:
if (file_printf(ms, R_MAGIC_DESC, file_fmttime(p->l, 1)) == -1)
return -1;
t = ms->offset + sizeof(time_t);
break;
case FILE_LDATE:
case FILE_BELDATE:
case FILE_LELDATE:
case FILE_MELDATE:
if (file_printf(ms, R_MAGIC_DESC, file_fmttime(p->l, 0)) == -1)
return -1;
t = ms->offset + sizeof(time_t);
break;
case FILE_QDATE:
case FILE_BEQDATE:
case FILE_LEQDATE:
if (file_printf(ms, R_MAGIC_DESC, file_fmttime((ut32)p->q, 1))
== -1)
return -1;
t = ms->offset + sizeof(ut64);
break;
case FILE_QLDATE:
case FILE_BEQLDATE:
case FILE_LEQLDATE:
if (file_printf(ms, R_MAGIC_DESC, file_fmttime((ut32)p->q, 0))
== -1)
return -1;
t = ms->offset + sizeof(ut64);
break;
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
vf = p->f;
switch (check_fmt(ms, m)) {
case -1:
return -1;
case 1:
buf = malloc (32);
if (snprintf (buf, 32, "%g", vf) < 0) {
free (buf);
return -1;
}
if (file_printf (ms, R_MAGIC_DESC, buf) == -1) {
free (buf);
return -1;
}
break;
default:
if (file_printf(ms, R_MAGIC_DESC, vf) == -1)
return -1;
break;
}
t = ms->offset + sizeof(float);
break;
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
vd = p->d;
switch (check_fmt(ms, m)) {
case -1:
return -1;
case 1:
buf = malloc (32);
if (snprintf (buf, 32, "%g", vd) < 0) {
free (buf);
return -1;
}
if (file_printf (ms, R_MAGIC_DESC, buf) == -1) {
free (buf);
return -1;
}
break;
default:
if (file_printf(ms, R_MAGIC_DESC, vd) == -1)
return -1;
break;
}
t = ms->offset + sizeof(double);
break;
case FILE_REGEX: {
char *cp;
int rval;
cp = strdupn((const char *)ms->search.s, ms->search.rm_len);
if (cp == NULL) {
file_oomem(ms, ms->search.rm_len);
return -1;
}
rval = file_printf(ms, R_MAGIC_DESC, cp);
free(cp);
if (rval == -1)
return -1;
if ((m->str_flags & REGEX_OFFSET_START))
t = ms->search.offset;
else
t = ms->search.offset + ms->search.rm_len;
break;
}
case FILE_SEARCH:
if (file_printf(ms, R_MAGIC_DESC, m->value.s) == -1)
return -1;
if ((m->str_flags & REGEX_OFFSET_START))
t = ms->search.offset;
else t = ms->search.offset + m->vallen;
break;
case FILE_DEFAULT:
if (file_printf(ms, R_MAGIC_DESC, m->value.s) == -1)
return -1;
t = ms->offset;
break;
default:
file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
return -1;
}
return(t);
}
#define DO_CVT(fld, cast) \
if (m->num_mask) \
switch (m->mask_op & FILE_OPS_MASK) { \
case FILE_OPAND: \
p->fld &= cast m->num_mask; \
break; \
case FILE_OPOR: \
p->fld |= cast m->num_mask; \
break; \
case FILE_OPXOR: \
p->fld ^= cast m->num_mask; \
break; \
case FILE_OPADD: \
p->fld += cast m->num_mask; \
break; \
case FILE_OPMINUS: \
p->fld -= cast m->num_mask; \
break; \
case FILE_OPMULTIPLY: \
p->fld *= cast m->num_mask; \
break; \
case FILE_OPDIVIDE: \
p->fld /= cast m->num_mask; \
break; \
case FILE_OPMODULO: \
p->fld %= cast m->num_mask; \
break; \
} \
if (m->mask_op & FILE_OPINVERSE) \
p->fld = ~p->fld \
static void cvt_8(union VALUETYPE *p, const struct r_magic *m) {
DO_CVT (b, (ut8));
}
static void cvt_16(union VALUETYPE *p, const struct r_magic *m) {
DO_CVT (h, (ut16));
}
static void cvt_32(union VALUETYPE *p, const struct r_magic *m) {
DO_CVT (l, (ut32));
}
static void cvt_64(union VALUETYPE *p, const struct r_magic *m) {
DO_CVT (q, (ut64));
}
#define DO_CVT2(fld, cast) \
if (m->num_mask) \
switch (m->mask_op & FILE_OPS_MASK) { \
case FILE_OPADD: \
p->fld += cast m->num_mask; \
break; \
case FILE_OPMINUS: \
p->fld -= cast m->num_mask; \
break; \
case FILE_OPMULTIPLY: \
p->fld *= cast m->num_mask; \
break; \
case FILE_OPDIVIDE: \
p->fld /= cast m->num_mask; \
break; \
} \
static void cvt_float(union VALUETYPE *p, const struct r_magic *m) {
DO_CVT2 (f, (float));
}
static void cvt_double(union VALUETYPE *p, const struct r_magic *m) {
DO_CVT2 (d, (double));
}
/*
* Convert the byte order of the data we are looking at
* While we're here, let's apply the mask operation
* (unless you have a better idea)
*/
static int mconvert(RMagic *ms, struct r_magic *m) {
union VALUETYPE *p = &ms->ms_value;
switch (m->type) {
case FILE_BYTE:
cvt_8(p, m);
return 1;
case FILE_SHORT:
cvt_16(p, m);
return 1;
case FILE_LONG:
case FILE_DATE:
case FILE_LDATE:
cvt_32(p, m);
return 1;
case FILE_QUAD:
case FILE_QDATE:
case FILE_QLDATE:
cvt_64(p, m);
return 1;
case FILE_STRING:
case FILE_BESTRING16:
case FILE_LESTRING16: {
size_t len;
/* Null terminate and eat *trailing* return */
p->s[sizeof(p->s) - 1] = '\0';
len = strlen(p->s);
if (len-- && p->s[len] == '\n')
p->s[len] = '\0';
return 1;
}
case FILE_PSTRING: {
char *ptr1 = p->s, *ptr2 = ptr1 + 1;
size_t len = *p->s;
if (len >= sizeof(p->s))
len = sizeof(p->s) - 1;
while (len--)
*ptr1++ = *ptr2++;
*ptr1 = '\0';
len = strlen(p->s);
if (len-- && p->s[len] == '\n')
p->s[len] = '\0';
return 1;
}
case FILE_BESHORT:
p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
cvt_16(p, m);
return 1;
case FILE_BELONG:
case FILE_BEDATE:
case FILE_BELDATE:
p->l = (st32)
((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
cvt_32(p, m);
return 1;
case FILE_BEQUAD:
case FILE_BEQDATE:
case FILE_BEQLDATE:
p->q = (ut64)
(((ut64)p->hq[0]<<56)|((ut64)p->hq[1]<<48)|
((ut64)p->hq[2]<<40)|((ut64)p->hq[3]<<32)|
((ut64)p->hq[4]<<24)|((ut64)p->hq[5]<<16)|
((ut64)p->hq[6]<<8)|((ut64)p->hq[7]));
cvt_64(p, m);
return 1;
case FILE_LESHORT:
p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
cvt_16(p, m);
return 1;
case FILE_LELONG:
case FILE_LEDATE:
case FILE_LELDATE:
p->l = (st32)
((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
cvt_32(p, m);
return 1;
case FILE_LEQUAD:
case FILE_LEQDATE:
case FILE_LEQLDATE:
p->q = (ut64)
(((ut64)p->hq[7]<<56)|((ut64)p->hq[6]<<48)|
((ut64)p->hq[5]<<40)|((ut64)p->hq[4]<<32)|
((ut64)p->hq[3]<<24)|((ut64)p->hq[2]<<16)|
((ut64)p->hq[1]<<8)|((ut64)p->hq[0]));
cvt_64(p, m);
return 1;
case FILE_MELONG:
case FILE_MEDATE:
case FILE_MELDATE:
p->l = (st32)
((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
cvt_32(p, m);
return 1;
case FILE_FLOAT:
cvt_float(p, m);
return 1;
case FILE_BEFLOAT:
p->l = ((ut32)p->hl[0]<<24)|((ut32)p->hl[1]<<16)|
((ut32)p->hl[2]<<8) |((ut32)p->hl[3]);
cvt_float(p, m);
return 1;
case FILE_LEFLOAT:
p->l = ((ut32)p->hl[3]<<24)|((ut32)p->hl[2]<<16)|
((ut32)p->hl[1]<<8) |((ut32)p->hl[0]);
cvt_float(p, m);
return 1;
case FILE_DOUBLE:
cvt_double(p, m);
return 1;
case FILE_BEDOUBLE:
p->q = ((ut64)p->hq[0]<<56)|((ut64)p->hq[1]<<48)|
((ut64)p->hq[2]<<40)|((ut64)p->hq[3]<<32)|
((ut64)p->hq[4]<<24)|((ut64)p->hq[5]<<16)|
((ut64)p->hq[6]<<8) |((ut64)p->hq[7]);
cvt_double(p, m);
return 1;
case FILE_LEDOUBLE:
p->q = ((ut64)p->hq[7]<<56)|((ut64)p->hq[6]<<48)|
((ut64)p->hq[5]<<40)|((ut64)p->hq[4]<<32)|
((ut64)p->hq[3]<<24)|((ut64)p->hq[2]<<16)|
((ut64)p->hq[1]<<8) |((ut64)p->hq[0]);
cvt_double(p, m);
return 1;
case FILE_REGEX:
case FILE_SEARCH:
case FILE_DEFAULT:
return 1;
default:
file_magerror(ms, "invalid type %d in mconvert()", m->type);
return 0;
}
}
static void mdebug(ut32 offset, const char *str, size_t len) {
eprintf ("mget @%d: ", offset);
file_showstr (stderr, str, len);
(void) fputc('\n', stderr);
(void) fputc('\n', stderr);
}
static int mcopy(RMagic *ms, union VALUETYPE *p, int type, int indir, const ut8 *s, ut32 offset, size_t nbytes, size_t linecnt) {
/*
* Note: FILE_SEARCH and FILE_REGEX do not actually copy
* anything, but setup pointers into the source
*/
if (indir == 0) {
switch (type) {
case FILE_SEARCH:
ms->search.s = (const char *)s + offset;
ms->search.s_len = nbytes - offset;
ms->search.offset = offset;
return 0;
case FILE_REGEX: {
const char *b;
const char *c;
const char *last; /* end of search region */
const char *buf; /* start of search region */
size_t lines;
if (s == NULL) {
ms->search.s_len = 0;
ms->search.s = NULL;
return 0;
}
buf = (const char *)s + offset;
last = (const char *)s + nbytes;
/* mget() guarantees buf <= last */
for (lines = linecnt, b = buf;
lines && ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
lines--, b++) {
last = b;
if (b[0] == '\r' && b[1] == '\n')
b++;
}
if (lines)
last = (const char *)s + nbytes;
ms->search.s = buf;
ms->search.s_len = last - buf;
ms->search.offset = offset;
ms->search.rm_len = 0;
return 0;
}
case FILE_BESTRING16:
case FILE_LESTRING16: {
const ut8 *src = s + offset;
const ut8 *esrc = s + nbytes;
char *dst = p->s;
char *edst = &p->s[sizeof(p->s) - 1];
if (type == FILE_BESTRING16)
src++;
/* check for pointer overflow */
if (src < s) {
file_magerror(ms, "invalid offset %zu in mcopy()",
offset);
return -1;
}
for (/*EMPTY*/; src < esrc; src += 2, dst++) {
if (dst < edst)
*dst = *src;
else
break;
if (*dst == '\0') {
if (type == FILE_BESTRING16 ?
*(src - 1) != '\0' :
*(src + 1) != '\0')
*dst = ' ';
}
}
*edst = '\0';
return 0;
}
case FILE_STRING: /* XXX - these two should not need */
case FILE_PSTRING: /* to copy anything, but do anyway. */
default:
break;
}
}
if (offset >= nbytes) {
(void)memset(p, '\0', sizeof (*p));
return 0;
}
nbytes = (nbytes - offset < sizeof (*p))?
nbytes - offset: sizeof (*p);
(void)memcpy(p, s + offset, nbytes);
/*
* the usefulness of padding with zeroes eludes me, it
* might even cause problems
*/
if (nbytes < sizeof(*p))
(void)memset(((char *)(void *)p) + nbytes, '\0',
sizeof(*p) - nbytes);
return 0;
}
static int mget(RMagic *ms, const ut8 *s, struct r_magic *m, size_t nbytes, unsigned int cont_level) {
ut32 offset = ms->offset;
ut32 count = m->str_range;
union VALUETYPE *p = &ms->ms_value;
if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
return -1;
if ((ms->flags & R_MAGIC_DEBUG) != 0) {
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
file_mdump(m);
}
if (m->flag & INDIR) {
int off = m->in_offset;
if (m->in_op & FILE_OPINDIRECT) {
const union VALUETYPE *q =
((const void *)(s + offset + off));
switch (m->in_type) {
case FILE_BYTE:
off = q->b;
break;
case FILE_SHORT:
off = q->h;
break;
case FILE_BESHORT:
off = (short)((q->hs[0]<<8)|(q->hs[1]));
break;
case FILE_LESHORT:
off = (short)((q->hs[1]<<8)|(q->hs[0]));
break;
case FILE_LONG:
off = q->l;
break;
case FILE_BELONG:
off = (st32)((q->hl[0]<<24)|(q->hl[1]<<16)| (q->hl[2]<<8)|(q->hl[3]));
break;
case FILE_LELONG:
off = (st32)((q->hl[3]<<24)|(q->hl[2]<<16)| (q->hl[1]<<8)|(q->hl[0]));
break;
case FILE_MELONG:
off = (st32)((q->hl[1]<<24)|(q->hl[0]<<16)| (q->hl[3]<<8)|(q->hl[2]));
break;
}
}
switch (m->in_type) {
case FILE_BYTE:
if (nbytes < (offset + 1))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND: offset = p->b & off; break;
case FILE_OPOR: offset = p->b | off; break;
case FILE_OPXOR: offset = p->b ^ off; break;
case FILE_OPADD: offset = p->b + off; break;
case FILE_OPMINUS: offset = p->b - off; break;
case FILE_OPMULTIPLY: offset = p->b * off; break;
case FILE_OPDIVIDE: offset = p->b / off; break;
case FILE_OPMODULO: offset = p->b % off; break;
}
} else
offset = p->b;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_BESHORT:
if (nbytes < (offset + 2))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND: offset = (short)((p->hs[0]<<8)| (p->hs[1])) & off; break;
case FILE_OPOR: offset = (short)((p->hs[0]<<8)| (p->hs[1])) | off; break;
case FILE_OPXOR: offset = (short)((p->hs[0]<<8)| (p->hs[1])) ^ off; break;
case FILE_OPADD: offset = (short)((p->hs[0]<<8)| (p->hs[1])) + off; break;
case FILE_OPMINUS: offset = (short)((p->hs[0]<<8)| (p->hs[1])) - off; break;
case FILE_OPMULTIPLY: offset = (short)((p->hs[0]<<8)| (p->hs[1])) * off; break;
case FILE_OPDIVIDE: offset = (short)((p->hs[0]<<8)| (p->hs[1])) / off; break;
case FILE_OPMODULO: offset = (short)((p->hs[0]<<8)| (p->hs[1])) % off; break;
}
} else
offset = (short)((p->hs[0]<<8)|
(p->hs[1]));
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_LESHORT:
if (nbytes < (offset + 2))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND: offset = (short)((p->hs[1]<<8)| (p->hs[0])) & off; break;
case FILE_OPOR: offset = (short)((p->hs[1]<<8)| (p->hs[0])) | off; break;
case FILE_OPXOR: offset = (short)((p->hs[1]<<8)| (p->hs[0])) ^ off; break;
case FILE_OPADD: offset = (short)((p->hs[1]<<8)| (p->hs[0])) + off; break;
case FILE_OPMINUS: offset = (short)((p->hs[1]<<8)| (p->hs[0])) - off; break;
case FILE_OPMULTIPLY: offset = (short)((p->hs[1]<<8)| (p->hs[0])) * off; break;
case FILE_OPDIVIDE: offset = (short)((p->hs[1]<<8)| (p->hs[0])) / off; break;
case FILE_OPMODULO: offset = (short)((p->hs[1]<<8)| (p->hs[0])) % off; break;
}
} else offset = (short)((p->hs[1]<<8)| (p->hs[0]));
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_SHORT:
if (nbytes < (offset + 2))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND: offset = p->h & off; break;
case FILE_OPOR: offset = p->h | off; break;
case FILE_OPXOR: offset = p->h ^ off; break;
case FILE_OPADD: offset = p->h + off; break;
case FILE_OPMINUS: offset = p->h - off; break;
case FILE_OPMULTIPLY: offset = p->h * off; break;
case FILE_OPDIVIDE: offset = p->h / off; break;
case FILE_OPMODULO: offset = p->h % off; break;
}
} else offset = p->h;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_BELONG:
if (nbytes < (offset + 4))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) & off;
break;
case FILE_OPOR:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) | off;
break;
case FILE_OPXOR:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) ^ off;
break;
case FILE_OPADD:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) + off;
break;
case FILE_OPMINUS:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) - off;
break;
case FILE_OPMULTIPLY:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) * off;
break;
case FILE_OPDIVIDE:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) / off;
break;
case FILE_OPMODULO:
offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) % off;
break;
}
} else offset = (st32)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3]));
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_LELONG:
if (nbytes < (offset + 4))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) & off;
break;
case FILE_OPOR:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) | off;
break;
case FILE_OPXOR:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) ^ off;
break;
case FILE_OPADD:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) + off;
break;
case FILE_OPMINUS:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) - off;
break;
case FILE_OPMULTIPLY:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) * off;
break;
case FILE_OPDIVIDE:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) / off;
break;
case FILE_OPMODULO:
offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) % off;
break;
}
} else offset = (st32)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0]));
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_MELONG:
if (nbytes < (offset + 4))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) & off;
break;
case FILE_OPOR:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) | off;
break;
case FILE_OPXOR:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) ^ off;
break;
case FILE_OPADD:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) + off;
break;
case FILE_OPMINUS:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) - off;
break;
case FILE_OPMULTIPLY:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) * off;
break;
case FILE_OPDIVIDE:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) / off;
break;
case FILE_OPMODULO:
offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2])) % off;
break;
}
} else offset = (st32)((p->hl[1]<<24)| (p->hl[0]<<16)| (p->hl[3]<<8)| (p->hl[2]));
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_LONG:
if (nbytes < (offset + 4))
return 0;
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND: offset = p->l & off; break;
case FILE_OPOR: offset = p->l | off; break;
case FILE_OPXOR: offset = p->l ^ off; break;
case FILE_OPADD: offset = p->l + off; break;
case FILE_OPMINUS: offset = p->l - off; break;
case FILE_OPMULTIPLY: offset = p->l * off; break;
case FILE_OPDIVIDE: offset = p->l / off; break;
case FILE_OPMODULO: offset = p->l % off; break;
}
} else
offset = p->l;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
}
if (m->flag & INDIROFFADD)
offset += ms->c.li[cont_level-1].off;
if (mcopy (ms, p, m->type, 0, s, offset, nbytes, count) == -1)
return -1;
ms->offset = offset;
if ((ms->flags & R_MAGIC_DEBUG) != 0) {
mdebug (offset, (char *)(void *)p,
sizeof (union VALUETYPE));
file_mdump(m);
}
}
/* Verify we have enough data to match magic type */
switch (m->type) {
case FILE_BYTE:
if (nbytes < (offset + 1)) /* should alway be true */
return 0;
break;
case FILE_SHORT:
case FILE_BESHORT:
case FILE_LESHORT:
if (nbytes < (offset + 2))
return 0;
break;
case FILE_LONG:
case FILE_BELONG:
case FILE_LELONG:
case FILE_MELONG:
case FILE_DATE:
case FILE_BEDATE:
case FILE_LEDATE:
case FILE_MEDATE:
case FILE_LDATE:
case FILE_BELDATE:
case FILE_LELDATE:
case FILE_MELDATE:
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
if (nbytes < (offset + 4))
return 0;
break;
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
if (nbytes < (offset + 8))
return 0;
break;
case FILE_STRING:
case FILE_PSTRING:
case FILE_SEARCH:
if (nbytes < (offset + m->vallen))
return 0;
break;
case FILE_REGEX:
if (nbytes < offset)
return 0;
break;
case FILE_DEFAULT: /* nothing to check */
default:
break;
}
return mconvert (ms, m);
}
static ut64 file_strncmp(const char *s1, const char *s2, size_t len, ut32 flags) {
/*
* Convert the source args to unsigned here so that (1) the
* compare will be unsigned as it is in strncmp() and (2) so
* the ctype functions will work correctly without extra
* casting.
*/
const ut8 *a = (const ut8 *)s1;
const ut8 *b = (const ut8 *)s2;
ut64 v;
/*
* What we want here is v = strncmp(s1, s2, len),
* but ignoring any nulls.
*/
v = 0;
if (0L == flags) { /* normal string: do it fast */
while (len-- > 0)
if ((v = *b++ - *a++) != '\0')
break;
} else { /* combine the others */
while (len-- > 0) {
if ((flags & STRING_IGNORE_LOWERCASE) &&
islower(*a)) {
if ((v = tolower(*b++) - *a++) != '\0')
break;
} else if ((flags & STRING_IGNORE_UPPERCASE) && isupper(*a)) {
if ((v = toupper(*b++) - *a++) != '\0')
break;
} else if ((flags & STRING_COMPACT_BLANK) && isspace(*a)) {
a++;
if (isspace(*b++)) {
while (isspace(*b))
b++;
} else {
v = 1;
break;
}
} else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) && isspace(*a)) {
a++;
while (isspace(*b))
b++;
} else {
if ((v = *b++ - *a++) != '\0')
break;
}
}
}
return v;
}
static ut64 file_strncmp16(const char *a, const char *b, size_t len, ut32 flags) {
/*
* XXX - The 16-bit string compare probably needs to be done
* differently, especially if the flags are to be supported.
* At the moment, I am unsure.
*/
flags = 0;
return file_strncmp(a, b, len, flags);
}
static int magiccheck(RMagic *ms, struct r_magic *m) {
ut64 l = m->value.q;
ut64 v;
float fl, fv;
double dl, dv;
int matched;
union VALUETYPE *p = &ms->ms_value;
switch (m->type) {
case FILE_BYTE:
v = p->b;
break;
case FILE_SHORT:
case FILE_BESHORT:
case FILE_LESHORT:
v = p->h;
break;
case FILE_LONG:
case FILE_BELONG:
case FILE_LELONG:
case FILE_MELONG:
case FILE_DATE:
case FILE_BEDATE:
case FILE_LEDATE:
case FILE_MEDATE:
case FILE_LDATE:
case FILE_BELDATE:
case FILE_LELDATE:
case FILE_MELDATE:
v = p->l;
break;
case FILE_QUAD:
case FILE_LEQUAD:
case FILE_BEQUAD:
case FILE_QDATE:
case FILE_BEQDATE:
case FILE_LEQDATE:
case FILE_QLDATE:
case FILE_BEQLDATE:
case FILE_LEQLDATE:
v = p->q;
break;
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
fl = m->value.f;
fv = p->f;
switch (m->reln) {
case 'x': matched = 1; break;
case '!': matched = fv != fl; break;
case '=': matched = fv == fl; break;
case '>': matched = fv > fl; break;
case '<': matched = fv < fl; break;
default:
matched = 0;
file_magerror(ms, "cannot happen with float: invalid relation `%c'", m->reln);
return -1;
}
return matched;
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
dl = m->value.d;
dv = p->d;
switch (m->reln) {
case 'x': matched = 1; break;
case '!': matched = dv != dl; break;
case '=': matched = dv == dl; break;
case '>': matched = dv > dl; break;
case '<': matched = dv < dl; break;
default:
matched = 0;
file_magerror (ms, "cannot happen with double: invalid relation `%c'", m->reln);
return -1;
}
return matched;
case FILE_DEFAULT:
l = 0;
v = 0;
break;
case FILE_STRING:
case FILE_PSTRING:
l = 0;
v = file_strncmp (m->value.s, p->s, (size_t)m->vallen, m->str_flags);
break;
case FILE_BESTRING16:
case FILE_LESTRING16:
l = 0;
v = file_strncmp16 (m->value.s, p->s, (size_t)m->vallen, m->str_flags);
break;
case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
size_t slen, idx;
if (ms->search.s == NULL)
return 0;
slen = R_MIN (m->vallen, sizeof (m->value.s));
l = 0;
v = 0;
for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
if (slen + idx > ms->search.s_len)
break;
v = file_strncmp (m->value.s, ms->search.s + idx, slen, m->str_flags);
if (v == 0) { /* found match */
ms->search.offset += idx;
break;
}
}
break;
}
case FILE_REGEX: {
int rc;
RRegex rx;
char errmsg[512];
if (ms->search.s == NULL)
return 0;
l = 0;
rc = r_regex_comp (&rx, m->value.s,
R_REGEX_EXTENDED|R_REGEX_NEWLINE|
((m->str_flags & STRING_IGNORE_CASE) ? R_REGEX_ICASE : 0));
if (rc) {
(void)r_regex_error(rc, &rx, errmsg, sizeof(errmsg));
file_magerror(ms, "regex error %d, (%s)",
rc, errmsg);
v = (ut64)-1;
} else {
RRegexMatch pmatch[1];
#ifndef R_REGEX_STARTEND
#define R_REGEX_STARTEND 0
size_t l = ms->search.s_len - 1;
char c = ms->search.s[l];
((char *)(intptr_t)ms->search.s)[l] = '\0';
#else
pmatch[0].rm_so = 0;
pmatch[0].rm_eo = ms->search.s_len;
#endif
rc = r_regex_exec (&rx, (const char *)ms->search.s, 1, pmatch, R_REGEX_STARTEND);
#if R_REGEX_STARTEND == 0
((char *)(intptr_t)ms->search.s)[l] = c;
#endif
switch (rc) {
case 0:
ms->search.s += (int)pmatch[0].rm_so;
ms->search.offset += (size_t)pmatch[0].rm_so;
ms->search.rm_len =
(size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
v = 0;
break;
case R_REGEX_NOMATCH:
v = 1;
break;
default:
(void)r_regex_error(rc, &rx, errmsg, sizeof(errmsg));
file_magerror(ms, "regexec error %d, (%s)",
rc, errmsg);
v = (ut64)-1;
break;
}
r_regex_fini (&rx);
}
if (v == (ut64)-1)
return -1;
break;
}
default:
file_magerror (ms, "invalid type %d in magiccheck()", m->type);
return -1;
}
v = file_signextend (ms, m, v);
switch (m->reln) {
case 'x':
if ((ms->flags & R_MAGIC_DEBUG) != 0)
(void) fprintf(stderr, "%"PFMT64u" == *any* = 1\n", (ut64)v);
matched = 1;
break;
case '!':
matched = v != l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
fprintf(stderr, "%"PFMT64u" != %"PFMT64u" = %d\n", (ut64)v, (ut64)l, matched);
break;
case '=':
matched = v == l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf ("%"PFMT64u" == %"PFMT64u" = %d\n", (ut64)v, (ut64)l, matched);
break;
case '>':
if (m->flag & UNSIGNED) {
matched = v > l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf ("%"PFMT64u" > %"PFMT64u" = %d\n", (ut64)v, (ut64)l, matched);
} else {
matched = (ut64) v > (ut64) l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf ("%"PFMT64u" > %"PFMT64u" = %d\n", (st64)v, (st64)l, matched);
}
break;
case '<':
if (m->flag & UNSIGNED) {
matched = v < l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf ("%"PFMT64u" < %"PFMT64u" = %d\n", (ut64)v, (ut64)l, matched);
} else {
matched = (ut64) v < (ut64) l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf ("%"PFMT64d" < %"PFMT64d" = %d\n", (st64)v, (st64)l, matched);
}
break;
case '&':
matched = (v & l) == l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf("((%"PFMT64x" & %"PFMT64x") == %"PFMT64x") = %d\n", (ut64)v, (ut64)l, (ut64)l, matched);
break;
case '^':
matched = (v & l) != l;
if ((ms->flags & R_MAGIC_DEBUG) != 0)
eprintf ("((%"PFMT64x" & %"PFMT64x") != %"PFMT64x") = %d\n", (ut64)v, (ut64)l, (ut64)l, matched);
break;
default:
matched = 0;
file_magerror (ms, "cannot happen: invalid relation `%c'", m->reln);
return -1;
}
return matched;
}
static int print_sep(RMagic *ms, int firstline) {
return firstline? 0: file_printf (ms, "\n- ");
}
#endif
|