rkujawa / mphidflash

Automatically exported from code.google.com/p/mphidflash
GNU General Public License v3.0
0 stars 0 forks source link

verifyBlock() function is hard to understand #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

What version of the product are you using? On what operating system?
v1.4

I offer you a commented version, the only code change is to remove unused 
variable ret:

/* check memory address & length are in a programmable memory area, as reported 
by device's Bootloader */
static int verifyBlock( unsigned int *addr, char *len )
{
    int i, isA, isL, MA, ML;
    for ( i = 0; i < devQuery.memBlocks; i++ )
    {
        /* only look at programmable memory blocks */
        if ( !devQuery.mem[ i ].Type ) continue;

        MA = devQuery.mem[ i ].Address;
        ML = devQuery.mem[ i ].Length;
        /* calc if first or last address is in this block */
        isA = ( *addr >= MA ) && ( *addr < MA + ML );
        isL = ( *addr + *len > MA ) && ( *addr + *len <= MA + ML );

        /* loop if neither */
        if ( !isA && !isL ) continue;

        /* both addresses is fine */
        if ( isA && isL ) return 0;
        /* if only start address we adjust length to end of block */
        if ( isA ) *len = ( MA + ML ) - *addr;
        /* if only last address we adjust start to start of block */
        if ( isL ) { *len = ( *addr + *len ) - MA; *addr = MA; }
        return 0;
    }
    /* not programmable */
    return 1;
}

Original issue reported on code.google.com by tonyna...@gmail.com on 1 Jul 2014 at 4:14

GoogleCodeExporter commented 9 years ago
Forgot to mention that the function is in hex.c at line 114, in r13 of trunk.

Original comment by tonyna...@gmail.com on 1 Jul 2014 at 5:01

GoogleCodeExporter commented 9 years ago
Comments added and some further formatting for readability.

Original comment by fnargwibble on 1 Jul 2014 at 8:24