00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __MYSAC_UTILS_H__
00020 #define __MYSAC_UTILS_H__
00021
00022 #include <stdint.h>
00023
00024 #include "mysac.h"
00025
00026 #define DEBUG(fmt, args...) fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##args);
00027
00028
00029
00030 static inline void INIT_LIST_HEAD(struct mysac_list_head *list) {
00031 list->next = list;
00032 list->prev = list;
00033 }
00034
00035
00036
00037
00038
00039
00040
00041 static inline void __list_add(struct mysac_list_head *new,
00042 struct mysac_list_head *prev,
00043 struct mysac_list_head *next) {
00044 next->prev = new;
00045 new->next = next;
00046 new->prev = prev;
00047 prev->next = new;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 static inline void list_add_tail(struct mysac_list_head *new,
00059 struct mysac_list_head *head) {
00060 __list_add(new, head->prev, head);
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070 static inline void __list_del(struct mysac_list_head * prev,
00071 struct mysac_list_head * next) {
00072 next->prev = prev;
00073 prev->next = next;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 static inline void list_del(struct mysac_list_head *entry) {
00083 __list_del(entry->prev, entry->next);
00084 }
00085
00086
00087
00088 static inline void to_my_2(int value, char *m) {
00089 m[1] = value >> 8;
00090 m[0] = value;
00091 }
00092 static inline void to_my_3(int value, char *m) {
00093 m[2] = value >> 16;
00094 m[1] = value >> 8;
00095 m[0] = value;
00096 }
00097 static inline void to_my_4(int value, char *m) {
00098 m[3] = value >> 24;
00099 m[2] = value >> 16;
00100 m[1] = value >> 8;
00101 m[0] = value;
00102 }
00103 static inline void to_my_8(long long value, char *m) {
00104 m[7] = value >> 56;
00105 m[6] = value >> 48;
00106 m[5] = value >> 40;
00107 m[4] = value >> 32;
00108 m[3] = value >> 24;
00109 m[2] = value >> 16;
00110 m[1] = value >> 8;
00111 m[0] = value;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 static inline int my_lcb_ll(char *m, unsigned long long *r, char *nul, int len) {
00125 if (len < 1)
00126 return -1;
00127 switch ((unsigned char)m[0]) {
00128
00129 case 251:
00130 *r = 0;
00131 *nul=1;
00132 return 1;
00133
00134 case 252:
00135 if (len < 3)
00136 return -1;
00137 *r = uint2korr(&m[1]);
00138 *nul=0;
00139 return 3;
00140
00141 case 253:
00142 if (len < 4)
00143 return -1;
00144 *r = uint3korr(&m[1]);
00145 *nul=0;
00146 return 4;
00147
00148 case 254:
00149 if (len < 9)
00150 return -1;
00151 *r = uint8korr(&m[1]);
00152 *nul=0;
00153 return 9;
00154
00155 default:
00156 *r = (unsigned char)m[0];
00157 *nul=0;
00158 return 1;
00159 }
00160 }
00161
00162 static inline int my_lcb(char *m, unsigned long *r, char *nul, int len) {
00163 unsigned long long val;
00164 int retcode;
00165 retcode = my_lcb_ll(m, &val, nul, len);
00166 *r = val;
00167 return retcode;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 static inline int set_my_lcb(unsigned long long len, int null, char *out, int out_len) {
00181
00182
00183 if (null == 1) {
00184 if (out_len < 1)
00185 return -1;
00186 out[0] = 251;
00187 return 1;
00188 }
00189
00190
00191 if (len >= 0 && len <= 250) {
00192 if (out_len < 1)
00193 return -1;
00194 out[0] = len;
00195 return 1;
00196 }
00197
00198
00199 if (len >= 251 && len < 0xffff) {
00200 if (out_len < 3)
00201 return -1;
00202 out[0] = 252;
00203 to_my_2(len, &out[1]);
00204 return 3;
00205 }
00206
00207
00208 if (len > 0xffff && len <= 0xffffff) {
00209 if (out_len < 4)
00210 return -1;
00211 out[0] = 253;
00212 to_my_3(len, &out[1]);
00213 return 4;
00214 }
00215
00216
00217 if (len > 0xffffff && len <= 0xffffffff) {
00218 if (out_len < 5)
00219 return -1;
00220 out[0] = 254;
00221 to_my_4(len, &out[1]);
00222 return 5;
00223 }
00224
00225
00226 if (len > 0xffffffff && len <= 0xffffffffffffffffULL) {
00227 if (out_len < 9)
00228 return -1;
00229 out[0] = 254;
00230 to_my_4(len, &out[1]);
00231 return 9;
00232 }
00233
00234
00235 return -1;
00236 }
00237
00238 #endif