/* Read image file (specified by UserHandle) into user specified buffer with specified offset and length. */
EFI_STATUS EFIAPI CoreReadImageFile(IN VOID *UserHandle, IN UINTN Offset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
/* Read image file (specified by UserHandle) into user specified buffer with specified offset and length. */
EFI_STATUS EFIAPI CoreReadImageFile(IN VOID *UserHandle, IN UINTN Offset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
{
  UINTN              EndPosition;
  IMAGE_FILE_HANDLE  *FHand;
  if ((UserHandle == NULL) || (ReadSize == NULL) || (Buffer == NULL)) {
    return EFI_INVALID_PARAMETER;
  }
  if (MAX_ADDRESS - Offset < *ReadSize) {
    return EFI_INVALID_PARAMETER;
  }
  FHand = (IMAGE_FILE_HANDLE  *)UserHandle;
  ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);
  EndPosition = Offset + *ReadSize;
  if (EndPosition > FHand->SourceSize) {
    *ReadSize = (UINT32)(FHand->SourceSize - Offset);
  }
  if (Offset >= FHand->SourceSize) {
    *ReadSize = 0;
  }
  CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);
  return EFI_SUCCESS;
}