~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/pack.cc

  • Committer: Eric Day
  • Date: 2009-11-10 21:50:22 UTC
  • mto: This revision was merged to the branch mainline in revision 1218.
  • Revision ID: eday@oddments.org-20091110215022-0b2nqmurv7b2l6wo
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <drizzled/global.h>
 
21
#include <drizzled/korr.h>
 
22
 
 
23
#include <stdint.h>
 
24
 
 
25
#include "pack.h"
 
26
 
 
27
/* Get the length of next field. Change parameter to point at fieldstart */
 
28
uint32_t drizzleclient_net_field_length(unsigned char **packet)
 
29
{
 
30
  register unsigned char *pos= (unsigned char *)*packet;
 
31
  if (*pos < 251)
 
32
  {
 
33
    (*packet)++;
 
34
    return (uint32_t) *pos;
 
35
  }
 
36
  if (*pos == 251)
 
37
  {
 
38
    (*packet)++;
 
39
    return NULL_LENGTH;
 
40
  }
 
41
  if (*pos == 252)
 
42
  {
 
43
    (*packet)+=3;
 
44
    return (uint32_t) uint2korr(pos+1);
 
45
  }
 
46
  if (*pos == 253)
 
47
  {
 
48
    (*packet)+=4;
 
49
    return (uint32_t) uint3korr(pos+1);
 
50
  }
 
51
  (*packet)+=9;          /* Must be 254 when here */
 
52
  return (uint32_t) uint4korr(pos+1);
 
53
}
 
54
 
 
55
/* The same as above but returns int64_t */
 
56
uint64_t drizzleclient_drizzleclient_net_field_length_ll(unsigned char **packet)
 
57
{
 
58
  register unsigned char *pos= *packet;
 
59
  if (*pos < 251)
 
60
  {
 
61
    (*packet)++;
 
62
    return (uint64_t) *pos;
 
63
  }
 
64
  if (*pos == 251)
 
65
  {
 
66
    (*packet)++;
 
67
    return (uint64_t) NULL_LENGTH;
 
68
  }
 
69
  if (*pos == 252)
 
70
  {
 
71
    (*packet)+=3;
 
72
    return (uint64_t) uint2korr(pos+1);
 
73
  }
 
74
  if (*pos == 253)
 
75
  {
 
76
    (*packet)+=4;
 
77
    return (uint64_t) uint3korr(pos+1);
 
78
  }
 
79
  (*packet)+=9;          /* Must be 254 when here */
 
80
#ifdef NO_CLIENT_LONGLONG
 
81
  return (uint64_t) uint4korr(pos+1);
 
82
#else
 
83
  return (uint64_t) uint8korr(pos+1);
 
84
#endif
 
85
}
 
86
 
 
87
/*
 
88
  Store an integer with simple packing into a output package
 
89
 
 
90
  SYNOPSIS
 
91
    drizzleclient_net_store_length()
 
92
    pkg      Store the packed integer here
 
93
    length    integers to store
 
94
 
 
95
  NOTES
 
96
    This is mostly used to store lengths of strings.
 
97
    We have to cast the result for the LL() becasue of a bug in Forte CC
 
98
    compiler.
 
99
 
 
100
  RETURN
 
101
   Position in 'pkg' after the packed length
 
102
*/
 
103
 
 
104
unsigned char *drizzleclient_net_store_length(unsigned char *packet, uint64_t length)
 
105
{
 
106
  if (length < (uint64_t) 251LL)
 
107
  {
 
108
    *packet=(unsigned char) length;
 
109
    return packet+1;
 
110
  }
 
111
  /* 251 is reserved for NULL */
 
112
  if (length < (uint64_t) 65536LL)
 
113
  {
 
114
    *packet++=252;
 
115
    int2store(packet,(uint32_t) length);
 
116
    return packet+2;
 
117
  }
 
118
  if (length < (uint64_t) 16777216LL)
 
119
  {
 
120
    *packet++=253;
 
121
    int3store(packet,(uint32_t) length);
 
122
    return packet+3;
 
123
  }
 
124
  *packet++=254;
 
125
  int8store(packet,length);
 
126
  return packet+8;
 
127
}
 
128