Changeset 3544 for trunk


Ignore:
Timestamp:
Aug 25, 2007, 8:48:53 AM (18 years ago)
Author:
bird
Message:

More kRdr stuff refactored.

Location:
trunk/kStuff
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/include/k/kErrors.h

    r3541 r3544  
    9191/** @} */
    9292
     93/** @name   kRdr Specific
     94 * @{ */
     95/** the base of the kRdr specific status codes. */
     96#define KRDR_ERR_BASE                                   (KDBG_ERR_BASE + 4)
     97/** The file reader can't take more concurrent mappings. */
     98#define KRDR_ERR_TOO_MANY_MAPPINGS                      (KRDR_ERR_BASE + 59)
     99/** @} */
     100
    93101/** @}*/
    94102
  • trunk/kStuff/kRdr/kRdrFile.c

    r3543 r3544  
    11/* $Id$ */
    22/** @file
     3 * kRdrFile - The Native File Provider
     4 */
     5
     6/*
     7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
    38 *
    4  * kLdr - The Dynamic Loader, file abstraction.
     9 * This file is part of kStuff.
    510 *
    6  * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
    7  *
    8  *
    9  * This file is part of kLdr.
    10  *
    11  * kLdr is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License as published by
    13  * the Free Software Foundation; either version 2 of the License, or
     11 * kStuff is free software; you can redistribute it and/or modify
     12 * it under the terms of the GNU Lesser General Public License as published
     13 * by the Free Software Foundation; either version 2 of the License, or
    1414 * (at your option) any later version.
    1515 *
    16  * kLdr is distributed in the hope that it will be useful,
     16 * kStuff is distributed in the hope that it will be useful,
    1717 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1818 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
     19 * GNU Lesser General Public License for more details.
    2020 *
    21  * You should have received a copy of the GNU General Public License
    22  * along with kLdr; if not, write to the Free Software
     21 * You should have received a copy of the GNU Lesser General Public License
     22 * along with kStuff; if not, write to the Free Software
    2323 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2424 *
     
    2929*   Header Files                                                               *
    3030*******************************************************************************/
    31 #ifdef __OS2__
     31#include <k/kDefs.h>
     32
     33#if K_OS == K_OS_OS2
    3234# define INCL_ERRORS
    3335# define INCL_BASE
    3436# include <os2.h>
    3537
    36 #elif defined(__WIN32__) || defined(__WIN64__) || defined(__WIN__)
     38#elif K_OS == K_OS_WINDOWS
    3739# define WIN32_NO_STATUS
    3840# include <Windows.h>
     
    152154#endif
    153155
    154 #include <kLdr.h>
    155 #include "kLdrHlp.h"
    156 
    157 
    158 /*******************************************************************************
    159 *   Defined Constants And Macros                                               *
    160 *******************************************************************************/
    161 /** @def KLDRRDRFILE_STRICT
    162  * Define KLDRRDRFILE_STRICT to enabled strict checks in KLDRRDRFILE. */
    163 #define KLDRRDRFILE_STRICT 1
    164 
    165 /** @def KLDRRDRFILE_ASSERT
    166  * Assert that an expression is true when KLDRRDRFILE_STRICT is defined.
    167  */
    168 #ifdef KLDRRDRFILE_STRICT
    169 # define KLDRRDRFILE_ASSERT(expr)  kldrHlpAssert(expr)
    170 #else
    171 # define KLDRRDRFILE_ASSERT(expr)  do {} while (0)
    172 #endif
     156#include "kRdrInternal.h"
     157#include <k/kHlpAlloc.h>
     158#include <k/kHlpString.h>
    173159
    174160
     
    179165 * Prepared stuff.
    180166 */
    181 typedef struct KLDRRDRFILEPREP
     167typedef struct KRDRFILEPREP
    182168{
    183169    /** The address of the prepared region. */
    184170    void           *pv;
    185171    /** The size of the prepared region. */
    186     size_t          cb;
    187 #if defined(__WIN__) || defined(__NT__)
     172    KSIZE           cb;
     173#if K_OS == K_OS_WINDOWS
    188174    /** Handle to the section created to map the file. */
    189175    HANDLE          hSection;
    190176#endif
    191 } KLDRRDRFILEPREP, *PKLDRRDRFILEPREP;
     177} KRDRFILEPREP, *PKRDRFILEPREP;
    192178
    193179/**
    194180 * The file provier instance for native files.
    195181 */
    196 typedef struct KLDRRDRFILE
     182typedef struct KRDRFILE
    197183{
    198184    /** The file reader vtable. */
    199     KLDRRDR             Core;
     185    KRDR                Core;
    200186    /** The file handle. */
    201 #ifdef __OS2__
     187#if K_OS == K_OS_OS2
    202188    HFILE               File;
    203 #elif defined(__WIN__) || defined(__NT__)
     189#elif K_OS == K_OS_WINDOWS
    204190    HANDLE              File;
    205191#else
     
    207193#endif
    208194    /** The current file offset. */
    209     KLDRFOFF            off;
     195    KFOFF               off;
    210196    /** The file size. */
    211     KLDRFOFF            cb;
     197    KFOFF               cb;
    212198    /** Array where we stuff the mapping area data. */
    213     KLDRRDRFILEPREP     aPreps[4];
     199    KRDRFILEPREP        aPreps[4];
    214200    /** The number of current preps. */
    215     uint32_t            cPreps;
     201    KU32                cPreps;
    216202    /** Number of mapping references. */
    217     int32_t             cMappings;
     203    KI32                cMappings;
    218204    /** The memory mapping. */
    219205    void               *pvMapping;
    220206    /** The filename. */
    221207    char                szFilename[1];
    222 } KLDRRDRFILE, *PKLDRRDRFILE;
     208} KRDRFILE, *PKRDRFILE;
    223209
    224210
     
    226212*   Internal Functions                                                         *
    227213*******************************************************************************/
    228 static void     kldrRdrFileDone(PKLDRRDR pRdr);
    229 static int      kldrRdrFileUnmap(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments);
    230 static int      kldrRdrFileGenericUnmap(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments);
    231 static int      kldrRdrFileProtect(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect);
    232 static int      kldrRdrFileGenericProtect(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect);
    233 static int      kldrRdrFileRefresh(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments);
    234 static int      kldrRdrFileGenericRefresh(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments);
    235 static int      kldrRdrFileMap(PKLDRRDR pRdr, void **ppvBase, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fFixed);
    236 static int      kldrRdrFileGenericMap(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fFixed);
    237 static size_t   kldrRdrFilePageSize(PKLDRRDR pRdr);
    238 static const char *kldrRdrFileName(PKLDRRDR pRdr);
    239 static KLDRFOFF kldrRdrFileTell(PKLDRRDR pRdr);
    240 static KLDRFOFF kldrRdrFileSize(PKLDRRDR pRdr);
    241 static int      kldrRdrFileAllUnmap(PKLDRRDR pRdr, const void *pvBits);
    242 static int      kldrRdrFileAllMap(PKLDRRDR pRdr, const void **ppvBits);
    243 static int      kldrRdrFileRead(PKLDRRDR pRdr, void *pvBuf, size_t cb, KLDRFOFF off);
    244 static int      kldrRdrFileDestroy(PKLDRRDR pRdr);
    245 static int      kldrRdrFileCreate(PPKLDRRDR ppRdr, const char *pszFilename);
     214static void     krdrFileDone(PKRDR pRdr);
     215static int      krdrFileUnmap(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
     216static int      krdrFileGenericUnmap(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments);
     217static int      krdrFileProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect);
     218static int      krdrFileGenericProtect(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect);
     219static int      krdrFileRefresh(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
     220static int      krdrFileGenericRefresh(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments);
     221static int      krdrFileMap(PKRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fFixed);
     222static int      krdrFileGenericMap(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments, unsigned fFixed);
     223static KSIZE   krdrFilePageSize(PKRDR pRdr);
     224static const char *krdrFileName(PKRDR pRdr);
     225static KIPTR    krdrFileNativeFH(PKRDR pRdr);
     226static KFOFF    krdrFileTell(PKRDR pRdr);
     227static KFOFF    krdrFileSize(PKRDR pRdr);
     228static int      krdrFileAllUnmap(PKRDR pRdr, const void *pvBits);
     229static int      krdrFileAllMap(PKRDR pRdr, const void **ppvBits);
     230static int      krdrFileRead(PKRDR pRdr, void *pvBuf, KSIZE cb, KFOFF off);
     231static int      krdrFileDestroy(PKRDR pRdr);
     232static int      krdrFileCreate(PPKRDR ppRdr, const char *pszFilename);
    246233
    247234
     
    250237*******************************************************************************/
    251238/** Native file provider operations. */
    252 const KLDRRDROPS g_kLdrRdrFileOps =
     239const KRDROPS g_kRdrFileOps =
    253240{
    254241    "native file",
    255242    NULL,
    256     kldrRdrFileCreate,
    257     kldrRdrFileDestroy,
    258     kldrRdrFileRead,
    259     kldrRdrFileAllMap,
    260     kldrRdrFileAllUnmap,
    261     kldrRdrFileSize,
    262     kldrRdrFileTell,
    263     kldrRdrFileName,
    264     kldrRdrFilePageSize,
    265     kldrRdrFileMap,
    266     kldrRdrFileRefresh,
    267     kldrRdrFileProtect,
    268     kldrRdrFileUnmap,
    269     kldrRdrFileDone,
     243    krdrFileCreate,
     244    krdrFileDestroy,
     245    krdrFileRead,
     246    krdrFileAllMap,
     247    krdrFileAllUnmap,
     248    krdrFileSize,
     249    krdrFileTell,
     250    krdrFileName,
     251    krdrFileNativeFH,
     252    krdrFilePageSize,
     253    krdrFileMap,
     254    krdrFileRefresh,
     255    krdrFileProtect,
     256    krdrFileUnmap,
     257    krdrFileDone,
    270258    42
    271259};
    272260
    273261
    274 #if defined(__WIN__) || defined(__NT__)
     262#if K_OS == K_OS_WINDOWS
    275263/**
    276264 * Converts a kLdr segment protection to NT protection for a mapping.
     
    279267 * @param   enmProt     kLdr protection.
    280268 */
    281 static ULONG kldrRdrFileGetNtMapProt(KLDRPROT enmProt)
     269static ULONG krdrFileGetNtMapProt(KLDRPROT enmProt)
    282270{
    283271    switch (enmProt)
     
    302290 * @param   enmProt     kLdr protection.
    303291 */
    304 static ULONG kldrRdrFileGetNtAllocProt(KLDRPROT enmProt)
     292static ULONG krdrFileGetNtAllocProt(KLDRPROT enmProt)
    305293{
    306294    switch (enmProt)
     
    320308
    321309
    322 /** @copydoc KLDRRDR::pfnDone */
    323 static void     kldrRdrFileDone(PKLDRRDR pRdr)
     310/** @copydoc KRDROPS::pfnDone */
     311static void     krdrFileDone(PKRDR pRdr)
    324312{
    325313}
     
    333321 * @param   pv      The base of the region.
    334322 */
    335 static PKLDRRDRFILEPREP kldrRdrFileFindPrepExact(PKLDRRDRFILE pFile, void *pv)
    336 {
    337     int32_t i = pFile->cPreps;
     323static PKRDRFILEPREP krdrFileFindPrepExact(PKRDRFILE pFile, void *pv)
     324{
     325    KI32 i = pFile->cPreps;
    338326    while (i-- > 0)
    339327        if (pFile->aPreps[i].pv == pv)
     
    343331
    344332
    345 /** @copydoc KLDRRDR::pfnUnmap */
    346 static int      kldrRdrFileUnmap(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments)
    347 {
    348     PKLDRRDRFILE        pRdrFile = (PKLDRRDRFILE)pRdr;
    349     PKLDRRDRFILEPREP    pPrep = kldrRdrFileFindPrepExact(pRdrFile, pvBase);
     333/** @copydoc KRDROPS::pfnUnmap */
     334static int      krdrFileUnmap(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
     335{
     336    PKRDRFILE        pRdrFile = (PKRDRFILE)pRdr;
     337    PKRDRFILEPREP       pPrep = krdrFileFindPrepExact(pRdrFile, pvBase);
    350338    int                 rc;
    351339    if (!pPrep)
    352         return KLDR_ERR_INVALID_PARAMETER;
    353 
    354 #if defined(__WIN__) || defined(__NT__)
     340        return KERR_INVALID_PARAMETER;
     341
     342#if K_OS == K_OS_WINDOWS
    355343    if (pPrep->hSection != NULL)
    356344    {
     
    360348#endif
    361349
    362     rc = kldrRdrFileGenericUnmap(pRdr, pPrep, cSegments, paSegments);
     350    rc = krdrFileGenericUnmap(pRdr, pPrep, cSegments, paSegments);
    363351
    364352    /* remove the mapping data on success. */
     
    373361
    374362
    375 /** Generic implementation of kldrRdrFileUnmap. */
    376 static int kldrRdrFileGenericUnmap(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments)
    377 {
    378     kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 1 /* unprotect */);
    379     return kldrHlpPageFree(pPrep->pv, pPrep->cb);
    380 }
    381 
    382 
    383 /** @copydoc KLDRRDR::pfnProtect */
    384 static int      kldrRdrFileProtect(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect)
    385 {
    386     PKLDRRDRFILE        pRdrFile = (PKLDRRDRFILE)pRdr;
    387     PKLDRRDRFILEPREP    pPrep = kldrRdrFileFindPrepExact(pRdrFile, pvBase);
     363/** Generic implementation of krdrFileUnmap. */
     364static int krdrFileGenericUnmap(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments)
     365{
     366    krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 1 /* unprotect */);
     367    return kHlpPageFree(pPrep->pv, pPrep->cb);
     368}
     369
     370
     371/** @copydoc KRDROPS::pfnProtect */
     372static int      krdrFileProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect)
     373{
     374    PKRDRFILE        pRdrFile = (PKRDRFILE)pRdr;
     375    PKRDRFILEPREP    pPrep = krdrFileFindPrepExact(pRdrFile, pvBase);
    388376    if (!pPrep)
    389         return KLDR_ERR_INVALID_PARAMETER;
    390 
    391 #if defined(__WIN__) || defined(__NT__)
     377        return KERR_INVALID_PARAMETER;
     378
     379#if K_OS == K_OS_WINDOWS
    392380    if (pPrep->hSection != NULL)
    393381    {
     
    397385#endif
    398386
    399     return kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, fUnprotectOrProtect);
    400 }
    401 
    402 
    403 /** Generic implementation of kldrRdrFileProtect. */
    404 static int kldrRdrFileGenericProtect(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect)
    405 {
    406     uint32_t i;
     387    return krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, fUnprotectOrProtect);
     388}
     389
     390
     391/** Generic implementation of krdrFileProtect. */
     392static int krdrFileGenericProtect(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect)
     393{
     394    KU32 i;
    407395
    408396    /*
     
    437425                    break;
    438426                default:
    439                     KLDRRDRFILE_ASSERT(!"bad enmProt");
     427                    kRdrAssert(!"bad enmProt");
    440428                    return -1;
    441429            }
     
    452440        pv = (uint8_t *)pPrep->pv + paSegments[i].RVA;
    453441
    454         rc = kldrHlpPageProtect(pv, paSegments[i].cbMapped, enmProt);
     442        rc = kHlpPageProtect(pv, paSegments[i].cbMapped, enmProt);
    455443        if (rc)
    456444            break;
     
    461449
    462450
    463 /** @copydoc KLDRRDR::pfnRefresh */
    464 static int      kldrRdrFileRefresh(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments)
    465 {
    466     PKLDRRDRFILE        pRdrFile = (PKLDRRDRFILE)pRdr;
    467     PKLDRRDRFILEPREP    pPrep = kldrRdrFileFindPrepExact(pRdrFile, pvBase);
     451/** @copydoc KRDROPS::pfnRefresh */
     452static int      krdrFileRefresh(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
     453{
     454    PKRDRFILE        pRdrFile = (PKRDRFILE)pRdr;
     455    PKRDRFILEPREP    pPrep = krdrFileFindPrepExact(pRdrFile, pvBase);
    468456    if (!pPrep)
    469         return KLDR_ERR_INVALID_PARAMETER;
    470 
    471 #if defined(__WIN__) || defined(__NT__)
     457        return KERR_INVALID_PARAMETER;
     458
     459#if K_OS == K_OS_WINDOWS
    472460    if (pPrep->hSection != NULL)
    473461    {
     
    477465#endif
    478466
    479     return kldrRdrFileGenericRefresh(pRdr, pPrep, cSegments, paSegments);
    480 }
    481 
    482 
    483 /** Generic implementation of kldrRdrFileRefresh. */
    484 static int      kldrRdrFileGenericRefresh(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments)
     467    return krdrFileGenericRefresh(pRdr, pPrep, cSegments, paSegments);
     468}
     469
     470
     471/** Generic implementation of krdrFileRefresh. */
     472static int      krdrFileGenericRefresh(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments)
    485473{
    486474    int rc;
    487475    int rc2;
    488     uint32_t i;
     476    KU32 i;
    489477
    490478    /*
    491479     * Make everything writable again.
    492480     */
    493     rc = kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 1 /* unprotect */);
     481    rc = krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 1 /* unprotect */);
    494482    if (rc)
    495483    {
    496         kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 0 /* protect */);
     484        krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 0 /* protect */);
    497485        return rc;
    498486    }
     
    502490     */
    503491    /** @todo only zero the areas not covered by raw file bits. */
    504     kLdrHlpMemSet(pPrep->pv, 0, pPrep->cb);
     492    kHlpMemSet(pPrep->pv, 0, pPrep->cb);
    505493
    506494    /*
     
    526514     * Protect the bits again.
    527515     */
    528     rc2 = kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 0 /* protect */);
     516    rc2 = krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 0 /* protect */);
    529517    if (rc2 && rc)
    530518        rc = rc2;
     
    534522
    535523
    536 /** @copydoc KLDRRDR::pfnMap */
    537 static int      kldrRdrFileMap(PKLDRRDR pRdr, void **ppvBase, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fFixed)
    538 {
    539     PKLDRRDRFILE        pRdrFile = (PKLDRRDRFILE)pRdr;
    540     PKLDRRDRFILEPREP    pPrep = &pRdrFile->aPreps[pRdrFile->cPreps];
    541     KLDRSIZE            cbTotal;
    542     const size_t        cbPage = pRdr->pOps->pfnPageSize(pRdr);
    543     int                 rc;
    544     uint32_t            i;
    545 
    546     if (pRdrFile->cPreps >= KLDR_ELEMENTS(pRdrFile->aPreps))
    547         return KLDR_ERR_TOO_MANY_MAPPINGS;
     524/** @copydoc KRDROPS::pfnMap */
     525static int      krdrFileMap(PKRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fFixed)
     526{
     527    PKRDRFILE       pRdrFile = (PKRDRFILE)pRdr;
     528    PKRDRFILEPREP   pPrep = &pRdrFile->aPreps[pRdrFile->cPreps];
     529    KLDRSIZE        cbTotal;
     530    const KSIZE     cbPage = pRdr->pOps->pfnPageSize(pRdr);
     531    int             rc;
     532    KU32            i;
     533
     534    if (pRdrFile->cPreps >= K_ELEMENTS(pRdrFile->aPreps))
     535        return KRDR_ERR_TOO_MANY_MAPPINGS;
    548536
    549537    /*
     
    560548            cbTotal = uRVASegmentEnd;
    561549    }
    562     pPrep->cb = (size_t)cbTotal;
     550    pPrep->cb = (KSIZE)cbTotal;
    563551    if (pPrep->cb != cbTotal)
    564552        return KLDR_ERR_ADDRESS_OVERFLOW;
    565553    pPrep->cb = (pPrep->cb + (cbPage - 1)) & ~(cbPage- 1);
    566554
    567 #if defined(__WIN__) || defined(__NT__)
     555#if K_OS == K_OS_WINDOWS
    568556    /*
    569557     * The NT memory mapped file API sucks in a lot of ways. Unless you're actually
     
    646634        {
    647635            pv = NULL;
    648             ViewSize = (size_t)cbTotal;
     636            ViewSize = (KSIZE)cbTotal;
    649637
    650638            Status = NtAllocateVirtualMemory(NtCurrentProcess(),
     
    682670                SectionOffset.QuadPart = paSegments[i].offFile;
    683671                ViewSize = paSegments[i].cbFile;
    684                 fPageProt = kldrRdrFileGetNtMapProt(paSegments[i].enmProt);
     672                fPageProt = krdrFileGetNtMapProt(paSegments[i].enmProt);
    685673                /* STATUS_MAPPED_ALIGNMENT
    686674                   STATUS_CONFLICTING_ADDRESSES
     
    706694            {
    707695                ViewSize = paSegments[i].cbMapped;
    708                 fPageProt = kldrRdrFileGetNtAllocProt(paSegments[i].enmProt);
     696                fPageProt = krdrFileGetNtAllocProt(paSegments[i].enmProt);
    709697                Status = NtAllocateVirtualMemory(NtCurrentProcess(),
    710698                                                 &pv,
     
    751739     */
    752740    pPrep->pv = fFixed ? *ppvBase : NULL;
    753     rc = kldrRdrFileGenericMap(pRdr, pPrep, cSegments, paSegments, fFixed);
     741    rc = krdrFileGenericMap(pRdr, pPrep, cSegments, paSegments, fFixed);
    754742    if (!rc)
    755743    {
     
    762750
    763751
    764 /** Generic implementation of kldrRdrFileMap. */
    765 static int  kldrRdrFileGenericMap(PKLDRRDR pRdr, PKLDRRDRFILEPREP pPrep, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fFixed)
     752/** Generic implementation of krdrFileMap. */
     753static int  krdrFileGenericMap(PKRDR pRdr, PKRDRFILEPREP pPrep, KU32 cSegments, PCKLDRSEG paSegments, unsigned fFixed)
    766754{
    767755    int rc;
    768     uint32_t i;
    769 
    770     /*
    771      * Generic mapping code using kldrHlpPageAlloc(), kldrHlpPageFree() and kldrHlpPageProtect().
    772      */
    773     rc = kldrHlpPageAlloc(&pPrep->pv, pPrep->cb, KLDRPROT_EXECUTE_READWRITE, fFixed);
     756    KU32 i;
     757
     758    /*
     759     * Generic mapping code using kHlpPageAlloc(), kHlpPageFree() and kHlpPageProtect().
     760     */
     761    rc = kHlpPageAlloc(&pPrep->pv, pPrep->cb, KLDRPROT_EXECUTE_READWRITE, fFixed);
    774762    if (rc)
    775763        return rc;
     
    797785    if (!rc)
    798786    {
    799         rc = kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 0 /* protect */);
     787        rc = krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 0 /* protect */);
    800788        if (!rc)
    801789            return 0;
    802         kldrRdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 1 /* unprotect */);
     790        krdrFileGenericProtect(pRdr, pPrep, cSegments, paSegments, 1 /* unprotect */);
    803791    }
    804792
    805793    /* bailout */
    806     kldrHlpPageFree(pPrep->pv, pPrep->cb);
     794    kHlpPageFree(pPrep->pv, pPrep->cb);
    807795    return rc;
    808796}
    809797
    810798
    811 /** @copydoc KLDRRDR::pfnPageSize */
    812 static size_t   kldrRdrFilePageSize(PKLDRRDR pRdr)
    813 {
    814 #ifdef __OS2__
     799/** @copydoc KRDROPS::pfnPageSize */
     800static KSIZE   krdrFilePageSize(PKRDR pRdr)
     801{
     802#if K_OS == K_OS_OS2
    815803    /* The page size on OS/2 wont change anytime soon. :-) */
    816804    return 0x1000;
     
    827815
    828816
    829 /** @copydoc KLDRRDR::pfnName */
    830 static const char *kldrRdrFileName(PKLDRRDR pRdr)
    831 {
    832     PKLDRRDRFILE pRdrFile = (PKLDRRDRFILE)pRdr;
     817/** @copydoc KRDROPS::pfnName */
     818static const char *krdrFileName(PKRDR pRdr)
     819{
     820    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
    833821    return &pRdrFile->szFilename[0];
    834822}
    835823
    836824
    837 /** @copydoc KLDRRDR::pfnTell */
    838 static KLDRFOFF kldrRdrFileTell(PKLDRRDR pRdr)
    839 {
    840     PKLDRRDRFILE pRdrFile = (PKLDRRDRFILE)pRdr;
     825static KIPTR krdrFileNativeFH(PKRDR pRdr)
     826{
     827    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
     828#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
     829    return (KIPTR)pRdrFile->File;
     830#else
     831# error "port me"
     832#endif
     833}
     834
     835
     836/** @copydoc KRDROPS::pfnTell */
     837static KFOFF krdrFileTell(PKRDR pRdr)
     838{
     839    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
    841840
    842841    /*
     
    845844    if (pRdrFile->off == -1)
    846845    {
    847 #ifdef __OS2__
     846#if K_OS == K_OS_OS2
    848847        ULONG ulNew;
    849848        APIRET rc = DosSetFilePtr(pRdrFile->File, 0, FILE_CURRENT, &ulNew);
     
    862861        if (rc)
    863862            return -1;
    864         pRdrFile->off = ((KLDRFOFF)offHigh << 32) | offLow;
     863        pRdrFile->off = ((KFOFF)offHigh << 32) | offLow;
    865864
    866865#else
     
    872871
    873872
    874 /** @copydoc KLDRRDR::pfnSize */
    875 static KLDRFOFF kldrRdrFileSize(PKLDRRDR pRdr)
    876 {
    877     PKLDRRDRFILE pRdrFile = (PKLDRRDRFILE)pRdr;
     873/** @copydoc KRDROPS::pfnSize */
     874static KFOFF krdrFileSize(PKRDR pRdr)
     875{
     876    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
    878877    return pRdrFile->cb;
    879878}
    880879
    881880
    882 /** @copydoc KLDRRDR::pfnAllUnmap */
    883 static int kldrRdrFileAllUnmap(PKLDRRDR pRdr, const void *pvBits)
    884 {
    885     PKLDRRDRFILE pRdrFile = (PKLDRRDRFILE)pRdr;
     881/** @copydoc KRDROPS::pfnAllUnmap */
     882static int krdrFileAllUnmap(PKRDR pRdr, const void *pvBits)
     883{
     884    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
    886885
    887886    /* check for underflow */
     
    896895    if (!--pRdrFile->cMappings)
    897896    {
    898         kldrHlpFree(pRdrFile->pvMapping);
     897        kHlpFree(pRdrFile->pvMapping);
    899898        pRdrFile->pvMapping = NULL;
    900899    }
     
    904903
    905904
    906 /** @copydoc KLDRRDR::pfnAllMap */
    907 static int kldrRdrFileAllMap(PKLDRRDR pRdr, const void **ppvBits)
    908 {
    909     PKLDRRDRFILE pRdrFile = (PKLDRRDRFILE)pRdr;
     905/** @copydoc KRDROPS::pfnAllMap */
     906static int krdrFileAllMap(PKRDR pRdr, const void **ppvBits)
     907{
     908    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
    910909
    911910    /*
     
    915914    {
    916915        int rc;
    917         KLDRFOFF cb = pRdrFile->Core.pOps->pfnSize(pRdr);
    918 
    919         pRdrFile->pvMapping = kldrHlpAlloc(cb);
     916        KFOFF cb = pRdrFile->Core.pOps->pfnSize(pRdr);
     917
     918        pRdrFile->pvMapping = kHlpAlloc(cb);
    920919        if (!pRdrFile->pvMapping)
    921920#if defined(__OS2__) || defined(__WIN__)
     
    927926        if (rc)
    928927        {
    929             kldrHlpFree(pRdrFile->pvMapping);
     928            kHlpFree(pRdrFile->pvMapping);
    930929            pRdrFile->pvMapping = NULL;
    931930            return rc;
     
    940939
    941940
    942 /** @copydoc KLDRRDR::pfnRead */
    943 static int kldrRdrFileRead(PKLDRRDR pRdr, void *pvBuf, size_t cb, KLDRFOFF off)
    944 {
    945     PKLDRRDRFILE pRdrFile = (PKLDRRDRFILE)pRdr;
     941/** @copydoc KRDROPS::pfnRead */
     942static int krdrFileRead(PKRDR pRdr, void *pvBuf, KSIZE cb, KFOFF off)
     943{
     944    PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
    946945
    947946    /*
     
    950949    if (pRdrFile->off != off)
    951950    {
    952 #ifdef __OS2__
     951#if K_OS == K_OS_OS2
    953952        ULONG ulNew;
    954953        APIRET rc;
     
    965964        LONG offLow;
    966965
    967         offHigh = sizeof(KLDRFOFF) == 4 ? 0 : (off >> 32);
     966        offHigh = sizeof(KFOFF) == 4 ? 0 : (off >> 32);
    968967        offLow = SetFilePointer(pRdrFile->File, (LONG)off, &offHigh, FILE_BEGIN);
    969968        if (    offLow != (LONG)off
    970             ||  offHigh != (LONG)(sizeof(KLDRFOFF) == 4 ? 0 : (off >> 32)))
     969            ||  offHigh != (LONG)(sizeof(KFOFF) == 4 ? 0 : (off >> 32)))
    971970        {
    972971            int rc = GetLastError();
     
    985984     * Do the read.
    986985     */
    987 #ifdef __OS2__
     986#if K_OS == K_OS_OS2
    988987    {
    989988    ULONG cbRead = 0;
     
    10281027
    10291028
    1030 /** @copydoc KLDRRDR::pfnDestroy */
    1031 static int kldrRdrFileDestroy(PKLDRRDR pRdr)
    1032 {
    1033     PKLDRRDRFILE    pRdrFile = (PKLDRRDRFILE)pRdr;
     1029/** @copydoc KRDROPS::pfnDestroy */
     1030static int krdrFileDestroy(PKRDR pRdr)
     1031{
     1032    PKRDRFILE    pRdrFile = (PKRDRFILE)pRdr;
    10341033    int             rc;
    1035 #ifdef __OS2__
     1034#if K_OS == K_OS_OS2
    10361035    rc = DosClose(pRdrFile->File);
    10371036
     
    10471046    if (pRdrFile->pvMapping)
    10481047    {
    1049         kldrHlpFree(pRdrFile->pvMapping);
     1048        kHlpFree(pRdrFile->pvMapping);
    10501049        pRdrFile->pvMapping = NULL;
    10511050    }
    10521051
    1053     kldrHlpFree(pRdr);
     1052    kHlpFree(pRdr);
    10541053    return rc;
    10551054}
    10561055
    10571056
    1058 /** @copydoc KLDRRDROPS::pfnCreate */
    1059 static int kldrRdrFileCreate(PPKLDRRDR ppRdr, const char *pszFilename)
    1060 {
    1061     size_t          cchFilename;
    1062     PKLDRRDRFILE    pRdrFile;
     1057/** @copydoc KRDROPS::pfnCreate */
     1058static int krdrFileCreate(PPKRDR ppRdr, const char *pszFilename)
     1059{
     1060    KSIZE       cchFilename;
     1061    PKRDRFILE   pRdrFile;
    10631062
    10641063    /*
    10651064     * Open the file and determin its size.
    10661065     */
    1067 #ifdef __OS2__
     1066#if K_OS == K_OS_OS2
    10681067    ULONG           ulAction = 0;
    10691068    FILESTATUS3     Info;
    10701069    APIRET          rc;
    10711070    HFILE           File = 0;
    1072     KLDRFOFF        cb;
     1071    KFOFF           cb;
    10731072    char            szFilename[CCHMAXPATH];
    10741073
     
    10761075    {
    10771076        char *psz;
    1078         cchFilename = kLdrHlpStrLen(szFilename);
    1079         psz = (char *)kLdrHlpAllocA(cchFilename + 1);
    1080         kLdrHlpMemCopy(psz, pszFilename, cchFilename + 1);
     1077        cchFilename = kHlpStrLen(szFilename);
     1078        psz = (char *)kHlpAllocA(cchFilename + 1);
     1079        kHlpMemCopy(psz, pszFilename, cchFilename + 1);
    10811080        pszFilename = psz;
    10821081    }
     
    11091108    int                 rc;
    11101109    HANDLE              File;
    1111     KLDRFOFF            cb;
     1110    KFOFF            cb;
    11121111    char                szFilename[MAX_PATH];
    11131112
     
    11341133        return rc;
    11351134    }
    1136     if (sizeof(KLDRFOFF) == 4)
     1135    if (sizeof(KFOFF) == 4)
    11371136        cb = High ? 0x7fffffff : Low;
    11381137    else
    1139         cb = ((KLDRFOFF)High << 32) | Low;
     1138        cb = ((KFOFF)High << 32) | Low;
    11401139
    11411140#else
     
    11471146     * Allocate the reader instance.
    11481147     */
    1149     cchFilename = kLdrHlpStrLen(szFilename);
    1150     pRdrFile = (PKLDRRDRFILE)kldrHlpAlloc(sizeof(*pRdrFile) + cchFilename);
     1148    cchFilename = kHlpStrLen(szFilename);
     1149    pRdrFile = (PKRDRFILE)kHlpAlloc(sizeof(*pRdrFile) + cchFilename);
    11511150    if (!pRdrFile)
    11521151#if defined(__OS2__)
     
    11681167     */
    11691168    pRdrFile->Core.u32Magic = KLDRRDR_MAGIC;
    1170     pRdrFile->Core.pOps = &g_kLdrRdrFileOps;
     1169    pRdrFile->Core.pOps = &g_kRdrFileOps;
    11711170    pRdrFile->File = File;
    11721171    pRdrFile->cb = cb;
     
    11741173    pRdrFile->cMappings = 0;
    11751174    pRdrFile->cPreps = 0;
    1176     kLdrHlpMemCopy(&pRdrFile->szFilename[0], szFilename, cchFilename + 1);
     1175    kHlpMemCopy(&pRdrFile->szFilename[0], szFilename, cchFilename + 1);
    11771176
    11781177    *ppRdr = &pRdrFile->Core;
  • trunk/kStuff/kRdr/kRdrInternal.h

    r3543 r3544  
    3030#include <k/kHlpAssert.h>
    3131#include <k/kMagics.h>
     32#include <k/kRdrAll.h>
     33#include <k/kErrors.h>
    3234
    3335#ifdef __cplusplus
Note: See TracChangeset for help on using the changeset viewer.