fsal.h 23 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 *
 * Filename:
 * ---------
 *   fsal.h
 *
 * Project:
 * --------
 *   MAUI
 *
 * Description:
 * ------------
 *   File System Abstraction Layer, with buffered read/write.
 *
 * Author:
 * -------
 * -------
 *
 *==============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *==============================================================================
 *******************************************************************************/

/**
 * \file fsal.h
 * \author Murphy Chen
 * \version $Revision:   1.26  $
 * \date $Modtime:   May 16 2005 23:18:18  $
 *
 * FSAL stands for File System Abstraction Layer.
 * This layer provides the following functionalities:
 * - Isolate the underlying file systems.
 * - Provide buffered read/write operations.
 * - Provide bits/bytes accessing functions.
 */

#ifndef __FSAL_BUFFER_H__
#define __FSAL_BUFFER_H__

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

#ifndef FSAL_STANDALONE
   /// For target and MoDIS
   #define FSAL_VERBOSE 0
   #include "fs_gprot.h"
   #include "kal_general_types.h"
   #include "med_status.h"
   #define FSAL_PLATFORM_KAL 1
   #define FSAL_PLATFORM_WIN32 0
   #define FSAL_PLATFORM_MEMORY 0
#else
   /// For Win32 standalone application, unit test and PC Simulator
   #include <stdio.h>
   #include <assert.h>
   #include <string.h>
   #ifndef ASSERT
      #define ASSERT(x) assert(x)
   #endif
   #define FSAL_VERBOSE 0
   #define kal_mem_cpy(a,b,c) memcpy(a,b,c)
   #include "kal_general_types.h"
   #define MED_STAT_FSAL_START 0
   #define FSAL_PLATFORM_KAL 0
   #define FSAL_PLATFORM_WIN32 1
   #define FSAL_PLATFORM_MEMORY 0
#endif

typedef enum {
   FSAL_OK = MED_STAT_FSAL_START,
   FSAL_OPEN_ERROR,
   FSAL_READ_ERROR,
   FSAL_WRITE_ERROR,
   FSAL_SEEK_ERROR,
   FSAL_CLOSE_ERROR,
   FSAL_INVALID_ARGUMENT,
   /// The field of the structure is invalid. Possibly caused by memory corruption.
   FSAL_MEMORY_CORRUPTION,
   FSAL_FATAL_ERROR,
   FSAL_DEVICE_BUSY,
   FSAL_READ_OVER_EOF,
   FSAL_SEEK_OVER_EOF
} FSAL_Status;



typedef enum {
   /// Open for reading.
   FSAL_READ = 0,
   /// Open for reading and writing.
   /// The file is created if it is not existed, otherwise it is truncated.
   FSAL_WRITE,
   /// Open for reading and appending.
   /// The file is created if it is not existed.
   FSAL_APPEND,
   /// Open a ROM file for reading.
   /// FSAL_Direct_SetRamFileSize shall be called before calling FSAL_Open.
   FSAL_ROMFILE,
   ///
   FSAL_READ_SHARED,
   FSAL_NONBLOCKING = 0x80
} FSAL_FileMode;


typedef enum {
   FSAL_BIG_ENDIAN = 0,
   FSAL_LITTLE_ENDIAN
} FSAL_ByteOrder;

/**
 * The memory space of the FSAL structure is prepared by the caller.
 * Accesses to each file requires a separate instance of each FSAL structure.
 * The content of the structuer will be initialized via the FSAL_Open
 * function call.
 * And the pointer to the structure need to be passed as the first argument
 * to each FSAL function invocations.
 */
typedef struct {
   /* file system abstration layer*/
#if FSAL_PLATFORM_KAL
   FS_HANDLE hFile;
   /// error code from file system
   kal_int32 iFSErrorCode;
#elif FSAL_PLATFORM_MEMORY
   kal_uint32 uMaxRamFileSize;
#elif FSAL_PLATFORM_WIN32
   FILE *hFile;
#endif
   /// For ROM file. Point to the start memory address of the file.
   kal_uint8 *pbFile;
   kal_uint32 uRamFileSize;
   kal_uint32 uRamFileOffset;
   kal_uint32 uFileSize;
   kal_uint32 uFileOffset;
   kal_bool bBuffering;
   kal_uint8 *pbBuf;
   kal_uint32 uBufSize;
   kal_uint32 uCachedBlock;
   kal_uint32 uCachedFileSize;
   kal_bool bDirty; /* whether the cache has been written to */
   kal_bool bPartialCached; /* whether the cache bloch isn't entire */
   kal_uint32 uLastReadCount;
   kal_uint32 uDRMpermission;

   FSAL_ByteOrder uByteOrder;
} STFSAL;

/* ------ Private Macros ------ */

#define FSAL_CHECK_ARG(exp) \
   if (!(exp)) \
      return FSAL_INVALID_ARGUMENT

#define FSAL_ASSERT(exp) \
   if (!(exp)) \
      return FSAL_FATAL_ERROR

#define FSAL_ASSERT_NO_RET_VAL(exp) \
   if (!(exp)) \
      return

/* ------ public functions ------ */

/**
 * Open the file.
 *
 * @param pstFSAL pointer to FSAL structure prepared by caller.
 * FSAL will initialize the structure.
 * @param szFile pointer to a string contained the file name when opened in FSAL_READ,
 * FSAL_WRITE, or FSAL_APPEND mode; pointer to the start memory address of the file
 * when opened in FSAL_ROMFILE mode.
 * @param eMode file operation mode.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Open(STFSAL *pstFSAL, void *szFile, FSAL_FileMode eMode);

/**
 * Open the file which has been opened via file I/O API with its file handle.
 * Please do not call FSAL_Close for this handle.
 *
 * @param pstFSAL pointer to FSAL structure prepared by caller.
 * FSAL will initialize the structure.
 * @param pFileHandle file handle of the underlying file I/O API.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Open_WithHandle(STFSAL *pstFSAL, void *pFileHandle);

/**
 * Open an existing file. When using this interface, you need to consider
 * about the issues of multiple access to the same file.
 * Please do not call FSAL_Close for this handle.
 *
 * @param pstFSAL pointer to FSAL structure prepared by caller.
 * FSAL will initialize the structure.
 * @param pstFSAL_Existing pointer to an existing FSAL structure,
 * FSAL will access file opened via the file handle in that structure.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Open_Attach(STFSAL *pstFSAL, STFSAL *pstFSAL_Existing);

/**
 * Client prepares its own buffer and call this function
 * to set buffer for FSAL cache.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param uBufferSize size of the buffer in unit of byte.
 * @param pbBuf memory location of the buffer
 *
 * @return None.
 */
void FSAL_SetBuffer(STFSAL *pstFSAL, kal_uint32 uBufferSize, kal_uint8 *pbBuf);

/**
 * Flush FSAL cache and close the file.
 *
 * @param pstFSAL pointer to FSAL structure.
 *
 * @return None.
 */
FSAL_Status FSAL_Close(STFSAL *pstFSAL);

/* ------ buffering functions (fsal_buffer.c) ------ */

/**
 * Read data from the file.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbBuf memory location of the data.
 * @param uSize number of bytes to read. uSize must be larger than 0.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Read(STFSAL *pstFSAL, kal_uint8* pbBuf, kal_uint32 uSize);

/**
 * Write data to the file.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbBuf memory location of the data.
 * @param uSize number of bytes to write. uSize must be larger than 0.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Write(STFSAL *pstFSAL, kal_uint8 *pbBuf, kal_uint32 uSize);

/**
 * Change file offset.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param uOffset offsets in unit of bytes.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Seek(STFSAL *pstFSAL, kal_uint32 uOffset);

/**
 * Flush FSAL cache to the underlying file system.
 *
 * @param pstFSAL pointer to FSAL structure.
 *
 * @return None.
 */
FSAL_Status FSAL_CacheFlush(STFSAL *pstFSAL);

/**
 * Get the current file position.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puPosition memory location for retrieving the file position.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_GetCurPos(STFSAL *pstFSAL, kal_uint32 *puPosition);

/**
 * Get the file size.
 * The current file position remains the same after invocation.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puFileSize memory location for retrieving the file size.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_GetFileSize(STFSAL *pstFSAL, kal_uint32 *puFileSize);



/* ------ bits/bytes read access functions (fsal_read.c) ------ */

/**
 * Read n bytes.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbData memory location for storing the result bytes
 * @param uLen number of bytes to read
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Read_Bytes(STFSAL *pstFSAL, kal_uint8 *pbData, kal_uint32 uLen);

/**
 * Read n bits.
 * When calling this function for the first time, *puBitCnt must be 0.
 * *puBitCnt will be updated after each invocation.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puBitCnt memory location for storing the bit offset.
 * @param pbValue memory location for storing the result bits.
 * @param uBitLen number of bits to read (1~8).
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Read_Bits(STFSAL *pstFSAL, kal_uint32 *puBitCnt, kal_uint8 *pbValue, kal_uint32 uBitLen);

/**
 * Read 4 bytes representing an unsigned integer.
 * The content stored on the file is assumed to be big-endian.
 * This function will handle the endian-conversion issue.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puValue memory location for storing the result unsigned integer
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Read_UINT(STFSAL *pstFSAL, kal_uint32 *puValue);

/**
 * Read 3 bytes representing an unsigned integer.
 * The content stored on the file is assumed to be big-endian.
 * This function will handle the endian-conversion issue.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puValue memory location for storing the result unsigned integer
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Read_UINT24(STFSAL *pstFSAL, kal_uint32 *puValue);

/**
 * Read 2 bytes representing an unsigned integer.
 * The content stored on the file is assumed to be big-endian.
 * This function will handle the endian-conversion issue.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pwValue memory location for storing the result unsigned integer
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Read_UINT16(STFSAL *pstFSAL, kal_uint16 *pwValue);

/**
 * Skip n bytes.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param uLen number of bytes to skip
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Skip_Bytes(STFSAL *pstFSAL, kal_uint32 uLen);

/**
 * Get next n bytes. The file offset does not change after invocation.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbValue memory location for storing the result bytes
 * @param uLen number of bytes to peek
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Peek_Bytes(STFSAL *pstFSAL, kal_uint8 *pbValue, kal_uint32 uLen);

/**
 * Get next unsinged integer. The file offset does not change after invocation.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puValue memory location for storing the result unsigned integer
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Peek_UINT(STFSAL *pstFSAL, kal_uint32 *puValue);


/* ------ bits/bytes write access functions (fsal_write.c) ------ */

/**
 * Write n bytes.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbData memory location of the bytes
 * @param uLen number of bytes to write
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Write_Bytes(STFSAL *pstFSAL, kal_uint8 *pbData, kal_uint32 uLen);

/**
 * Write 4 bytes representing an unsigned integer.
 * The value written to the file will be stored as big-endian.
 * This function will handle the endian-conversion issue.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param uValue The value to be written to the file.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Write_UINT(STFSAL *pstFSAL, kal_uint32 uValue);

/**
 * Write 2 bytes representing an unsigned integer.
 * The value written to the file will be stored as big-endian.
 * This function will handle the endian-conversion issue.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param wValue The value to be written to the file.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Write_UINT16(STFSAL* pstFSAL, kal_uint16 wValue);

/**
 * Write 1 byte representing an unsigned character.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param bValue The value to be written to the file.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Write_UINT8(STFSAL* pstFSAL, kal_uint8 bValue);

/**
 * Write 4 bytes representing an unsigned integer at a given file offset.
 * File position remains the same after the invocation.
 * The value written to the file will be stored as big-endian.
 * This function will handle the endian-conversion issue.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param uValue The value to be written to the file.
 * @param uOffset The file offset to write to.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Write_UINT_AT(STFSAL* pstFSAL, kal_uint32 uValue, kal_uint32 uOffset);

/* ------ direct access functions (bypass buffering mechanism) ------ */

/**
 * Open the file. (Internal Use by FSAL Only)
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param szFile pointer to a string contained the file name.
 * @param eMode file operation mode.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_Open(STFSAL *pstFSAL, void *szFile, FSAL_FileMode eMode);

/**
 * Close the file. (Internal Use by FSAL Only)
 *
 * @param pstFSAL pointer to FSAL structure.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_Close(STFSAL *pstFSAL);

/**
 * Read data from the file directly.
 *
 * Use with caution. Becuase the file offset will be out-of-sync
 * between FSAL and underlying file system.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbBuf memory location of the data.
 * @param uSize number of bytes to read. uSize must be larger than 0.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_Read(STFSAL *pstFSAL, kal_uint8* pbBuf, kal_uint32 uSize);

/**
 * Write data to the file directly.
 *
 * Use with caution. Becuase the file offset will be out-of-sync
 * between FSAL and underlying file system.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param pbBuf memory location of the data.
 * @param uSize number of bytes to write. uSize must be larger than 0.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_Write(STFSAL *pstFSAL, kal_uint8* pbBuf, kal_uint32 uSize);

/**
 * Change file offset of the underlying file system directly.
 *
 * Use with caution. Becuase the file offset will be out-of-sync
 * between FSAL and underlying file system.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param uOffset offsets in unit of bytes.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_Seek(STFSAL *pstFSAL, kal_uint32 uOffset);

/**
 * Get the current file position of the underlying file system directly.
 *
 * Use with caution. Becuase the file offset may be out-of-sync
 * between FSAL and underlying file system.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puPosition memory location for retrieving the file position.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_GetCurPos(STFSAL *pstFSAL, kal_uint32* puPosition);

/**
 * Get the file size of the underlying file system directly.
 * The current file position remains the same after invocation.
 *
 * Use with caution. Becuase the file size may be out-of-sync
 * between FSAL and underlying file system.
 *
 * @param pstFSAL pointer to FSAL structure.
 * @param puFileSize memory location for retrieving the file size.
 *
 * @return If the function succeeds, the return value is FSAL_OK.
 * Otherwise, an error code is returned.
 */
FSAL_Status FSAL_Direct_GetFileSize(STFSAL *pstFSAL, kal_uint32 *puFileSize);

/**
 * When using ram file, this funtion shall be called before calling FSAL_Open.
 */
void FSAL_Direct_SetRamFileSize(STFSAL *pstFSAL, kal_uint32 uSize);

/**
 * When writing to ram file, if after a write operation, the file offset exceeds
 * the specified ram file size, an assertion will be issued in order to detect
 * memory corruption.
 */
void FSAL_Direct_SetMaxRamFileSize(STFSAL *pstFSAL, kal_uint32 uSize);

/**
 * Get the last file system error.
 */
int FSAL_GetLastError(STFSAL *pstFSAL);

/**
 * If the pstFSAL is a handle of ram file, return KAL_TRUE; otherwise return KAL_FALSE.
 */
kal_bool FSAL_IsRamFile(STFSAL *pstFSAL);

/**
 * If the pstFSAL is a handle of ram file, return pointer to the memory block; otherwise return NULL.
 */
kal_uint8* FSAL_GetRamFilePointer(STFSAL *pstFSAL);

/**
 * Get the data count (in bytes) read in the previous read operation.
 * Generally, call this function if the return value of the previous read operation if FSAL_READ_OVER_EOF.
 */
int FSAL_GetLastReadCount(STFSAL *pstFSAL);


FSAL_Status FSAL_SetByteOrder(STFSAL *pstFSAL, FSAL_ByteOrder byteOrder);

kal_int32 FSAL_SetSeekHint(STFSAL *pstFSAL, kal_uint32 hint_num, void *hint);

typedef struct
{
    FS_HANDLE (* open_file)  (kal_wchar *file_path, kal_uint32 flags, kal_uint8 permission);
    kal_int32 (* close_file) (FS_HANDLE object);
    kal_int32 (* read_file)  (FS_HANDLE source, void *buffer, kal_uint32 size, kal_uint32 *length);
    kal_int64 (* seek_file)  (FS_HANDLE source, kal_int64 offset, kal_uint8 ref);
    kal_int32 (* file_size)  (FS_HANDLE source, kal_uint32 *size);
    kal_int32 (* file_pos)   (FS_HANDLE source, kal_uint32 *pos);
    kal_int32 (* seek_hint)  (FS_HANDLE source, kal_uint32 hint_num, void *hint);
}fsal_fsop_func_type;

int FSAL_SetFSOperations(fsal_fsop_func_type *fs_operations);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif