blob: 9d36e891c066b711a66a1670b70fc470179d4c89 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Nick Terrell66e811d2021-01-04 17:53:52 -05002 * Copyright (c) 2016-2021, Yann Collet, Facebook, Inc.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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 Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet977f1f32016-01-21 15:38:47 +010010
Yann Collet977f1f32016-01-21 15:38:47 +010011/* 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)
17extern "C" {
18#endif
19
20
Yann Collet72bff502016-02-03 12:06:24 +010021/* ****************************************
22* Dependencies
Yann Collet977f1f32016-01-21 15:38:47 +010023******************************************/
Nick Terrell80f577b2020-08-06 20:18:05 -070024#include "zstd_deps.h" /* size_t */
Yann Colleta17fd732016-10-11 16:41:09 -070025#include "zstd_errors.h" /* enum list */
Yann Collet977f1f32016-01-21 15:38:47 +010026
27
Yann Collet72bff502016-02-03 12:06:24 +010028/* ****************************************
Yann Collet977f1f32016-01-21 15:38:47 +010029* 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 Collet72bff502016-02-03 12:06:24 +010042/*-****************************************
Yann Colletde406ee2016-03-20 15:46:10 +010043* Customization (error_public.h)
Yann Collet977f1f32016-01-21 15:38:47 +010044******************************************/
Yann Collet982ffc72016-02-05 02:33:10 +010045typedef ZSTD_ErrorCode ERR_enum;
Yann Collet977f1f32016-01-21 15:38:47 +010046#define PREFIX(name) ZSTD_error_##name
47
Yann Collet72bff502016-02-03 12:06:24 +010048
49/*-****************************************
50* Error codes handling
51******************************************/
Yann Collet20bd2462020-05-11 19:29:36 -070052#undef ERROR /* already defined on Visual Studio */
Nick Terrell7c365eb2017-08-25 17:44:32 -070053#define ERROR(name) ZSTD_ERROR(name)
54#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
Yann Collet977f1f32016-01-21 15:38:47 +010055
56ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
57
Yann Colletc75e4c22016-05-10 17:47:11 +020058ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
Yann Collet977f1f32016-01-21 15:38:47 +010059
Carl Woffendenedd9a072020-04-07 11:02:06 +020060/* 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 Collet72bff502016-02-03 12:06:24 +010064
65/*-****************************************
Yann Collet977f1f32016-01-21 15:38:47 +010066* Error Strings
67******************************************/
68
Yann Colletef2357d2016-10-11 17:24:50 -070069const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
Yann Collet977f1f32016-01-21 15:38:47 +010070
Yann Colletc75e4c22016-05-10 17:47:11 +020071ERR_STATIC const char* ERR_getErrorName(size_t code)
72{
73 return ERR_getErrorString(ERR_getErrorCode(code));
74}
Yann Collet977f1f32016-01-21 15:38:47 +010075
76#if defined (__cplusplus)
77}
78#endif
79
80#endif /* ERROR_H_MODULE */