Browse Source

some smaller fixes as well as a complete test create_token implementation.

master
Georg Hopp 14 years ago
parent
commit
aaea686fed
  1. 2
      bigpoint_dyntype.c
  2. 8
      bigpoint_hash.c
  3. 22
      bigpoint_packet.c
  4. 4
      bigpoint_packet.h
  5. 99
      createToken.c
  6. 100
      tests/packet_test.c

2
bigpoint_dyntype.c

@ -25,7 +25,7 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params)
break; break;
case BIGPOINT_DYNTYPE_HASH: case BIGPOINT_DYNTYPE_HASH:
(_this->data)._hash = new(BIGPOINT_HASH);
(_this->data)._hash = va_arg(* params, struct BIGPOINT_HASH *);
break; break;
default: default:

8
bigpoint_hash.c

@ -85,15 +85,15 @@ _updateHashSize(struct BIGPOINT_HASH * _this)
_this->keys = realloc(_this->keys, sizeof(char*) * _this->size); _this->keys = realloc(_this->keys, sizeof(char*) * _this->size);
memset(_this->keys + (_this->used * sizeof(char*)), memset(_this->keys + (_this->used * sizeof(char*)),
HASH_ENTRY_CHUNK_SIZE * sizeof(char*),
0);
0,
HASH_ENTRY_CHUNK_SIZE * sizeof(char*));
_this->values = realloc( _this->values = realloc(
_this->values, _this->values,
sizeof(struct BIGPOINT_DYNTYPE *) * _this->size); sizeof(struct BIGPOINT_DYNTYPE *) * _this->size);
memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)), memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)),
HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *),
0);
0,
HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *));
} }
} }

22
bigpoint_packet.c

@ -1,13 +1,13 @@
#include <json/json.h> #include <json/json.h>
#include "bigpoint_container_packet.h"
#include "bigpoint_packet.h"
static static
void void
__construct(struct BIGPOINT_PACKET * _this, va_list * params) __construct(struct BIGPOINT_PACKET * _this, va_list * params)
{ {
bigpoint_container_packet_set_default_content(_this);
bigpoint_packet_set_default_content(_this);
} }
static static
@ -53,38 +53,40 @@ __toJson(struct BIGPOINT_PACKET * _this)
} }
static const static const
struct BIGPOINT_CLASS _bigpoint_packet = {
struct BIGPOINT_CCLASS _bigpoint_packet = {
sizeof(struct BIGPOINT_PACKET), sizeof(struct BIGPOINT_PACKET),
(ctor)__construct, (ctor)__construct,
__jsonConst,
(jCtor)__jsonConst,
(dtor)__destruct, (dtor)__destruct,
__toJson
(jTo)__toJson
}; };
const struct BIGPOINT_CCLASS * const BIGPOINT_PACKET = &_bigpoint_packet; const struct BIGPOINT_CCLASS * const BIGPOINT_PACKET = &_bigpoint_packet;
void *
struct BIGPOINT_DYNTYPE *
bigpoint_packet_getHeader( bigpoint_packet_getHeader(
struct BIGPOINT_PACKET * _this) struct BIGPOINT_PACKET * _this)
{ {
return _this->content[BIGPOINT_PACKET_HEADER]; return _this->content[BIGPOINT_PACKET_HEADER];
} }
void *
struct BIGPOINT_DYNTYPE *
bigpoint_packet_getData( bigpoint_packet_getData(
struct BIGPOINT_PACKET * _this) struct BIGPOINT_PACKET * _this)
{ {
return _this->content[BIGPOINT_PACKET_DATA]; return _this->content[BIGPOINT_PACKET_DATA];
} }
void bigpoint_packet_setHeader(
void
bigpoint_packet_setHeader(
struct BIGPOINT_PACKET * _this, struct BIGPOINT_PACKET * _this,
struct BIGPOINT_DYNTYPE * header) struct BIGPOINT_DYNTYPE * header)
{ {
_this->content[BIGPOINT_PACKET_HEADER] = header; _this->content[BIGPOINT_PACKET_HEADER] = header;
} }
void bigpoint_packet_setData(
void
bigpoint_packet_setData(
struct BIGPOINT_PACKET * _this, struct BIGPOINT_PACKET * _this,
struct BIGPOINT_DYNTYPE * data) struct BIGPOINT_DYNTYPE * data)
{ {
@ -92,7 +94,7 @@ void bigpoint_packet_setData(
} }
void void
bigpoint_container_packet_set_default_content(
bigpoint_packet_set_default_content(
struct BIGPOINT_PACKET * _this) struct BIGPOINT_PACKET * _this)
{ {
_this->content[BIGPOINT_PACKET_HEADER] = NULL; _this->content[BIGPOINT_PACKET_HEADER] = NULL;

4
bigpoint_packet.h

@ -17,10 +17,10 @@ struct BIGPOINT_PACKET {
extern const struct BIGPOINT_CCLASS * const BIGPOINT_PACKET; extern const struct BIGPOINT_CCLASS * const BIGPOINT_PACKET;
void * bigpoint_packet_getHeader(
struct BIGPOINT_DYNTYPE * bigpoint_packet_getHeader(
struct BIGPOINT_PACKET * _this); struct BIGPOINT_PACKET * _this);
void * bigpoint_packet_getData(
struct BIGPOINT_DYNTYPE * bigpoint_packet_getData(
struct BIGPOINT_PACKET * _this); struct BIGPOINT_PACKET * _this);
void bigpoint_packet_setHeader( void bigpoint_packet_setHeader(

99
createToken.c

@ -0,0 +1,99 @@
#include <mcrypt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <json/json.h>
#include "bigpoint_cclass.h"
#include "bigpoint_packet.h"
#include "bigpoint_dyntype.h"
#include "bigpoint_hash.h"
#include "bigpoint_crypt.h"
#include "base64.h"
void
setHashString(struct BIGPOINT_HASH * hash, const char * key, const char * value)
{
struct BIGPOINT_DYNTYPE * dyn;
dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_STRING, strlen(value), value);
bigpoint_hash_set(hash, key, dyn);
}
void
setHashInt(struct BIGPOINT_HASH * hash, const char * key, const int value)
{
struct BIGPOINT_DYNTYPE * dyn;
dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), value);
bigpoint_hash_set(hash, key, dyn);
}
int
main(int argc, char * argv[])
{
struct BIGPOINT_CRYPT * crypt;
struct BIGPOINT_PACKET * packet;
struct BIGPOINT_HASH * data;
struct json_object * json;
const char * json_str;
char * encrypted;
char * b64d;
char pass[] = "1234";
size_t length;
packet = new(BIGPOINT_PACKET);
bigpoint_packet_setHeader(
packet,
new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), time(NULL)));
data = new(BIGPOINT_HASH);
setHashString(data, "#C#", "this comes from C");
setHashString(data, "usr", "ppohg");
setHashString(data, "pas", "yyyyy");
setHashInt(data, "val", 321);
bigpoint_packet_setData(
packet,
new(BIGPOINT_DYNTYPE,
BIGPOINT_DYNTYPE_HASH,
sizeof(struct BIGPOINT_HASH *),
data));
json = toJson(packet);
json_str = json_object_to_json_string(json);
length = strlen(json_str);
crypt = new(BIGPOINT_CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB);
encrypted = bigpoint_crypt_encrypt(crypt, json_str, pass, &length);
delete(crypt);
json_object_put(json);
b64d = calloc(BASE64_LENGTH(length), sizeof(char));
base64_encode(encrypted, length, b64d, BASE64_LENGTH(length));
free(encrypted);
b64d = realloc(b64d, BASE64_LENGTH(length) + 1);
b64d[BASE64_LENGTH(length)] = '\0';
printf("%s\n", b64d);
free(b64d);
delete(bigpoint_hash_get(data, "#C#"));
delete(bigpoint_hash_get(data, "usr"));
delete(bigpoint_hash_get(data, "pas"));
delete(bigpoint_hash_get(data, "val"));
delete(bigpoint_packet_getHeader(packet));
delete(bigpoint_packet_getData(packet));
delete(packet);
return 0;
}
// vim: set et ts=4 sw=4:

100
tests/packet_test.c

@ -0,0 +1,100 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <json/json.h>
#include "../bigpoint_cclass.h"
#include "../bigpoint_packet.h"
#include "../bigpoint_dyntype.h"
#include "../bigpoint_hash.h"
void
setHashString(struct BIGPOINT_HASH * hash, const char * key, const char * value)
{
struct BIGPOINT_DYNTYPE * dyn;
dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_STRING, strlen(value), value);
bigpoint_hash_set(hash, key, dyn);
}
void
setHashInt(struct BIGPOINT_HASH * hash, const char * key, const int value)
{
struct BIGPOINT_DYNTYPE * dyn;
dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), value);
bigpoint_hash_set(hash, key, dyn);
}
int
main(int argc, char * argv[])
{
struct BIGPOINT_PACKET * packet;
struct BIGPOINT_HASH * data;
struct json_object * json;
char * json_str;
packet = new(BIGPOINT_PACKET);
bigpoint_packet_setHeader(
packet,
new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), time(NULL)));
data = new(BIGPOINT_HASH);
setHashString(data, "#C#", "this comes from C");
setHashString(data, "usr", "ppohg");
setHashString(data, "pas", "yyyyy");
setHashInt(data, "val", 321);
bigpoint_packet_setData(
packet,
new(BIGPOINT_DYNTYPE,
BIGPOINT_DYNTYPE_HASH,
sizeof(struct BIGPOINT_HASH *),
data));
json = toJson(packet);
json_str = calloc(
strlen(json_object_to_json_string(json)) + 1,
sizeof(char));
memcpy(json_str,
json_object_to_json_string(json),
strlen(json_object_to_json_string(json)));
printf("%s\n", json_str);
delete(bigpoint_hash_get(data, "#C#"));
delete(bigpoint_hash_get(data, "usr"));
delete(bigpoint_hash_get(data, "pas"));
delete(bigpoint_hash_get(data, "val"));
delete(bigpoint_packet_getHeader(packet));
delete(bigpoint_packet_getData(packet));
delete(packet);
json_object_put(json);
json = json_tokener_parse(json_str);
packet = newFromJson(BIGPOINT_PACKET, json);
printf("%s\n", json_object_to_json_string(json));
data = (bigpoint_packet_getData(packet)->data)._hash;
delete(bigpoint_hash_get(data, "#C#"));
delete(bigpoint_hash_get(data, "usr"));
delete(bigpoint_hash_get(data, "pas"));
delete(bigpoint_hash_get(data, "val"));
delete(bigpoint_packet_getHeader(packet));
delete(bigpoint_packet_getData(packet));
delete(packet);
free(json_str);
json_object_put(json);
return 0;
}
// vim: set et ts=4 sw=4:
Loading…
Cancel
Save