コード例 #1
0
ファイル: ucli_util.c プロジェクト: Sovietaced/bigcode
uint8_t*
ucli_util_data_from_string(const char* string, int* size)
{
    unsigned char* bytes; 
    unsigned char byte; 
    int i;
    int nibble_count;
    int string_len; 

    if(string == NULL || size == NULL || string[0] == 0) {
        return NULL; 
    }

    if(!UCLI_STRCMP(string, "NULL")) { 
        *size = 0; 
        return NULL; 
    }

    if(!UCLI_STRCMP(string, "builtin")) { 
        string = builtin__; 
    }

    string_len = UCLI_STRLEN(string); 

    if( (bytes = aim_zmalloc((string_len+1) / 2)) == NULL) {
        return NULL;
    }

    byte = 0; 
    nibble_count = 0; 

    for(i = 0; i < string_len; i++) {
        int hv = ucli_hex_val__(string[i]);
        if(hv >= 0) {
            byte |= hv; 
            if( (nibble_count % 2) == 0) {
                byte <<= 4; 
            }
            else {
                bytes[nibble_count/2] = byte; 
                byte = 0; 
            }
            nibble_count++; 
        }
    }
    if(nibble_count & 1) {
        /* Last nibble will be padded as zero */
        bytes[nibble_count++/2] = byte; 
    }
        
    *size = nibble_count / 2; 
    return bytes; 
}
コード例 #2
0
ファイル: ucli_argparse.c プロジェクト: floodlight/bigcode
static int
ucli_vargparse_type__(ucli_context_t* uc,
                      char c, char* type, const char* arg,
                      aim_va_list_t* vargs)
{
    int rv;
    aim_datatype_t* dt;

    if(arg == NULL) {
        return UCLI_STATUS_E_ARG;
    }

    if(type && type[0] == '%') {
        /* Immediate string specified. The argument must be that string. */
        if(type[1] == 0) {
            /* No string */
            return UCLI_STATUS_E_INTERNAL;
        }
        if(!UCLI_STRCMP(type+1, arg)) {
            return UCLI_STATUS_OK;
        }
        ucli_error(uc, "expected '%s', not '%s'", type+1, arg);
        return UCLI_STATUS_E_ARG;
    }

    dt = aim_datatype_find(c, type);
    if(dt == NULL) {
        /* Unrecognized type */
        ucli_error(uc, "<bug: no handler for type '%c:%s'>", (c) ? c : '.', (type) ? type : "[NULL]");
        AIM_LOG_ERROR("bug: no handler for type '%c:%s'", (c) ? c : '.', (type) ? type : "[NULL]");
        return UCLI_STATUS_E_INTERNAL;
    }

    {
        aim_datatype_context_t dtc;
        dtc.dt = dt;
        dtc.epvs = &uc->pvs;
        rv = dt->from_str(&dtc, arg, vargs);
    }


    if( (rv < 0) && dt->desc) {
        ucli_e_arg(uc, arg, dt->desc);
    }
    return rv;
}