@@ -124,7 +124,66 @@ public int searchForDataRegEx(String search) throws IOException {
124124 // Return -1 if no match
125125 return -1 ;
126126 }
127-
127+
128+ /*
129+ * A function to search for an address in a hex file
130+ * @param search the address to search for
131+ * @return the index of the address. -1 if not found.
132+ */
133+ public int searchForAddress ( long address ) throws IOException {
134+ long lastBaseAddr = 0 ;
135+ String data ;
136+ // Iterate through
137+ ListIterator i = hexLines .listIterator ();
138+ while ( i .hasNext ()) {
139+ // Have to call nextIndex() before next()
140+ int index = i .nextIndex ();
141+ String line = i .next ().toString ();
142+
143+ switch (getRecordType (line )) {
144+ case 2 : { // Extended Segment Address
145+ data = getRecordData (line );
146+ if ( data .length () != 4 ) {
147+ return -1 ;
148+ }
149+ int hi = Integer .parseInt ( data .substring (0 , 1 ), 16 );
150+ int lo = Integer .parseInt ( data .substring (1 ), 16 );
151+ lastBaseAddr = (long ) hi * (long ) 0x1000 + (long ) lo * (long ) 0x10 ;
152+ if ( lastBaseAddr > address ) {
153+ return -1 ;
154+ }
155+ break ;
156+ }
157+ case 4 : {
158+ data = getRecordData (line );
159+ if ( data .length () != 4 ) {
160+ return -1 ;
161+ }
162+ lastBaseAddr = Integer .parseInt ( data , 16 );
163+ lastBaseAddr *= (long ) 0x10000 ;
164+ if ( lastBaseAddr > address ) {
165+ return -1 ;
166+ }
167+ break ;
168+ }
169+ case 0 :
170+ case 0x0D : {
171+ if ( address - lastBaseAddr < 0x10000 ) {
172+ long a = lastBaseAddr + getRecordAddress (line );
173+ int n = getRecordDataLength ( line ) / 2 ; // bytes
174+ if ( a <= address && a + n > address ) {
175+ return index ;
176+ }
177+ }
178+ break ;
179+ }
180+ }
181+ }
182+
183+ // Return -1 if no match
184+ return -1 ;
185+ }
186+
128187 /*
129188 * Returns data from an index
130189 * @param index
@@ -267,6 +326,5 @@ public static byte[] recordToByteArray(String hexString, int offset, int packetN
267326
268327 return data ;
269328 }
270-
271329}
272330
0 commit comments