diff --git a/src/p_exe.cpp b/src/p_exe.cpp index 13fdee31..00586be9 100644 --- a/src/p_exe.cpp +++ b/src/p_exe.cpp @@ -338,7 +338,7 @@ unsigned optimize_relocs(upx_byte *b, const unsigned size, set_le16 (crel_save+2,seg_high); //OutputFile::dump("x.rel", crel_save, crel - crel_save); - return crel - crel_save; + return (unsigned) (crel - crel_save); } @@ -605,12 +605,11 @@ void PackExe::unpack(OutputFile *fo) { relocs -= get_le16(obuf+ph.u_len-2); ph.u_len -= 2; - upx_byte *p; wrkmem.alloc(4*MAXRELOCS); - unsigned es = 0,ones = get_le16(relocs); - unsigned seghi = get_le16(relocs+2); - p = relocs + 4; + unsigned es = 0, ones = get_le16(relocs); + const unsigned seghi = get_le16(relocs+2); + const upx_byte *p = relocs + 4; while (ones) { @@ -628,10 +627,10 @@ void PackExe::unpack(OutputFile *fo) dorel = true; if (*p == 0) { - upx_byte *q; + const upx_byte *q; for (q = obuf+es*16+di; !(*q == 0x9a && get_le16(q+3) <= seghi); q++) ; - di = q - (obuf+es*16) + 3; + di = ptr_diff(q, obuf+es*16) + 3; } else if (*p == 1) { diff --git a/src/p_w32pe.cpp b/src/p_w32pe.cpp index 30c18b49..def39cd6 100644 --- a/src/p_w32pe.cpp +++ b/src/p_w32pe.cpp @@ -66,6 +66,18 @@ static const # define strcpy(a,b) strcpy((char *)(a),(const char *)(b)) #endif +#if 1 +static unsigned my_strlen(const char *s) +{ + size_t l = strlen((const char*)s); assert((unsigned) l == l); return (unsigned) l; +} +static unsigned my_strlen(const unsigned char *s) +{ + size_t l = strlen((const char*)s); assert((unsigned) l == l); return (unsigned) l; +} +#define strlen my_strlen +#endif + // Unicode string compare static bool ustrsame(const void *s1, const void *s2) @@ -728,11 +740,11 @@ unsigned PackW32Pe::processImports() // pass 1 } ppi++; - unsigned esize = ptr_diff(tarr,idlls[ic]->lookupt); + unsigned esize = ptr_diff((char *)tarr, (char *)idlls[ic]->lookupt); lookups.add(idlls[ic]->lookupt,esize); - if (ptr_diff(ibuf + idlls[ic]->iat,idlls[ic]->lookupt)) + if (ptr_diff(ibuf + idlls[ic]->iat, (char *)idlls[ic]->lookupt)) { - memcpy(ibuf + idlls[ic]->iat,idlls[ic]->lookupt,esize); + memcpy(ibuf + idlls[ic]->iat, idlls[ic]->lookupt, esize); iats.add(idlls[ic]->iat,esize); } names.add(idlls[ic]->name,strlen(idlls[ic]->name) + 1 + 1); @@ -908,26 +920,26 @@ void Export::convert(unsigned eoffs,unsigned esize) #endif } -void Export::build(char *newbase,unsigned newoffs) +void Export::build(char *newbase, unsigned newoffs) { - char *functionp = newbase + sizeof(edir); - char *namep = functionp + 4 * edir.functions; - char *ordinalp = namep + 4 * edir.names; - char *enamep = ordinalp + 2 * edir.names; - char *exports = enamep + strlen(ename) + 1; + char * const functionp = newbase + sizeof(edir); + char * const namep = functionp + 4 * edir.functions; + char * const ordinalp = namep + 4 * edir.names; + char * const enamep = ordinalp + 2 * edir.names; + char * exports = enamep + strlen(ename) + 1; - edir.addrtable = newoffs + functionp - newbase; - edir.ordinaltable = newoffs + ordinalp - newbase; + edir.addrtable = newoffs + ptr_diff(functionp, newbase); + edir.ordinaltable = newoffs + ptr_diff(ordinalp, newbase); memcpy(ordinalp,ordinals,2 * edir.names); - edir.name = newoffs + enamep - newbase; + edir.name = newoffs + ptr_diff(enamep, newbase); strcpy(enamep,ename); - edir.nameptrtable = newoffs + namep - newbase; + edir.nameptrtable = newoffs + ptr_diff(namep, newbase); unsigned ic; for (ic = 0; ic < edir.names; ic++) { strcpy(exports,names[ic]); - set_le32(namep + 4 * ic,newoffs + exports - newbase); + set_le32(namep + 4 * ic,newoffs + ptr_diff(exports, newbase)); exports += strlen(exports) + 1; } @@ -936,7 +948,7 @@ void Export::build(char *newbase,unsigned newoffs) if (names[edir.names + ic]) { strcpy(exports,names[edir.names + ic]); - set_le32(functionp + 4 * ic,newoffs + exports - newbase); + set_le32(functionp + 4 * ic,newoffs + ptr_diff(exports, newbase)); exports += strlen(exports) + 1; } diff --git a/src/p_wcle.cpp b/src/p_wcle.cpp index 4bcc603b..51dd47de 100644 --- a/src/p_wcle.cpp +++ b/src/p_wcle.cpp @@ -166,7 +166,7 @@ void PackWcle::encodeEntryTable() //if (Opt_debug) printf("%d entries encoded.\n",n); UNUSED(n); - soentries = p - ientries + 1; + soentries = ptr_diff(p, ientries) + 1; oentries = ientries; ientries = NULL; } @@ -402,7 +402,7 @@ void PackWcle::preprocessFixups() set_le32(fix,0xFFFFFFFFUL); fix += 4; - sofixups = fix - ifixups; + sofixups = ptr_diff(fix, ifixups); } @@ -590,7 +590,7 @@ void PackWcle::decodeFixups() while (*selfrel_fixups != 0xC3) selfrel_fixups += 9; selfrel_fixups++; - unsigned selectlen = (selfrel_fixups - selector_fixups)/9; + unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9; ofixups = new upx_byte[fixupn*9+1000+selectlen*5]; upx_bytep fp = ofixups; @@ -656,11 +656,11 @@ void PackWcle::decodeFixups() fp += fp[1] ? 9 : 7; jc += 2; } - set_le32(ofpage_table+ic,fp-ofixups); + set_le32(ofpage_table+ic,ptr_diff(fp,ofixups)); } for (ic=0; ic < FIXUP_EXTRA; ic++) *fp++ = 0; - sofixups = fp-ofixups; + sofixups = ptr_diff(fp, ofixups); } @@ -748,7 +748,7 @@ void PackWcle::decodeEntryTable() //if (Opt_debug) printf("\n%d entries decoded.\n",n); - soentries = p - ientries + 1; + soentries = ptr_diff(p, ientries) + 1; oentries = ientries; ientries = NULL; }