~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/pack.c

  • Committer: Stewart Smith
  • Date: 2008-10-15 04:21:24 UTC
  • mto: This revision was merged to the branch mainline in revision 516.
  • Revision ID: stewart@flamingspork.com-20081015042124-kdmb74bcbky1k1nz
remove my_pthread_[gs]etspecific

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