📄 lookup.c
字号:
/* ============================================================================ Project Name : jayaCard Module Name : proto/bios/fs/lookup.c Version : $Id: lookup.c,v 1.28 2004/04/24 12:07:31 dgil Exp $ Description: Lookup EF, DF and applications __fs_lookup_SFI() lookup a file in the current DF using its SFI __fs_lookup_FID() lookup a file in the current DF using its File IDentifier __fs_lookup_name() looukup a DF using its name or AID. This lookup is EMV compatible, using a SD file in each DF to store names. The Original Code is jayaCard code. The Initial Developer of the Original Code is Gilles Dumortier. Portions created by the Initial Developer are Copyright (C) 2002-2004 the Initial Developer. All Rights Reserved. Contributor(s): This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see http://www.gnu.org/licenses/gpl.html History Rev Description 030103 dgil wrote it from scratch ============================================================================*/#include "precomp.h"#ifdef JAYA_FILESYSTEM/* ============================================================================ import from fstools.c ========================================================================= */void __fs_load_EF_header(jword link);void __fs_load_DF_header(jword link);void __fs_check_DF(void);/* ============================================================================ __fs_lookup_SFI() Input parameter : sfi SFI_CURRENT only check that we have a current file check also the validity of the SFI value Search for the file, using the provided SFI. The search is done in the current DF only. Implicit Output parameter : current_EF filled Some error code in lasterr if any problem. ========================================================================= */void __fs_lookup_SFI(jbyte sfi){ LOG1("FS","__fs_lookup_SFI(): sfi==0x%.2X",sfi); /* security issue: check that current_DF is valid */ __fs_check_DF(); if (lasterr != SUCCESS) { LOG2("FS","__fs_lookup_SFI(): sfi==0x%.2X __fs_check_DF() failure %d",sfi,lasterr); goto err; } /* check SFI parameter */ if (sfi == SFI_CURRENT) { /* check we have a current EF */ if (current_EF_addr==NO_CURRENT_FILE) { BIOS_SETERR(ERR_NO_EF_SELECTED); goto err; } /* check also that this EF use sfi indicator ! */ /*if ((current_EF.sfi&SFI_MASK)!=SFI_NOTUSE) */ return; } if (sfi>=SFI_MASK) { BIOS_SETERR(ERR_INVALID_SFI); goto err; } /* init the stream with the first file of the current DF */ if (current_DF.u4.first_file==0x0000) { /* this DF is empty ! */ goto notfound; } __fs_load_EF_header(current_DF.u4.first_file);loop: if (lasterr!=SUCCESS) goto err; if ((sfi == (current_EF.sfi&SFI_MASK) ) && ((current_EF.fdesc&FDESC_TYPE_MASK)!=FDESC_TYPE_DF)) { /* same SFI and its an EF */ #ifdef JAYACFG_FILESYSTEM_ALIAS if ((current_EF.fdesc&FDESC_ALIAS)==FDESC_ALIAS) { /* load aliased in current_EF ; __x missing test on SHAREABLE attribut */ __fs_load_EF_header(current_EF.u3.alias); } #endif return; } /* next sibling EF/DF file ? */ if (current_EF.u2.next_file==0x0000) {notfound: BIOS_SETERR(ERR_FILE_NOT_FOUND); goto err; } /* init the stream with the next sibling EF/DF file */ __fs_load_EF_header(current_EF.u2.next_file); goto loop;err: /* no more EF selected ! */ FS_NEW_SESSION(jfalse,jfalse); return;}/* ============================================================================ __fs_lookup_FID Input parameter : fid and mode Search for the EF/DF file, using the provided FID. The search is done in the current DF. Implicit Output parameter : current_EF filled Some error code in lasterr if any problem. ========================================================================= */void __fs_lookup_FID(jword fid,jbyte mode){ LOG1("FS","__fs_lookup_FID(): fid==0x%.4X",fid); /* security issue: check that current_DF is valid */ __fs_check_DF(); if (lasterr != SUCCESS) { LOG2("FS","__fs_lookup_FID(): fid==0x%.4X __fs_check_DF() failure %d",fid,lasterr); goto err; } /* init the stream with the first EF file of the current DF or the first DF of the MF (depends on the content of current_DF ;-) */ if (current_DF.u4.first_file==0x0000) { /* this DF is empty ! */ LOG1("FS","__fs_lookup_FID(): fid==0x%.4X empty current DF",fid); goto notfound; } __fs_load_EF_header(current_DF.u4.first_file);loop: if (lasterr!=SUCCESS) { LOG2("FS","__fs_lookup_FID(): fid==0x%.4X error %d loading the EF header !",fid,lasterr); goto err; } if (fid==current_EF.fid) { /* same FID */ #ifdef JAYACFG_FILESYSTEM_ALIAS if ((current_EF.fdesc&FDESC_ALIAS)==FDESC_ALIAS) { /* load aliased in current_EF ; __x missing test on SHAREABLE attribut */ __fs_load_EF_header(current_EF.u3.alias); } #endif if ( (mode==LOOKUP_MODE_EF_ONLY) && (((current_EF.fdesc&FDESC_TYPE_MASK)==FDESC_TYPE_DF)) ) { LOG1("FS","__fs_lookup_FID(): fid==0x%.4X lookup for EF but file is DF !",fid); goto notfound; } if ( (mode==LOOKUP_MODE_DF_ONLY) && (((current_EF.fdesc&FDESC_TYPE_MASK)!=FDESC_TYPE_DF)) ) { LOG1("FS","__fs_lookup_FID(): fid==0x%.4X lookup for DF but file is EF !",fid); goto notfound; } return; } /* next sibling file ? */ if (current_EF.u2.next_file==0x0000) { LOG1("FS","__fs_lookup_FID(): fid==0x%.4X no more file to lookup in this DF !",fid);notfound: BIOS_SETERR(ERR_FILE_NOT_FOUND); goto err; } /* init the stream with the next sibling EF file */ __fs_load_EF_header(current_EF.u2.next_file); goto loop;err: /* no more EF selected ! */ FS_NEW_SESSION(jfalse,jfalse); return;}/* ============================================================================ save/restore context for TLV seek ========================================================================= */void __tlv_save_context(void){ u.wBlock[JAYA_WCRYPTO_TEMP0] = current_EF.u3.body_ef; current_EF.u3.body_ef = current_tlv_val; u.wBlock[JAYA_WCRYPTO_TEMP0+1] = current_EF.u4.sizefile; current_EF.u4.sizefile = current_tlv_len; u.wBlock[JAYA_WCRYPTO_TEMP0+2] = next_tlv;}void __tlv_restore_context(void){ current_EF.u3.body_ef = u.wBlock[JAYA_WCRYPTO_TEMP0]; current_EF.u4.sizefile = u.wBlock[JAYA_WCRYPTO_TEMP0+1]; next_tlv = u.wBlock[JAYA_WCRYPTO_TEMP0+2];}/* ============================================================================ __fs_lookup_name Search for the DF file, using the provided AID or DFNAME. The search is done from the MF. Input parameter : AID or DFNAME AID = RID | PIX RID = 5 bytes PIX = 0 to 11 bytes System Directory (SD file) EF file identified by an SFI in the range 1 to 10. Our implementation use SFI==1 (which is returned in the FCI of the DF) One record Tag 70 Data Length ( Tag 61 , Length Dir entry, Dir entry ) x n where dir entry could be : DDF: Tag 9D Len 5-16 DDF Name, ex: 1PAY.SYS.DDF01 or ADF: Tag 4F Len 5-16 AID Tag 50 Len 1-16 Application Label Note: DDF = Directory Definition File ADF = Application Definition File Implicit Output parameter : current_DF filled Some error code in lasterr if any problem. ========================================================================= */#ifdef JAYA_LOOKUP_NAMEvoid __fs_lookup_name(jbyte xdata* name,jbyte len,jbool bFirst){ LOG2("FS","__fs_lookup_name(): name==%.2X... length=%d",name[0],len); /* init the stream with the first DF of the MF */ if (bFirst) { FS_NEW_SESSION(jtrue,jtrue); if (current_DF.u4.first_file==0x0000) { /* the MF is empty ! */ goto notfound; } __fs_load_DF_header(current_DF.u4.first_file); } else { /* security issue: check that current_DF is valid */ if (current_DF_addr == NO_CURRENT_FILE) { BIOS_SETERR(ERR_NO_DF_SELECTED); goto err; } goto next; }loop: if (lasterr!=SUCCESS) goto err; /* DF invalidated ? */ #ifdef JAYACFG_FILESYSTEM_INVALIDATE if ( (current_DF.fdesc&FDESC_INVALIDATED) == FDESC_INVALIDATED ) { LOG1("FS","__fs_lookup_name() fid=0x%.4X INVALIDATED !",current_DF.fid); goto next; } #endif /* lookup file containing the AID/DFNAME information */ __fs_lookup_FID(FID_SD,LOOKUP_MODE_EF_ONLY); if (lasterr==NOERR) { /* current_EF on the SD file tlv = 70 length listOfDir */ if (__tlv_seek_first_tag(TAG_LISTOFDIR)) { /* reprogram the current_EF for tag 0x61 seeking */ LOG2("FS","__fs_lookup_name(): name==%.2X... length=%d : found TAG_LISTOFDIR",name[0],len); current_EF.u3.body_ef = current_tlv_val; current_EF.u4.sizefile = current_tlv_len; next_TAG_DIRENTRY: /* tlv = 61 length directoryEntry */ if ( __tlv_seek_first_tag(TAG_DIRENTRY) ) { LOG2("FS","__fs_lookup_name(): name==%.2X... length=%d : found TAG_DIRENTRY",name[0],len); __tlv_save_context(); if ( (__tlv_seek_first_tag(0x9D)) || (__tlv_seek_first_tag(0x4F)) ) { /* found DFNAME or AID */ LOG3("FS","__fs_lookup_name(): name==%.2X... length=%d?%d : found DFNAME or AID",name[0],len,current_tlv_len); if (len==current_tlv_len) { /* same size */ HAL_EEPROM_READ(u.bBlock,current_tlv_val,len); #if 0 /* ifdef DEBUG */ printf("\n"); for (int i=0;i<len;i++) { printf("%.2X (%c) != %.2X (%c), \n",u.bBlock[i],u.bBlock[i],name[i],name[i]); } printf("\n"); #endif /* found our name ? */ if (HAL_MEMCMP(name,u.bBlock,len)==JSEC_OK) return; } } else { /* next tag ? */ __tlv_restore_context(); if ( __tlv_seek_next_tag(TAG_DIRENTRY) ) goto next_TAG_DIRENTRY; } } } /* tag TAG_LISTOFDIR */ } /* reset the error to continue on next DF */ lasterr = NOERR; /* next sibling file ? */next: if (current_DF.u2.next_file==0x0000) {notfound: BIOS_SETERR(ERR_FILE_NOT_FOUND); goto err; } /* init the stream with the next sibling DF file */ __fs_load_DF_header(current_DF.u2.next_file); goto loop;err: /* no more DF/EF selected ! */ FS_NEW_SESSION(jtrue,jtrue); return;}#endif/* ========================================================================= That's all folks ! ========================================================================= */#endif/* JAYA_FILESYSTEM */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -