Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC 7049 conformance issues #5

Closed
maxgribov opened this issue Jun 22, 2015 · 6 comments
Closed

RFC 7049 conformance issues #5

maxgribov opened this issue Jun 22, 2015 · 6 comments
Milestone

Comments

@maxgribov
Copy link

Recently tried wrap your lib in objective - c (for iOS/MAC OS client - server exchange purposes) and bump into couple issues conformance with RFC 7049 standard.

First: wrong byte order after encoding
RFC 7049, Appendix A

       //...
        int value = 1000000;
        cbor_item_t *cborItem = cbor_build_uint32(value);

        unsigned char *buffer;
        size_t buffer_size,
        length = cbor_serialize_alloc(cborItem, &buffer, &buffer_size);

        NSData *bufferData = [NSData dataWithBytes:(const void *)buffer length:sizeof(unsigned char)*length];
        //...

Result binary data (in ascii hex): 0x1a40420f00
Must be (according to RFC 7049): 0x1a000f4240

_All bytes after first byte in reverse order._

Second: negative values of all wides of integers encoding incorrect. For example, if I even reorder bytes in correct way - result not confirms to RFC 7049:

       //...
        short value = -1000;
        cbor_item_t *cborItem = cbor_build_uint16(value);
        cbor_mark_negint(cborItem);

        unsigned char *buffer;
        size_t buffer_size,
        length = cbor_serialize_alloc(cborItem, &buffer, &buffer_size);

        NSData *bufferData = [NSData dataWithBytes:(const void *)buffer length:sizeof(unsigned char)*length];
        //...

Result binary data (in ascii hex): 0x39fc18
Must be (according to RFC 7049): 0x3903e7

Third: floats encoding into something absolutely crazy. But any float encoded before with cbor_build_float4 can't be decoded with cbor_float_get_float4... It always returns 0.0

       //...
        case CBOR_TYPE_FLOAT_CTRL:

            switch (cbor_float_get_width(itemFromData)) {
                case CBOR_FLOAT_0:{

                    if (cbor_ctrl_is_bool(itemFromData)) {

                        resultNumber = [NSNumber numberWithBool:cbor_ctrl_value(itemFromData)];

                    }

                }
                    break;
                case CBOR_FLOAT_16:
                    resultNumber = [NSNumber numberWithFloat:cbor_float_get_float2(itemFromData)];
                    break;
                case CBOR_FLOAT_32:
                    resultNumber = [NSNumber numberWithFloat:cbor_float_get_float4(itemFromData)];
                    break;
                case CBOR_FLOAT_64:
                    resultNumber = [NSNumber numberWithDouble:cbor_float_get_float8(itemFromData)];
                    break;

            }

            break;
        //...

There is chance, that I am using your lib in some wrong way, but I quit trying understand this... Maybe you can help me?

@ekroon
Copy link
Contributor

ekroon commented Sep 10, 2015

Maybe something went wrong with the compilation, you could try #8

@dgrigsby
Copy link

I have also seen the "First" issue described above for input: 100000.0
Expected: 0xFA47C35000 (per RFC 7049)
Observed: 0xFA0050C347

@dgrigsby
Copy link

@ekroon it's indeed possible this is related to #8.
I am currently running on the Mac XCode iOS simulator.
Highlighted line seems to be not working as expected:
size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
{
if (buffer_size >= 5) {
buffer[0] = 0x1A + offset;

#ifdef HAVE_ENDIAN_H
*(uint32_t *) &buffer[1] = htobe32(value);
#else
#ifdef IS_BIG_ENDIAN
====> *(uint32_t *) &buffer[1] = value;
#else
buffer[1] = value >> 24;
buffer[2] = value >> 16;
buffer[3] = value >> 8;
buffer[4] = value;
#endif
#endif

    return 5;
} else
    return 0;

}

@dgrigsby
Copy link

Yes, I'm on 0.3.1 and IS_BIG_ENDIAN is incorrectly true for the XCode Simulator.
#8 would take care of this if it were incorporated, and if you use CMake to generate your xcodeproj.

@dgrigsby
Copy link

Issue is likely all #ifdef IS_BIG_ENDIAN should be changed to #if IS_BIG_ENDIAN.

@PJK PJK modified the milestone: v0.5 Nov 7, 2016
PJK added a commit that referenced this issue Jan 6, 2017
PJK added a commit that referenced this issue Jan 10, 2017
PJK added a commit that referenced this issue Jan 11, 2017
@PJK
Copy link
Owner

PJK commented Jan 12, 2017

I believe this has been fixed by @dgrigsby through #10

@PJK PJK closed this as completed Jan 12, 2017
PJK added a commit that referenced this issue Sep 30, 2017
PJK added a commit that referenced this issue Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants