aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSString+XML.m
blob: 0e16ddba34089c824e11db47476467aec45984fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
//  GTMNSString+XML.m
//
//  Copyright 2007-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMDefines.h"
#import "GTMNSString+XML.h"
#import "GTMGarbageCollection.h"

enum {
  kGTMXMLCharModeEncodeQUOT  = 0,
  kGTMXMLCharModeEncodeAMP   = 1,
  kGTMXMLCharModeEncodeAPOS  = 2,
  kGTMXMLCharModeEncodeLT    = 3,
  kGTMXMLCharModeEncodeGT    = 4,
  kGTMXMLCharModeValid       = 99,
  kGTMXMLCharModeInvalid     = 100,
};
typedef NSUInteger GTMXMLCharMode;

static NSString *gXMLEntityList[] = {
  // this must match the above order
  @""",
  @"&",
  @"'",
  @"<",
  @">",
};

GTM_INLINE GTMXMLCharMode XMLModeForUnichar(UniChar c) {

  // Per XML spec Section 2.2 Characters
  //   ( http://www.w3.org/TR/REC-xml/#charsets )
  //
  //   Char    ::=       #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
  //                      [#x10000-#x10FFFF]

  if (c <= 0xd7ff)  {
    if (c >= 0x20) {
      switch (c) {
        case 34:
          return kGTMXMLCharModeEncodeQUOT;
        case 38:
          return kGTMXMLCharModeEncodeAMP;
        case 39:
          return kGTMXMLCharModeEncodeAPOS;
        case 60:
          return kGTMXMLCharModeEncodeLT;
        case 62:
          return kGTMXMLCharModeEncodeGT;
        default:
          return kGTMXMLCharModeValid;
      }
    } else {
      if (c == '\n')
        return kGTMXMLCharModeValid;
      if (c == '\r')
        return kGTMXMLCharModeValid;
      if (c == '\t')
        return kGTMXMLCharModeValid;
      return kGTMXMLCharModeInvalid;
    }
  }

  if (c < 0xE000)
    return kGTMXMLCharModeInvalid;

  if (c <= 0xFFFD)
    return kGTMXMLCharModeValid;

  // UniChar can't have the following values
  // if (c < 0x10000)
  //   return kGTMXMLCharModeInvalid;
  // if (c <= 0x10FFFF)
  //   return kGTMXMLCharModeValid;

  return kGTMXMLCharModeInvalid;
} // XMLModeForUnichar


static NSString *AutoreleasedCloneForXML(NSString *src, BOOL escaping) {
  //
  // NOTE:
  // We don't use CFXMLCreateStringByEscapingEntities because it's busted in
  // 10.3 (http://lists.apple.com/archives/Cocoa-dev/2004/Nov/msg00059.html) and
  // it doesn't do anything about the chars that are actually invalid per the
  // xml spec.
  //
  
  // we can't use the CF call here because it leaves the invalid chars
  // in the string.
  NSUInteger length = [src length];
  if (!length) {
    return src;
  }
  
  NSMutableString *finalString = [NSMutableString string];

  // this block is common between GTMNSString+HTML and GTMNSString+XML but
  // it's so short that it isn't really worth trying to share.
  const UniChar *buffer = CFStringGetCharactersPtr((CFStringRef)src);
  if (!buffer) {
    // We want this buffer to be autoreleased.
    NSMutableData *data = [NSMutableData dataWithLength:length * sizeof(UniChar)];
    if (!data) {
      // COV_NF_START  - Memory fail case
      _GTMDevLog(@"couldn't alloc buffer");
      return nil;
      // COV_NF_END
    }
    [src getCharacters:[data mutableBytes]];
    buffer = [data bytes];
  }
  
  const UniChar *goodRun = buffer;
  NSUInteger goodRunLength = 0;
  
  for (NSUInteger i = 0; i < length; ++i) {
    
    GTMXMLCharMode cMode = XMLModeForUnichar(buffer[i]);
    
    // valid chars go as is, and if we aren't doing entities, then
    // everything goes as is.
    if ((cMode == kGTMXMLCharModeValid) ||
        (!escaping && (cMode != kGTMXMLCharModeInvalid))) {
      // goes as is
      goodRunLength += 1;
    } else {
      // it's something we have to encode or something invalid
      
      // start by adding what we already collected (if anything)
      if (goodRunLength) {
        CFStringAppendCharacters((CFMutableStringRef)finalString, 
                                 goodRun, 
                                 goodRunLength);
        goodRunLength = 0;
      }
      
      // if it wasn't invalid, add the encoded version
      if (cMode != kGTMXMLCharModeInvalid) {
        // add this encoded
        [finalString appendString:gXMLEntityList[cMode]];
      }
      
      // update goodRun to point to the next UniChar
      goodRun = buffer + i + 1;
    }
  }
  
  // anything left to add?
  if (goodRunLength) {
    CFStringAppendCharacters((CFMutableStringRef)finalString, 
                             goodRun, 
                             goodRunLength);
  }
  return finalString;
} // AutoreleasedCloneForXML

@implementation NSString (GTMNSStringXMLAdditions)

- (NSString *)gtm_stringBySanitizingAndEscapingForXML {
  return AutoreleasedCloneForXML(self, YES);
} // gtm_stringBySanitizingAndEscapingForXML

- (NSString *)gtm_stringBySanitizingToXMLSpec {
  return AutoreleasedCloneForXML(self, NO);
} // gtm_stringBySanitizingToXMLSpec

@end