Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Nick Terrell | ac58c8d | 2020-03-26 15:19:05 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 10 | |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 11 | /* Note : this module is expected to remain private, do not expose it */ |
| 12 | |
| 13 | #ifndef ERROR_H_MODULE |
| 14 | #define ERROR_H_MODULE |
| 15 | |
| 16 | #if defined (__cplusplus) |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | |
Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 21 | /* **************************************** |
| 22 | * Dependencies |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 23 | ******************************************/ |
Nick Terrell | 80f577b | 2020-08-06 20:18:05 -0700 | [diff] [blame^] | 24 | #include "zstd_deps.h" /* size_t */ |
Yann Collet | a17fd73 | 2016-10-11 16:41:09 -0700 | [diff] [blame] | 25 | #include "zstd_errors.h" /* enum list */ |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 26 | |
| 27 | |
Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 28 | /* **************************************** |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 29 | * Compiler-specific |
| 30 | ******************************************/ |
| 31 | #if defined(__GNUC__) |
| 32 | # define ERR_STATIC static __attribute__((unused)) |
| 33 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
| 34 | # define ERR_STATIC static inline |
| 35 | #elif defined(_MSC_VER) |
| 36 | # define ERR_STATIC static __inline |
| 37 | #else |
| 38 | # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ |
| 39 | #endif |
| 40 | |
| 41 | |
Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 42 | /*-**************************************** |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 43 | * Customization (error_public.h) |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 44 | ******************************************/ |
Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 45 | typedef ZSTD_ErrorCode ERR_enum; |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 46 | #define PREFIX(name) ZSTD_error_##name |
| 47 | |
Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 48 | |
| 49 | /*-**************************************** |
| 50 | * Error codes handling |
| 51 | ******************************************/ |
Yann Collet | 20bd246 | 2020-05-11 19:29:36 -0700 | [diff] [blame] | 52 | #undef ERROR /* already defined on Visual Studio */ |
Nick Terrell | 7c365eb | 2017-08-25 17:44:32 -0700 | [diff] [blame] | 53 | #define ERROR(name) ZSTD_ERROR(name) |
| 54 | #define ZSTD_ERROR(name) ((size_t)-PREFIX(name)) |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 55 | |
| 56 | ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } |
| 57 | |
Yann Collet | c75e4c2 | 2016-05-10 17:47:11 +0200 | [diff] [blame] | 58 | ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); } |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 59 | |
Carl Woffenden | edd9a07 | 2020-04-07 11:02:06 +0200 | [diff] [blame] | 60 | /* check and forward error code */ |
| 61 | #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e |
| 62 | #define CHECK_F(f) { CHECK_V_F(_var_err__, f); } |
| 63 | |
Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 64 | |
| 65 | /*-**************************************** |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 66 | * Error Strings |
| 67 | ******************************************/ |
| 68 | |
Yann Collet | ef2357d | 2016-10-11 17:24:50 -0700 | [diff] [blame] | 69 | const char* ERR_getErrorString(ERR_enum code); /* error_private.c */ |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 70 | |
Yann Collet | c75e4c2 | 2016-05-10 17:47:11 +0200 | [diff] [blame] | 71 | ERR_STATIC const char* ERR_getErrorName(size_t code) |
| 72 | { |
| 73 | return ERR_getErrorString(ERR_getErrorCode(code)); |
| 74 | } |
Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 75 | |
| 76 | #if defined (__cplusplus) |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | #endif /* ERROR_H_MODULE */ |