1 | //␊ |
2 | // SmbiosExtractor.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 11-11-27.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "SmbiosExtractor.h"␊ |
9 | #include "ShellProcess.h"␊ |
10 | #include "string_util.h"␊ |
11 | #include "smbios.h"␊ |
12 | ␊ |
13 | @implementation SmbiosExtractor␊ |
14 | ␊ |
15 | @synthesize smString;␊ |
16 | @synthesize bufSize;␊ |
17 | -(id) init␊ |
18 | {␊ |
19 | ␉self = [super init];␊ |
20 | ␉if (self!=nil)␊ |
21 | ␉{␊ |
22 | ␉␉buffer = nil;␊ |
23 | ␉␉bufSize=0;␊ |
24 | ␉␉[ self extractSmBios];␊ |
25 | ␉}␊ |
26 | ␉␊ |
27 | ␉return self;␊ |
28 | }␊ |
29 | ␊ |
30 | -(void) cleanup␊ |
31 | {␊ |
32 | ␉if (buffer!=nil)␊ |
33 | ␉{␊ |
34 | ␉␉delete [] buffer;␊ |
35 | ␉␉buffer = nil;␊ |
36 | ␉␉bufSize=0;␊ |
37 | ␉␉smString = nil;␊ |
38 | ␉}␊ |
39 | ␉␊ |
40 | ␉if (smString!=nil)␊ |
41 | ␉{␊ |
42 | ␉␉[smString release];␊ |
43 | ␉␉smString = nil;␊ |
44 | ␉}␊ |
45 | ␉␊ |
46 | }␊ |
47 | ␊ |
48 | -(void) dealloc␊ |
49 | {␊ |
50 | ␉[self cleanup ];␊ |
51 | ␉[super dealloc];␊ |
52 | }␊ |
53 | ␊ |
54 | -(NSString*) description␊ |
55 | {␉␊ |
56 | ␉return smString;␊ |
57 | }␊ |
58 | ␊ |
59 | -(NSUInteger) extractSmBios␊ |
60 | {␊ |
61 | ␉␊ |
62 | ␉const char* SmbiosCmd = "ioreg -lw0 | grep \"\\\"SMBIOS\\\" = <\"";␊ |
63 | ␉const char* SmbiosTag = "\"SMBIOS\" = <";␊ |
64 | ␉ShellProcess p(SmbiosCmd);␊ |
65 | ␉char line[8192]="";␊ |
66 | ␉const size_t tagLen= strlen(SmbiosTag);␊ |
67 | ␉␊ |
68 | ␉if (p.get_line(line, sizeof(line)) )␊ |
69 | ␉{␊ |
70 | ␉␉const char * p = strstr(line, SmbiosTag)+ tagLen;␊ |
71 | ␉␉std::string s = trim(p, " \t\r\n>");␊ |
72 | ␉␉NSLog(@"Found SMBIOS (%d) [%s]", s.length(), s.c_str() );␊ |
73 | ␉␉[self cleanup];␊ |
74 | ␉␉self.smString = [NSString stringWithUTF8String:s.c_str()];␊ |
75 | ␉␉bufSize = s.length()/2;␊ |
76 | ␉␉buffer = new UInt8[bufSize];␊ |
77 | ␉␉for(size_t pos =0; pos < bufSize; pos++)␊ |
78 | ␉␉{␊ |
79 | ␉␉␉buffer[pos] = (HexToDec(s[pos*2])*16 + HexToDec(s[pos*2+1]));␊ |
80 | ␉␉}␊ |
81 | ␉␉␊ |
82 | ␉␉decodeSMBIOSTable(buffer, buffer+bufSize);␉␉␉␉␊ |
83 | ␉}␊ |
84 | ␉return 0;␊ |
85 | }␊ |
86 | ␊ |
87 | -(NSString*) stringFrom:(NSUInteger) start withLen:(NSUInteger) len␊ |
88 | {␊ |
89 | ␉return [smString substringWithRange: NSMakeRange(start*2, len) ];␊ |
90 | }␊ |
91 | ␊ |
92 | -(UInt8) ByteFrom:(NSUInteger) position␊ |
93 | {␊ |
94 | ␉return position < bufSize ? buffer[position]: 0;␊ |
95 | }␊ |
96 | ␊ |
97 | @end␊ |
98 | |