/*---------------------------------------------------------------------------* Project: utilities for Revolution archiver File: utils.c Copyright (C) 2001-2006 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: utils.c,v $ Revision 1.1 2006/04/20 01:41:22 hiratsu Initial check-in. 1 7/02/01 11:34p Hashida Initial revision. $NoKeywords: $ *---------------------------------------------------------------------------*/ #include "darch.h" /*---------------------------------------------------------------------------* Name: CopyUtility Description: Copy part of a file to other file Arguments: srcfid file descriptor of source file srcoff offset in the source file dstfid file descriptor of destination file dstoff offset in the destination file size copy size Returns: none *---------------------------------------------------------------------------*/ void CopyUtility(int srcfid, int srcoff, int dstfid, int dstoff, int size) { void* buffer; long int copiedSize; long int copySize; buffer = BigBuffer; // clear buffer memset(BigBuffer, 0, ALLOC_MEMSIZE); // perform seek for the destination and source if ( -1 == lseek(srcfid, srcoff, SEEK_SET) ) { fprintf(stderr, "%s: Error occurred when issuing seek to the input file\n", progName); exit(1); } if ( -1 == lseek(dstfid, dstoff, SEEK_SET) ) { fprintf(stderr, "%s: Error occurred when issuing seek to the output file\n", progName); exit(1); } copiedSize = 0; while (copiedSize < size) { copySize = MIN(size - copiedSize, ALLOC_MEMSIZE); // Read from the source if ( copySize != read(srcfid, buffer, copySize) ) { fprintf(stderr, "%s: Error occurred when reading file\n", progName); perror("read"); exit(1); } // Write to the destination if ( copySize != write(dstfid, buffer, copySize) ) { fprintf(stderr, "%s: Error occurred when writing the output file\n", progName); perror("write"); exit(1); } copiedSize += copySize; } } /*---------------------------------------------------------------------------* Name: DiffUtility Description: Diff part of a file against part of other file Arguments: fidA file descriptor of fileA Aoff offset in fileA fidB file descriptor of fileB Boff offset in fileB size diff size Returns: TRUE if same, FALSE otherwise *---------------------------------------------------------------------------*/ BOOL DiffUtility(int fidA, int Aoff, int fidB, int Boff, int size) { void* bufferA; void* bufferB; long int diffedSize; long int diffSize; // clear buffer // memset(BigBuffer, 0, ALLOC_MEMSIZE); bufferA = BigBuffer; bufferB = (void*)((u32)BigBuffer + ALLOC_MEMSIZE/2); // perform seek for file A and B if ( -1 == lseek(fidA, Aoff, SEEK_SET) ) { fprintf(stderr, "%s: Error occurred when issuing seek to a file\n", progName); exit(1); } if ( -1 == lseek(fidB, Boff, SEEK_SET) ) { fprintf(stderr, "%s: Error occurred when issuing seek to a file\n", progName); exit(1); } diffedSize = 0; while (diffedSize < size) { diffSize = MIN(size - diffedSize, ALLOC_MEMSIZE/2); // Read from file A if ( diffSize != read(fidA, bufferA, diffSize) ) { fprintf(stderr, "%s: Error occurred when reading a file\n", progName); exit(1); } // Read from file B if ( diffSize != read(fidB, bufferB, diffSize) ) { fprintf(stderr, "%s: Error occurred when reading a file\n", progName); exit(1); } // diff if (memcmp(bufferA, bufferB, diffSize)) { free(bufferA); free(bufferB); return FALSE; } diffedSize += diffSize; } return TRUE; }