MYASN1Object.m
changeset 28 54b373aa65ab
parent 20 df9da0f6b358
     1.1 --- a/MYASN1Object.m	Fri Jun 05 08:57:18 2009 -0700
     1.2 +++ b/MYASN1Object.m	Fri Aug 07 11:24:53 2009 -0700
     1.3 @@ -113,6 +113,46 @@
     1.4  
     1.5  @implementation MYASN1BigInteger
     1.6  
     1.7 +- (id) initWithSignedData: (NSData*)signedData {
     1.8 +    // Skip unnecessary leading 00 (if positive) or FF (if negative) bytes:
     1.9 +    const SInt8 *start = signedData.bytes, *last = start + signedData.length - 1;
    1.10 +    const SInt8 *pos = start;
    1.11 +    while (pos<last && ((pos[0]==0 && pos[1]>=0) || (pos[0]==-1 && pos[1]<0)))
    1.12 +        pos++;
    1.13 +    if (pos > start)
    1.14 +        signedData = [NSData dataWithBytes: pos length: last-pos+1];
    1.15 +    return [self initWithTag: 2 ofClass: 0 constructed: NO value: signedData];
    1.16 +}
    1.17 +
    1.18 +- (id) initWithUnsignedData: (NSData*) unsignedData {
    1.19 +    const UInt8 *start = unsignedData.bytes;
    1.20 +    if (*start >= 0x80) {
    1.21 +        // Prefix with 00 byte so high bit isn't misinterpreted as a sign bit:
    1.22 +        NSMutableData *fixedData = [NSMutableData dataWithCapacity: unsignedData.length + 1];
    1.23 +        UInt8 zero = 0;
    1.24 +        [fixedData appendBytes: &zero length: 1];
    1.25 +        [fixedData appendData: unsignedData];
    1.26 +        unsignedData = fixedData;
    1.27 +    }
    1.28 +    return [self initWithSignedData: unsignedData];
    1.29 +}
    1.30 +
    1.31 +- (NSData*) signedData {
    1.32 +    return self.value;
    1.33 +}
    1.34 +
    1.35 +- (NSData*) unsignedData {
    1.36 +    // Strip any leading zero bytes that were inserted for sign-bit padding:
    1.37 +    NSData *data = self.value;
    1.38 +    const UInt8 *start = data.bytes, *last = start + data.length - 1;
    1.39 +    const UInt8 *pos = start;
    1.40 +    while (pos<last && *pos==0)
    1.41 +        pos++;
    1.42 +    if (pos > start)
    1.43 +        data = [NSData dataWithBytes: pos length: last-pos+1];
    1.44 +    return data;
    1.45 +}
    1.46 +
    1.47  @end
    1.48  
    1.49  
    1.50 @@ -158,3 +198,27 @@
    1.51  }
    1.52  
    1.53  @end
    1.54 +
    1.55 +
    1.56 +
    1.57 +/*
    1.58 + Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    1.59 + 
    1.60 + Redistribution and use in source and binary forms, with or without modification, are permitted
    1.61 + provided that the following conditions are met:
    1.62 + 
    1.63 + * Redistributions of source code must retain the above copyright notice, this list of conditions
    1.64 + and the following disclaimer.
    1.65 + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    1.66 + and the following disclaimer in the documentation and/or other materials provided with the
    1.67 + distribution.
    1.68 + 
    1.69 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    1.70 + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    1.71 + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    1.72 + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.73 + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    1.74 +  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    1.75 + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    1.76 + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.77 + */