objchg.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 612 行 · 第 1/2 页

C
612
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  tool for changing any item in LNAMES and to modify 
*                 EXTDEF/PUBDEF symbols by name pattern
*
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "pcobj.h"
#include "hashtab.h"
#include "misc.h"

#define MAX_LINE_LEN 512

typedef unsigned char byte;
typedef byte *data_ptr;

static symbol      **pubdef_tab;
static symbol      **extdef_tab;
static data_ptr    NamePtr;
static byte        NameLen;
static unsigned_16 RecLen;
static data_ptr    RecBuff;
static data_ptr    RecPtr;
static unsigned_16 RecMaxLen;
static int         isMS386;
static char        *symbol_name_change = NULL;

static void usage( void )
/***********************/
{
    SymbolFini( extdef_tab );
    SymbolFini( pubdef_tab );
    printf( "Usage: objchg <options> <list of object or library files>\n" );
    printf( "  <options> -l=<old>=<new>  rename LNAMES item\n" );
    printf( "            -m=....         symbol name pattern\n" );
    printf( "            -s=<file>       file with symbols\n" );
    exit( -1 );
}

static int EndRec( void )
/***********************/
{
    return( RecPtr >= (RecBuff + RecLen) );
}

static byte GetByte( void )
/*************************/
{
    byte        ret;

    ret = *RecPtr;
    RecPtr++;
    return( ret );
}

static unsigned_16 GetUInt( void )
/********************************/
{
    unsigned_16 word;

    word = *(unsigned_16 *)RecPtr;
    CONV_LE_16( word );
    RecPtr += 2;
    return( word );
}

static unsigned_32 GetOffset( void )
/**********************************/
{
    if( isMS386 ) {
        unsigned_32 dword;

        dword = *(unsigned_32 *)RecPtr;
        CONV_LE_32( dword );
        RecPtr += 4;
        return( dword );
    } else {
        unsigned_16 word;

        word = *(unsigned_16 *)RecPtr;
        CONV_LE_16( word );
        RecPtr += 2;
        return( word );
    }
}

static unsigned_16 GetIndex( void )
/*********************************/
{
    unsigned_16 index;

    index = GetByte();
    if( index & 0x80 ) {
      index = ( (index & 0x7f) << 8 ) + GetByte();
    }
    return( index );
}

static char *GetName( void )
/**************************/
{
    NameLen = GetByte();
    NamePtr = RecPtr;
    RecPtr += NameLen;
    return( NamePtr );
}

static char *PutUInt( char *p, unsigned_16 data )
/***********************************************/
{
    CONV_LE_16( data );
    *((unsigned_16 *)p) = data;
    p += 2;
    return( p );
}

static char *PutOffset( char *p, unsigned_32 data )
/*************************************************/
{
    if( isMS386 ) {
        CONV_LE_32( data );
        *((unsigned_32 *)p) = data;
        p += 4;
    } else {
        CONV_LE_16( data );
        *((unsigned_16 *)p) = data;
        p += 2;
    }
    return( p );
}

static char *PutIndex( char *p, unsigned_16 index )
/*************************************************/
{
    if( index > 0x7f )
        *(p++) = ( index >> 8 ) | 0x80;
    *(p++) = index;
    return( p );
}

static void ResizeBuff( unsigned_16 reqd_len )
/********************************************/
{
    if( reqd_len > RecMaxLen ) {
        RecMaxLen = reqd_len;
        if( RecBuff != NULL ) {
            free( RecBuff );
        }
        RecBuff = malloc( RecMaxLen );
        if( RecBuff == NULL ) {
            printf( "**FATAL** Out of memory!\n" );
            exit( -1 );
        }
    }
}

static byte create_chksum( char *data, int newlen, byte cksum )
/*************************************************************/
{
    int     i;

    for( i = 0; i < newlen - 1; i++ ) {
        cksum += data[i];
    }
    return( cksum );
}

static char *change_name( char *dst, char *src )
/**********************************************/
{
    int     i;
    char    *p;

    p = dst++;
    if( symbol_name_change != NULL ) {
        for( i = 0; i < strlen( symbol_name_change ); i++ ) {
            if( symbol_name_change[i] == '*' ) {
                int     len;

                len = strlen( src );
                memcpy( dst, src, len );
                dst += len;
            } else {
                *(dst++) = symbol_name_change[i];
            }
        }
    }
    *p = dst - p - 1;
    return( dst );
}

static int ChangeLNAMES( byte rec_type, FILE *fo, unsigned_16 newlen )
/********************************************************************/
{
    byte        hdr[ 3 ];
    char        *data;
    char        *p;
    byte        cksum;

    hdr[ 0 ] = rec_type;
    hdr[ 1 ] = newlen;
    hdr[ 2 ] = newlen >> 8;
    if( fwrite( hdr, 1, 3, fo ) != 3 )
        return( 0 );
    cksum  = hdr[ 0 ];
    cksum += hdr[ 1 ];
    cksum += hdr[ 2 ];
    data = malloc( newlen );
    p = data;
    while( ! EndRec() ) {
        char     *n;
        byte     b;

        GetName();
        b = *RecPtr;
        *RecPtr = 0;
        n = SymbolExists( extdef_tab, NamePtr );
        if( n != NULL ) {
            NameLen = strlen( n );
            NamePtr = n;
        }
        *(p++) = NameLen;
        memcpy( p, NamePtr, NameLen );
        p += NameLen;
        *RecPtr = b;
    }
    *(p++) = 0 - create_chksum( data, newlen, cksum );
    if( fwrite( data, 1, newlen, fo ) != newlen ) {
        free( data );
        return( 0 );
    }
    free( data );
    return( 1 );
}

static int ChangeEXTDEF( byte rec_type, FILE *fo, unsigned_16 newlen )
/********************************************************************/
{
    byte        hdr[ 3 ];
    char        *data;
    char        *p;
    char        *tmp;
    long        lng1;
    byte        cksum;

    hdr[ 0 ] = rec_type;
    hdr[ 1 ] = newlen;
    hdr[ 2 ] = newlen >> 8;
    if( fwrite( hdr, 1, 3, fo ) != 3 )
        return( 0 );
    cksum  = hdr[ 0 ];
    cksum += hdr[ 1 ];
    cksum += hdr[ 2 ];
    data = malloc( newlen );
    p = data;
    while( ! EndRec() ) {
        GetName();
        tmp = RecPtr;
        lng1 = GetIndex();
        *tmp = 0;
        if( SymbolExists( pubdef_tab, NamePtr ) != NULL ) {
            p = change_name( p, NamePtr );
        } else {
            *(p++) = NameLen;
            memcpy( p, NamePtr, NameLen );
            p += NameLen;
        }
        p = PutIndex( p, lng1 );
    }
    *(p++) = 0 - create_chksum( data, newlen, cksum );
    if( fwrite( data, 1, newlen, fo ) != newlen ) {
        free( data );
        return( 0 );
    }
    free( data );
    return( 1 );
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?