taskrambler  v0.1.8
Web server and task management solution.
user.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "user.h"
24 #include "uuid.h"
25 #include "class.h"
26 
27 #include "interface/serializable.h"
28 #include "interface/indexable.h"
29 
30 #include "utils/memory.h"
31 
32 
33 static
34 int
35 userCtor(void * _this, va_list * params)
36 {
37  User this = _this;
38  char * username = va_arg(* params, char *);
39 
40  if (NULL != username) {
41  size_t nusername = va_arg(* params, size_t);
42  char * email = va_arg(* params, char *);
43  size_t nemail = va_arg(* params, size_t);
44  char * firstname = va_arg(* params, char *);
45  size_t nfirstname = va_arg(* params, size_t);
46  char * surname = va_arg(* params, char *);
47  size_t nsurname = va_arg(* params, size_t);
48 
49  size_t storage_size =
50  nusername + 1 +
51  nemail + 1 +
52  nfirstname + 1 +
53  nsurname + 1 +
54  4 * sizeof(size_t);
55 
56  this->username = memMalloc(storage_size);
57  memcpy(this->username, username, nusername);
58  this->username[nusername] = '\0';
59 
60  this->email = this->username + nusername + 1;
61  memcpy(this->email, email, nemail);
62  this->email[nemail] = '\0';
63 
64  this->firstname = this->email + nemail + 1;
65  memcpy(this->firstname, firstname, nfirstname);
66  this->firstname[nfirstname] = '\0';
67 
68  this->surname = this->firstname + nfirstname + 1;
69  memcpy(this->surname, surname, nsurname);
70  this->surname[nsurname] = '\0';
71 
72  this->nusername = (size_t *)(this->surname + nsurname + 1);
73  *this->nusername = nusername;
74 
75  this->nemail = this->nusername + 1;
76  *this->nemail = nemail;
77 
78  this->nfirstname = this->nemail + 1;
79  *this->nfirstname = nfirstname;
80 
81  this->nsurname = this->nfirstname + 1;
82  *this->nsurname = nsurname;
83  }
84 
85  return 0;
86 }
87 
88 static
89 void
90 userDtor(void * _this)
91 {
92  User this = _this;
93 
94  if (NULL != this->username) {
95  MEM_FREE(this->username);
96  }
97 }
98 
99 static
100 void
102  void * _this,
103  unsigned char ** serialized,
104  size_t * nserialized)
105 {
106  User this = _this;
107 
108  *nserialized =
109  *this->nusername + 1 +
110  *this->nemail + 1 +
111  *this->nfirstname + 1 +
112  *this->nsurname + 1 +
113  4 * sizeof(size_t);
114 
115  *serialized = memMalloc(*nserialized);
116 
117  memcpy(*serialized, this->username, *nserialized);
118 }
119 
120 static
121 void
123  void * _this,
124  const unsigned char * serialized,
125  size_t nserialized)
126 {
127  User this = _this;
128  size_t * user_data_sizes;
129 
130  this->username = memMalloc(nserialized);
131  memcpy(this->username, serialized, nserialized);
132 
133  user_data_sizes =
134  (size_t *)(this->username + nserialized - 4 * sizeof(size_t));
135 
136  this->nusername = user_data_sizes;
137  this->nemail = user_data_sizes + 1;
138  this->nfirstname = user_data_sizes + 2;
139  this->nsurname = user_data_sizes + 3;
140 
141  this->email = this->username + *this->nusername + 1;
142  this->firstname = this->email + *this->nemail + 1;
143  this->surname = this->firstname + *this->nfirstname + 1;
144 }
145 
146 static
147 Uuid
148 userIndexUuid(void * _this, Uuid namespace)
149 {
150  User this = _this;
151 
152  return uuidVersion3(
153  (unsigned char *)this->username,
154  *this->nusername,
155  namespace);
156 }
157 
158 
159 INIT_IFACE(Class, userCtor, userDtor, NULL);
161 INIT_IFACE(Indexable, userIndexUuid);
162 CREATE_CLASS(User, NULL, IFACE(Class), IFACE(Serializable), IFACE(Indexable));
163 
164 // vim: set ts=4 sw=4:
static void userDtor(void *_this)
Definition: user.c:90
#define MEM_FREE(seg)
Definition: memory.h:28
static void userUnserialize(void *_this, const unsigned char *serialized, size_t nserialized)
Definition: user.c:122
INIT_IFACE(Class, userCtor, userDtor, NULL)
static int userCtor(void *_this, va_list *params)
Definition: user.c:35
CREATE_CLASS(User, NULL, IFACE(Class), IFACE(Serializable), IFACE(Indexable))
Uuid uuidVersion3(const unsigned char *, size_t, Uuid)
Definition: version3.c:39
void * memMalloc(size_t)
Definition: memory.c:783
static void userSerialize(void *_this, unsigned char **serialized, size_t *nserialized)
Definition: user.c:101
#define IFACE(name)
Definition: interface.h:34
static Uuid userIndexUuid(void *_this, Uuid namespace)
Definition: user.c:148