Re: Strange malloc/free issue inside a category
Re: Strange malloc/free issue inside a category
- Subject: Re: Strange malloc/free issue inside a category
- From: Agent M <email@hidden>
- Date: Sun, 30 Jul 2006 21:01:35 -0400
The other helpful folks identified your bug, but in case you are
looking for some pretty-printing hex dump, here is some free code:
@implementation NSData (HexDump)
-(NSString*)hexdump
{
/* dumps size bytes of *data to stdout. Looks like:
* [0000] 75 6E 6B 6E 6F 77 6E 20
* 30 FF 00 00 00 00 39 00 unknown 0.....9.
* (in a single line of course)
*/
NSMutableString *ret;
const unsigned char *data=[self bytes];
const unsigned char *p = [self bytes];
size_t size=[self length];
unsigned char c;
int n;
char bytestr[4] = {0};
char addrstr[10] = {0};
char hexstr[ 16*3 + 5] = {0};
char charstr[16*1 + 5] = {0};
ret=[NSMutableString string];
for(n=1;n<=size;n++) {
if (n == 1) {
/* store address for this line */
snprintf(addrstr, sizeof(addrstr), "%.4x",
((unsigned int)p-(unsigned int)data) );
}
c = *p;
if (isalnum(c) == 0) {
c = '.';
}
/* store hex str (for left side) */
snprintf(bytestr, sizeof(bytestr), "X ", *p);
strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
/* store char str (for right side) */
snprintf(bytestr, sizeof(bytestr), "%c", c);
strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
if(n == 0) {
/* line completed */
[ret appendString:[NSString stringWithFormat:@"[%4.4s] %-50.50s
%s\n", addrstr, hexstr, charstr]];
// printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
hexstr[0] = 0;
charstr[0] = 0;
} else if(n%8 == 0) {
/* half line: add whitespaces */
strncat(hexstr, " ", sizeof(hexstr)-strlen(hexstr)-1);
strncat(charstr, " ", sizeof(charstr)-strlen(charstr)-1);
}
p++; /* next byte */
}
if (strlen(hexstr) > 0) {
/* print rest of buffer if not empty */
[ret appendString:[NSString stringWithFormat:@"[%4.4s] %-50.50s
%s\n", addrstr, hexstr, charstr]];
//printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
}
return ret;
}
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
AgentM
email@hidden
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden