blob: ff0b829fc8ab33d003a7c13b4b5a81104f30aeb6 [file] [log] [blame]
Yann Collet977f1f32016-01-21 15:38:47 +01001/* ******************************************************************
2 Error codes and messages
3 Copyright (C) 2013-2016, Yann Collet
4
5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 You can contact the author at :
Yann Colletde406ee2016-03-20 15:46:10 +010031 - Homepage : http://www.zstd.net
Yann Collet977f1f32016-01-21 15:38:47 +010032****************************************************************** */
33/* Note : this module is expected to remain private, do not expose it */
34
35#ifndef ERROR_H_MODULE
36#define ERROR_H_MODULE
37
38#if defined (__cplusplus)
39extern "C" {
40#endif
41
42
Yann Collet72bff502016-02-03 12:06:24 +010043/* ****************************************
44* Dependencies
Yann Collet977f1f32016-01-21 15:38:47 +010045******************************************/
Yann Collet72bff502016-02-03 12:06:24 +010046#include <stddef.h> /* size_t */
Yann Collet977f1f32016-01-21 15:38:47 +010047#include "error_public.h" /* enum list */
48
49
Yann Collet72bff502016-02-03 12:06:24 +010050/* ****************************************
Yann Collet977f1f32016-01-21 15:38:47 +010051* Compiler-specific
52******************************************/
53#if defined(__GNUC__)
54# define ERR_STATIC static __attribute__((unused))
55#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
56# define ERR_STATIC static inline
57#elif defined(_MSC_VER)
58# define ERR_STATIC static __inline
59#else
60# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
61#endif
62
63
Yann Collet72bff502016-02-03 12:06:24 +010064/*-****************************************
Yann Colletde406ee2016-03-20 15:46:10 +010065* Customization (error_public.h)
Yann Collet977f1f32016-01-21 15:38:47 +010066******************************************/
Yann Collet982ffc72016-02-05 02:33:10 +010067typedef ZSTD_ErrorCode ERR_enum;
Yann Collet977f1f32016-01-21 15:38:47 +010068#define PREFIX(name) ZSTD_error_##name
69
Yann Collet72bff502016-02-03 12:06:24 +010070
71/*-****************************************
72* Error codes handling
73******************************************/
Yann Collet977f1f32016-01-21 15:38:47 +010074#ifdef ERROR
Yann Collet72bff502016-02-03 12:06:24 +010075# undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
Yann Collet977f1f32016-01-21 15:38:47 +010076#endif
Yann Colletde406ee2016-03-20 15:46:10 +010077#define ERROR(name) ((size_t)-PREFIX(name))
Yann Collet977f1f32016-01-21 15:38:47 +010078
79ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
80
Yann Collet72bff502016-02-03 12:06:24 +010081ERR_STATIC ERR_enum ERR_getError(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
Yann Collet977f1f32016-01-21 15:38:47 +010082
Yann Collet72bff502016-02-03 12:06:24 +010083
84/*-****************************************
Yann Collet977f1f32016-01-21 15:38:47 +010085* Error Strings
86******************************************/
87
88ERR_STATIC const char* ERR_getErrorName(size_t code)
89{
Yann Collet72bff502016-02-03 12:06:24 +010090 static const char* notErrorCode = "Unspecified error code";
91 switch( ERR_getError(code) )
Yann Collet977f1f32016-01-21 15:38:47 +010092 {
Yann Collet72bff502016-02-03 12:06:24 +010093 case PREFIX(no_error): return "No error detected";
94 case PREFIX(GENERIC): return "Error (generic)";
95 case PREFIX(prefix_unknown): return "Unknown frame descriptor";
96 case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
97 case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode";
98 case PREFIX(init_missing): return "Context should be init first";
99 case PREFIX(memory_allocation): return "Allocation error : not enough memory";
100 case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
101 case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
102 case PREFIX(srcSize_wrong): return "Src size incorrect";
103 case PREFIX(corruption_detected): return "Corrupted block detected";
Yann Colletde406ee2016-03-20 15:46:10 +0100104 case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
105 case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
Yann Collet72bff502016-02-03 12:06:24 +0100106 case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
107 case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
108 case PREFIX(maxCode):
Yann Colletde406ee2016-03-20 15:46:10 +0100109 default: return notErrorCode; /* impossible, due to ERR_getError() */
Yann Collet977f1f32016-01-21 15:38:47 +0100110 }
111}
112
113
114#if defined (__cplusplus)
115}
116#endif
117
118#endif /* ERROR_H_MODULE */