1 /*---------------------------------------------------------------------------*
2   Project:  utilities for Revolution archiver
3   File:     utils.c
4 
5   Copyright (C) 2001-2006 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: utils.c,v $
14   Revision 1.1  2006/04/20 01:41:22  hiratsu
15   Initial check-in.
16 
17 
18     1     7/02/01 11:34p Hashida
19     Initial revision.
20 
21   $NoKeywords: $
22  *---------------------------------------------------------------------------*/
23 
24 #include "darch.h"
25 
26 
27  /*---------------------------------------------------------------------------*
28     Name:               CopyUtility
29 
30     Description:        Copy part of a file to other file
31 
32     Arguments:          srcfid              file descriptor of source file
33                         srcoff              offset in the source file
34                         dstfid              file descriptor of destination file
35                         dstoff              offset in the destination file
36                         size                copy size
37 
38     Returns:            none
39  *---------------------------------------------------------------------------*/
CopyUtility(int srcfid,int srcoff,int dstfid,int dstoff,int size)40 void CopyUtility(int srcfid, int srcoff, int dstfid, int dstoff, int size)
41 {
42     void*                   buffer;
43     long int                copiedSize;
44     long int                copySize;
45 
46     buffer = BigBuffer;
47 
48     // clear buffer
49     memset(BigBuffer, 0, ALLOC_MEMSIZE);
50 
51     // perform seek for the destination and source
52     if ( -1 == lseek(srcfid, srcoff, SEEK_SET) )
53     {
54         fprintf(stderr, "%s: Error occurred when issuing seek to the input file\n",
55                 progName);
56         exit(1);
57     }
58 
59     if ( -1 == lseek(dstfid, dstoff, SEEK_SET) )
60     {
61         fprintf(stderr, "%s: Error occurred when issuing seek to the output file\n",
62                 progName);
63         exit(1);
64     }
65 
66     copiedSize = 0;
67 
68     while (copiedSize < size)
69     {
70         copySize = MIN(size - copiedSize, ALLOC_MEMSIZE);
71 
72         // Read from the source
73         if ( copySize != read(srcfid, buffer, copySize) )
74         {
75             fprintf(stderr, "%s: Error occurred when reading file\n",
76                     progName);
77             perror("read");
78             exit(1);
79         }
80 
81         // Write to the destination
82         if ( copySize != write(dstfid, buffer, copySize) )
83         {
84             fprintf(stderr, "%s: Error occurred when writing the output file\n",
85                     progName);
86             perror("write");
87             exit(1);
88         }
89 
90         copiedSize += copySize;
91     }
92 }
93 
94 
95 
96  /*---------------------------------------------------------------------------*
97     Name:               DiffUtility
98 
99     Description:        Diff part of a file against part of other file
100 
101     Arguments:          fidA                file descriptor of fileA
102                         Aoff                offset in fileA
103                         fidB                file descriptor of fileB
104                         Boff                offset in fileB
105                         size                diff size
106 
107     Returns:            TRUE if same, FALSE otherwise
108  *---------------------------------------------------------------------------*/
DiffUtility(int fidA,int Aoff,int fidB,int Boff,int size)109 BOOL DiffUtility(int fidA, int Aoff, int fidB, int Boff, int size)
110 {
111     void*                   bufferA;
112     void*                   bufferB;
113     long int                diffedSize;
114     long int                diffSize;
115 
116     // clear buffer
117 //    memset(BigBuffer, 0, ALLOC_MEMSIZE);
118 
119     bufferA = BigBuffer;
120     bufferB = (void*)((u32)BigBuffer + ALLOC_MEMSIZE/2);
121 
122     // perform seek for file A and B
123     if ( -1 == lseek(fidA, Aoff, SEEK_SET) )
124     {
125         fprintf(stderr, "%s: Error occurred when issuing seek to a file\n",
126                 progName);
127         exit(1);
128     }
129 
130     if ( -1 == lseek(fidB, Boff, SEEK_SET) )
131     {
132         fprintf(stderr, "%s: Error occurred when issuing seek to a file\n",
133                 progName);
134         exit(1);
135     }
136 
137     diffedSize = 0;
138 
139     while (diffedSize < size)
140     {
141         diffSize = MIN(size - diffedSize, ALLOC_MEMSIZE/2);
142 
143         // Read from file A
144         if ( diffSize != read(fidA, bufferA, diffSize) )
145         {
146             fprintf(stderr, "%s: Error occurred when reading a file\n",
147                     progName);
148             exit(1);
149         }
150 
151         // Read from file B
152         if ( diffSize != read(fidB, bufferB, diffSize) )
153         {
154             fprintf(stderr, "%s: Error occurred when reading a file\n",
155                     progName);
156             exit(1);
157         }
158 
159         // diff
160         if (memcmp(bufferA, bufferB, diffSize))
161         {
162             free(bufferA);
163             free(bufferB);
164             return FALSE;
165         }
166 
167         diffedSize += diffSize;
168     }
169 
170     return TRUE;
171 }
172 
173