2002-02-02 11:48:54 +00:00
|
|
|
#import "HexEditorDelegate.h"
|
2002-02-14 23:24:53 +00:00
|
|
|
#import "HexWindowController.h"
|
2002-02-02 11:48:54 +00:00
|
|
|
|
2002-02-07 03:45:43 +00:00
|
|
|
/* Ideas, method names, and occasionally code stolen from HexEditor by Raphael Sebbe http://raphaelsebbe.multimania.com/ */
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
@implementation HexEditorDelegate
|
|
|
|
|
2002-04-30 23:44:23 +00:00
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
2002-06-04 13:57:55 +00:00
|
|
|
if( !self ) return nil;
|
|
|
|
|
|
|
|
editedLow = NO;
|
2002-04-30 23:44:23 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
/* data re-representation methods */
|
|
|
|
|
|
|
|
- (NSString *)offsetRepresentation:(NSData *)data;
|
|
|
|
{
|
2002-11-13 03:35:54 +00:00
|
|
|
int dataLength = [data length], bytesPerRow = [controller bytesPerRow];
|
2002-03-21 04:02:26 +00:00
|
|
|
int rows = (dataLength / bytesPerRow) + ((dataLength % bytesPerRow)? 1:0);
|
2002-02-02 11:48:54 +00:00
|
|
|
NSMutableString *representation = [NSMutableString string];
|
|
|
|
|
2002-11-13 03:35:54 +00:00
|
|
|
for( int row = 0; row < rows; row++ )
|
2002-03-21 04:02:26 +00:00
|
|
|
[representation appendFormat:@"%08lX:", row * bytesPerRow];
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)hexRepresentation:(NSData *)data;
|
|
|
|
{
|
2002-11-13 03:35:54 +00:00
|
|
|
int currentByte = 0, dataLength = [data length], bytesPerRow = [controller bytesPerRow];
|
2002-03-21 04:02:26 +00:00
|
|
|
int rows = (dataLength / bytesPerRow) + ((dataLength % bytesPerRow)? 1:0);
|
|
|
|
char buffer[bytesPerRow*3 +1], hex1, hex2;
|
2002-02-11 01:22:17 +00:00
|
|
|
char *bytes = (char *) [data bytes];
|
2002-02-02 11:48:54 +00:00
|
|
|
NSMutableString *representation = [NSMutableString string];
|
|
|
|
|
2002-03-31 17:02:40 +00:00
|
|
|
// calculate bytes
|
2002-11-13 03:35:54 +00:00
|
|
|
for( int row = 0; row < rows; row++ )
|
2002-02-02 11:48:54 +00:00
|
|
|
{
|
2002-11-13 03:35:54 +00:00
|
|
|
for( int addr = 0; addr < bytesPerRow; addr++ )
|
2002-02-02 11:48:54 +00:00
|
|
|
{
|
|
|
|
if( currentByte < dataLength )
|
|
|
|
{
|
2002-02-11 01:22:17 +00:00
|
|
|
hex1 = bytes[currentByte];
|
|
|
|
hex2 = bytes[currentByte];
|
2002-02-02 11:48:54 +00:00
|
|
|
hex1 >>= 4;
|
|
|
|
hex1 &= 0x0F;
|
|
|
|
hex2 &= 0x0F;
|
|
|
|
hex1 += (hex1 < 10)? 0x30 : 0x37;
|
|
|
|
hex2 += (hex2 < 10)? 0x30 : 0x37;
|
|
|
|
|
|
|
|
buffer[addr*3] = hex1;
|
|
|
|
buffer[addr*3 +1] = hex2;
|
|
|
|
buffer[addr*3 +2] = 0x20;
|
|
|
|
|
|
|
|
// advance current byte
|
|
|
|
currentByte++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[addr*3] = 0x00;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear last byte on line
|
2002-03-21 04:02:26 +00:00
|
|
|
buffer[bytesPerRow*3] = 0x00;
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
// append buffer to representation
|
|
|
|
[representation appendString:[NSString stringWithCString:buffer]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)asciiRepresentation:(NSData *)data;
|
|
|
|
{
|
2002-11-13 03:35:54 +00:00
|
|
|
int currentByte = 0, dataLength = [data length], bytesPerRow = [controller bytesPerRow];
|
2002-03-21 04:02:26 +00:00
|
|
|
int rows = (dataLength / bytesPerRow) + ((dataLength % bytesPerRow)? 1:0);
|
|
|
|
char buffer[bytesPerRow +1];
|
2002-02-11 01:22:17 +00:00
|
|
|
char *bytes = (char *) [data bytes];
|
2002-02-02 11:48:54 +00:00
|
|
|
NSMutableString *representation = [NSMutableString string];
|
|
|
|
|
2002-03-31 17:02:40 +00:00
|
|
|
// calculate bytes
|
2002-11-13 03:35:54 +00:00
|
|
|
for( int row = 0; row < rows; row++ )
|
2002-02-02 11:48:54 +00:00
|
|
|
{
|
2002-11-13 03:35:54 +00:00
|
|
|
for( int addr = 0; addr < bytesPerRow; addr++ )
|
2002-02-02 11:48:54 +00:00
|
|
|
{
|
|
|
|
if( currentByte < dataLength )
|
|
|
|
{
|
2002-03-31 17:02:40 +00:00
|
|
|
if( bytes[currentByte] > 0x20 && bytes[currentByte] < 0x7F )
|
2002-02-11 01:22:17 +00:00
|
|
|
buffer[addr] = bytes[currentByte];
|
2002-03-31 17:02:40 +00:00
|
|
|
else if( bytes[currentByte] == 0x20 )
|
|
|
|
buffer[addr] = 0xCA; // nbsp to stop maligned wraps
|
2002-02-02 11:48:54 +00:00
|
|
|
else buffer[addr] = 0x2E; // full stop
|
|
|
|
|
|
|
|
// advance current byte
|
|
|
|
currentByte++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[addr] = 0x00;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear last byte on line
|
2002-03-21 04:02:26 +00:00
|
|
|
buffer[bytesPerRow] = 0x00;
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
// append buffer to representation
|
|
|
|
[representation appendString:[NSString stringWithCString:buffer]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delegation methods */
|
|
|
|
|
|
|
|
- (NSRange)textView:(NSTextView *)textView willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange toCharacterRange:(NSRange)newSelectedCharRange
|
|
|
|
{
|
2002-02-06 20:57:56 +00:00
|
|
|
NSRange hexRange, asciiRange, byteRange = NSMakeRange(0,0);
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
// temporarilly removing the delegate stops this function being called recursivly!
|
|
|
|
id oldDelegate = [hex delegate];
|
|
|
|
[hex setDelegate:nil];
|
|
|
|
[ascii setDelegate:nil];
|
|
|
|
|
|
|
|
if( textView == hex ) // we're selecting hexadecimal
|
|
|
|
{
|
2002-02-06 20:57:56 +00:00
|
|
|
byteRange = [self byteRangeFromHexRange:newSelectedCharRange];
|
|
|
|
asciiRange = [self asciiRangeFromByteRange:byteRange];
|
2002-02-02 11:48:54 +00:00
|
|
|
[ascii setSelectedRange:asciiRange];
|
|
|
|
}
|
|
|
|
else if( textView == ascii ) // we're selecting ASCII
|
|
|
|
{
|
2002-02-06 20:57:56 +00:00
|
|
|
byteRange = [self byteRangeFromAsciiRange:newSelectedCharRange];
|
|
|
|
hexRange = [self hexRangeFromByteRange:byteRange];
|
2002-03-31 17:02:40 +00:00
|
|
|
if( hexRange.length > 0 )
|
|
|
|
hexRange.length -= 1;
|
2002-02-02 11:48:54 +00:00
|
|
|
[hex setSelectedRange:hexRange];
|
|
|
|
}
|
2002-02-06 20:57:56 +00:00
|
|
|
|
|
|
|
// put the new selection into the message bar
|
|
|
|
[message setStringValue:[NSString stringWithFormat:@"Current selection: %@", NSStringFromRange(byteRange)]];
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
// restore delegates
|
|
|
|
[hex setDelegate:oldDelegate];
|
|
|
|
[ascii setDelegate:oldDelegate];
|
|
|
|
return newSelectedCharRange;
|
|
|
|
}
|
2002-02-06 20:57:56 +00:00
|
|
|
|
2002-02-14 23:24:53 +00:00
|
|
|
/* range conversion methods */
|
|
|
|
|
2002-02-06 20:57:56 +00:00
|
|
|
- (NSRange)byteRangeFromHexRange:(NSRange)hexRange;
|
|
|
|
{
|
|
|
|
// valid for all window widths
|
|
|
|
NSRange byteRange = NSMakeRange(0,0);
|
|
|
|
byteRange.location = (hexRange.location / 3);
|
|
|
|
byteRange.length = (hexRange.length / 3) + ((hexRange.length % 3)? 1:0);
|
|
|
|
return byteRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange)hexRangeFromByteRange:(NSRange)byteRange;
|
|
|
|
{
|
2002-03-21 04:02:26 +00:00
|
|
|
// valid for all window widths
|
2002-02-06 20:57:56 +00:00
|
|
|
NSRange hexRange = NSMakeRange(0,0);
|
|
|
|
hexRange.location = (byteRange.location * 3);
|
|
|
|
hexRange.length = (byteRange.length * 3);
|
|
|
|
return hexRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange)byteRangeFromAsciiRange:(NSRange)asciiRange;
|
|
|
|
{
|
2002-03-21 04:02:26 +00:00
|
|
|
// one-to-one mapping
|
|
|
|
return asciiRange;
|
2002-02-06 20:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange)asciiRangeFromByteRange:(NSRange)byteRange;
|
|
|
|
{
|
2002-03-21 04:02:26 +00:00
|
|
|
// one-to-one mapping
|
|
|
|
return byteRange;
|
2002-02-06 20:57:56 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
- (NSTextView *)hex
|
|
|
|
{
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTextView *)ascii
|
|
|
|
{
|
|
|
|
return ascii;
|
|
|
|
}
|
|
|
|
|
2002-04-30 23:44:23 +00:00
|
|
|
- (BOOL)editedLow
|
|
|
|
{
|
|
|
|
return editedLow;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setEditedLow:(BOOL)flag
|
|
|
|
{
|
|
|
|
editedLow = flag;
|
|
|
|
}
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
@end
|