aircampro

header for embedded encoders library

Jul 9th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.62 KB | None | 0 0
  1. #ifndef _enCoders_H
  2. #define _enCoders_H
  3. /**
  4.  *
  5.  * @section License
  6.  *
  7.  * SPDX-License-Identifier: GPL-2.0-or-later
  8.  *
  9.  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
  10.  *
  11.  * This file is part of CycloneCrypto Open.
  12.  *
  13.  * This program is free software; you can redistribute it and/or
  14.  * modify it under the terms of the GNU General Public License
  15.  * as published by the Free Software Foundation; either version 2
  16.  * of the License, or (at your option) any later version.
  17.  *
  18.  * This program is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with this program; if not, write to the Free Software Foundation,
  25.  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  26.  *
  27.  * @author Oryx Embedded SARL (www.oryx-embedded.com)
  28.  * @version 1.9.6
  29.  *
  30.  * ported by Copyright (C) 2020 A C P Avaiation Walkerburn Scotland
  31.  **/
  32. #include "definitions.h"
  33. #include <stdint.h>
  34.  
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #endif
  39.  
  40. #ifndef char_t
  41. #define char_t char
  42. #endif
  43. #ifndef size_t
  44. #define size_t int32_t
  45. #endif
  46. #ifndef error_t
  47. #define error_t int16_t                                                         // define error type used in cyclone
  48. #endif
  49. #ifndef uint_t
  50. #define uint_t uint16_t
  51. #endif
  52. #ifndef bool_t
  53. #define bool_t uint8_t
  54. #endif
  55. #define int_t int16_t
  56. #define NO_ERROR 0u                                                             // error handlers for cyclone
  57. #define ERROR_INVALID_PARAMETER 1u
  58. #define ERROR_INVALID_LENGTH 2u
  59. #define ERROR_INVALID_SYNTAX 3u
  60. #define ERROR_BUFFER_OVERFLOW 4u
  61. #define ERROR_WRONG_STATE 5u
  62. #define ERROR_TIMEOUT 6u
  63. #define ERROR_WOULD_BLOCK 7u
  64. #define ERROR_END_OF_STREAM 8u
  65. #define ERROR_INVALID_VERSION 9u
  66. #define ERROR_INVALID_CHARACTER 10u
  67. #define ERROR_INVALID_TAG 11u
  68. #define ERROR_WRONG_ENCODING 12u
  69. #define ERROR_INVALID_CLASS 13u
  70. #define ERROR_INVALID_TYPE 14u
  71. #define ERROR_WRONG_IDENTIFIER 15u
  72. #define ERROR_NOT_IMPLEMENTED 16u
  73. #define ERROR_OUT_OF_MEMORY 17u
  74.  
  75. // ------------------ asn1 -----------------------------------------------------
  76.  
  77. #define ASN1_TAG_NUMBER_MASK        0x1Fu                                       //Tag number mask
  78.  
  79. #define ASN1_ENCODING_MASK          0x20u                                       //ASN.1 encoding
  80. #define ASN1_ENCODING_PRIMITIVE     0x00u
  81. #define ASN1_ENCODING_CONSTRUCTED   0x20u
  82.  
  83. #define ASN1_CLASS_MASK             0xC0u                                       //ASN.1 class
  84. #define ASN1_CLASS_UNIVERSAL        0x00u
  85. #define ASN1_CLASS_APPLICATION      0x40u
  86. #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80u
  87. #define ASN1_CLASS_PRIVATE          0xC0u
  88.  
  89. /**
  90.  * @brief ASN.1 data types
  91.  **/
  92. typedef enum
  93. {
  94.    ASN1_TYPE_BOOLEAN           = 1,
  95.    ASN1_TYPE_INTEGER           = 2,
  96.    ASN1_TYPE_BIT_STRING        = 3,
  97.    ASN1_TYPE_OCTET_STRING      = 4,
  98.    ASN1_TYPE_NULL              = 5,
  99.    ASN1_TYPE_OBJECT_IDENTIFIER = 6,
  100.    ASN1_TYPE_OBJECT_DESCRIPTOR = 7,
  101.    ASN1_TYPE_EXTERNAL          = 8,
  102.    ASN1_TYPE_REAL              = 9,
  103.    ASN1_TYPE_ENUMERATED        = 10,
  104.    ASN1_TYPE_UTF8_STRING       = 12,
  105.    ASN1_TYPE_SEQUENCE          = 16,
  106.    ASN1_TYPE_SET               = 17,
  107.    ASN1_TYPE_NUMERIC_STRING    = 18,
  108.    ASN1_TYPE_PRINTABLE_STRING  = 19,
  109.    ASN1_TYPE_TELETEX_STRING    = 20,
  110.    ASN1_TYPE_VIDEOTEX_STRING   = 21,
  111.    ASN1_TYPE_IA5_STRING        = 22,
  112.    ASN1_TYPE_UTC_TIME          = 23,
  113.    ASN1_TYPE_GENERALIZED_TIME  = 24,
  114.    ASN1_TYPE_GRAPHIC_STRING    = 25,
  115.    ASN1_TYPE_VISIBLE_STRING    = 26,
  116.    ASN1_TYPE_GENERAL_STRING    = 27,
  117.    ASN1_TYPE_UNIVERSAL_STRING  = 28,
  118.    ASN1_TYPE_BMP_STRING        = 30,
  119. } Asn1Type;
  120.  
  121. /**
  122.  * @brief ASN.1 tag
  123.  **/
  124. typedef struct
  125. {
  126.    bool_t constructed;
  127.    uint_t objClass;
  128.    uint_t objType;
  129.    size_t length;
  130.    const uint8_t *value;
  131.    size_t totalLength;
  132. } Asn1Tag;
  133.  
  134. /**
  135.  * @brief Arbitrary precision integer
  136.  **/
  137. typedef struct
  138. {
  139.    int_t sign;
  140.    uint_t size;
  141.    uint_t *dataV;
  142. } Mpi;
  143. /**
  144.  * @brief MPI import/export format
  145.  **/
  146. typedef enum
  147. {
  148.    MPI_FORMAT_LITTLE_ENDIAN = 0u,
  149.    MPI_FORMAT_BIG_ENDIAN    = 1u
  150. } MpiFormat;
  151.  
  152. //Size of the sub data type
  153. #define MPI_INT_SIZE sizeof(uint_t)
  154.  
  155. //Dependencies
  156. //#include "core/crypto.h"
  157.  
  158. #define HUFFMAN_TABLE_SIZE 257                                                  // 256 characters plus EOF
  159. typedef struct huffmanTable_s {
  160.     uint8_t  codeLen;
  161.     uint16_t hCode;
  162. } huffmanTable_t;
  163.  
  164. typedef struct huffmanState_s {
  165.     uint16_t bytesWritten;
  166.     uint8_t *outByte;
  167.     uint16_t outBufLen;
  168.     uint8_t outBit;
  169. } huffmanState_t;
  170.  
  171. extern const huffmanTable_t huffmanTable[HUFFMAN_TABLE_SIZE];
  172.  
  173. struct huffmanInfo_s {
  174.     uint16_t uncompressedByteCount;
  175. };
  176.  
  177. #define SSIZE_MAX INT16_MAX
  178.  
  179. // ------------------ Radix64 encoding related functions -----------------------
  180. extern void radix64Encode(const void *inputV, size_t inputLen, char_t *outputV, size_t *outputLen);
  181. extern error_t radix64Decode(const char_t *input, size_t inputLen, void *outputV, size_t *outputLen);
  182. // ------------------ Base64 encoding related functions ------------------------
  183. extern void base64Encode(const void *input, size_t inputLen, char_t *output, size_t *outputLen);
  184. extern error_t base64Decode(const char_t *input, size_t inputLen, void *output,size_t *outputLen);
  185. // ------------------ Base64url encoding related functions ---------------------
  186. extern void base64urlEncode(const void *input, size_t inputLen, char_t *output, size_t *outputLen);
  187. extern error_t base64urlDecode(const char_t *input, size_t inputLen, void *output, size_t *outputLen);
  188. // ------------------ ASN.1 related functions ----------------------------------
  189. extern error_t asn1ReadTag(const uint8_t *dataV, size_t length, Asn1Tag *tag);
  190. extern error_t asn1ReadSequence(const uint8_t *dataV, size_t length, Asn1Tag *tag);
  191. extern error_t asn1ReadOctetString(const uint8_t *dataV, size_t length, Asn1Tag *tag);
  192. extern error_t asn1ReadOid(const uint8_t *dataV, size_t length, Asn1Tag *tag);
  193. extern error_t asn1ReadBoolean(const uint8_t *dataV, size_t length, Asn1Tag *tag, bool_t *value);
  194. extern error_t asn1ReadInt32(const uint8_t *dataV, size_t length, Asn1Tag *tag, int32_t *value);
  195. extern error_t mpiGrow(Mpi *r, uint8_t size);
  196. extern error_t asn1ReadMpi(const uint8_t *dataV, size_t length, Asn1Tag *tag, Mpi *value);
  197. extern error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *dataV, size_t *written);
  198. extern error_t asn1WriteInt32(int32_t value, bool_t reverse, uint8_t *dataV,size_t *written);
  199. extern uint8_t mpiGetBitLength(const Mpi *a);
  200. extern uint8_t mpiGetByteLength(const Mpi *a);
  201. extern error_t mpiExport(const Mpi *a, uint8_t *dataV, uint8_t length, MpiFormat format);
  202. extern error_t asn1WriteMpi(const Mpi *value, bool_t reverse, uint8_t *dataV, size_t *written);
  203. extern error_t asn1CheckTag(const Asn1Tag *tag, bool_t constructed, uint_t objClass, uint_t objType);
  204. extern error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length);
  205. extern error_t asn1DumpObject(const uint8_t *dataV, size_t length, uint_t level);
  206. extern int16_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2);
  207. /* ---------------- weigand display ----------------------------------------- */
  208. extern void ConvertWiegand(char* CodBinLeitura, char* CartaoWiegand );
  209. /* ---------------- zigZag -------------------------------------------------- */
  210. extern uint32_t zigzagEncode(int32_t value);
  211. /* ---------------- huffman ------------------------------------------------- */
  212. extern int16_t huffmanEncodeBuf(uint8_t *outBuf, int16_t outBufLen, const uint8_t *inBuf, int16_t inLen, const huffmanTable_t *huffmanTable);
  213. extern int16_t huffmanEncodeBufStreaming(huffmanState_t *state, const uint8_t *inBuf, int16_t inLen, const huffmanTable_t *huffmanTable);
  214. /* ---------------- vigener cipher ------------------------------------------ */
  215. extern void vigenersCipherEncode( char* cipherText, char* keyword, char* message  );
  216. extern void vigenersCipherDecode( char* cipherText, char* keyword, char* message  );
  217. /* ---------------- rot13 cipher -------------------------------------------- */
  218. extern void rot13CipherEncode( char* cipherText, char* keyword, char* message  );      
  219. extern void rot13CipherDecode( char* cipherText, char* keyword, char* message  );
  220. /* ---------------- utf 8 / 16 ---------------------------------------------- */
  221. extern int16_t utf8_encode(int32_t codepoint, char *buffer, size_t *size);             //utf8
  222. extern size_t utf8_check_first(char byte);
  223. extern size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint);
  224. extern const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint);
  225. extern int16_t utf8_check_string(const char *string, size_t length);
  226. extern int16_t encode_utf8(uint8_t  *out, uint32_t in);
  227. extern int16_t decode_utf8(uint32_t *out, const uint8_t *in);
  228. extern int16_t decode_utf16(uint32_t *out, const uint16_t *in);
  229. extern int16_t encode_utf16(uint16_t *out, uint32_t in);
  230. extern size_t utf16_to_utf32(uint32_t  *out, const uint16_t *in, size_t len);
  231. extern size_t utf16_to_utf8(uint8_t *out, const uint16_t *in, size_t len);
  232. extern size_t utf32_to_utf16(uint16_t *out, const uint32_t *in,  size_t len);
  233. extern size_t utf32_to_utf8(uint8_t *out, const uint32_t *in, size_t len);
  234. extern size_t utf8_to_utf16(uint16_t *out, const uint8_t *in, size_t len);
  235. extern size_t utf8_to_utf32(uint32_t *out, const uint8_t *in, size_t len);
  236.  
  237. //C++ guard
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241.  
  242. #endif
Add Comment
Please, Sign In to add comment