1 /**
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of https://github.com/facebook/zstd.
7  * An additional grant of patent rights can be found in the PATENTS file in the
8  * same directory.
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License version 2 as published by the
12  * Free Software Foundation. This program is dual-licensed; you may select
13  * either version 2 of the GNU General Public License ("GPL") or BSD license
14  * ("BSD").
15  */
16 
17 /* Note : this module is expected to remain private, do not expose it */
18 
19 #ifndef ERROR_H_MODULE
20 #define ERROR_H_MODULE
21 
22 /**
23  * enum ZSTD_ErrorCode - zstd error codes
24  *
25  * Functions that return size_t can be checked for errors using ZSTD_isError()
26  * and the ZSTD_ErrorCode can be extracted using ZSTD_getErrorCode().
27  */
28 typedef enum {
29 	ZSTD_error_no_error,
30 	ZSTD_error_GENERIC,
31 	ZSTD_error_prefix_unknown,
32 	ZSTD_error_version_unsupported,
33 	ZSTD_error_parameter_unknown,
34 	ZSTD_error_frameParameter_unsupported,
35 	ZSTD_error_frameParameter_unsupportedBy32bits,
36 	ZSTD_error_frameParameter_windowTooLarge,
37 	ZSTD_error_compressionParameter_unsupported,
38 	ZSTD_error_init_missing,
39 	ZSTD_error_memory_allocation,
40 	ZSTD_error_stage_wrong,
41 	ZSTD_error_dstSize_tooSmall,
42 	ZSTD_error_srcSize_wrong,
43 	ZSTD_error_corruption_detected,
44 	ZSTD_error_checksum_wrong,
45 	ZSTD_error_tableLog_tooLarge,
46 	ZSTD_error_maxSymbolValue_tooLarge,
47 	ZSTD_error_maxSymbolValue_tooSmall,
48 	ZSTD_error_dictionary_corrupted,
49 	ZSTD_error_dictionary_wrong,
50 	ZSTD_error_dictionaryCreation_failed,
51 	ZSTD_error_maxCode
52 } ZSTD_ErrorCode;
53 
54 /* ****************************************
55 *  Compiler-specific
56 ******************************************/
57 #define ERR_STATIC static __attribute__((unused))
58 
59 /*-****************************************
60 *  Customization (error_public.h)
61 ******************************************/
62 typedef ZSTD_ErrorCode ERR_enum;
63 #define PREFIX(name) ZSTD_error_##name
64 
65 /*-****************************************
66 *  Error codes handling
67 ******************************************/
68 #define ERROR(name) ((size_t)-PREFIX(name))
69 
ERR_isError(size_t code)70 ERR_STATIC unsigned __init ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
71 
ERR_getErrorCode(size_t code)72 ERR_STATIC ERR_enum __init ERR_getErrorCode(size_t code)
73 {
74 	if (!ERR_isError(code))
75 		return (ERR_enum)0;
76 	return (ERR_enum)(0 - code);
77 }
78 
79 /**
80  * ZSTD_isError() - tells if a size_t function result is an error code
81  * @code:  The function result to check for error.
82  *
83  * Return: Non-zero iff the code is an error.
84  */
ZSTD_isError(size_t code)85 static __attribute__((unused)) unsigned int __init ZSTD_isError(size_t code)
86 {
87 	return code > (size_t)-ZSTD_error_maxCode;
88 }
89 
90 /**
91  * ZSTD_getErrorCode() - translates an error function result to a ZSTD_ErrorCode
92  * @functionResult: The result of a function for which ZSTD_isError() is true.
93  *
94  * Return:          The ZSTD_ErrorCode corresponding to the functionResult or 0
95  *                  if the functionResult isn't an error.
96  */
ZSTD_getErrorCode(size_t functionResult)97 static __attribute__((unused)) ZSTD_ErrorCode __init ZSTD_getErrorCode(
98 	size_t functionResult)
99 {
100 	if (!ZSTD_isError(functionResult))
101 		return (ZSTD_ErrorCode)0;
102 	return (ZSTD_ErrorCode)(0 - functionResult);
103 }
104 
105 #endif /* ERROR_H_MODULE */
106