ber_helper_functions.h
1 /** 2 * @file ber_helper_functions.h 3 * @author Rene Ceska xceska06 (xceska06@stud.fit.vutbr.cz) 4 * @brief Helper functions for parsing BER LDAP 5 * @date 2023-11-19 6 */ 7 #ifndef BER_HELPER_FUNCTIONS_H 8 #define BER_HELPER_FUNCTIONS_H 9 #include <vector> 10 #include "inc/ber_constants.h" 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string> 14 15 /** 16 * @brief converts std::vector<unsigned char> to lowercase 17 * @param input std::vector<unsigned char> to be converted 18 * @return converted std::vector<unsigned char> 19 */ 20 std::vector<unsigned char> ToLowerCase(std::vector<unsigned char> input); 21 22 /** 23 * @brief parses 1 integer from ldap coded message 24 * 25 * @param s start of the integer in char array 26 * @return int - parsed integer 27 */ 28 int ParseINT(unsigned char *s, int *err); 29 30 /** 31 * @brief writes int in BER LDAP format to char array 32 * 33 * @param s start of the string in char array 34 * @param value int to be written 35 * @return -1 if error, 0 if success 36 */ 37 38 typedef enum filterTypes { 39 AND, 40 OR, 41 NOT, 42 equalityMatch, 43 substrings, 44 undefined, 45 } filterTypes; 46 47 typedef enum berObjectTypes { 48 berSequenceObject, 49 berIntObject, 50 berStringObject, 51 berSetObject, 52 berEnumObject, 53 berBoolObject, 54 berUndefined, 55 berErr, 56 } berObjectTypes; 57 58 /** 59 * @brief returns the number of bytes that will be used to encode the int 60 * 61 * @param value 62 * @return int 63 */ 64 int HowManyBytesWillIntUse(int value); 65 66 /** 67 * @brief writes int in BER LDAP format to char array 68 * 69 * @param s start of the string in char array 70 * @param value int to be written 71 * @return -1 if error, 0 if success 72 */ 73 int WriteIntAppend(std::vector<unsigned char> &s, int value); 74 75 /** 76 * @brief appends length in BER LDAP format to char array (4 bytes) 77 * 78 * @param start vector to append to 79 * @param value 80 * @param err 81 */ 82 void AppendLenght4Bytes(std::vector<unsigned char> &start, int value); 83 84 /** 85 * @brief Get the length of the data of BER attribute 86 * 87 * @param start start of the length 88 * @param err 1 if error, 0 if success 89 * @param end end of the array 90 * @return 91 */ 92 int GetLength(std::vector<unsigned char>::iterator start, int *err,std::vector<unsigned char>::iterator end); 93 94 /** 95 * @brief Parses value of BERInteger from char array 96 * 97 * @param s start of the integer in char array 98 * @param err 1 if error, 0 if success 99 * @param end end of the array 100 * @return unsigned int 101 */ 102 unsigned int ParseINT(std::vector<unsigned char>::iterator s, int *err,std::vector<unsigned char>::iterator end); 103 104 /** 105 * @brief Get the Length Of Length of BER attribute 106 * 107 * @param start start of the length 108 * @param err 1 if error, 0 if success 109 * @param end end of the array 110 * @return int 111 */ 112 int GetLengthOfLength(std::vector<unsigned char>::iterator start, int *err,std::vector<unsigned char>::iterator end); 113 114 /** 115 * @brief skips n BER attributes from char array, returns incremented iterator 116 * 117 * @param start start of the tag 118 * @param n number of tags to skip 119 * @param err 1 if error, 0 if success 120 * @param end end of the array 121 */ 122 void SkipTags(std::vector<unsigned char>::iterator &start, int n, int *err, std::vector<unsigned char>::iterator end); 123 124 /** 125 * @brief goes into the sequence/set tag and returns incremented iterator which points to the first attribute in the sequence/set 126 * 127 * @param start start of the tag 128 * @param err 1 if error, 0 if success 129 * @param end end of the array 130 */ 131 void GoIntoTag(std::vector<unsigned char>::iterator &start, int *err, std::vector<unsigned char>::iterator end); 132 133 /** 134 * @brief Increases 4Bytes longform length of the attribute by n 135 * 136 * @param start start of the length 137 * @param n number by which the length will be increased 138 * @param err 1 if error, 0 if success 139 * @param end end of the array 140 */ 141 void IncreaseLength4Bytes(std::vector<unsigned char>::iterator &start, int n, 142 int *err,std::vector<unsigned char>::iterator end); 143 144 /** 145 * @brief Parses type of filter from char array and returns its enum 146 * 147 * @param start start of the filter 148 * @return filterTypes 149 */ 150 filterTypes getFilterType(std::vector<unsigned char>::iterator start); 151 152 #endif