/* create a new APDU from a raw set of bytes. This will decode all the above fields. users of VCARDAPDU's can then depend on the already decoded values.  */
VCardAPDU* vcard_apdu_new(unsigned char *raw_apdu, int len, vcard_7816_status_t *status)
/* create a new APDU from a raw set of bytes. This will decode all the above fields. users of VCARDAPDU's can then depend on the already decoded values.  */
VCardAPDU* vcard_apdu_new(unsigned char *raw_apdu, int len, vcard_7816_status_t *status)
{
    VCardAPDU *new_apdu;
    *status = VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE;
    if (len < 4) {
        *status = VCARD7816_STATUS_ERROR_WRONG_LENGTH;
        return NULL;
    }
    new_apdu = (VCardAPDU *)g_malloc(sizeof(VCardAPDU));
    new_apdu->a_data = g_malloc(len);
    memcpy(new_apdu->a_data, raw_apdu, len);
    new_apdu->a_len = len;
    *status = vcard_apdu_set_class(new_apdu);
    if (*status != VCARD7816_STATUS_SUCCESS) {
        g_free(new_apdu);
        return NULL;
    }
    *status = vcard_apdu_set_length(new_apdu);
    if (*status != VCARD7816_STATUS_SUCCESS) {
        g_free(new_apdu);
        new_apdu = NULL;
    }
    return new_apdu;
}