~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzle_common.h

  • Committer: Monty Taylor
  • Date: 2008-10-22 01:52:54 UTC
  • Revision ID: monty@inaugust.com-20081022015254-65qfk9f2v0b8jlk3
Moved drizzle_com to drizzled/drizzle_common. Started splitting it up.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
** Common definition between mysql server & client
22
22
*/
23
23
 
24
 
#ifndef _libdrizzle_drizzle_com_h
25
 
#define _libdrizzle_drizzle_com_h
 
24
#ifndef DRIZZLED_DRIZZLE_COMMON_H
 
25
#define DRIZZLED_DRIZZLE_COMMON__H
26
26
 
27
 
#include <config.h>
28
27
#include <unistd.h>
29
28
#include <stdbool.h>
30
29
#include <stdint.h>
366
365
 
367
366
#define net_new_transaction(net) ((net)->pkt_nr=0)
368
367
 
369
 
#ifdef __cplusplus
370
 
extern "C" {
371
 
#endif
372
 
 
373
 
 
374
 
  struct rand_struct {
375
 
    unsigned long seed1,seed2,max_value;
376
 
    double max_value_dbl;
377
 
  };
378
 
 
379
 
#ifdef __cplusplus
380
 
}
381
 
#endif
382
368
 
383
369
  /* The following is for user defined functions */
384
370
 
423
409
extern "C" {
424
410
#endif
425
411
 
426
 
/*
427
 
  These functions are used for authentication by client and server and
428
 
  implemented in sql/password.c
429
 
*/
430
 
 
431
 
  void randominit(struct rand_struct *, uint32_t seed1, uint32_t seed2);
432
 
  double my_rnd(struct rand_struct *);
433
 
  void create_random_string(char *to, unsigned int length,
434
 
                            struct rand_struct *rand_st);
435
 
 
436
 
  void hash_password(uint32_t *to, const char *password, uint32_t password_len);
437
 
 
438
 
  void make_scrambled_password(char *to, const char *password);
439
 
  void scramble(char *to, const char *message, const char *password);
440
 
  bool check_scramble(const char *reply, const char *message,
441
 
                      const unsigned char *hash_stage2);
442
 
  void get_salt_from_password(unsigned char *res, const char *password);
443
 
  void make_password_from_salt(char *to, const unsigned char *hash_stage2);
444
 
  char *octet2hex(char *to, const char *str, unsigned int len);
445
 
 
446
 
/* end of password.c */
447
412
 
448
413
  char *get_tty_password(const char *opt_message);
449
414
 
451
416
  uint64_t net_field_length_ll(unsigned char **packet);
452
417
  unsigned char *net_store_length(unsigned char *pkg, uint64_t length);
453
418
 
454
 
/*
455
 
  Define-funktions for reading and storing in machine independent format
456
 
  (low byte first)
457
 
*/
458
 
 
459
 
/* Optimized store functions for Intel x86 */
460
 
#if defined(__i386__)
461
 
#define sint2korr(A)    (*((int16_t *) (A)))
462
 
#define sint3korr(A)    ((int32_t) ((((unsigned char) (A)[2]) & 128) ? \
463
 
                                  (((uint32_t) 255L << 24) | \
464
 
                                   (((uint32_t) (unsigned char) (A)[2]) << 16) |\
465
 
                                   (((uint32_t) (unsigned char) (A)[1]) << 8) | \
466
 
                                   ((uint32_t) (unsigned char) (A)[0])) : \
467
 
                                  (((uint32_t) (unsigned char) (A)[2]) << 16) |\
468
 
                                  (((uint32_t) (unsigned char) (A)[1]) << 8) | \
469
 
                                  ((uint32_t) (unsigned char) (A)[0])))
470
 
#define sint4korr(A)    (*((long *) (A)))
471
 
#define uint2korr(A)    (*((uint16_t *) (A)))
472
 
#if defined(HAVE_purify)
473
 
#define uint3korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
474
 
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
475
 
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16))
476
 
#else
477
 
/*
478
 
   ATTENTION !
479
 
   
480
 
    Please, note, uint3korr reads 4 bytes (not 3) !
481
 
    It means, that you have to provide enough allocated space !
482
 
*/
483
 
#define uint3korr(A)    (long) (*((unsigned int *) (A)) & 0xFFFFFF)
484
 
#endif /* HAVE_purify */
485
 
#define uint4korr(A)    (*((uint32_t *) (A)))
486
 
#define uint5korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
487
 
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
488
 
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
489
 
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
490
 
                                    (((uint64_t) ((unsigned char) (A)[4])) << 32))
491
 
#define uint6korr(A)    ((uint64_t)(((uint32_t)    ((unsigned char) (A)[0]))          + \
492
 
                                     (((uint32_t)    ((unsigned char) (A)[1])) << 8)   + \
493
 
                                     (((uint32_t)    ((unsigned char) (A)[2])) << 16)  + \
494
 
                                     (((uint32_t)    ((unsigned char) (A)[3])) << 24)) + \
495
 
                         (((uint64_t) ((unsigned char) (A)[4])) << 32) +       \
496
 
                         (((uint64_t) ((unsigned char) (A)[5])) << 40))
497
 
#define uint8korr(A)    (*((uint64_t *) (A)))
498
 
#define sint8korr(A)    (*((int64_t *) (A)))
499
 
#define int2store(T,A)  *((uint16_t*) (T))= (uint16_t) (A)
500
 
#define int3store(T,A)  do { *(T)=  (unsigned char) ((A));\
501
 
                            *(T+1)=(unsigned char) (((uint32_t) (A) >> 8));\
502
 
                            *(T+2)=(unsigned char) (((A) >> 16)); } while (0)
503
 
#define int4store(T,A)  *((long *) (T))= (long) (A)
504
 
#define int5store(T,A)  do { *(T)= (unsigned char)((A));\
505
 
                             *((T)+1)=(unsigned char) (((A) >> 8));\
506
 
                             *((T)+2)=(unsigned char) (((A) >> 16));\
507
 
                             *((T)+3)=(unsigned char) (((A) >> 24)); \
508
 
                             *((T)+4)=(unsigned char) (((A) >> 32)); } while(0)
509
 
#define int6store(T,A)  do { *(T)=    (unsigned char)((A));          \
510
 
                             *((T)+1)=(unsigned char) (((A) >> 8));  \
511
 
                             *((T)+2)=(unsigned char) (((A) >> 16)); \
512
 
                             *((T)+3)=(unsigned char) (((A) >> 24)); \
513
 
                             *((T)+4)=(unsigned char) (((A) >> 32)); \
514
 
                             *((T)+5)=(unsigned char) (((A) >> 40)); } while(0)
515
 
#define int8store(T,A)  *((uint64_t *) (T))= (uint64_t) (A)
516
 
 
517
 
typedef union {
518
 
  double v;
519
 
  long m[2];
520
 
} doubleget_union;
521
 
#define doubleget(V,M)  \
522
 
do { doubleget_union _tmp; \
523
 
     _tmp.m[0] = *((long*)(M)); \
524
 
     _tmp.m[1] = *(((long*) (M))+1); \
525
 
     (V) = _tmp.v; } while(0)
526
 
#define doublestore(T,V) do { *((long *) T) = ((doubleget_union *)&V)->m[0]; \
527
 
                             *(((long *) T)+1) = ((doubleget_union *)&V)->m[1]; \
528
 
                         } while (0)
529
 
#define float4get(V,M)   do { *((float *) &(V)) = *((float*) (M)); } while(0)
530
 
#define float8get(V,M)   doubleget((V),(M))
531
 
#define float4store(V,M) memcpy(V, (&M), sizeof(float))
532
 
#define floatstore(T,V)  memcpy((T), (&V), sizeof(float))
533
 
#define floatget(V,M)    memcpy(&V, (M), sizeof(float))
534
 
#define float8store(V,M) doublestore((V),(M))
535
 
#else
536
 
 
537
 
/*
538
 
  We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
539
 
  were done before)
540
 
*/
541
 
#define sint2korr(A)    (int16_t) (((int16_t) ((unsigned char) (A)[0])) +\
542
 
                                 ((int16_t) ((int16_t) (A)[1]) << 8))
543
 
#define sint3korr(A)    ((int32_t) ((((unsigned char) (A)[2]) & 128) ? \
544
 
                                  (((uint32_t) 255L << 24) | \
545
 
                                   (((uint32_t) (unsigned char) (A)[2]) << 16) |\
546
 
                                   (((uint32_t) (unsigned char) (A)[1]) << 8) | \
547
 
                                   ((uint32_t) (unsigned char) (A)[0])) : \
548
 
                                  (((uint32_t) (unsigned char) (A)[2]) << 16) |\
549
 
                                  (((uint32_t) (unsigned char) (A)[1]) << 8) | \
550
 
                                  ((uint32_t) (unsigned char) (A)[0])))
551
 
#define sint4korr(A)    (int32_t) (((int32_t) ((unsigned char) (A)[0])) +\
552
 
                                (((int32_t) ((unsigned char) (A)[1]) << 8)) +\
553
 
                                (((int32_t) ((unsigned char) (A)[2]) << 16)) +\
554
 
                                (((int32_t) ((int16_t) (A)[3]) << 24)))
555
 
#define sint8korr(A)    (int64_t) uint8korr(A)
556
 
#define uint2korr(A)    (uint16_t) (((uint16_t) ((unsigned char) (A)[0])) +\
557
 
                                  ((uint16_t) ((unsigned char) (A)[1]) << 8))
558
 
#define uint3korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
559
 
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
560
 
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16))
561
 
#define uint4korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
562
 
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
563
 
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
564
 
                                  (((uint32_t) ((unsigned char) (A)[3])) << 24))
565
 
#define uint5korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
566
 
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
567
 
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
568
 
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
569
 
                                    (((uint64_t) ((unsigned char) (A)[4])) << 32))
570
 
#define uint6korr(A)    ((uint64_t)(((uint32_t)    ((unsigned char) (A)[0]))          + \
571
 
                                     (((uint32_t)    ((unsigned char) (A)[1])) << 8)   + \
572
 
                                     (((uint32_t)    ((unsigned char) (A)[2])) << 16)  + \
573
 
                                     (((uint32_t)    ((unsigned char) (A)[3])) << 24)) + \
574
 
                         (((uint64_t) ((unsigned char) (A)[4])) << 32) +       \
575
 
                         (((uint64_t) ((unsigned char) (A)[5])) << 40))
576
 
#define uint8korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
577
 
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
578
 
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
579
 
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
580
 
                        (((uint64_t) (((uint32_t) ((unsigned char) (A)[4])) +\
581
 
                                    (((uint32_t) ((unsigned char) (A)[5])) << 8) +\
582
 
                                    (((uint32_t) ((unsigned char) (A)[6])) << 16) +\
583
 
                                    (((uint32_t) ((unsigned char) (A)[7])) << 24))) <<\
584
 
                                    32))
585
 
#define int2store(T,A)       do { uint32_t def_temp= (uint32_t) (A) ;\
586
 
                                  *((unsigned char*) (T))=  (unsigned char)(def_temp); \
587
 
                                   *((unsigned char*) (T)+1)=(unsigned char)((def_temp >> 8)); \
588
 
                             } while(0)
589
 
#define int3store(T,A)       do { /*lint -save -e734 */\
590
 
                                  *((unsigned char*)(T))=(unsigned char) ((A));\
591
 
                                  *((unsigned char*) (T)+1)=(unsigned char) (((A) >> 8));\
592
 
                                  *((unsigned char*)(T)+2)=(unsigned char) (((A) >> 16)); \
593
 
                                  /*lint -restore */} while(0)
594
 
#define int4store(T,A)       do { *((char *)(T))=(char) ((A));\
595
 
                                  *(((char *)(T))+1)=(char) (((A) >> 8));\
596
 
                                  *(((char *)(T))+2)=(char) (((A) >> 16));\
597
 
                                  *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
598
 
#define int5store(T,A)       do { *((char *)(T))=     (char)((A));  \
599
 
                                  *(((char *)(T))+1)= (char)(((A) >> 8)); \
600
 
                                  *(((char *)(T))+2)= (char)(((A) >> 16)); \
601
 
                                  *(((char *)(T))+3)= (char)(((A) >> 24)); \
602
 
                                  *(((char *)(T))+4)= (char)(((A) >> 32)); \
603
 
                                } while(0)
604
 
#define int6store(T,A)       do { *((char *)(T))=     (char)((A)); \
605
 
                                  *(((char *)(T))+1)= (char)(((A) >> 8)); \
606
 
                                  *(((char *)(T))+2)= (char)(((A) >> 16)); \
607
 
                                  *(((char *)(T))+3)= (char)(((A) >> 24)); \
608
 
                                  *(((char *)(T))+4)= (char)(((A) >> 32)); \
609
 
                                  *(((char *)(T))+5)= (char)(((A) >> 40)); \
610
 
                                } while(0)
611
 
#define int8store(T,A)       do { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
612
 
                                  int4store((T),def_temp); \
613
 
                                  int4store((T+4),def_temp2); } while(0)
614
 
#ifdef WORDS_BIGENDIAN
615
 
#define float4store(T,A) do { *(T)= ((unsigned char *) &A)[3];\
616
 
                              *((T)+1)=(char) ((unsigned char *) &A)[2];\
617
 
                              *((T)+2)=(char) ((unsigned char *) &A)[1];\
618
 
                              *((T)+3)=(char) ((unsigned char *) &A)[0]; } while(0)
619
 
 
620
 
#define float4get(V,M)   do { float def_temp;\
621
 
                              ((unsigned char*) &def_temp)[0]=(M)[3];\
622
 
                              ((unsigned char*) &def_temp)[1]=(M)[2];\
623
 
                              ((unsigned char*) &def_temp)[2]=(M)[1];\
624
 
                              ((unsigned char*) &def_temp)[3]=(M)[0];\
625
 
                              (V)=def_temp; } while(0)
626
 
#define float8store(T,V) do { *(T)= ((unsigned char *) &V)[7];\
627
 
                              *((T)+1)=(char) ((unsigned char *) &V)[6];\
628
 
                              *((T)+2)=(char) ((unsigned char *) &V)[5];\
629
 
                              *((T)+3)=(char) ((unsigned char *) &V)[4];\
630
 
                              *((T)+4)=(char) ((unsigned char *) &V)[3];\
631
 
                              *((T)+5)=(char) ((unsigned char *) &V)[2];\
632
 
                              *((T)+6)=(char) ((unsigned char *) &V)[1];\
633
 
                              *((T)+7)=(char) ((unsigned char *) &V)[0]; } while(0)
634
 
 
635
 
#define float8get(V,M)   do { double def_temp;\
636
 
                              ((unsigned char*) &def_temp)[0]=(M)[7];\
637
 
                              ((unsigned char*) &def_temp)[1]=(M)[6];\
638
 
                              ((unsigned char*) &def_temp)[2]=(M)[5];\
639
 
                              ((unsigned char*) &def_temp)[3]=(M)[4];\
640
 
                              ((unsigned char*) &def_temp)[4]=(M)[3];\
641
 
                              ((unsigned char*) &def_temp)[5]=(M)[2];\
642
 
                              ((unsigned char*) &def_temp)[6]=(M)[1];\
643
 
                              ((unsigned char*) &def_temp)[7]=(M)[0];\
644
 
                              (V) = def_temp; } while(0)
645
 
#else
646
 
#define float4get(V,M)   memcpy(&V, (M), sizeof(float))
647
 
#define float4store(V,M) memcpy(V, (&M), sizeof(float))
648
 
 
649
 
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
650
 
#define doublestore(T,V) do { *(((char*)T)+0)=(char) ((unsigned char *) &V)[4];\
651
 
                              *(((char*)T)+1)=(char) ((unsigned char *) &V)[5];\
652
 
                              *(((char*)T)+2)=(char) ((unsigned char *) &V)[6];\
653
 
                              *(((char*)T)+3)=(char) ((unsigned char *) &V)[7];\
654
 
                              *(((char*)T)+4)=(char) ((unsigned char *) &V)[0];\
655
 
                              *(((char*)T)+5)=(char) ((unsigned char *) &V)[1];\
656
 
                              *(((char*)T)+6)=(char) ((unsigned char *) &V)[2];\
657
 
                              *(((char*)T)+7)=(char) ((unsigned char *) &V)[3]; }\
658
 
                         while(0)
659
 
#define doubleget(V,M)   do { double def_temp;\
660
 
                              ((unsigned char*) &def_temp)[0]=(M)[4];\
661
 
                              ((unsigned char*) &def_temp)[1]=(M)[5];\
662
 
                              ((unsigned char*) &def_temp)[2]=(M)[6];\
663
 
                              ((unsigned char*) &def_temp)[3]=(M)[7];\
664
 
                              ((unsigned char*) &def_temp)[4]=(M)[0];\
665
 
                              ((unsigned char*) &def_temp)[5]=(M)[1];\
666
 
                              ((unsigned char*) &def_temp)[6]=(M)[2];\
667
 
                              ((unsigned char*) &def_temp)[7]=(M)[3];\
668
 
                              (V) = def_temp; } while(0)
669
 
#endif /* __FLOAT_WORD_ORDER */
670
 
 
671
 
#define float8get(V,M)   doubleget((V),(M))
672
 
#define float8store(V,M) doublestore((V),(M))
673
 
#endif /* WORDS_BIGENDIAN */
674
 
 
675
 
#endif /* __i386__ */
676
 
 
677
 
/*
678
 
  Macro for reading 32-bit integer from network byte order (big-endian)
679
 
  from unaligned memory location.
680
 
*/
681
 
#define int4net(A)        (int32_t) (((uint32_t) ((unsigned char) (A)[3]))        |\
682
 
                                  (((uint32_t) ((unsigned char) (A)[2])) << 8)  |\
683
 
                                  (((uint32_t) ((unsigned char) (A)[1])) << 16) |\
684
 
                                  (((uint32_t) ((unsigned char) (A)[0])) << 24))
685
 
/*
686
 
  Define-funktions for reading and storing in machine format from/to
687
 
  short/long to/from some place in memory V should be a (not
688
 
  register) variable, M is a pointer to byte
689
 
*/
690
 
 
691
 
#ifdef WORDS_BIGENDIAN
692
 
 
693
 
#define ushortget(V,M)  do { V = (uint16_t) (((uint16_t) ((unsigned char) (M)[1]))+\
694
 
                                 ((uint16_t) ((uint16_t) (M)[0]) << 8)); } while(0)
695
 
#define shortget(V,M)   do { V = (short) (((short) ((unsigned char) (M)[1]))+\
696
 
                                 ((short) ((short) (M)[0]) << 8)); } while(0)
697
 
#define longget(V,M)    do { int32_t def_temp;\
698
 
                             ((unsigned char*) &def_temp)[0]=(M)[0];\
699
 
                             ((unsigned char*) &def_temp)[1]=(M)[1];\
700
 
                             ((unsigned char*) &def_temp)[2]=(M)[2];\
701
 
                             ((unsigned char*) &def_temp)[3]=(M)[3];\
702
 
                             (V)=def_temp; } while(0)
703
 
#define ulongget(V,M)   do { uint32_t def_temp;\
704
 
                            ((unsigned char*) &def_temp)[0]=(M)[0];\
705
 
                            ((unsigned char*) &def_temp)[1]=(M)[1];\
706
 
                            ((unsigned char*) &def_temp)[2]=(M)[2];\
707
 
                            ((unsigned char*) &def_temp)[3]=(M)[3];\
708
 
                            (V)=def_temp; } while(0)
709
 
#define shortstore(T,A) do { uint32_t def_temp=(uint32_t) (A) ;\
710
 
                             *(((char*)T)+1)=(char)(def_temp); \
711
 
                             *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
712
 
#define longstore(T,A)  do { *(((char*)T)+3)=((A));\
713
 
                             *(((char*)T)+2)=(((A) >> 8));\
714
 
                             *(((char*)T)+1)=(((A) >> 16));\
715
 
                             *(((char*)T)+0)=(((A) >> 24)); } while(0)
716
 
 
717
 
#define floatget(V,M)     memcpy(&V, (M), sizeof(float))
718
 
#define floatstore(T, V)   memcpy((T), (&V), sizeof(float))
719
 
#define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
720
 
#define doublestore(T, V)  memcpy((T), &V, sizeof(double))
721
 
#define int64_tget(V, M)   memcpy(&V, (M), sizeof(uint64_t))
722
 
#define int64_tstore(T, V) memcpy((T), &V, sizeof(uint64_t))
723
 
 
724
 
#else
725
 
 
726
 
#define ushortget(V,M)  do { V = uint2korr(M); } while(0)
727
 
#define shortget(V,M)   do { V = sint2korr(M); } while(0)
728
 
#define longget(V,M)    do { V = sint4korr(M); } while(0)
729
 
#define ulongget(V,M)   do { V = uint4korr(M); } while(0)
730
 
#define shortstore(T,V) int2store(T,V)
731
 
#define longstore(T,V)  int4store(T,V)
732
 
#ifndef floatstore
733
 
#define floatstore(T,V)   memcpy((T), (&V), sizeof(float))
734
 
#define floatget(V,M)     memcpy(&V, (M), sizeof(float))
735
 
#endif
736
 
#ifndef doubleget
737
 
#define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
738
 
#define doublestore(T,V)  memcpy((T), &V, sizeof(double))
739
 
#endif /* doubleget */
740
 
#define int64_tget(V,M)   memcpy(&V, (M), sizeof(uint64_t))
741
 
#define int64_tstore(T,V) memcpy((T), &V, sizeof(uint64_t))
742
 
 
743
 
#endif /* WORDS_BIGENDIAN */
744
419
 
745
420
 
746
421
#ifdef __cplusplus