Skip to content

Commit

Permalink
chore(gencodecs): add FIELD_BITMASK() macro for u64bitmask
Browse files Browse the repository at this point in the history
  • Loading branch information
lcsmuller committed Mar 4, 2022
1 parent 5682608 commit 061b6b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gencodecs/api/gateway.pre.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ PUB_STRUCT(discord_identify)
/** presence structure for initial presence information */
FIELD_STRUCT_PTR(presence, discord_presence_update, *)
/** the gateway intents you wish to receive */
FIELD(intents, int, 0)
FIELD_BITMASK(intents)
STRUCT_END

STRUCT(discord_identify_connection)
Expand Down
10 changes: 8 additions & 2 deletions gencodecs/discord_codecs.pre.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ PP_INCLUDE("types.h")
#define GENCODECS_JSON_ENCODER_PTR_json_char(b, buf, size, _var, _type) \
if (0 > (code = jsonb_token(b, buf, size, _var, strlen(_var)))) \
return code
#define GENCODECS_JSON_ENCODER_u64snowflake(b, buf, size, _var, _type) \
#define GENCODECS_JSON_ENCODER_uint64_t(b, buf, size, _var, _type) \
{ \
char tok[64]; \
int toklen; \
toklen = sprintf(tok, "%" PRIu64, _var); \
if (0 > (code = jsonb_string(b, buf, size, tok, toklen))) \
return code; \
}
#define GENCODECS_JSON_ENCODER_u64snowflake GENCODECS_JSON_ENCODER_uint64_t
#define GENCODECS_JSON_ENCODER_u64bitmask GENCODECS_JSON_ENCODER_uint64_t
#define GENCODECS_JSON_ENCODER_u64unix_ms(b, buf, size, _var, _type) \
{ \
char tok[64]; \
Expand All @@ -34,8 +36,10 @@ PP_INCLUDE("types.h")
_var = _gc_strndup(buf + f->val->start, f->val->end - f->val->start); \
ret += f->val->end - f->val->start; \
}
#define GENCODECS_JSON_DECODER_u64snowflake(f, buf, _var, _type) \
#define GENCODECS_JSON_DECODER_uint64_t(f, buf, _var, _type) \
if (f) sscanf(buf + f->val->start, "%" SCNu64, &_var)
#define GENCODECS_JSON_DECODER_u64snowflake GENCODECS_JSON_DECODER_uint64_t
#define GENCODECS_JSON_DECODER_u64bitmask GENCODECS_JSON_DECODER_uint64_t
#define GENCODECS_JSON_DECODER_u64unix_ms(f, buf, _var, _type) \
if (f && f->val->type == JSMN_PRIMITIVE) \
cog_iso8601_to_unix_ms(buf + f->val->start, \
Expand All @@ -44,6 +48,8 @@ PP_INCLUDE("types.h")
/* Custom field macros */
#define FIELD_SNOWFLAKE(_name) \
FIELD_PRINTF(_name, u64snowflake, "\"%" PRIu64 "\"", "%" SCNu64)
#define FIELD_BITMASK(_name) \
FIELD_PRINTF(_name, u64bitmask, "\"%" PRIu64 "\"", "%" SCNu64)
#define FIELD_TIMESTAMP(_name) \
FIELD_CUSTOM(_name, #_name, u64unix_ms, DECOR_BLANK, INIT_BLANK, \
CLEANUP_BLANK, GENCODECS_JSON_ENCODER_u64unix_ms, \
Expand Down
10 changes: 10 additions & 0 deletions include/discord-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
} \
} while (0)

/**
* @brief Shortcut for checking OOB-write attempts
* @note unsigned values are expected
*
* @param nbytes amount of bytes to be written
* @param destsz size of dest in bytes
*/
#define ASSERT_NOT_OOB(nbytes, destsz) \
ASSERT_S((size_t)nbytes < (size_t)destsz, "Out of bounds write attempt");

/** @defgroup DiscordInternalAdapter REST API
* @brief Wrapper to the Discord REST API
* @{ */
Expand Down

0 comments on commit 061b6b5

Please sign in to comment.