00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #define LINUX
00060
00061 #if defined __KERNEL__ || defined _KERNEL
00062
00063
00064 #if defined LINUX || defined LINUX26_SUPPORT || LINUX_VERSION_CODE
00065 #include <linux/stddef.h>
00066 #include <linux/types.h>
00067 #elif defined __FreeBSD__
00068 #include <stdio.h>
00069 #include <sys/types.h>
00070 #elif defined __sun__
00071 #include <stdio.h>
00072 #include <sys/types.h>
00073 #endif
00074
00075 #else
00076
00077
00078
00079 #include <stdio.h>
00080
00081
00082 #include <sys/types.h>
00083 #ifndef __BIT_TYPES_DEFINED__
00084 #if defined __sun__ || defined __OpenBSD__ || defined __FreeBSD__
00085 #include <inttypes.h>
00086 #else
00087 #include <stdint.h>
00088 #endif
00089 #endif
00090
00091 #endif
00092
00093 #include "dazuko_transport.h"
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 #undef WANT_INT_PTR_NOTIFICATION
00108
00109 #if defined(_LP64) || defined(_I32LPx)
00110 #if defined WANT_INT_PTR_NOTIFICATION
00111 #warning using 64bit integer to store pointers
00112 #endif
00113 #define int_ptr_t int64_t
00114 #else
00115 #if defined WANT_INT_PTR_NOTIFICATION
00116 #warning using 32bit integer to store pointers
00117 #endif
00118 #define int_ptr_t int32_t
00119 #endif
00120
00121
00122
00123
00124
00125 static int getbyte(unsigned char **p, int *b)
00126 {
00127 if (p == NULL)
00128 return -1;
00129
00130 if (*p == NULL)
00131 return -1;
00132
00133 if (b == NULL)
00134 return -1;
00135
00136 *b = **p;
00137 (*p)++;
00138
00139 return 0;
00140 }
00141
00142
00143
00144 static int getinteger(unsigned char **p, int *b, int count)
00145 {
00146 int res;
00147 int idx;
00148
00149 if (p == NULL)
00150 return -1;
00151
00152 if (*p == NULL)
00153 return -1;
00154
00155 if (b == NULL)
00156 return -1;
00157
00158 res = 0;
00159 for (idx=0 ; idx<count ; idx++)
00160 {
00161 res <<= 8;
00162 res += **p;
00163 (*p)++;
00164 }
00165 *b = res;
00166
00167 return 0;
00168 }
00169
00170 int dazuko_reqstream_chunksize(unsigned char *ll, int *size)
00171 {
00172
00173
00174
00175 unsigned char *p = ll;
00176
00177 return getinteger(&p, size, 4);
00178 }
00179
00180
00181
00182 static int getpointer(unsigned char **p, char **b, int count)
00183 {
00184 int_ptr_t res;
00185 int idx;
00186
00187 if (p == NULL)
00188 return -1;
00189
00190 if (*p == NULL)
00191 return -1;
00192
00193 if (b == NULL)
00194 return -1;
00195
00196 res = 0;
00197 for (idx = 0; idx<count ; idx++)
00198 {
00199 res <<= 8;
00200 res += **p;
00201 (*p)++;
00202 }
00203
00204 *b = (void *)res;
00205
00206 return 0;
00207 }
00208
00209
00210
00211 static int putbyte(unsigned char **p, int b)
00212 {
00213 if (p == NULL)
00214 return -1;
00215
00216 if (*p == NULL)
00217 return -1;
00218
00219 **p = b;
00220 (*p)++;
00221
00222 return 0;
00223 }
00224
00225
00226
00227 static int putinteger(unsigned char **p, int b, int count)
00228 {
00229 int val;
00230 int idx;
00231
00232 if (p == NULL)
00233 return -1;
00234
00235 if (*p == NULL)
00236 return -1;
00237
00238 val = b;
00239 for (idx=1 ; idx<=count ; idx++)
00240 {
00241 *(*p + count - idx) = (val & 0xFF);
00242 val >>= 8;
00243 }
00244 *p += count;
00245
00246 return 0;
00247 }
00248
00249
00250
00251 static int putpointer(unsigned char **p, char *b, int count)
00252 {
00253 int_ptr_t val;
00254 int idx;
00255
00256 if (p == NULL)
00257 return -1;
00258
00259 if (*p == NULL)
00260 return -1;
00261
00262 val = (int_ptr_t)b;
00263 for (idx=1 ; idx<=count ; idx++)
00264 {
00265 *(*p + count - idx) = (val & 0xFF);
00266 val >>= 8;
00267 }
00268 *p += count;
00269
00270 return(0);
00271 }
00272
00273
00274
00275 static int skipinteger(unsigned char **p, int count)
00276 {
00277 if (p == NULL)
00278 return -1;
00279
00280 if (*p == NULL)
00281 return -1;
00282
00283 (*p) += count;
00284
00285 return 0;
00286 }
00287
00288 static int skippointer(unsigned char **p, int count)
00289 {
00290 if (p == NULL)
00291 return -1;
00292
00293 if (*p == NULL)
00294 return -1;
00295
00296 (*p) += count;
00297
00298 return(0);
00299 }
00300
00301
00302
00303
00304 int dazuko_reqstream_dim_chunk0(int size_chr, int size_int, int size_ptr)
00305 {
00306
00307
00308
00309
00310
00311
00312
00313 return (4 + (4 * size_chr) + (4 * size_int) + (2 * size_ptr));
00314 }
00315
00316
00317
00318
00319
00320 int dazuko_reqstream_hl2ll(struct dazuko_request *req, unsigned char *ll)
00321 {
00322 unsigned char *wrptr;
00323 int size_chr;
00324 int size_int;
00325 int size_ptr;
00326 int size_req;
00327 int type_as_int;
00328
00329 wrptr = ll;
00330
00331
00332 size_chr = sizeof(char);
00333 size_int = sizeof(int);
00334 size_ptr = sizeof(void *);
00335 size_req = dazuko_reqstream_dim_chunk0(size_chr, size_int, size_ptr);
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 if (size_chr != 1)
00347 return -1;
00348
00349
00350 type_as_int = (req->type[0] << 8) + (req->type[1] << 0);
00351
00352
00353 if (putinteger(&wrptr, size_req, 4) != 0) return -1;
00354 if (putbyte(&wrptr, 0) != 0) return -1;
00355 if (putbyte(&wrptr, size_chr) != 0) return -1;
00356 if (putbyte(&wrptr, size_int) != 0) return -1;
00357 if (putbyte(&wrptr, size_ptr) != 0) return -1;
00358 if (putinteger(&wrptr, type_as_int, size_int) != 0) return -1;
00359 if (putinteger(&wrptr, req->buffer_size, size_int) != 0) return -1;
00360 if (putpointer(&wrptr, req->buffer, size_ptr) != 0) return -1;
00361 if (putinteger(&wrptr, req->reply_buffer_size, size_int) != 0) return -1;
00362 if (putpointer(&wrptr, req->reply_buffer, size_ptr) != 0) return -1;
00363 if (putinteger(&wrptr, req->reply_buffer_size_used, size_int) != 0) return -1;
00364
00365
00366 return 0;
00367 }
00368
00369
00370
00371
00372
00373 int dazuko_reqstream_ll2hl(unsigned char *ll, struct dazuko_request *req, int strict)
00374 {
00375 unsigned char *rdptr;
00376 int size_req;
00377 int req_used;
00378 int size_chr;
00379 int size_int;
00380 int size_ptr;
00381 int type_as_int;
00382
00383 rdptr = (unsigned char *)ll;
00384
00385
00386 if (getinteger(&rdptr, &size_req, 4) != 0) return -1;
00387 if (strict)
00388 {
00389 if (size_req != dazuko_reqstream_dim_chunk0(sizeof(char), sizeof(int), sizeof(void *)))
00390 return -1;
00391 }
00392
00393
00394 if (getbyte(&rdptr, &req_used) != 0) return -1;
00395 if (strict)
00396 {
00397 if (!req_used)
00398 return -1;
00399 }
00400
00401
00402 if (getbyte(&rdptr, &size_chr) != 0) return -1;
00403 if (getbyte(&rdptr, &size_int) != 0) return -1;
00404 if (getbyte(&rdptr, &size_ptr) != 0) return -1;
00405 if (strict)
00406 {
00407 if ((size_chr != sizeof(char)) || (size_int != sizeof(int)) || (size_ptr != sizeof(void *)))
00408 return -1;
00409 }
00410 if (size_req != dazuko_reqstream_dim_chunk0(size_chr, size_int, size_ptr))
00411 return -1;
00412
00413
00414 if (getinteger(&rdptr, &type_as_int, size_int) != 0) return -1;
00415 if (getinteger(&rdptr, &req->buffer_size, size_int) != 0) return -1;
00416 if (getpointer(&rdptr, &req->buffer, size_ptr) != 0) return -1;
00417 if (getinteger(&rdptr, &req->reply_buffer_size, size_int) != 0) return -1;
00418 if (getpointer(&rdptr, &req->reply_buffer, size_ptr) != 0) return -1;
00419 if (getinteger(&rdptr, &req->reply_buffer_size_used, size_int) != 0) return -1;
00420
00421
00422 req->type[0] = (type_as_int >> 8) & 0xFF;
00423 req->type[1] = (type_as_int >> 0) & 0xFF;
00424
00425
00426 return 0;
00427 }
00428
00429
00430
00431
00432
00433 int dazuko_reqstream_updll(struct dazuko_request *req, unsigned char *ll)
00434 {
00435 unsigned char *wrptr;
00436 int size_chr;
00437 int size_int;
00438 int size_ptr;
00439 int size_req;
00440
00441 wrptr = ll;
00442
00443
00444 if (getinteger(&wrptr, &size_req, 4) != 0) return -1;
00445
00446
00447 if (size_req < dazuko_reqstream_dim_chunk0(1, 4, 4))
00448 return -1;
00449
00450
00451
00452
00453
00454
00455
00456 if (putbyte(&wrptr, 1) != 0) return -1;
00457
00458
00459 if (getbyte(&wrptr, &size_chr) != 0) return -1;
00460 if (getbyte(&wrptr, &size_int) != 0) return -1;
00461 if (getbyte(&wrptr, &size_ptr) != 0) return -1;
00462 if (size_chr != 1)
00463 return -1;
00464 if (size_req != dazuko_reqstream_dim_chunk0(size_chr, size_int, size_ptr))
00465 return -1;
00466
00467
00468
00469 if (skipinteger(&wrptr, size_int) != 0) return -1;
00470 if (skipinteger(&wrptr, size_int) != 0) return -1;
00471 if (skippointer(&wrptr, size_ptr) != 0) return -1;
00472 if (skipinteger(&wrptr, size_int) != 0) return -1;
00473 if (skippointer(&wrptr, size_ptr) != 0) return -1;
00474 if (putinteger(&wrptr, req->reply_buffer_size_used, size_int) != 0) return -1;
00475
00476
00477 return 0;
00478 }
00479