- test of header payload rewritten
[strongswan.git] / Source / charon / generator.c
1 /**
2 * @file generator.c
3 *
4 * @brief Generic generator class used to generate IKEv2-header and payloads.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <arpa/inet.h>
26 #include <stdio.h>
27
28
29 #include "generator.h"
30
31 #include "types.h"
32 #include "utils/allocator.h"
33 #include "utils/linked_list.h"
34 #include "utils/logger_manager.h"
35 #include "payloads/payload.h"
36 #include "payloads/proposal_substructure.h"
37 #include "payloads/transform_substructure.h"
38
39
40 extern logger_manager_t *global_logger_manager;
41
42 /**
43 * Private part of a generator_t object
44 */
45 typedef struct private_generator_s private_generator_t;
46
47 struct private_generator_s {
48 /**
49 * Public part of a generator_t object
50 */
51 generator_t public;
52
53 /* private functions and fields */
54
55
56 /**
57 * Generates a U_INT-Field type
58 *
59 * @param this private_generator_t object
60 * @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
61 * @param offset offset of value in data struct
62 * @param generator_contexts generator_contexts_t object where the context is written or read from
63 * @return - SUCCESS if succeeded
64 * - OUT_OF_RES if out of ressources
65 */
66 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset);
67
68 /**
69 * Generates a RESERVED BIT field or a RESERVED BYTE field
70 *
71 * @param this private_generator_t object
72 * @param generator_contexts generator_contexts_t object where the context is written or read from
73 * @param bits number of bits to generate
74 * @return - SUCCESS if succeeded
75 * - OUT_OF_RES if out of ressources
76 * - FAILED if bit count not supported
77 */
78 status_t (*generate_reserved_field) (private_generator_t *this,int bits);
79
80 /**
81 * Generates a FLAG field
82 *
83 * @param this private_generator_t object
84 * @param generator_contexts generator_contexts_t object where the context is written or read from
85 * @param offset offset of flag value in data struct
86 * @return - SUCCESS if succeeded
87 * - OUT_OF_RES if out of ressources
88 */
89 status_t (*generate_flag) (private_generator_t *this,u_int32_t offset);
90
91 /**
92 * Writes the current buffer content into a chunk_t
93 *
94 * Memory of specific chunk_t gets allocated.
95 *
96 * @param this calling private_generator_t object
97 * @param data pointer of chunk_t to write to
98 * @return
99 * - SUCCESSFUL if succeeded
100 * - OUT_OF_RES otherwise
101 */
102 status_t (*write_chunk) (private_generator_t *this,chunk_t *data);
103
104 /**
105 * Generates a bytestream from a chunk_t
106 *
107 * @param this private_generator_t object
108 * @param offset offset of chunk_t value in data struct
109 * @return - SUCCESS if succeeded
110 * - OUT_OF_RES if out of ressources
111 */
112 status_t (*generate_from_chunk) (private_generator_t *this,u_int32_t offset);
113
114 /**
115 * Makes sure enough space is available in buffer to store amount of bits.
116 *
117 * If buffer is to small to hold the specific amount of bits it
118 * is increased using reallocation function of allocator.
119 *
120 * @param this calling private_generator_t object
121 * @param bits number of bits to make available in buffer
122 * @return
123 * - SUCCESSFUL if succeeded
124 * - OUT_OF_RES otherwise
125 */
126 status_t (*make_space_available) (private_generator_t *this,size_t bits);
127
128 /**
129 * Writes a specific amount of byte into the buffer.
130 *
131 * If buffer is to small to hold the specific amount of bytes it
132 * is increased.
133 *
134 * @param this calling private_generator_t object
135 * @param bytes pointer to bytes to write
136 * @param number_of_bytes number of bytes to write into buffer
137 * @return
138 * - SUCCESSFUL if succeeded
139 * - OUT_OF_RES otherwise
140 */
141 status_t (*write_bytes_to_buffer) (private_generator_t *this,void * bytes,size_t number_of_bytes);
142
143
144 /**
145 * Writes a specific amount of byte into the buffer at a specific offset.
146 *
147 * @warning buffer size is not check to hold the data if offset is to large.
148 *
149 * @param this calling private_generator_t object
150 * @param bytes pointer to bytes to write
151 * @param number_of_bytes number of bytes to write into buffer
152 * @param offset offset to write the data into
153 * @return
154 * - SUCCESSFUL if succeeded
155 * - OUT_OF_RES otherwise
156 */
157 status_t (*write_bytes_to_buffer_at_offset) (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset);
158
159 /**
160 * Buffer used to generate the data into.
161 */
162 u_int8_t *buffer;
163
164 /**
165 * Current write position in buffer (one byte aligned).
166 */
167 u_int8_t *out_position;
168
169 /**
170 * Position of last byte in buffer.
171 */
172 u_int8_t *roof_position;
173
174 /**
175 * Current bit writing to in current byte (between 0 and 7).
176 */
177 size_t current_bit;
178
179 /**
180 * Associated data struct to read informations from.
181 */
182 void * data_struct;
183
184 /*
185 * Last payload length position offset in the buffer
186 */
187 u_int32_t last_payload_length_position_offset;
188
189 /**
190 * Offset of the header length field in the buffer
191 */
192 u_int32_t header_length_position_offset;
193
194 /**
195 * Last SPI size
196 */
197 u_int8_t last_spi_size;
198
199 /*
200 * Attribute format of the last generated transform attribute
201 *
202 * Used to check if a variable value field is used or not for
203 * the transform attribute value.
204 */
205 bool attribute_format;
206
207 /*
208 * Depending on the value of attribute_format this field is used
209 * to hold the length of the transform attribute in bytes
210 */
211 u_int16_t attribute_length;
212
213 /**
214 * Associated Logger
215 */
216 logger_t *logger;
217 };
218
219 /**
220 * Implements private_generator_t's generate_u_int_type function.
221 * See #private_generator_s.generate_u_int_type.
222 */
223 static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset)
224 {
225 size_t number_of_bits = 0;
226 status_t status;
227
228
229 switch (int_type)
230 {
231 case U_INT_4:
232 number_of_bits = 4;
233 break;
234 case U_INT_8:
235 number_of_bits = 8;
236 break;
237 case U_INT_16:
238 number_of_bits = 16;
239 break;
240 case U_INT_32:
241 number_of_bits = 32;
242 break;
243 case U_INT_64:
244 number_of_bits = 64;
245 break;
246 case ATTRIBUTE_TYPE:
247 number_of_bits = 15;
248 break;
249 default:
250 return FAILED;
251 }
252 if (((number_of_bits % 8) == 0) && (this->current_bit != 0))
253 {
254 /* current bit has to be zero for values multiple of 8 bits */
255 return FAILED;
256 }
257
258 status = this->make_space_available(this,number_of_bits);
259
260 if (status != SUCCESS)
261 {
262 return status;
263 }
264
265 switch (int_type)
266 {
267 case U_INT_4:
268 {
269 if (this->current_bit == 0)
270 {
271 u_int8_t high_val = *((u_int8_t *)(this->data_struct + offset)) << 4;
272 u_int8_t low_val = *(this->out_position) & 0x0F;
273
274 *(this->out_position) = high_val | low_val;
275 /* write position is not changed, just bit position is moved */
276 this->current_bit = 4;
277 }
278 else if (this->current_bit == 4)
279 {
280 u_int high_val = *(this->out_position) & 0xF0;
281 u_int low_val = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
282 *(this->out_position) = high_val | low_val;
283 this->out_position++;
284 this->current_bit = 0;
285
286 }
287 else
288 {
289 /* 4 Bit integers must have a 4 bit alignment */
290 return FAILED;
291 };
292 break;
293 }
294
295 case U_INT_8:
296 {
297 *this->out_position = *((u_int8_t *)(this->data_struct + offset));
298 this->out_position++;
299 break;
300
301 }
302 case ATTRIBUTE_TYPE:
303 {
304 if (this->current_bit != 1)
305 {
306 return FAILED;
307 }
308 u_int8_t attribute_format_flag = *(this->out_position) & 0x80;
309
310 u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
311
312 int16_val = int16_val & 0xFF7F;
313
314 int16_val = int16_val | attribute_format_flag;
315
316 this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
317 this->current_bit = 0;
318 break;
319
320 }
321
322 case U_INT_16:
323 {
324 u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
325 this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
326
327 break;
328 }
329 case U_INT_32:
330 {
331 u_int32_t int32_val = htonl(*((u_int32_t*)(this->data_struct + offset)));
332 this->write_bytes_to_buffer(this,&int32_val,sizeof(u_int32_t));
333 break;
334 }
335 case U_INT_64:
336 {
337 u_int32_t int32_val_low = htonl(*((u_int32_t*)(this->data_struct + offset)));
338 u_int32_t int32_val_high = htonl(*((u_int32_t*)(this->data_struct + offset) + 1));
339 this->write_bytes_to_buffer(this,&int32_val_high,sizeof(u_int32_t));
340 this->write_bytes_to_buffer(this,&int32_val_low,sizeof(u_int32_t));
341 break;
342 }
343
344 default:
345 return FAILED;
346
347 }
348
349 return SUCCESS;
350 }
351
352 /**
353 * Implements private_generator_t's generate_reserved_field function.
354 * See #private_generator_s.generate_reserved_field.
355 */
356 static status_t generate_reserved_field (private_generator_t *this,int bits)
357 {
358 status_t status;
359
360 if ((bits != 1) && (bits != 8))
361 {
362 return FAILED;
363 }
364 status = this->make_space_available(this,bits);
365 if (status != SUCCESS)
366 {
367 return status;
368 }
369
370 if (bits == 1)
371 {
372 u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
373
374 *(this->out_position) = *(this->out_position) & reserved_bit;
375 this->current_bit++;
376 if (this->current_bit >= 8)
377 {
378 this->current_bit = this->current_bit % 8;
379 this->out_position++;
380 }
381 }
382 else
383 {
384 /* one byte */
385 if (this->current_bit > 0)
386 {
387 return FAILED;
388 }
389 *(this->out_position) = 0x00;
390 this->out_position++;
391 }
392
393 return SUCCESS;
394
395
396 }
397
398 /**
399 * Implements private_generator_t's generate_flag function.
400 * See #private_generator_s.generate_flag.
401 */
402 static status_t generate_flag (private_generator_t *this,u_int32_t offset)
403 {
404 status_t status;
405 u_int8_t flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
406 u_int8_t flag = (flag_value << (7 - this->current_bit));
407
408 status = this->make_space_available(this,1);
409 if (status != SUCCESS)
410 {
411 return status;
412 }
413
414 *(this->out_position) = *(this->out_position) | flag;
415
416 this->current_bit++;
417 if (this->current_bit >= 8)
418 {
419 this->current_bit = this->current_bit % 8;
420 this->out_position++;
421 }
422 return SUCCESS;
423 }
424
425 /**
426 * Implements private_generator_t's generate_from_chunk function.
427 * See #private_generator_s.generate_from_chunk.
428 */
429 static status_t generate_from_chunk (private_generator_t *this,u_int32_t offset)
430 {
431 if (this->current_bit != 0)
432 {
433 return FAILED;
434 }
435 chunk_t *attribute_value = (chunk_t *)(this->data_struct + offset);
436
437 return this->write_bytes_to_buffer (this,attribute_value->ptr,attribute_value->len);
438
439 }
440
441 /**
442 * Implements private_generator_t's generator_context_make_space_available function.
443 * See #private_generator_s.generator_context_make_space_available.
444 */
445 static status_t make_space_available (private_generator_t *this, size_t bits)
446 {
447 while ((((this->roof_position - this->out_position) * 8) - this->current_bit) < bits)
448 {
449 size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
450 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
451 size_t out_position_offset = ((this->out_position) - (this->buffer));
452 u_int8_t *new_buffer;
453
454 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
455 if (new_buffer == NULL)
456 {
457 return OUT_OF_RES;
458 }
459
460 this->buffer = new_buffer;
461
462 this->out_position = (this->buffer + out_position_offset);
463 this->roof_position = (this->buffer + new_buffer_size);
464
465 }
466
467 return SUCCESS;
468 }
469
470 /**
471 * Implements private_generator_t's write_bytes_to_buffer function.
472 * See #private_generator_s.write_bytes_to_buffer.
473 */
474 static status_t write_bytes_to_buffer (private_generator_t *this,void * bytes,size_t number_of_bytes)
475 {
476 u_int8_t *read_position = (u_int8_t *) bytes;
477 int i;
478 status_t status;
479
480 status = this->make_space_available(this,number_of_bytes * 8);
481
482 if (status != SUCCESS)
483 {
484 return status;
485 }
486
487 for (i = 0; i < number_of_bytes; i++)
488 {
489 *(this->out_position) = *(read_position);
490 read_position++;
491 this->out_position++;
492 }
493 return status;
494 }
495
496 /**
497 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
498 * See #private_generator_s.write_bytes_to_buffer_at_offset.
499 * TODO automatic buffer increasing!
500 */
501 static status_t write_bytes_to_buffer_at_offset (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset)
502 {
503 u_int8_t *read_position = (u_int8_t *) bytes;
504 int i;
505 u_int8_t *write_position = this->buffer + offset;
506
507 for (i = 0; i < number_of_bytes; i++)
508 {
509 *(write_position) = *(read_position);
510 read_position++;
511 write_position++;
512 }
513 return SUCCESS;
514 }
515
516 /**
517 * Implements generator_t's write_chunk function.
518 * See #generator_s.write_chunk.
519 */
520 static status_t write_to_chunk (private_generator_t *this,chunk_t *data)
521 {
522 size_t data_length = this->out_position - this->buffer;
523 u_int32_t header_length_field = data_length;
524
525 /* write length into header length field */
526 if (this->header_length_position_offset > 0)
527 {
528 u_int32_t int32_val = htonl(header_length_field);
529 this->write_bytes_to_buffer_at_offset(this,&int32_val,sizeof(u_int32_t),this->header_length_position_offset);
530 }
531
532 if (this->current_bit > 0)
533 data_length++;
534 data->ptr = allocator_alloc(data_length);
535 if (data->ptr == NULL)
536 {
537 data->len = 0;
538 return OUT_OF_RES;
539 }
540 memcpy(data->ptr,this->buffer,data_length);
541 data->len = data_length;
542 return SUCCESS;
543 }
544
545
546
547 /**
548 * Implements generator_t's generate_payload function.
549 * See #generator_s.generate_payload.
550 */
551 static status_t generate_payload (private_generator_t *this,payload_t *payload)
552 {
553 int i;
554 status_t status;
555 this->data_struct = payload;
556 size_t rule_count;
557 encoding_rule_t *rules;
558
559
560 payload_type_t payload_type = payload->get_type(payload);
561
562 this->logger->log(this->logger,CONTROL,"Start generating payload of type %s",mapping_find(payload_type_t_mappings,payload_type));
563
564 payload->get_encoding_rules(payload,&rules,&rule_count);
565
566 for (i = 0; i < rule_count;i++)
567 {
568 status = SUCCESS;
569 switch (rules[i].type)
570 {
571 /* all u int values are generated in generate_u_int_type */
572 case U_INT_4:
573 case U_INT_8:
574 case U_INT_16:
575 case U_INT_32:
576 case U_INT_64:
577 status = this->generate_u_int_type(this,rules[i].type,rules[i].offset);
578 break;
579 case RESERVED_BIT:
580 {
581 status = this->generate_reserved_field(this,1);
582
583 break;
584 }
585 case RESERVED_BYTE:
586 {
587 status = this->generate_reserved_field(this,8);
588 break;
589 }
590 case FLAG:
591 {
592 status = this->generate_flag(this,rules[i].offset);
593 break;
594 }
595 case PAYLOAD_LENGTH:
596 /* payload length is generated like an U_INT_16 */
597 this->last_payload_length_position_offset = (this->out_position - this->buffer);
598 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
599 break;
600
601 case HEADER_LENGTH:
602 /* header length is generated like an U_INT_32 */
603 this->header_length_position_offset = (this->out_position - this->buffer);
604 status = this->generate_u_int_type(this,U_INT_32,rules[i].offset);
605 break;
606 case SPI_SIZE:
607 /* spi size is handled as 8 bit unsigned integer */
608 status = this->generate_u_int_type(this,U_INT_8,rules[i].offset);
609 this->last_spi_size = *((u_int8_t *)(this->data_struct + rules[i].offset));
610 break;
611 case SPI:
612 {
613 this->logger->log(this->logger,CONTROL_MORE,"SPI value");
614 /* the attribute value is generated */
615 status = this->generate_from_chunk(this,rules[i].offset);
616 break;
617 }
618 case TRANSFORMS:
619 {
620 this->logger->log(this->logger,CONTROL_MORE,"Generate Transforms");
621 /* before iterative generate the transforms, store the current length position */
622 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
623
624 u_int16_t length_of_proposal = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
625 u_int16_t int16_val;
626 linked_list_t *transforms = *((linked_list_t **)(this->data_struct + rules[i].offset));
627
628 linked_list_iterator_t *iterator;
629 /* create forward iterator */
630 status = transforms->create_iterator(transforms,&iterator,TRUE);
631 if (status != SUCCESS)
632 {
633 return status;
634 }
635 while (iterator->has_next(iterator))
636 {
637 payload_t *current_transform;
638 u_int32_t before_generate_position_offset;
639 u_int32_t after_generate_position_offset;
640
641 status = iterator->current(iterator,(void **)&current_transform);
642 if (status != SUCCESS)
643 {
644 iterator->destroy(iterator);
645 return status;
646 }
647
648 before_generate_position_offset = (this->out_position - this->buffer);
649 this->public.generate_payload(&(this->public),current_transform);
650 after_generate_position_offset = (this->out_position - this->buffer);
651
652 /* increase size of transform */
653 length_of_proposal += (after_generate_position_offset - before_generate_position_offset);
654 }
655
656 iterator->destroy(iterator);
657
658 this->logger->log(this->logger,CONTROL_MORE,"Length of Transform is %d, offset is %d",length_of_proposal,payload_length_position_offset);
659
660 int16_val = htons(length_of_proposal);
661 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
662
663 break;
664 }
665 case TRANSFORM_ATTRIBUTES:
666 {
667 this->logger->log(this->logger,CONTROL_MORE,"Generate Transform attributes");
668 /* before iterative generate the transform attributes, store the current length position */
669 u_int32_t transform_length_position_offset = this->last_payload_length_position_offset;
670
671 u_int16_t length_of_transform = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
672 u_int16_t int16_val;
673 linked_list_t *transform_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
674
675 linked_list_iterator_t *iterator;
676 /* create forward iterator */
677 status = transform_attributes->create_iterator(transform_attributes,&iterator,TRUE);
678 if (status != SUCCESS)
679 {
680 return status;
681 }
682 while (iterator->has_next(iterator))
683 {
684 payload_t *current_attribute;
685 u_int32_t before_generate_position_offset;
686 u_int32_t after_generate_position_offset;
687
688 status = iterator->current(iterator,(void **)&current_attribute);
689 if (status != SUCCESS)
690 {
691 iterator->destroy(iterator);
692 return status;
693 }
694
695 before_generate_position_offset = (this->out_position - this->buffer);
696 this->public.generate_payload(&(this->public),current_attribute);
697 after_generate_position_offset = (this->out_position - this->buffer);
698
699 /* increase size of transform */
700 length_of_transform += (after_generate_position_offset - before_generate_position_offset);
701 }
702
703 iterator->destroy(iterator);
704
705 this->logger->log(this->logger,CONTROL_MORE,"Length of Transform is %d, offset is %d",length_of_transform,transform_length_position_offset);
706
707 int16_val = htons(length_of_transform);
708 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),transform_length_position_offset);
709
710 break;
711 }
712 case ATTRIBUTE_FORMAT:
713 {
714 this->logger->log(this->logger,CONTROL_MORE,"Generate Attribute Format flag");
715 /* Attribute format is a flag which is stored in context*/
716
717 status = this->generate_flag(this,rules[i].offset);
718 this->attribute_format = *((bool *) (this->data_struct + rules[i].offset));
719 break;
720 }
721 case ATTRIBUTE_TYPE:
722 {
723 this->logger->log(this->logger,CONTROL_MORE,"Generate Attribute Type field");
724 // the attribute type is a 15 bit integer so it has to be generated special
725 status = this->generate_u_int_type(this,ATTRIBUTE_TYPE,rules[i].offset);
726 break;
727 }
728 case ATTRIBUTE_LENGTH_OR_VALUE:
729 {
730 this->logger->log(this->logger,CONTROL_MORE,"Generate Attribute Length or Value field");
731 if (this->attribute_format == FALSE)
732 {
733 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
734 /* this field hold the length of the attribute */
735 this->attribute_length = *((u_int16_t *)(this->data_struct + rules[i].offset));
736 }
737 else
738 {
739 status = this->write_bytes_to_buffer(this,(this->data_struct + rules[i].offset),2);
740 }
741 break;
742 }
743 case ATTRIBUTE_VALUE:
744 {
745 if (this->attribute_format == FALSE)
746 {
747 this->logger->log(this->logger,CONTROL_MORE,"Attribute value has not fixed size");
748 /* the attribute value is generated */
749 status = this->generate_from_chunk(this,rules[i].offset);
750 }
751 break;
752 }
753 default:
754 return NOT_SUPPORTED;
755 }
756 }
757
758 return status;
759 }
760
761 /**
762 * Implements generator_t's destroy function.
763 * See #generator_s.destroy.
764 */
765 static status_t destroy(private_generator_t *this)
766 {
767 allocator_free(this->buffer);
768 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
769 allocator_free(this);
770 return SUCCESS;
771 }
772
773 /*
774 * Described in header
775 */
776 generator_t * generator_create()
777 {
778 private_generator_t *this;
779
780 this = allocator_alloc_thing(private_generator_t);
781 if (this == NULL)
782 {
783 return NULL;
784 }
785
786 /* initiate public functions */
787 this->public.generate_payload = (status_t(*)(generator_t*, payload_t *)) generate_payload;
788 this->public.destroy = (status_t(*)(generator_t*)) destroy;
789 this->public.write_to_chunk = (status_t (*) (generator_t *,chunk_t *)) write_to_chunk;
790
791
792 /* initiate private functions */
793 // this->generate = generate;
794 this->generate_u_int_type = generate_u_int_type;
795 this->generate_reserved_field = generate_reserved_field;
796 this->generate_flag = generate_flag;
797 this->generate_from_chunk = generate_from_chunk;
798 this->make_space_available = make_space_available;
799 this->write_bytes_to_buffer = write_bytes_to_buffer;
800 this->write_bytes_to_buffer_at_offset = write_bytes_to_buffer_at_offset;
801
802
803 /* allocate memory for buffer */
804 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
805 if (this->buffer == NULL)
806 {
807 allocator_free(this);
808 return NULL;
809 }
810
811 /* initiate private variables */
812 this->out_position = this->buffer;
813 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
814 this->data_struct = NULL;
815 this->current_bit = 0;
816 this->last_payload_length_position_offset = 0;
817 this->header_length_position_offset = 0;
818 this->logger = global_logger_manager->create_logger(global_logger_manager,GENERATOR,NULL);
819 return &(this->public);
820 }