Giter VIP home page Giter VIP logo

Comments (6)

SrikanthKabadi avatar SrikanthKabadi commented on July 17, 2024

Here is the list of functions which I have modified to make it work for negative integers.

public func readRawLittleEndian32() -> Int32
    {
        var b1:SignedByte = readRawByte()
        var b2:SignedByte = readRawByte()
        var b3:SignedByte = readRawByte()
        var b4:SignedByte = readRawByte()
        var result:Int32 = (Int32(b1) & 0xff)
        result |= ((Int32(b2) & 0xff) <<  8)
        result |= ((Int32(b3) & 0xff) << 16)
        result |= ((Int32(b4) & 0xff) << 24)
        return result
    }
    public  func readRawLittleEndian64() -> Int64
    {
        var b1:SignedByte = readRawByte()
        var b2:SignedByte = readRawByte()
        var b3:SignedByte = readRawByte()
        var b4:SignedByte = readRawByte()
        var b5:SignedByte = readRawByte()
        var b6:SignedByte = readRawByte()
        var b7:SignedByte = readRawByte()
        var b8:SignedByte = readRawByte()
        var result:Int64  = (Int64(b1) & 0xff)
        result |= ((Int64(b2) & 0xff) <<  8)
        result |= ((Int64(b3) & 0xff) << 16)
        result |= ((Int64(b4) & 0xff) << 24)
        result |= ((Int64(b5) & 0xff) << 32)
        result |= ((Int64(b6) & 0xff) << 40)
        result |= ((Int64(b7) & 0xff) << 48)
        result |= ((Int64(b8) & 0xff) << 56)

        return result
    }

public func readRawByte() ->SignedByte
    {
        if (bufferPos == bufferSize)
        {
            refillBuffer(true)
        }
        var pointer = UnsafeMutablePointer<SignedByte>(buffer.mutableBytes)
        var res = pointer[Int(bufferPos++)]
        return res
    }

public func readRawVarint32() -> Int32
    {
        var tmp : SignedByte = readRawByte();
        if (tmp >= 0) {
            return Int32(tmp);
        }
        var result : Int32 = Int32(tmp) & 0x7f;
        tmp = readRawByte()
        if (tmp >= 0) {
            result |= Int32(tmp) << 7;
        } else {
            result |= (Int32(tmp) & 0x7f) << 7;
            tmp = readRawByte()
            if (tmp >= 0) {
                result |= Int32(tmp) << 14;
            } else {
                result |= (Int32(tmp) & 0x7f) << 14;
                tmp = readRawByte()
                if (tmp >= 0) {
                    result |= Int32(tmp) << 21;
                } else {
                    result |= (Int32(tmp) & 0x7f) << 21;
                    tmp = readRawByte()
                    result |= (Int32(tmp) << 28);
                    if (tmp < 0) {
                        // Discard upper 32 bits.
                        for (var i : Int = 0; i < 5; i++) {
                            if (readRawByte() >= 0) {
                                return result;
                            }
                        }

                        NSException(name:"InvalidProtocolBuffer", reason:"malformedVarint", userInfo: nil).raise()
                    }
                }
            }
        }
        return result;
    }

from protobuf-swift.

alexeyxo avatar alexeyxo commented on July 17, 2024

Can you open a pull request?

from protobuf-swift.

jamesweb1 avatar jamesweb1 commented on July 17, 2024

I find when I use parseFromNSData, and it will throw Terminating app due to uncaught exception 'InvalidProtocolBuffer', reason: 'malformedVarint. Is it right in this release? I saw the same issue #3 , but it seems to already fixed.

from protobuf-swift.

alexeyxo avatar alexeyxo commented on July 17, 2024

@jamesweb1
Do you have an example? Where you see this error? Because, my unittest worked fine with negative integers.

from protobuf-swift.

jamesweb1 avatar jamesweb1 commented on July 17, 2024

I have a very simple example:

  • person.proto
message Person {
    required string name = 1;
    required int32 age = 2;
    optional string company = 3;
}
  • testNegative in unittest
let personBuilder = PersonBuilder()
personBuilder.setAge(-100)
             .setName("test")
             .setCompany("unknown")
let person = personBuilder.build()
let data = person.data()
let person_new = Person.parseFromData(data)

And I get the exception as follow:

<unknown>:0: error: -[bdiloggerTests.ProtoBufTests testNegative] : failed: caught "InvalidProtocolBuffer", "malformedVarint"
(
    0   CoreFoundation                      0x000000010fb3fc65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010f7d8bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010fb3f8a9 -[NSException raise] + 9
    3   ProtocolBuffers                     0x00000001133a4ded _TFC15ProtocolBuffers16CodedInputStream15readRawVarint32fS0_FT_VSs5Int32 + 1693
    4   ProtocolBuffers                     0x00000001133a43c3 _TFC15ProtocolBuffers16CodedInputStream9readInt32fS0_FT_VSs5Int32 + 51
    5   bdiloggerTests                      0x0000000112fc5f6b _TFC14bdiloggerTests13PersonBuilder25mergeFromCodedInputStreamfS0_FTC15ProtocolBuffers16CodedInputStream17extensionRegistryCS1_17ExtensionRegistry_S0_ + 779
    6   ProtocolBuffers                     0x000000011339bcc3 _TFC15ProtocolBuffers22AbstractMessageBuilder13mergeFromDatafDS0_FTCSo6NSData17extensionRegistryCS_17ExtensionRegistry_DS0_ + 147
    7   bdiloggerTests                      0x0000000112fc5889 _TZFC14bdiloggerTests6Person13parseFromDatafMS0_FCSo6NSDataS0_ + 105
    8   bdiloggerTests                      0x000000011304ce15 _TFC14bdiloggerTests13ProtoBufTests12testNegativefS0_FT_T_ + 261
    9   bdiloggerTests                      0x000000011304ce72 _TToFC14bdiloggerTests13ProtoBufTests12testNegativefS0_FT_T_ + 34
    10  CoreFoundation                      0x000000010fa35dec __invoking___ + 140
    11  CoreFoundation                      0x000000010fa35c42 -[NSInvocation invoke] + 290
    12  XCTest                              0x000000011139712a -[XCTestCase invokeTest] + 253
    13  XCTest                              0x000000011139732b -[XCTestCase performTest:] + 150
    14  XCTest                              0x00000001113a0be5 -[XCTest run] + 260
    15  XCTest                              0x000000011139603b -[XCTestSuite performTest:] + 379
    16  XCTest                              0x00000001113a0be5 -[XCTest run] + 260
    17  XCTest                              0x000000011139603b -[XCTestSuite performTest:] + 379
    18  XCTest                              0x00000001113a0be5 -[XCTest run] + 260
    19  XCTest                              0x000000011139603b -[XCTestSuite performTest:] + 379
    20  XCTest                              0x00000001113a0be5 -[XCTest run] + 260
    21  XCTest                              0x00000001113930d9 __25-[XCTestDriver _runSuite]_block_invoke + 56
    22  XCTest                              0x000000011139de8d -[XCTestObservationCenter _observeTestExecutionForBlock:] + 162
    23  XCTest                              0x0000000111393010 -[XCTestDriver _runSuite] + 269
    24  XCTest                              0x0000000111393a3d -[XCTestDriver _checkForTestManager] + 234
    25  XCTest                              0x00000001113a3ad0 +[XCTestProbe runTests:] + 182
    26  xctest                              0x000000010f2be1a6 xctest + 4518
    27  xctest                              0x000000010f2be419 xctest + 5145
    28  xctest                              0x000000010f2bde23 xctest + 3619
    29  libdyld.dylib                       0x0000000110e0b145 start + 1
)

sorry for replying so late.

from protobuf-swift.

alexeyxo avatar alexeyxo commented on July 17, 2024

Thanks a lot, guys!

from protobuf-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.