ResBytestream.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:19k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*******************************************************************************
  36.  * Filename:
  37.  * ---------
  38.  *  ResBytestream.c
  39.  *
  40.  * Project:
  41.  * --------
  42.  *  MAUI
  43.  *
  44.  * Description:
  45.  * ------------
  46.  *  
  47.  *
  48.  * Author:
  49.  * -------
  50.  *  
  51.  *
  52.  *==============================================================================
  53.  *             HISTORY
  54.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! 
  55.  *------------------------------------------------------------------------------
  56.  * removed!
  57.  *
  58.  *------------------------------------------------------------------------------
  59.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! 
  60.  *==============================================================================
  61.  *******************************************************************************/
  62. /**
  63.  * Copyright Notice
  64.  * (c) 2002 - 2003, Pixtel Communications, Inc., 1489 43rd Ave. W.,
  65.  * Vancouver, B.C. V6M 4K8 Canada. All Rights Reserved.
  66.  *  (It is illegal to remove this copyright notice from this software or any
  67.  *  portion of it)
  68.  */
  69. /**********************************************************************************
  70.    External tool required for the MMI
  71.    Filename:      bytestream.c
  72.    Author:        manju
  73.    Date Created:  June-10-2002
  74.    Contains:      bytestream data type
  75.                These are used to process ROM images of binary files
  76. **********************************************************************************/
  77. #include "ResBytestream.h"
  78. /*****************************************************************************
  79.  * FUNCTION
  80.  *  bytestream_initialize
  81.  * DESCRIPTION
  82.  *  
  83.  * PARAMETERS
  84.  *  file        [?]         
  85.  *  data        [?]         
  86.  *  size        [IN]        
  87.  * RETURNS
  88.  *  void
  89.  *****************************************************************************/
  90. void bytestream_initialize(bytestream *file, U8 *data, long int size)
  91. {
  92.     /*----------------------------------------------------------------*/
  93.     /* Local Variables                                                */
  94.     /*----------------------------------------------------------------*/
  95.     /*----------------------------------------------------------------*/
  96.     /* Code Body                                                      */
  97.     /*----------------------------------------------------------------*/
  98.     file->data = data;
  99.     file->size = size;
  100.     file->current_offset = 0;
  101. }
  102. /*****************************************************************************
  103.  * FUNCTION
  104.  *  bytestream_fclose
  105.  * DESCRIPTION
  106.  *  
  107.  * PARAMETERS
  108.  *  file        [?]     
  109.  * RETURNS
  110.  *  void
  111.  *****************************************************************************/
  112. void bytestream_fclose(bytestream *file)
  113. {
  114.     /*----------------------------------------------------------------*/
  115.     /* Local Variables                                                */
  116.     /*----------------------------------------------------------------*/
  117.     /*----------------------------------------------------------------*/
  118.     /* Code Body                                                      */
  119.     /*----------------------------------------------------------------*/
  120.     file->data = NULL;
  121.     file->size = 0;
  122.     file->current_offset = 0;
  123. }
  124. /*****************************************************************************
  125.  * FUNCTION
  126.  *  bytestream_fseek
  127.  * DESCRIPTION
  128.  *  
  129.  * PARAMETERS
  130.  *  file        [?]         
  131.  *  offset      [IN]        
  132.  *  mode        [IN]        
  133.  * RETURNS
  134.  *  
  135.  *****************************************************************************/
  136. U8 bytestream_fseek(bytestream *file, long int offset, int mode)
  137. {
  138.     /*----------------------------------------------------------------*/
  139.     /* Local Variables                                                */
  140.     /*----------------------------------------------------------------*/
  141.     int start_offset, final_offset;
  142.     /*----------------------------------------------------------------*/
  143.     /* Code Body                                                      */
  144.     /*----------------------------------------------------------------*/
  145.     switch (mode)
  146.     {
  147.         case SEEK_END:
  148.             start_offset = file->size - 1;
  149.             break;
  150.         case SEEK_SET:
  151.             start_offset = 0;
  152.             break;
  153.         case SEEK_CUR:
  154.         default:
  155.             start_offset = file->current_offset;
  156.             break;
  157.     }
  158.     final_offset = start_offset + offset;
  159.     if (final_offset < 0 || final_offset >= file->size)
  160.     {
  161.         return (1);
  162.     }
  163.     else
  164.     {
  165.         file->current_offset = final_offset;
  166.         return (0);
  167.     }
  168. }
  169. /*****************************************************************************
  170.  * FUNCTION
  171.  *  bytestream_feof
  172.  * DESCRIPTION
  173.  *  
  174.  * PARAMETERS
  175.  *  file        [?]     
  176.  * RETURNS
  177.  *  
  178.  *****************************************************************************/
  179. U8 bytestream_feof(bytestream *file)
  180. {
  181.     /*----------------------------------------------------------------*/
  182.     /* Local Variables                                                */
  183.     /*----------------------------------------------------------------*/
  184.     /*----------------------------------------------------------------*/
  185.     /* Code Body                                                      */
  186.     /*----------------------------------------------------------------*/
  187.     if (file->current_offset >= file->size)
  188.     {
  189.         return (1);
  190.     }
  191.     else
  192.     {
  193.         return (0);
  194.     }
  195. }
  196. /*****************************************************************************
  197.  * FUNCTION
  198.  *  bytestream_fgetbyte
  199.  * DESCRIPTION
  200.  *  
  201.  * PARAMETERS
  202.  *  file        [?]     
  203.  * RETURNS
  204.  *  
  205.  *****************************************************************************/
  206. U8 bytestream_fgetbyte(bytestream *file)
  207. {
  208.     /*----------------------------------------------------------------*/
  209.     /* Local Variables                                                */
  210.     /*----------------------------------------------------------------*/
  211.     U8 c;
  212.     /*----------------------------------------------------------------*/
  213.     /* Code Body                                                      */
  214.     /*----------------------------------------------------------------*/
  215.     if (bytestream_feof(file))
  216.     {
  217.         return (0);
  218.     }
  219.     c = file->data[file->current_offset];
  220.     file->current_offset++;
  221.     return (c);
  222. }
  223. /*****************************************************************************
  224.  * FUNCTION
  225.  *  bytestream_fputbyte
  226.  * DESCRIPTION
  227.  *  
  228.  * PARAMETERS
  229.  *  file        [?]         
  230.  *  c           [IN]        
  231.  * RETURNS
  232.  *  
  233.  *****************************************************************************/
  234. U8 bytestream_fputbyte(bytestream *file, U8 c)
  235. {
  236.     /*----------------------------------------------------------------*/
  237.     /* Local Variables                                                */
  238.     /*----------------------------------------------------------------*/
  239.     /*----------------------------------------------------------------*/
  240.     /* Code Body                                                      */
  241.     /*----------------------------------------------------------------*/
  242.     if (bytestream_feof(file))
  243.     {
  244.         return (0);
  245.     }
  246.     file->data[file->current_offset] = c;
  247.     file->current_offset++;
  248.     return (1);
  249. }
  250. /*****************************************************************************
  251.  * FUNCTION
  252.  *  bytestream_fread
  253.  * DESCRIPTION
  254.  *  
  255.  * PARAMETERS
  256.  *  buffer      [?]         
  257.  *  size        [IN]        
  258.  *  number      [IN]        
  259.  *  file        [?]         
  260.  * RETURNS
  261.  *  
  262.  *****************************************************************************/
  263. size_t bytestream_fread(U8 *buffer, size_t size, size_t number, bytestream *file)
  264. {
  265.     /*----------------------------------------------------------------*/
  266.     /* Local Variables                                                */
  267.     /*----------------------------------------------------------------*/
  268.     unsigned long int count = 0, i;
  269.     /*----------------------------------------------------------------*/
  270.     /* Code Body                                                      */
  271.     /*----------------------------------------------------------------*/
  272.     for (i = 0; i < (long)size * number; i++)
  273.     {
  274.         if (bytestream_feof(file))
  275.         {
  276.             break;
  277.         }
  278.         buffer[count++] = bytestream_fgetbyte(file);
  279.     }
  280.     return (count);
  281. }
  282. /*****************************************************************************
  283.  * FUNCTION
  284.  *  bytestream_fgetword
  285.  * DESCRIPTION
  286.  *  
  287.  * PARAMETERS
  288.  *  file        [?]     
  289.  * RETURNS
  290.  *  
  291.  *****************************************************************************/
  292. U16 bytestream_fgetword(bytestream *file)
  293. {
  294.     /*----------------------------------------------------------------*/
  295.     /* Local Variables                                                */
  296.     /*----------------------------------------------------------------*/
  297.     U16 MSB, LSB;
  298.     /*----------------------------------------------------------------*/
  299.     /* Code Body                                                      */
  300.     /*----------------------------------------------------------------*/
  301.     LSB = bytestream_fgetbyte(file);
  302.     MSB = bytestream_fgetbyte(file);
  303.     return ((MSB << 8) | LSB);
  304. }
  305. /*****************************************************************************
  306.  * FUNCTION
  307.  *  bytestream_fgetdword
  308.  * DESCRIPTION
  309.  *  
  310.  * PARAMETERS
  311.  *  file        [?]     
  312.  * RETURNS
  313.  *  
  314.  *****************************************************************************/
  315. U32 bytestream_fgetdword(bytestream *file)
  316. {
  317.     /*----------------------------------------------------------------*/
  318.     /* Local Variables                                                */
  319.     /*----------------------------------------------------------------*/
  320.     U32 MSB, LSB;
  321.     /*----------------------------------------------------------------*/
  322.     /* Code Body                                                      */
  323.     /*----------------------------------------------------------------*/
  324.     LSB = bytestream_fgetword(file);
  325.     MSB = bytestream_fgetword(file);
  326.     return ((MSB << 16) | LSB);
  327. }
  328. /*****************************************************************************
  329.  * FUNCTION
  330.  *  bytestream_fputword
  331.  * DESCRIPTION
  332.  *  
  333.  * PARAMETERS
  334.  *  file        [?]         
  335.  *  w           [IN]        
  336.  * RETURNS
  337.  *  
  338.  *****************************************************************************/
  339. U8 bytestream_fputword(bytestream *file, U16 w)
  340. {
  341.     /*----------------------------------------------------------------*/
  342.     /* Local Variables                                                */
  343.     /*----------------------------------------------------------------*/
  344.     U8 MSB, LSB, rvalue;
  345.     /*----------------------------------------------------------------*/
  346.     /* Code Body                                                      */
  347.     /*----------------------------------------------------------------*/
  348.     LSB = w & 0xff;
  349.     MSB = w >> 8;
  350.     rvalue = bytestream_fputbyte(file, LSB);
  351.     rvalue = bytestream_fputbyte(file, MSB);
  352.     return (rvalue);
  353. }
  354. /*****************************************************************************
  355.  * FUNCTION
  356.  *  bytestream_fputword_bigendian
  357.  * DESCRIPTION
  358.  *  
  359.  * PARAMETERS
  360.  *  file        [?]         
  361.  *  w           [IN]        
  362.  * RETURNS
  363.  *  
  364.  *****************************************************************************/
  365. U8 bytestream_fputword_bigendian(bytestream *file, U16 w)
  366. {
  367.     /*----------------------------------------------------------------*/
  368.     /* Local Variables                                                */
  369.     /*----------------------------------------------------------------*/
  370.     U8 MSB, LSB, rvalue;
  371.     /*----------------------------------------------------------------*/
  372.     /* Code Body                                                      */
  373.     /*----------------------------------------------------------------*/
  374.     LSB = w & 0xff;
  375.     MSB = w >> 8;
  376.     rvalue = bytestream_fputbyte(file, MSB);
  377.     rvalue = bytestream_fputbyte(file, LSB);
  378.     return (rvalue);
  379. }
  380. /*****************************************************************************
  381.  * FUNCTION
  382.  *  bytestream_fputdword
  383.  * DESCRIPTION
  384.  *  
  385.  * PARAMETERS
  386.  *  file        [?]         
  387.  *  d           [IN]        
  388.  * RETURNS
  389.  *  
  390.  *****************************************************************************/
  391. U8 bytestream_fputdword(bytestream *file, U32 d)
  392. {
  393.     /*----------------------------------------------------------------*/
  394.     /* Local Variables                                                */
  395.     /*----------------------------------------------------------------*/
  396.     U16 MSB, LSB;
  397.     U8 rvalue;
  398.     /*----------------------------------------------------------------*/
  399.     /* Code Body                                                      */
  400.     /*----------------------------------------------------------------*/
  401.     LSB = (U16) d & 0xffff;
  402.     MSB = (U16) d >> 16;
  403.     rvalue = bytestream_fputword(file, LSB);
  404.     rvalue = bytestream_fputword(file, MSB);
  405.     return (rvalue);
  406. }
  407. /*****************************************************************************
  408.  * FUNCTION
  409.  *  bytestream_fputdword_bigendian
  410.  * DESCRIPTION
  411.  *  
  412.  * PARAMETERS
  413.  *  file        [?]         
  414.  *  d           [IN]        
  415.  * RETURNS
  416.  *  
  417.  *****************************************************************************/
  418. U8 bytestream_fputdword_bigendian(bytestream *file, U32 d)
  419. {
  420.     /*----------------------------------------------------------------*/
  421.     /* Local Variables                                                */
  422.     /*----------------------------------------------------------------*/
  423.     U16 MSB, LSB;
  424.     U8 rvalue;
  425.     /*----------------------------------------------------------------*/
  426.     /* Code Body                                                      */
  427.     /*----------------------------------------------------------------*/
  428.     LSB = (U16) d & 0xffff;
  429.     MSB = (U16) d >> 16;
  430.     rvalue = bytestream_fputword_bigendian(file, MSB);
  431.     rvalue = bytestream_fputword_bigendian(file, LSB);
  432.     return (rvalue);
  433. }
  434. /*****************************************************************************
  435.  * FUNCTION
  436.  *  bytestream_fgetword_bigendian
  437.  * DESCRIPTION
  438.  *  
  439.  * PARAMETERS
  440.  *  file        [?]     
  441.  * RETURNS
  442.  *  
  443.  *****************************************************************************/
  444. U16 bytestream_fgetword_bigendian(bytestream *file)
  445. {
  446.     /*----------------------------------------------------------------*/
  447.     /* Local Variables                                                */
  448.     /*----------------------------------------------------------------*/
  449.     U16 MSB, LSB;
  450.     /*----------------------------------------------------------------*/
  451.     /* Code Body                                                      */
  452.     /*----------------------------------------------------------------*/
  453.     MSB = bytestream_fgetbyte(file);
  454.     LSB = bytestream_fgetbyte(file);
  455.     return ((MSB << 8) | LSB);
  456. }
  457. /*****************************************************************************
  458.  * FUNCTION
  459.  *  bytestream_fgetdword_bigendian
  460.  * DESCRIPTION
  461.  *  
  462.  * PARAMETERS
  463.  *  file        [?]     
  464.  * RETURNS
  465.  *  
  466.  *****************************************************************************/
  467. U32 bytestream_fgetdword_bigendian(bytestream *file)
  468. {
  469.     /*----------------------------------------------------------------*/
  470.     /* Local Variables                                                */
  471.     /*----------------------------------------------------------------*/
  472.     U32 MSB, LSB;
  473.     /*----------------------------------------------------------------*/
  474.     /* Code Body                                                      */
  475.     /*----------------------------------------------------------------*/
  476.     MSB = bytestream_fgetword_bigendian(file);
  477.     LSB = bytestream_fgetword_bigendian(file);
  478.     return ((MSB << 16) | LSB);
  479. }
  480. /* Used to read variable length data: Useful only for MIDI files  */
  481. /*****************************************************************************
  482.  * FUNCTION
  483.  *  bytestream_fgetvdata
  484.  * DESCRIPTION
  485.  *  
  486.  * PARAMETERS
  487.  *  file        [?]     
  488.  * RETURNS
  489.  *  
  490.  *****************************************************************************/
  491. U32 bytestream_fgetvdata(bytestream *file)
  492. {
  493.     /*----------------------------------------------------------------*/
  494.     /* Local Variables                                                */
  495.     /*----------------------------------------------------------------*/
  496.     U32 rvalue;
  497.     U8 b;
  498.     /*----------------------------------------------------------------*/
  499.     /* Code Body                                                      */
  500.     /*----------------------------------------------------------------*/
  501.     if ((rvalue = bytestream_fgetbyte(file)) & 0x80)
  502.     {
  503.         rvalue &= 0x7f;
  504.         do
  505.         {
  506.             rvalue = (rvalue << 7) + ((b = bytestream_fgetbyte(file)) & 0x7f);
  507.         } while (b & 0x80);
  508.     }
  509.     return (rvalue);
  510. }