Browse code

compiler indepenedent sign-extended signed right shift macroes

git-svn: trunk@3126

aCaB authored on 2007/07/09 01:43:56
Showing 8 changed files
... ...
@@ -1,3 +1,8 @@
1
+Sun Jul  8 17:25:04 CEST 2007 (acab)
2
+------------------------------------
3
+  * misc: Implement compiler indepenedent sign-extended signed right shift
4
+  	  when needed - reported by Michal Spadlinski <gim913 * gmail.com>
5
+
1 6
 Sat Jul 07 10:52:00 CEST 2007 (edwin)
2 7
 ----------------------------------
3 8
   * libclamav/phishcheck.c: fix typo in regex
... ...
@@ -216,6 +216,9 @@
216 216
 /* have resolv.h */
217 217
 #undef HAVE_RESOLV_H
218 218
 
219
+/* Define signed right shift implementation */
220
+#undef HAVE_SAR
221
+
219 222
 /* Define to 1 if you have the `sendmsg' function. */
220 223
 #undef HAVE_SENDMSG
221 224
 
... ...
@@ -25852,6 +25852,63 @@ cat >>confdefs.h <<_ACEOF
25852 25852
 _ACEOF
25853 25853
 
25854 25854
 
25855
+{ echo "$as_me:$LINENO: checking if signed right shift is sign extended" >&5
25856
+echo $ECHO_N "checking if signed right shift is sign extended... $ECHO_C" >&6; }
25857
+if test "$cross_compiling" = yes; then
25858
+  have_signed_rightshift_extended=no
25859
+else
25860
+  cat >conftest.$ac_ext <<_ACEOF
25861
+/* confdefs.h.  */
25862
+_ACEOF
25863
+cat confdefs.h >>conftest.$ac_ext
25864
+cat >>conftest.$ac_ext <<_ACEOF
25865
+/* end confdefs.h.  */
25866
+int main(void){int a=-1;int b=a>>1;return(a!=b);}
25867
+_ACEOF
25868
+rm -f conftest$ac_exeext
25869
+if { (ac_try="$ac_link"
25870
+case "(($ac_try" in
25871
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
25872
+  *) ac_try_echo=$ac_try;;
25873
+esac
25874
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
25875
+  (eval "$ac_link") 2>&5
25876
+  ac_status=$?
25877
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
25878
+  (exit $ac_status); } && { ac_try='./conftest$ac_exeext'
25879
+  { (case "(($ac_try" in
25880
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
25881
+  *) ac_try_echo=$ac_try;;
25882
+esac
25883
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
25884
+  (eval "$ac_try") 2>&5
25885
+  ac_status=$?
25886
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
25887
+  (exit $ac_status); }; }; then
25888
+  have_signed_rightshift_extended=yes
25889
+else
25890
+  echo "$as_me: program exited with status $ac_status" >&5
25891
+echo "$as_me: failed program was:" >&5
25892
+sed 's/^/| /' conftest.$ac_ext >&5
25893
+
25894
+( exit $ac_status )
25895
+have_signed_rightshift_extended=no
25896
+fi
25897
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
25898
+fi
25899
+
25900
+
25901
+if test $have_signed_rightshift_extended = yes; then
25902
+
25903
+cat >>confdefs.h <<\_ACEOF
25904
+#define HAVE_SAR 1
25905
+_ACEOF
25906
+
25907
+fi
25908
+{ echo "$as_me:$LINENO: result: $have_signed_rightshift_extended" >&5
25909
+echo "${ECHO_T}$have_signed_rightshift_extended" >&6; };
25910
+
25911
+
25855 25912
 ac_config_files="$ac_config_files libclamav/Makefile clamscan/Makefile database/Makefile docs/Makefile clamd/Makefile clamdscan/Makefile clamav-milter/Makefile freshclam/Makefile sigtool/Makefile clamconf/Makefile etc/Makefile Makefile clamav-config libclamav.pc docs/man/clamav-milter.8 docs/man/clamconf.1 docs/man/clamd.8 docs/man/clamd.conf.5 docs/man/clamdscan.1 docs/man/clamscan.1 docs/man/freshclam.1 docs/man/freshclam.conf.5 docs/man/sigtool.1"
25856 25913
 
25857 25914
 cat >confcache <<\_ACEOF
... ...
@@ -1059,6 +1059,18 @@ DEFAULT_FD_SETSIZE=256)
1059 1059
 AC_MSG_RESULT($DEFAULT_FD_SETSIZE)
1060 1060
 AC_DEFINE_UNQUOTED(DEFAULT_FD_SETSIZE, $DEFAULT_FD_SETSIZE, "default FD_SETSIZE value")
1061 1061
 
1062
+dnl Check signed right shift implementation
1063
+AC_MSG_CHECKING(if signed right shift is sign extended)
1064
+AC_TRY_RUN([int main(void){int a=-1;int b=a>>1;return(a!=b);}],
1065
+    [have_signed_rightshift_extended=yes],
1066
+    [have_signed_rightshift_extended=no],
1067
+    [have_signed_rightshift_extended=no])
1068
+if test $have_signed_rightshift_extended = yes; then
1069
+    AC_DEFINE(HAVE_SAR, 1, Define signed right shift implementation)
1070
+fi
1071
+AC_MSG_RESULT($have_signed_rightshift_extended);
1072
+
1073
+
1062 1074
 AC_OUTPUT([
1063 1075
 libclamav/Makefile
1064 1076
 clamscan/Makefile
... ...
@@ -132,6 +132,14 @@ typedef struct {
132 132
 #define ROL(a,b) a = ( a << (b % (sizeof(a)<<3) ))  |  (a >> (  (sizeof(a)<<3)  -  (b % (sizeof(a)<<3 )) ) )
133 133
 #define ROR(a,b) a = ( a >> (b % (sizeof(a)<<3) ))  |  (a << (  (sizeof(a)<<3)  -  (b % (sizeof(a)<<3 )) ) )
134 134
 
135
+/* Implementation independent sign-extended signed right shift */
136
+#ifdef HAVE_SAR
137
+#define SRS(n,s) ((n)>>(s))
138
+#else
139
+#define SRS(n,s) (((n)>>(s)) ^ (1<<(sizeof(n)*8-1-s)) - (1<<(sizeof(n)*8-1-s)))
140
+#endif
141
+#define SAR(n,s) n = SRS(n,s)
142
+
135 143
 #ifndef	FALSE
136 144
 #define FALSE (0)
137 145
 #endif
... ...
@@ -400,7 +400,7 @@ int getbit_from_table(uint16_t *intable, struct UNSP *read_struct) {
400 400
     read_struct->bitmap = nval;
401 401
     nval = *intable;
402 402
     sval = 0x800 - nval;
403
-    sval = ((int32_t)sval)>>5; /* signed */
403
+    sval = SRS((int32_t)sval,5); /* signed */
404 404
     sval += nval;
405 405
     *intable=sval;
406 406
     if (read_struct->bitmap<0x1000000) { /* unsigned */
... ...
@@ -543,7 +543,7 @@ int unupack399(char *bs, uint32_t bl, uint32_t init_eax, char *init_ebx, uint32_
543 543
 						return -1;
544 544
 					/* cdq, loc_edx = (loc_eax&0x80000000)?0xffffffff:0; */
545 545
 					loc_ecx = temp_ebp;
546
-					temp_ebp = (int32_t)loc_eax >> 31; /* thx, desp */
546
+					temp_ebp = SRS((int32_t)loc_eax, 31); /* thx, desp */
547 547
 					/* loc_483a00 */
548 548
 					do {
549 549
 						temp_ebp += temp_ebp;
... ...
@@ -394,7 +394,7 @@ int upx_inflate2d(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_
394 394
       if (!backbytes)
395 395
 	break;
396 396
       backsize = backbytes & 1;
397
-      backbytes>>=1;
397
+      SAR(backbytes,1);
398 398
       unp_offset = backbytes;
399 399
     } else {
400 400
       if ( (backsize = (uint32_t)doubleebx(src, &myebx, &scur, ssize)) == 0xffffffff )
... ...
@@ -474,7 +474,7 @@ int upx_inflate2e(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_
474 474
       if (!backbytes)
475 475
 	break;
476 476
       backsize = backbytes & 1; /* Using backsize to carry on the shifted out bit (UPX uses CF) */
477
-      backbytes>>=1;
477
+      SAR(backbytes,1);
478 478
       unp_offset = backbytes;
479 479
     } else {
480 480
       if ( (backsize = (uint32_t)doubleebx(src, &myebx, &scur, ssize)) == 0xffffffff )