]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/reveng/poly.c
chg: @piwi's code cleanup and some more.
[proxmark3-svn] / client / reveng / poly.c
index 1e22b8d27d7a014611a86de4844ccca751ab0bf5..35e38af3f71c1dc7e5a20d016b493e8e226f4493 100644 (file)
@@ -1,9 +1,9 @@
 /* poly.c
- * Greg Cook, 9/Apr/2015
+ * Greg Cook, 26/Jul/2016
  */
 
-/* CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder
- * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015  Gregory Cook
+/* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016  Gregory Cook
  *
  * This file is part of CRC RevEng.
  *
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with CRC RevEng.  If not, see <http://www.gnu.org/licenses/>.
+ * along with CRC RevEng.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-/* 2015-04-03: added direct mode to strtop()
+/* 2016-06-27: pcmp() shortcut returns 0 when pointers identical
+ * 2015-07-29: discard leading $, &, 0x from argument to strtop()
+ * 2015-04-03: added direct mode to strtop()
  * 2014-01-11: added LOFS(), RNDUP()
  * 2013-09-16: SIZE(), IDX(), OFS() macros bitshift if BMP_POF2
  * 2013-02-07: conditional non-2^n fix, pmpar() return mask constant type
@@ -139,7 +141,7 @@ filtop(FILE *input, unsigned long length, int flags, int bperhx) {
        bmp_t accu = BMP_C(0);
        bmp_t mask = bperhx == BMP_BIT ? ~BMP_C(0) : (BMP_C(1) << bperhx) - BMP_C(1);
        unsigned long iter = 0UL, idx;
-       int cmask = ~(~0 << CHAR_BIT), c;
+       int cmask = (1 << CHAR_BIT) - 1, c;
        int count = 0, ofs;
        poly_t poly = PZERO;
        if(bperhx == 0) return(poly);
@@ -204,13 +206,22 @@ strtop(const char *string, int flags, int bperhx) {
        bmp_t accu;
        bmp_t mask = bperhx == BMP_BIT ? ~BMP_C(0) : (BMP_C(1) << bperhx) - BMP_C(1);
        int pass, count, ofs;
-       int cmask = ~(~0 << CHAR_BIT), c;
+       int cmask = (1 << CHAR_BIT) - 1 , c;
        const char *s;
 
        poly_t poly = PZERO;
        if(bperhx > BMP_BIT || bperhx <= 0 || string == NULL || *string == '\0')
                return(poly);
 
+       if(~flags & P_DIRECT) {
+               if(*string == '$' || *string == '&')
+                       ++string;
+               else if(*string == '0'
+                       && (string[1] == 'x' || string[1] == 'X'))
+                       string += 2;
+       }
+       length = (*string != '\0');
+
        for(pass=0; pass<2 && length > 0UL; ++pass) {
                s = string;
                length = 0UL;
@@ -501,6 +512,8 @@ pcmp(const poly_t *a, const poly_t *b) {
        if(a->length > b->length) return(1);
        aptr = a->bitmap;
        bptr = b->bitmap;
+       if(aptr == bptr)
+               return(0);
        for(iter=0UL; iter < a->length; iter += BMP_BIT) {
                if(*aptr < *bptr)
                        return(-1);
@@ -786,15 +799,15 @@ prev(poly_t *poly) {
        unsigned long fulllength = poly->length + ofs;
        bmp_t accu;
 
-       if(ofs)
+       if(ofs) {
                /* removable optimisation */
                if(poly->length < (unsigned long) BMP_BIT) {
                        *poly->bitmap = rev(*poly->bitmap >> ofs, (int) poly->length) << ofs;
                        return;
                }
-
-               /* claim remaining bits of last word (as we use public function pshift()) */
-               poly->length = fulllength;
+       }
+       /* claim remaining bits of last word (as we use public function pshift()) */
+       poly->length = fulllength;
 
        /* reverse and swap words in the array, leaving it right-justified */
        while(leftidx < rightidx) {
Impressum, Datenschutz