~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/pack.c

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

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 "libdrizzle.h"
 
21
#include <libdrizzle/pack.h>
 
22
#include <drizzled/korr.h>
 
23
#include <stdint.h>
 
24
 
 
25
/* Get the length of next field. Change parameter to point at fieldstart */
 
26
uint32_t net_field_length(unsigned char **packet)
 
27
{
 
28
  register unsigned char *pos= (unsigned char *)*packet;
 
29
  if (*pos < 251)
 
30
  {
 
31
    (*packet)++;
 
32
    return (uint32_t) *pos;
 
33
  }
 
34
  if (*pos == 251)
 
35
  {
 
36
    (*packet)++;
 
37
    return NULL_LENGTH;
 
38
  }
 
39
  if (*pos == 252)
 
40
  {
 
41
    (*packet)+=3;
 
42
    return (uint32_t) uint2korr(pos+1);
 
43
  }
 
44
  if (*pos == 253)
 
45
  {
 
46
    (*packet)+=4;
 
47
    return (uint32_t) uint3korr(pos+1);
 
48
  }
 
49
  (*packet)+=9;          /* Must be 254 when here */
 
50
  return (uint32_t) uint4korr(pos+1);
 
51
}
 
52
 
 
53
/* The same as above but returns int64_t */
 
54
uint64_t net_field_length_ll(unsigned char **packet)
 
55
{
 
56
  register unsigned char *pos= *packet;
 
57
  if (*pos < 251)
 
58
  {
 
59
    (*packet)++;
 
60
    return (uint64_t) *pos;
 
61
  }
 
62
  if (*pos == 251)
 
63
  {
 
64
    (*packet)++;
 
65
    return (uint64_t) NULL_LENGTH;
 
66
  }
 
67
  if (*pos == 252)
 
68
  {
 
69
    (*packet)+=3;
 
70
    return (uint64_t) uint2korr(pos+1);
 
71
  }
 
72
  if (*pos == 253)
 
73
  {
 
74
    (*packet)+=4;
 
75
    return (uint64_t) uint3korr(pos+1);
 
76
  }
 
77
  (*packet)+=9;          /* Must be 254 when here */
 
78
#ifdef NO_CLIENT_LONGLONG
 
79
  return (uint64_t) uint4korr(pos+1);
 
80
#else
 
81
  return (uint64_t) uint8korr(pos+1);
 
82
#endif
 
83
}
 
84
 
 
85
/*
 
86
  Store an integer with simple packing into a output package
 
87
 
 
88
  SYNOPSIS
 
89
    net_store_length()
 
90
    pkg      Store the packed integer here
 
91
    length    integers to store
 
92
 
 
93
  NOTES
 
94
    This is mostly used to store lengths of strings.
 
95
    We have to cast the result for the LL() becasue of a bug in Forte CC
 
96
    compiler.
 
97
 
 
98
  RETURN
 
99
   Position in 'pkg' after the packed length
 
100
*/
 
101
 
 
102
unsigned char *net_store_length(unsigned char *packet, uint64_t length)
 
103
{
 
104
  if (length < (uint64_t) 251LL)
 
105
  {
 
106
    *packet=(unsigned char) length;
 
107
    return packet+1;
 
108
  }
 
109
  /* 251 is reserved for NULL */
 
110
  if (length < (uint64_t) 65536LL)
 
111
  {
 
112
    *packet++=252;
 
113
    int2store(packet,(uint32_t) length);
 
114
    return packet+2;
 
115
  }
 
116
  if (length < (uint64_t) 16777216LL)
 
117
  {
 
118
    *packet++=253;
 
119
    int3store(packet,(uint32_t) length);
 
120
    return packet+3;
 
121
  }
 
122
  *packet++=254;
 
123
  int8store(packet,length);
 
124
  return packet+8;
 
125
}
 
126