diff options
Diffstat (limited to 'src/jsmn.h')
| -rw-r--r-- | src/jsmn.h | 18 |
1 files changed, 12 insertions, 6 deletions
| @@ -3,6 +3,8 @@ | |||
| 3 | * Simple JSON parser (header file) | 3 | * Simple JSON parser (header file) |
| 4 | * | 4 | * |
| 5 | * Copyright (c) 2010 Serge A. Zaitsev | 5 | * Copyright (c) 2010 Serge A. Zaitsev |
| 6 | * Updated to use size_t for token offsets and harden against overflows. | ||
| 7 | * (Nikias Bassen, January 2026) | ||
| 6 | * | 8 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal | 10 | * of this software and associated documentation files (the "Software"), to deal |
| @@ -25,6 +27,8 @@ | |||
| 25 | #ifndef __JSMN_H_ | 27 | #ifndef __JSMN_H_ |
| 26 | #define __JSMN_H_ | 28 | #define __JSMN_H_ |
| 27 | 29 | ||
| 30 | #include <stddef.h> | ||
| 31 | |||
| 28 | /** | 32 | /** |
| 29 | * JSON type identifier. Basic types are: | 33 | * JSON type identifier. Basic types are: |
| 30 | * o Object | 34 | * o Object |
| @@ -46,6 +50,8 @@ typedef enum { | |||
| 46 | JSMN_ERROR_INVAL = -2, | 50 | JSMN_ERROR_INVAL = -2, |
| 47 | /* The string is not a full JSON packet, more bytes expected */ | 51 | /* The string is not a full JSON packet, more bytes expected */ |
| 48 | JSMN_ERROR_PART = -3, | 52 | JSMN_ERROR_PART = -3, |
| 53 | /* Input exceeds implementation-defined limits */ | ||
| 54 | JSMN_ERROR_LIMIT = -4, | ||
| 49 | /* Everything was fine */ | 55 | /* Everything was fine */ |
| 50 | JSMN_SUCCESS = 0 | 56 | JSMN_SUCCESS = 0 |
| 51 | } jsmnerr_t; | 57 | } jsmnerr_t; |
| @@ -58,9 +64,9 @@ typedef enum { | |||
| 58 | */ | 64 | */ |
| 59 | typedef struct { | 65 | typedef struct { |
| 60 | jsmntype_t type; | 66 | jsmntype_t type; |
| 61 | int start; | 67 | size_t start; |
| 62 | int end; | 68 | size_t end; |
| 63 | int size; | 69 | size_t size; |
| 64 | #ifdef JSMN_PARENT_LINKS | 70 | #ifdef JSMN_PARENT_LINKS |
| 65 | int parent; | 71 | int parent; |
| 66 | #endif | 72 | #endif |
| @@ -71,8 +77,8 @@ typedef struct { | |||
| 71 | * the string being parsed now and current position in that string | 77 | * the string being parsed now and current position in that string |
| 72 | */ | 78 | */ |
| 73 | typedef struct { | 79 | typedef struct { |
| 74 | unsigned int pos; /* offset in the JSON string */ | 80 | size_t pos; /* offset in the JSON string */ |
| 75 | unsigned int end; /* offset after last character of JSON string */ | 81 | size_t end; /* offset after last character of JSON string */ |
| 76 | int toknext; /* next token to allocate */ | 82 | int toknext; /* next token to allocate */ |
| 77 | int toksuper; /* superior token node, e.g parent object or array */ | 83 | int toksuper; /* superior token node, e.g parent object or array */ |
| 78 | } jsmn_parser; | 84 | } jsmn_parser; |
| @@ -86,7 +92,7 @@ void jsmn_init(jsmn_parser *parser); | |||
| 86 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing | 92 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing |
| 87 | * a single JSON object. | 93 | * a single JSON object. |
| 88 | */ | 94 | */ |
| 89 | jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, | 95 | jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t length, |
| 90 | jsmntok_t *tokens, unsigned int num_tokens); | 96 | jsmntok_t *tokens, unsigned int num_tokens); |
| 91 | 97 | ||
| 92 | #endif /* __JSMN_H_ */ | 98 | #endif /* __JSMN_H_ */ |
