Skip to content

Commit 2c22d71

Browse files
De-inline extract_number and move it back into cborparser.c
This function wasn't getting inlined anyway, might as well make it have one canonical copy and make that get called. Signed-off-by: Thiago Macieira <[email protected]>
1 parent cf3116e commit 2c22d71

File tree

5 files changed

+69
-85
lines changed

5 files changed

+69
-85
lines changed

src/cborencoder_close_container_checked.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "cbor.h"
3232
#include "cborinternal_p.h"
3333
#include "compilersupport_p.h"
34-
#include "extract_number_p.h"
3534

3635
#include <assert.h>
3736

@@ -66,7 +65,7 @@ CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborE
6665

6766
/* check what the original length was */
6867
uint64_t actually_added;
69-
err = extract_number(&ptr, encoder->data.ptr, &actually_added);
68+
err = _cbor_value_extract_number(&ptr, encoder->data.ptr, &actually_added);
7069
if (err)
7170
return err;
7271

src/cborinternal_p.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#ifndef CBORINTERNAL_P_H
2626
#define CBORINTERNAL_P_H
2727

28+
#include "compilersupport_p.h"
29+
30+
#ifndef CBOR_INTERNAL_API
31+
# define CBOR_INTERNAL_API
32+
#endif
33+
2834
/*
2935
* CBOR Major types
3036
* Encoded in the high 3 bits of the descriptor byte
@@ -73,4 +79,6 @@ enum {
7379
BreakByte = (unsigned)Break | (SimpleTypesType << MajorTypeShift)
7480
};
7581

82+
CBOR_INTERNAL_API CBOR_INTERNAL_API_CC CborError _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len);
83+
7684
#endif /* CBORINTERNAL_P_H */

src/cborparser.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "cbor.h"
3232
#include "cborinternal_p.h"
3333
#include "compilersupport_p.h"
34-
#include "extract_number_p.h"
3534

3635
#include <assert.h>
3736
#include <string.h>
@@ -146,10 +145,58 @@
146145
* \endif
147146
*/
148147

148+
static inline uint16_t get16(const uint8_t *ptr)
149+
{
150+
uint16_t result;
151+
memcpy(&result, ptr, sizeof(result));
152+
return cbor_ntohs(result);
153+
}
154+
155+
static inline uint32_t get32(const uint8_t *ptr)
156+
{
157+
uint32_t result;
158+
memcpy(&result, ptr, sizeof(result));
159+
return cbor_ntohl(result);
160+
}
161+
162+
static inline uint64_t get64(const uint8_t *ptr)
163+
{
164+
uint64_t result;
165+
memcpy(&result, ptr, sizeof(result));
166+
return cbor_ntohll(result);
167+
}
168+
169+
CborError _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
170+
{
171+
uint8_t additional_information = **ptr & SmallValueMask;
172+
++*ptr;
173+
if (additional_information < Value8Bit) {
174+
*len = additional_information;
175+
return CborNoError;
176+
}
177+
if (unlikely(additional_information > Value64Bit))
178+
return CborErrorIllegalNumber;
179+
180+
size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
181+
if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
182+
return CborErrorUnexpectedEOF;
183+
} else if (bytesNeeded == 1) {
184+
*len = (uint8_t)(*ptr)[0];
185+
} else if (bytesNeeded == 2) {
186+
*len = get16(*ptr);
187+
} else if (bytesNeeded == 4) {
188+
*len = get32(*ptr);
189+
} else {
190+
*len = get64(*ptr);
191+
}
192+
*ptr += bytesNeeded;
193+
return CborNoError;
194+
}
195+
149196
static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
150197
{
151198
uint64_t v;
152-
CborError err = extract_number(ptr, parser->end, &v);
199+
CborError err = _cbor_value_extract_number(ptr, parser->end, &v);
153200
if (err) {
154201
*len = 0;
155202
return err;
@@ -275,7 +322,7 @@ static CborError preparse_next_value(CborValue *it)
275322
static CborError advance_internal(CborValue *it)
276323
{
277324
uint64_t length;
278-
CborError err = extract_number(&it->ptr, it->parser->end, &length);
325+
CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
279326
assert(err == CborNoError);
280327

281328
if (it->type == CborByteStringType || it->type == CborTextStringType) {
@@ -519,7 +566,7 @@ CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
519566
++recursed->ptr;
520567
} else {
521568
uint64_t len;
522-
err = extract_number(&recursed->ptr, recursed->parser->end, &len);
569+
err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
523570
assert(err == CborNoError);
524571

525572
recursed->remaining = (uint32_t)len;

src/compilersupport_p.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/****************************************************************************
22
**
3-
** Copyright (C) 2015 Intel Corporation
3+
** Copyright (C) 2017 Intel Corporation
44
**
55
** Permission is hereby granted, free of charge, to any person obtaining a copy
66
** of this software and associated documentation files (the "Software"), to deal
@@ -77,6 +77,14 @@
7777
#endif
7878
#define DBL_DECIMAL_DIG_STR STRINGIFY(DBL_DECIMAL_DIG)
7979

80+
#if defined(__GNUC__) && defined(__i386__)
81+
# define CBOR_INTERNAL_API_CC __attribute__((regparm(3)))
82+
#elif defined(_MSC_VER) && defined(_M_IX86)
83+
# define CBOR_INTERNAL_API_CC __fastcall
84+
#else
85+
# define CBOR_INTERNAL_API_CC
86+
#endif
87+
8088
#ifndef __has_builtin
8189
# define __has_builtin(x) 0
8290
#endif

src/extract_number_p.h

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)