src/sg_ses.c
static int
read_hex(const char * inp, unsigned char * arr, int * arr_len, int verb)
{
    int in_len, k, j, m, off, split_line;
    unsigned int h;
    const char * lcp;
    char * cp;
    char * c2p;
    char line[512];
    char carry_over[4];
    FILE * fp = NULL;

    if ((NULL == inp) || (NULL == arr) || (NULL == arr_len))
        return 1;
    lcp = inp;
    in_len = strlen(inp);
    if (0 == in_len) {
        *arr_len = 0;
    }
    if (('-' == inp[0]) || ('@' == inp[0])) {    /* read from stdin or file */
        if ('-' == inp[0])
            fp = stdin;
        else {
            fp = fopen(inp + 1, "r");
            if (NULL == fp) {
                pr2serr("read_hex: unable to open file: %s\n", inp + 1);
                return 1;
            }
        }
        carry_over[0] = 0;
        for (j = 0, off = 0; j < MX_DATA_IN; ++j) {
            /* limit lines read to MX_DATA_IN */
            if (NULL == fgets(line, sizeof(line), fp))
                break;
            in_len = strlen(line);
            if (in_len > 0) {
                if ('\n' == line[in_len - 1]) {
                    --in_len;
                    line[in_len] = '\0';
                    split_line = 0;
                } else
                    split_line = 1;
            }
            if (in_len < 1) {
                carry_over[0] = 0;
                continue;
            }
            if (carry_over[0]) {
                if (isxdigit(line[0])) {
                    carry_over[1] = line[0];
                    carry_over[2] = '\0';
                    if (1 == sscanf(carry_over, "%x", &h))
                        arr[off - 1] = h;       /* back up and overwrite */
                    else {
                        pr2serr("read_hex: carry_over error ['%s'] around "
                                "line %d\n", carry_over, j + 1);
                        goto err_with_fp;
                    }
                    lcp = line + 1;
                    --in_len;
                } else
                    lcp = line;
                carry_over[0] = 0;
            } else
                lcp = line;
            m = strspn(lcp, " \t");
            if (m == in_len)
                continue;
            lcp += m;
            in_len -= m;
            if ('#' == *lcp)
                continue;
            k = strspn(lcp, "0123456789aAbBcCdDeEfF ,\t");
            if (in_len != k) {
                pr2serr("read_hex: syntax error at line %d, pos %d\n", j + 1,
                        m + k + 1);
                goto err_with_fp;
            }
            for (k = 0; k < (MX_DATA_IN - off); ++k) {
                if (1 == sscanf(lcp, "%x", &h)) {
                    if (h > 0xff) {
                        pr2serr("read_hex: hex number larger than 0xff in "
                                "line %d, pos %d\n", j + 1,
                                (int)(lcp - line + 1));
                        goto err_with_fp;
                    }
                    if (split_line && (1 == strlen(lcp))) {
                        /* single trailing hex digit might be a split pair */
                        carry_over[0] = *lcp;
                    }
                    arr[off + k] = h;
                    lcp = strpbrk(lcp, " ,\t");
                    if (NULL == lcp)
                        break;
                    lcp += strspn(lcp, " ,\t");
                    if ('\0' == *lcp)
                        break;
                } else {
                    pr2serr("read_hex: error in line %d, at pos %d\n", j + 1,
                            (int)(lcp - line + 1));
                    goto err_with_fp;
                }
            }
            off += k + 1;
            if (off >= MX_DATA_IN)
                break;
        }
        *arr_len = off;
    } else {        /* hex string on command line */
        k = strspn(inp, "0123456789aAbBcCdDeEfF, ");
        if (in_len != k) {
            pr2serr("read_hex: error at pos %d\n", k + 1);
            return 1;
        }
        for (k = 0; k < MX_DATA_IN; ++k) {
            if (1 == sscanf(lcp, "%x", &h)) {
                if (h > 0xff) {
                    pr2serr("read_hex: hex number larger than 0xff at pos "
                            "%d\n", (int)(lcp - inp + 1));
                    return 1;
                }
                arr[k] = h;
                cp = (char *)strchr(lcp, ',');
                c2p = (char *)strchr(lcp, ' ');
                if (NULL == cp)
                    cp = c2p;
                if (NULL == cp)
                    break;
                if (c2p && (c2p < cp))
                    cp = c2p;
                lcp = cp + 1;
            } else {
                pr2serr("read_hex: error at pos %d\n", (int)(lcp - inp + 1));
                return 1;
            }
        }
        *arr_len = k + 1;
    }
    if (verb > 3)
        dStrHex((const char *)arr, *arr_len, 0);
    if (fp && (fp != stdin))
        fclose(fp);
    return 0;

err_with_fp:
    if (fp && (fp != stdin))
        fclose(fp);
    return 1;
}
/* Reads hex data from command line, stdin or a file. Returns 0 on success,
 * 1 otherwise. */
Reads hex data from command line, stdin or a file. Returns 0 on success,
1 otherwise.
Reads hex data from command line stdin or a file . Returns 0 on success 1 otherwise .
static int
read_hex(const char * inp, unsigned char * arr, int * arr_len, int verb)
{
    int in_len, k, j, m, off, split_line;
    unsigned int h;
    const char * lcp;
    char * cp;
    char * c2p;
    char line[512];
    char carry_over[4];
    FILE * fp = NULL;
    if ((NULL == inp) || (NULL == arr) || (NULL == arr_len))
        return 1;
    lcp = inp;
    in_len = strlen(inp);
    if (0 == in_len) {
        *arr_len = 0;
    }
    if (('-' == inp[0]) || ('@' == inp[0])) {    
        if ('-' == inp[0])
            fp = stdin;
        else {
            fp = fopen(inp + 1, "r");
            if (NULL == fp) {
                pr2serr("read_hex: unable to open file: %s\n", inp + 1);
                return 1;
            }
        }
        carry_over[0] = 0;
        for (j = 0, off = 0; j < MX_DATA_IN; ++j) {
            if (NULL == fgets(line, sizeof(line), fp))
                break;
            in_len = strlen(line);
            if (in_len > 0) {
                if ('\n' == line[in_len - 1]) {
                    --in_len;
                    line[in_len] = '\0';
                    split_line = 0;
                } else
                    split_line = 1;
            }
            if (in_len < 1) {
                carry_over[0] = 0;
                continue;
            }
            if (carry_over[0]) {
                if (isxdigit(line[0])) {
                    carry_over[1] = line[0];
                    carry_over[2] = '\0';
                    if (1 == sscanf(carry_over, "%x", &h))
                        arr[off - 1] = h;       
                    else {
                        pr2serr("read_hex: carry_over error ['%s'] around "
                                "line %d\n", carry_over, j + 1);
                        goto err_with_fp;
                    }
                    lcp = line + 1;
                    --in_len;
                } else
                    lcp = line;
                carry_over[0] = 0;
            } else
                lcp = line;
            m = strspn(lcp, " \t");
            if (m == in_len)
                continue;
            lcp += m;
            in_len -= m;
            if ('#' == *lcp)
                continue;
            k = strspn(lcp, "0123456789aAbBcCdDeEfF ,\t");
            if (in_len != k) {
                pr2serr("read_hex: syntax error at line %d, pos %d\n", j + 1,
                        m + k + 1);
                goto err_with_fp;
            }
            for (k = 0; k < (MX_DATA_IN - off); ++k) {
                if (1 == sscanf(lcp, "%x", &h)) {
                    if (h > 0xff) {
                        pr2serr("read_hex: hex number larger than 0xff in "
                                "line %d, pos %d\n", j + 1,
                                (int)(lcp - line + 1));
                        goto err_with_fp;
                    }
                    if (split_line && (1 == strlen(lcp))) {
                        carry_over[0] = *lcp;
                    }
                    arr[off + k] = h;
                    lcp = strpbrk(lcp, " ,\t");
                    if (NULL == lcp)
                        break;
                    lcp += strspn(lcp, " ,\t");
                    if ('\0' == *lcp)
                        break;
                } else {
                    pr2serr("read_hex: error in line %d, at pos %d\n", j + 1,
                            (int)(lcp - line + 1));
                    goto err_with_fp;
                }
            }
            off += k + 1;
            if (off >= MX_DATA_IN)
                break;
        }
        *arr_len = off;
    } else {        
        k = strspn(inp, "0123456789aAbBcCdDeEfF, ");
        if (in_len != k) {
            pr2serr("read_hex: error at pos %d\n", k + 1);
            return 1;
        }
        for (k = 0; k < MX_DATA_IN; ++k) {
            if (1 == sscanf(lcp, "%x", &h)) {
                if (h > 0xff) {
                    pr2serr("read_hex: hex number larger than 0xff at pos "
                            "%d\n", (int)(lcp - inp + 1));
                    return 1;
                }
                arr[k] = h;
                cp = (char *)strchr(lcp, ',');
                c2p = (char *)strchr(lcp, ' ');
                if (NULL == cp)
                    cp = c2p;
                if (NULL == cp)
                    break;
                if (c2p && (c2p < cp))
                    cp = c2p;
                lcp = cp + 1;
            } else {
                pr2serr("read_hex: error at pos %d\n", (int)(lcp - inp + 1));
                return 1;
            }
        }
        *arr_len = k + 1;
    }
    if (verb > 3)
        dStrHex((const char *)arr, *arr_len, 0);
    if (fp && (fp != stdin))
        fclose(fp);
    return 0;
err_with_fp:
    if (fp && (fp != stdin))
        fclose(fp);
    return 1;
}
static int read_hex ( const char * inp , unsigned char * arr , int * arr_len , int verb ) { int in_len , k , j , m , off , split_line ; unsigned int h ; const char * lcp ; char * cp ; char * c2p ; char line [ 512 ] ; char carry_over [ 4 ] ; FILE * fp = NULL ; if ( ( NULL == inp ) || ( NULL == arr ) || ( NULL == arr_len ) ) return 1 ; lcp = inp ; in_len = strlen ( inp ) ; if ( 0 == in_len ) { * arr_len = 0 ; } if ( ( ' ' == inp [ 0 ] ) || ( ' ' == inp [ 0 ] ) ) { if ( ' ' == inp [ 0 ] ) fp = stdin ; else { fp = fopen ( inp + 1 , " " ) ; if ( NULL == fp ) { pr2serr ( " \n " , inp + 1 ) ; return 1 ; } } carry_over [ 0 ] = 0 ; for ( j = 0 , off = 0 ; j < MX_DATA_IN ; ++ j ) { if ( NULL == fgets ( line , sizeof ( line ) , fp ) ) break ; in_len = strlen ( line ) ; if ( in_len > 0 ) { if ( ' \n ' == line [ in_len - 1 ] ) { -- in_len ; line [ in_len ] = ' \0 ' ; split_line = 0 ; } else split_line = 1 ; } if ( in_len < 1 ) { carry_over [ 0 ] = 0 ; continue ; } if ( carry_over [ 0 ] ) { if ( isxdigit ( line [ 0 ] ) ) { carry_over [ 1 ] = line [ 0 ] ; carry_over [ 2 ] = ' \0 ' ; if ( 1 == sscanf ( carry_over , " " , & h ) ) arr [ off - 1 ] = h ; else { pr2serr ( " " " \n " , carry_over , j + 1 ) ; goto err_with_fp ; } lcp = line + 1 ; -- in_len ; } else lcp = line ; carry_over [ 0 ] = 0 ; } else lcp = line ; m = strspn ( lcp , " \t " ) ; if ( m == in_len ) continue ; lcp += m ; in_len -= m ; if ( ' ' == * lcp ) continue ; k = strspn ( lcp , " \t " ) ; if ( in_len != k ) { pr2serr ( " \n " , j + 1 , m + k + 1 ) ; goto err_with_fp ; } for ( k = 0 ; k < ( MX_DATA_IN - off ) ; ++ k ) { if ( 1 == sscanf ( lcp , " " , & h ) ) { if ( h > 0xff ) { pr2serr ( " " " \n " , j + 1 , ( int ) ( lcp - line + 1 ) ) ; goto err_with_fp ; } if ( split_line && ( 1 == strlen ( lcp ) ) ) { carry_over [ 0 ] = * lcp ; } arr [ off + k ] = h ; lcp = strpbrk ( lcp , " \t " ) ; if ( NULL == lcp ) break ; lcp += strspn ( lcp , " \t " ) ; if ( ' \0 ' == * lcp ) break ; } else { pr2serr ( " \n " , j + 1 , ( int ) ( lcp - line + 1 ) ) ; goto err_with_fp ; } } off += k + 1 ; if ( off >= MX_DATA_IN ) break ; } * arr_len = off ; } else { k = strspn ( inp , " " ) ; if ( in_len != k ) { pr2serr ( " \n " , k + 1 ) ; return 1 ; } for ( k = 0 ; k < MX_DATA_IN ; ++ k ) { if ( 1 == sscanf ( lcp , " " , & h ) ) { if ( h > 0xff ) { pr2serr ( " " " \n " , ( int ) ( lcp - inp + 1 ) ) ; return 1 ; } arr [ k ] = h ; cp = ( char * ) strchr ( lcp , ' ' ) ; c2p = ( char * ) strchr ( lcp , ' ' ) ; if ( NULL == cp ) cp = c2p ; if ( NULL == cp ) break ; if ( c2p && ( c2p < cp ) ) cp = c2p ; lcp = cp + 1 ; } else { pr2serr ( " \n " , ( int ) ( lcp - inp + 1 ) ) ; return 1 ; } } * arr_len = k + 1 ; } if ( verb > 3 ) dStrHex ( ( const char * ) arr , * arr_len , 0 ) ; if ( fp && ( fp != stdin ) ) fclose ( fp ) ; return 0 ; err_with_fp : if ( fp && ( fp != stdin ) ) fclose ( fp ) ; return 1 ; }
Reads hex data from command line, stdin or a file.
Reads hex data from command line stdin or a file .
/* read from stdin or file */ /* limit lines read to MX_DATA_IN */ /* back up and overwrite */ /* single trailing hex digit might be a split pair */ /* hex string on command line */