lib/sg_lib.c
static void
dStrHexFp(const char* str, int len, int no_ascii, FILE * fp)
{
    const char * p = str;
    const char * formatstr;
    unsigned char c;
    char buff[82];
    int a = 0;
    int bpstart = 5;
    const int cpstart = 60;
    int cpos = cpstart;
    int bpos = bpstart;
    int i, k, blen;

    if (len <= 0)
        return;
    blen = (int)sizeof(buff);
    if (0 == no_ascii)  /* address at left and ASCII at right */
        formatstr = "%.76s\n";
    else if (no_ascii > 0)
        formatstr = "%s\n";     /* was: "%.58s\n" */
    else /* negative: no address at left and no ASCII at right */
        formatstr = "%s\n";     /* was: "%.48s\n"; */
    memset(buff, ' ', 80);
    buff[80] = '\0';
    if (no_ascii < 0) {
        bpstart = 0;
        bpos = bpstart;
        for (k = 0; k < len; k++) {
            c = *p++;
            if (bpos == (bpstart + (8 * 3)))
                bpos++;
            my_snprintf(&buff[bpos], blen - bpos, "%.2x",
                        (int)(unsigned char)c);
            buff[bpos + 2] = ' ';
            if ((k > 0) && (0 == ((k + 1) % 16))) {
                trimTrailingSpaces(buff);
                fprintf(fp, formatstr, buff);
                bpos = bpstart;
                memset(buff, ' ', 80);
            } else
                bpos += 3;
        }
        if (bpos > bpstart) {
            buff[bpos + 2] = '\0';
            trimTrailingSpaces(buff);
            fprintf(fp, "%s\n", buff);
        }
        return;
    }
    /* no_ascii>=0, start each line with address (offset) */
    k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
    buff[k + 1] = ' ';

    for (i = 0; i < len; i++) {
        c = *p++;
        bpos += 3;
        if (bpos == (bpstart + (9 * 3)))
            bpos++;
        my_snprintf(&buff[bpos], blen - bpos, "%.2x", (int)(unsigned char)c);
        buff[bpos + 2] = ' ';
        if (no_ascii)
            buff[cpos++] = ' ';
        else {
            if ((c < ' ') || (c >= 0x7f))
                c = '.';
            buff[cpos++] = c;
        }
        if (cpos > (cpstart + 15)) {
            if (no_ascii)
                trimTrailingSpaces(buff);
            fprintf(fp, formatstr, buff);
            bpos = bpstart;
            cpos = cpstart;
            a += 16;
            memset(buff, ' ', 80);
            k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
            buff[k + 1] = ' ';
        }
    }
    if (cpos > cpstart) {
        buff[cpos] = '\0';
        if (no_ascii)
            trimTrailingSpaces(buff);
        fprintf(fp, "%s\n", buff);
    }
}
/* Note the ASCII-hex output goes to stdout. [Most other output from functions
 * in this file go to sg_warnings_strm (default stderr).]
 * 'no_ascii' allows for 3 output types:
 *     > 0     each line has address then up to 16 ASCII-hex bytes
 *     = 0     in addition, the bytes are listed in ASCII to the right
 *     < 0     only the ASCII-hex bytes are listed (i.e. without address) */
Note the ASCII-hex output goes to stdout. [Most other output from functions
in this file go to sg_warnings_strm (default stderr).]
'no_ascii' allows for 3 output types:
> 0     each line has address then up to 16 ASCII-hex bytes
= 0     in addition, the bytes are listed in ASCII to the right
< 0     only the ASCII-hex bytes are listed
Note the ASCII - hex output goes to stdout . [ Most other output from functions in this file go to sg_warnings_strm ( default stderr ) . ] ' no_ascii ' allows for 3 output types : > 0 each line has address then up to 16 ASCII - hex bytes = 0 in addition the bytes are listed in ASCII to the right < 0 only the ASCII - hex bytes are listed
static void
dStrHexFp(const char* str, int len, int no_ascii, FILE * fp)
{
    const char * p = str;
    const char * formatstr;
    unsigned char c;
    char buff[82];
    int a = 0;
    int bpstart = 5;
    const int cpstart = 60;
    int cpos = cpstart;
    int bpos = bpstart;
    int i, k, blen;
    if (len <= 0)
        return;
    blen = (int)sizeof(buff);
    if (0 == no_ascii)  
        formatstr = "%.76s\n";
    else if (no_ascii > 0)
        formatstr = "%s\n";     
    else 
        formatstr = "%s\n";     
    memset(buff, ' ', 80);
    buff[80] = '\0';
    if (no_ascii < 0) {
        bpstart = 0;
        bpos = bpstart;
        for (k = 0; k < len; k++) {
            c = *p++;
            if (bpos == (bpstart + (8 * 3)))
                bpos++;
            my_snprintf(&buff[bpos], blen - bpos, "%.2x",
                        (int)(unsigned char)c);
            buff[bpos + 2] = ' ';
            if ((k > 0) && (0 == ((k + 1) % 16))) {
                trimTrailingSpaces(buff);
                fprintf(fp, formatstr, buff);
                bpos = bpstart;
                memset(buff, ' ', 80);
            } else
                bpos += 3;
        }
        if (bpos > bpstart) {
            buff[bpos + 2] = '\0';
            trimTrailingSpaces(buff);
            fprintf(fp, "%s\n", buff);
        }
        return;
    }
    k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
    buff[k + 1] = ' ';
    for (i = 0; i < len; i++) {
        c = *p++;
        bpos += 3;
        if (bpos == (bpstart + (9 * 3)))
            bpos++;
        my_snprintf(&buff[bpos], blen - bpos, "%.2x", (int)(unsigned char)c);
        buff[bpos + 2] = ' ';
        if (no_ascii)
            buff[cpos++] = ' ';
        else {
            if ((c < ' ') || (c >= 0x7f))
                c = '.';
            buff[cpos++] = c;
        }
        if (cpos > (cpstart + 15)) {
            if (no_ascii)
                trimTrailingSpaces(buff);
            fprintf(fp, formatstr, buff);
            bpos = bpstart;
            cpos = cpstart;
            a += 16;
            memset(buff, ' ', 80);
            k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
            buff[k + 1] = ' ';
        }
    }
    if (cpos > cpstart) {
        buff[cpos] = '\0';
        if (no_ascii)
            trimTrailingSpaces(buff);
        fprintf(fp, "%s\n", buff);
    }
}
static void dStrHexFp ( const char * str , int len , int no_ascii , FILE * fp ) { const char * p = str ; const char * formatstr ; unsigned char c ; char buff [ 82 ] ; int a = 0 ; int bpstart = 5 ; const int cpstart = 60 ; int cpos = cpstart ; int bpos = bpstart ; int i , k , blen ; if ( len <= 0 ) return ; blen = ( int ) sizeof ( buff ) ; if ( 0 == no_ascii ) formatstr = " \n " ; else if ( no_ascii > 0 ) formatstr = " \n " ; else formatstr = " \n " ; memset ( buff , ' ' , 80 ) ; buff [ 80 ] = ' \0 ' ; if ( no_ascii < 0 ) { bpstart = 0 ; bpos = bpstart ; for ( k = 0 ; k < len ; k ++ ) { c = * p ++ ; if ( bpos == ( bpstart + ( 8 * 3 ) ) ) bpos ++ ; my_snprintf ( & buff [ bpos ] , blen - bpos , " " , ( int ) ( unsigned char ) c ) ; buff [ bpos + 2 ] = ' ' ; if ( ( k > 0 ) && ( 0 == ( ( k + 1 ) % 16 ) ) ) { trimTrailingSpaces ( buff ) ; fprintf ( fp , formatstr , buff ) ; bpos = bpstart ; memset ( buff , ' ' , 80 ) ; } else bpos += 3 ; } if ( bpos > bpstart ) { buff [ bpos + 2 ] = ' \0 ' ; trimTrailingSpaces ( buff ) ; fprintf ( fp , " \n " , buff ) ; } return ; } k = my_snprintf ( buff + 1 , blen - 1 , " " , a ) ; buff [ k + 1 ] = ' ' ; for ( i = 0 ; i < len ; i ++ ) { c = * p ++ ; bpos += 3 ; if ( bpos == ( bpstart + ( 9 * 3 ) ) ) bpos ++ ; my_snprintf ( & buff [ bpos ] , blen - bpos , " " , ( int ) ( unsigned char ) c ) ; buff [ bpos + 2 ] = ' ' ; if ( no_ascii ) buff [ cpos ++ ] = ' ' ; else { if ( ( c < ' ' ) || ( c >= 0x7f ) ) c = ' ' ; buff [ cpos ++ ] = c ; } if ( cpos > ( cpstart + 15 ) ) { if ( no_ascii ) trimTrailingSpaces ( buff ) ; fprintf ( fp , formatstr , buff ) ; bpos = bpstart ; cpos = cpstart ; a += 16 ; memset ( buff , ' ' , 80 ) ; k = my_snprintf ( buff + 1 , blen - 1 , " " , a ) ; buff [ k + 1 ] = ' ' ; } } if ( cpos > cpstart ) { buff [ cpos ] = ' \0 ' ; if ( no_ascii ) trimTrailingSpaces ( buff ) ; fprintf ( fp , " \n " , buff ) ; } }
Note the ASCII-hex output goes to stdout.
Note the ASCII - hex output goes to stdout .
/* address at left and ASCII at right */ /* was: "%.58s\n" */ /* negative: no address at left and no ASCII at right */ /* was: "%.48s\n"; */ /* no_ascii>=0, start each line with address (offset) */