FX_DER.C
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:20k
源码类别:

DVD

开发平台:

Others

  1. /**************************************************************************/ 
  2. /*                                                                        */ 
  3. /*            Copyright (c) 1996-2002 by Express Logic Inc.               */ 
  4. /*                                                                        */ 
  5. /*  This software is copyrighted by and is the sole property of Express   */ 
  6. /*  Logic, Inc.  All rights, title, ownership, or other interests         */ 
  7. /*  in the software remain the property of Express Logic, Inc.  This      */ 
  8. /*  software may only be used in accordance with the corresponding        */ 
  9. /*  license agreement.  Any unauthorized use, duplication, transmission,  */ 
  10. /*  distribution, or disclosure of this software is expressly forbidden.  */ 
  11. /*                                                                        */
  12. /*  This Copyright notice may not be removed or modified without prior    */ 
  13. /*  written consent of Express Logic, Inc.                                */ 
  14. /*                                                                        */ 
  15. /*  Express Logic, Inc. reserves the right to modify this software        */ 
  16. /*  without notice.                                                       */ 
  17. /*                                                                        */ 
  18. /*  Express Logic, Inc.                     info@expresslogic.com         */
  19. /*  11423 West Bernardo Court               http://www.expresslogic.com   */
  20. /*  San Diego, CA  92127                                                  */
  21. /*                                                                        */
  22. /**************************************************************************/
  23. /**************************************************************************/
  24. /**************************************************************************/
  25. /**                                                                       */ 
  26. /** FileX Component                                                       */ 
  27. /**                                                                       */
  28. /**   Directory (DIR)                                                     */
  29. /**                                                                       */
  30. /**************************************************************************/
  31. /**************************************************************************/
  32. #include "Config.h" // Global Configuration - do not remove!
  33. #ifdef ENABLE_FILEX
  34. #ifdef _DEBUG
  35. #undef IFTRACE
  36. #define IFTRACE if (gTraceFileSys)
  37. #include "DebugDbgMain.h"
  38. #endif //_DEBUG
  39. #define FX_SOURCE_CODE
  40. /* Include necessary system files.  */
  41. #include "PlaycoreFileSysFileXfx_api.h"
  42. #include "PlaycoreFileSysFileXfx_sys.h"
  43. #include "PlaycoreFileSysFileXfx_dir.h"
  44. #include "PlaycoreFileSysFileXfx_uti.h"
  45. /**************************************************************************/ 
  46. /*                                                                        */ 
  47. /*  FUNCTION                                               RELEASE        */ 
  48. /*                                                                        */ 
  49. /*    _fx_directory_entry_read                            PORTABLE C      */ 
  50. /*                                                           3.0          */ 
  51. /*  AUTHOR                                                                */ 
  52. /*                                                                        */ 
  53. /*    William E. Lamie, Express Logic, Inc.                               */ 
  54. /*                                                                        */ 
  55. /*  DESCRIPTION                                                           */ 
  56. /*                                                                        */ 
  57. /*    This function reads the supplied directory entry from the supplied  */ 
  58. /*    source directory.  If the supplied directory entry is NULL, then    */ 
  59. /*    the root directory is assumed.                                      */ 
  60. /*                                                                        */ 
  61. /*  INPUT                                                                 */ 
  62. /*                                                                        */ 
  63. /*    media_ptr                             Media control block pointer   */ 
  64. /*    source_dir                            Source directory entry        */ 
  65. /*    entry_ptr                             Directory entry number        */ 
  66. /*    destination_ptr                       Pointer to destination for    */ 
  67. /*                                            the directory entry         */ 
  68. /*                                                                        */ 
  69. /*  OUTPUT                                                                */ 
  70. /*                                                                        */ 
  71. /*    return status                                                       */ 
  72. /*    *entry_ptr should point to the 8:3 entry if it is a long name       */
  73. /*                                                                        */ 
  74. /*  CALLS                                                                 */ 
  75. /*                                                                        */ 
  76. /*    _fx_utility_log_sector_read           Read directory sector         */ 
  77. /*    _fx_utility_16_unsigned_read          Read an UINT from memory      */ 
  78. /*    _fx_utility_32_unsigned_read          Read an ULONG from memory     */ 
  79. /*                                                                        */ 
  80. /*  CALLED BY                                                             */ 
  81. /*                                                                        */ 
  82. /*    FileX System Functions                                              */ 
  83. /*                                                                        */ 
  84. /*  RELEASE HISTORY                                                       */ 
  85. /*                                                                        */ 
  86. /*    DATE              NAME                      DESCRIPTION             */ 
  87. /*                                                                        */ 
  88. /*  01-01-1999     William E. Lamie         Initial Version 1.0           */ 
  89. /*  04-19-1999     William E. Lamie         Corrected reading directory   */ 
  90. /*                                            time and date entries,      */ 
  91. /*                                            resulting in version 1.0a   */ 
  92. /*  03-01-2000     William E. Lamie         Modified comment(s),          */ 
  93. /*                                            resulting in version 1.0b.  */ 
  94. /*  01-28-2001     William E. Lamie         Modified comment(s) and       */ 
  95. /*                                            deleted old directory cache */ 
  96. /*                                            invalidate code, resulting  */ 
  97. /*                                            in version 2.0.             */  
  98. /*  03-01-2002     Mohammad N. Minhaz       Changed to 32 bit variables   */
  99. /*                                            In 32 bit root directory is */
  100. /*                                            treated like subdirectory,  */ 
  101. /*                                            changed long representation */
  102. /*                                            and added long name support */
  103. /*                                            resulting in version 3.0.   */ 
  104. /*                                                                        */ 
  105. /**************************************************************************/ 
  106. UINT  _fx_directory_entry_read(FX_MEDIA *media_ptr, FX_DIR_ENTRY *source_dir, 
  107.                                             ULONG *entry_ptr, FX_DIR_ENTRY *destination_ptr)
  108. {
  109. UINT        i, j, card, dotflag, get_short_name;
  110. UINT        status;
  111. ULONG       cluster, next_cluster, next_logical_sector;
  112. UINT        relative_cluster;
  113. UINT        relative_sector;
  114. ULONG       logical_sector;
  115. ULONG       byte_offset;
  116. ULONG       bytes_per_cluster;
  117. UCHAR_PTR   read_ptr;
  118. ULONG       fat_reserved; 
  119. ULONG       entry = *entry_ptr;
  120.     /* Determine which FAT is present and setup variables accordingly.  */
  121.     if (media_ptr -> fx_media_32_bit_FAT) fat_reserved = FX_RESERVED_1_32;
  122.     else fat_reserved = FX_RESERVED_1;
  123.     /* Calculate the byte offset of this directory entry.  */
  124.     byte_offset =  entry * FX_DIR_ENTRY_SIZE;
  125.     /* Determine if a sub-directory is specified.  */
  126.     if ((source_dir) || (media_ptr -> fx_media_32_bit_FAT))
  127.     {
  128.         /* Yes, a sub-directory is present.  */
  129.         /* Calculate the number of bytes per cluster.  */
  130.         bytes_per_cluster =  ((ULONG) media_ptr -> fx_media_bytes_per_sector) *
  131.                              ((ULONG) media_ptr -> fx_media_sectors_per_cluster);
  132.         /* Now determine the relative cluster in the sub-directory file.  */
  133.         relative_cluster =   byte_offset/bytes_per_cluster;
  134.         /* Calculate the byte offset within the cluster.  */
  135.         byte_offset %=  bytes_per_cluster;
  136.         /* Now figure out the relative sector within the cluster.  */
  137.         relative_sector =  byte_offset/((ULONG) media_ptr -> fx_media_bytes_per_sector);
  138.         /* Read the directory sector into the internal memory buffer.  */
  139.         /* Loop to position to the appropriate cluster.  */
  140.         if (source_dir)
  141.             cluster =  source_dir -> fx_dir_entry_cluster;
  142.         else cluster = media_ptr ->fx_media_root_clus_32;
  143.         for (i = 0; ; i++)
  144.         {   
  145.             /* Check the value of the new cluster - it must be a valid cluster number
  146.                or something is really wrong!  */
  147.             if ((cluster < FX_FAT_ENTRY_START) || (cluster > fat_reserved))
  148.             {
  149.                 /* Send error message back to caller.  */
  150.                 return(FX_FILE_CORRUPT);
  151.             }
  152.             /* Read the next cluster.  */
  153.             status =  _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
  154.             /* There is a potential for loop, but hardly anything can be done */
  155.             /* Check for I/O error.  */
  156.             if (status != FX_SUCCESS)
  157.             {
  158.                 /* Return error code.  */
  159.                 return(status);
  160.             }
  161.             if (i < relative_cluster) cluster = next_cluster;
  162.             else break;
  163.         }
  164.         /* At this point, the directory data sector needs to be read.  */
  165.         logical_sector =    ((ULONG) media_ptr -> fx_media_data_sector_start) +
  166.                                                 (((ULONG) cluster - FX_FAT_ENTRY_START) *
  167.                                                  ((ULONG) media_ptr -> fx_media_sectors_per_cluster)) +
  168.                                                  relative_sector;
  169.         /* determine if next sector is in next cluster or in next sector */
  170.         if ((bytes_per_cluster - byte_offset) >= (ULONG) media_ptr -> fx_media_bytes_per_sector)
  171.             next_logical_sector = logical_sector + 1;
  172.         else
  173.             if ((next_cluster >= FX_FAT_ENTRY_START) && (next_cluster <= fat_reserved))
  174.                 next_logical_sector =    ((ULONG) media_ptr -> fx_media_data_sector_start) +
  175.                           (((ULONG) next_cluster - FX_FAT_ENTRY_START) * ((ULONG) media_ptr -> fx_media_sectors_per_cluster));
  176.             else next_logical_sector = 0; /* no such sector possible */
  177.         /* Read the logical directory sector.  */
  178.         status =  _fx_utility_log_sector_read(media_ptr, (ULONG) logical_sector, 
  179.                                         media_ptr -> fx_media_memory_buffer, (ULONG) 1);
  180.         /* Determine if an error occurred.  */
  181.         if (status != FX_SUCCESS)
  182.         {
  183.             /* Return error code.  */
  184.             return(status);
  185.         }
  186.         /* Calculate the byte offset within this sector.  */
  187.         byte_offset %=  media_ptr -> fx_media_bytes_per_sector;
  188.     }
  189.     else
  190.     {
  191.         /* Read the entry from the root directory.  */
  192.         /* Determine which sector the requested root directory entry is in.  */
  193.         logical_sector =  (byte_offset/media_ptr -> fx_media_bytes_per_sector) +
  194.                                         (ULONG) media_ptr -> fx_media_root_sector_start;
  195.         next_logical_sector = logical_sector + 1;
  196.         if (next_logical_sector >= ((ULONG) media_ptr -> fx_media_root_sector_start + (ULONG) media_ptr -> fx_media_root_sectors))
  197.             next_logical_sector = 0;    /* no next logical sector possible */
  198.         /* Read the logical directory sector.  */
  199.         status =  _fx_utility_log_sector_read(media_ptr, (ULONG) logical_sector, 
  200.                                         media_ptr -> fx_media_memory_buffer, (ULONG) 1);
  201.         /* Determine if an error occurred.  */
  202.         if (status != FX_SUCCESS)
  203.         {
  204.             /* Return error code.  */
  205.             return(status);
  206.         }
  207.         /* Now calculate the byte offset into this sector.  */
  208.         byte_offset -=
  209.                     ((logical_sector - (ULONG) media_ptr -> fx_media_root_sector_start) *
  210.                         media_ptr -> fx_media_bytes_per_sector); 
  211.     }
  212.     /* Setup a pointer into the buffer.  */
  213.     read_ptr =  (UCHAR_PTR) media_ptr -> fx_media_memory_buffer + (UINT) byte_offset;
  214.     /* Save the logical sector and byte offset in the returned directory entry.  */
  215.     destination_ptr -> fx_dir_entry_log_sector =   logical_sector;
  216.     destination_ptr -> fx_dir_entry_byte_offset =  byte_offset;
  217.     /* check if long file name exists */
  218.     get_short_name = 0;
  219.     if ((*(read_ptr + 11) == FX_LONG_NAME) && (*read_ptr != FX_DIR_ENTRY_FREE)
  220. //       && ( (i = (*read_ptr & 0x1f)*13) < FX_MAX_LONG_NAME_LEN )
  221.         )
  222.     {
  223.      i = (*read_ptr & 0x1f)*13;
  224.         if ( i  >= FX_MAX_LONG_NAME_LEN)
  225.         {
  226. //         i = FX_MAX_LONG_NAME_LEN-1;
  227. get_short_name = 1;
  228.         } else
  229.         {
  230.         destination_ptr -> fx_dir_entry_name[i] = 0; /* null termintated string */
  231.         }
  232.         do {
  233.              card = (*read_ptr & 0x1f) - 1; /* get the lower 5 bit containing the cardinality */
  234.              /* for simplicity no checksum or cardinality checking is done */
  235.              if (get_short_name == 0)
  236.              {
  237.                    for(i = 1, j = 0; i < FX_DIR_ENTRY_SIZE; i+=2)
  238.                    {
  239.                          if ((i == 11) || (i ==26)) continue; 
  240.                                   /* i = 12, 27 is not generated due to +=2 */
  241.                          if (i == 13) 
  242.                          {
  243.                                i = 12;
  244.                                continue; /* this time next unicode is byte offset 14*/
  245.                          }
  246.                          /* each entry contains 13 unicode and first byte ascii, second byte is extended */
  247. #if 0
  248.                          if (((CHAR)read_ptr[i] >= 'a') && ((CHAR)read_ptr[i] <= 'z')) /* make upper */
  249.                                 destination_ptr -> fx_dir_entry_name[13*card + j] = (CHAR)read_ptr[i] - 0x20;
  250.                          else
  251. #endif
  252. // <<< ZKR GL112003 : Check if the charater code is english-based unicode.
  253. // if not, we should substitute '*' for it.
  254. #if 1 //def S1_GUI
  255.                         if ( (read_ptr[i+1]<3) && (read_ptr[i+1]>=0) )
  256.                                 destination_ptr -> fx_dir_entry_name[13*card + j] = (CHAR)read_ptr[i]; //
  257.                          else
  258.                                 destination_ptr -> fx_dir_entry_name[13*card + j] = (CHAR)'*';
  259. #else // S1_GUI
  260.                                 destination_ptr -> fx_dir_entry_name[13*card + j] = (CHAR)read_ptr[i];
  261. #endif
  262. // ZKR GL112003 >>>
  263.                          j++;
  264. // <<< ZKR GL112003 : Check null charater only if the charater code is english-based unicode. 
  265. #if 1 //def S1_GUI
  266.                          if ( ((CHAR)read_ptr[i] == 0) && ((read_ptr[i+1]<3) && (read_ptr[i+1]>=0)) )
  267. #else // S1_GUI
  268.                          if ( (CHAR)read_ptr[i] == 0 )
  269. #endif // S1_GUI
  270. // ZKR GL112003 >>>
  271.                               break;
  272.                     }
  273.                }
  274.                if (byte_offset + FX_DIR_ENTRY_SIZE >= media_ptr -> fx_media_bytes_per_sector)
  275.                {
  276.                      /* read new sector */
  277.                      if (next_logical_sector != 0)
  278.                       logical_sector = next_logical_sector;
  279.                      else
  280.                       return(FX_FILE_CORRUPT);
  281.                      
  282.                      status =  _fx_utility_log_sector_read(media_ptr, (ULONG) logical_sector, 
  283.                                        media_ptr -> fx_media_memory_buffer, (ULONG) 1);
  284.                      if (status != FX_SUCCESS)
  285.                       return(status);
  286.                      byte_offset = 0;
  287.                 }
  288.                 else byte_offset += FX_DIR_ENTRY_SIZE;
  289.                 read_ptr =  (UCHAR_PTR) media_ptr -> fx_media_memory_buffer + (UINT) byte_offset;
  290.                 entry++;
  291.        } while (card > 0);
  292.        destination_ptr -> fx_dir_long_name_present = 1;
  293.     }
  294.     else 
  295.     {
  296.        get_short_name = 1;
  297.     }
  298.     if (get_short_name == 1)
  299.     {
  300.        destination_ptr -> fx_dir_long_name_present = 0;
  301.         destination_ptr -> fx_dir_entry_name[0] = 0;
  302.         dotflag = 0;
  303.         for (i = 0, j = 0; i < (FX_DIR_NAME_SIZE+FX_DIR_EXT_SIZE); i++)
  304.         {
  305.             if ((CHAR) read_ptr[i] == 0)
  306.                 break;
  307.             if ((CHAR) read_ptr[i] == '.') 
  308.                 dotflag = 2;    /* happen for the first two dir entries, no extra dot*/
  309.             if ((CHAR) read_ptr[i] == ' ')
  310.             {
  311.                 if (dotflag == 0) dotflag = 1; /* put a dot if a char comes aft space */
  312.                     continue;
  313.             }
  314.             
  315.             if (i == FX_DIR_NAME_SIZE)
  316.                 if (dotflag == 0)
  317.                     dotflag = 1; /* not a space/null, no dot yet inserted */
  318.             if (dotflag == 1)
  319.             {
  320.                 destination_ptr -> fx_dir_entry_name[j++] = '.';
  321.                 dotflag = 2;    /* no more dot for spaces */
  322.             }
  323. #if 0
  324.             if (((CHAR)read_ptr[i] >= 'a') && ((CHAR)read_ptr[i] <= 'z')) /* make upper */
  325.                 destination_ptr -> fx_dir_entry_name[j] = read_ptr[i] - 0x20;
  326.             else
  327. #endif
  328.                 destination_ptr -> fx_dir_entry_name[j] = read_ptr[i];
  329.             j++;
  330.         }
  331.         destination_ptr -> fx_dir_entry_name[j] = 0;
  332.     }
  333.     /* Load up the destination directory entry.  */
  334.     read_ptr += (FX_DIR_NAME_SIZE+FX_DIR_EXT_SIZE);
  335.     /* Copy the attribute into the destination.  */
  336.     destination_ptr -> fx_dir_entry_attributes =  *read_ptr++;
  337.     /* Advance the read_ptr past the reserved bytes.  */
  338.     read_ptr += FX_DIR_RESERVED;    
  339.     /* read the upper 2 bytes of starting cluster - req. only for 32 bit FAT */
  340.     if (media_ptr -> fx_media_32_bit_FAT)
  341.     {
  342.         destination_ptr -> fx_dir_entry_cluster =  _fx_utility_16_unsigned_read(read_ptr);
  343.         destination_ptr -> fx_dir_entry_cluster <<= 16;
  344.     }
  345.     else destination_ptr -> fx_dir_entry_cluster = 0;
  346.     read_ptr += 2;  /* Always 2 bytes */
  347.     /* Copy the time into the destination.  */
  348.     destination_ptr -> fx_dir_entry_time =  _fx_utility_16_unsigned_read(read_ptr);
  349.     read_ptr += 2;  /* Always 2 bytes */
  350.     /* Copy the date into the destination.  */
  351.     destination_ptr -> fx_dir_entry_date =  _fx_utility_16_unsigned_read(read_ptr);
  352.     read_ptr += 2;  /* Always 2 bytes */
  353.     /* Copy the starting cluster into the destination.  */
  354.     destination_ptr -> fx_dir_entry_cluster +=  _fx_utility_16_unsigned_read(read_ptr);
  355.     read_ptr += 2;  /* Always 2 bytes */
  356.     /* Copy the file size into the destination.  */
  357.     destination_ptr -> fx_dir_entry_file_size =  _fx_utility_32_unsigned_read(read_ptr);
  358.     *entry_ptr = entry;
  359.     /* Return success to the caller.  */
  360.     return(FX_SUCCESS);
  361. }
  362. #endif