source: libabac/abac_m64.c @ b4b0d0a

mei_rt2
Last change on this file since b4b0d0a was b4b0d0a, checked in by Mei <mei@…>, 11 years ago

1) more tweaking..

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/**
2**  abac_m64.c
3**/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <assert.h>
9#include <err.h>
10
11#include <credentials/certificates/certificate.h>
12
13#include "zlib.h"
14#define CHUNK 1024 /* 16384 */
15
16static int debug=0;
17static int zip=1; /* to choose to compress/decompress */
18
19/*
20using strongswan's m64 utility so we can stay within the
21buf len size that strongwan set in using its certifiate
22generation code (512)
23    chunk_t b64 = chunk_to_base64(chunk, NULL);
24    chunk_t chk = chunk_from_base64(base64, NULL);
25*/
26
27static int _compress(char *string, int ilen, char*ostring)
28{
29    int olen=CHUNK;
30    int rc=compress(ostring,&olen,string,ilen); 
31    if(rc==Z_OK) { 
32        if(debug) fprintf(stderr,"compress from %d to %d\n", ilen, olen);
33        return olen;
34    }
35    return 0;
36}
37
38/* all of these are deep copy of a string */
39char* abac_encode_string(char *string)
40{
41    assert(string);
42    char *ret=NULL;
43    char *ptr=string;;
44    int len=strlen(ptr);
45    char ostring[CHUNK];
46    if(zip) {
47        int olen=_compress(ptr, len,  ostring);
48        if(olen==0) { /* can not do the compress */
49           if(debug) fprintf(stderr,"ERROR, can not compress the string!!\n");
50           errx(1, "ERROR, compress failed!!");
51        } else {
52           ptr=ostring;
53           len=olen;
54        }
55    } 
56    chunk_t orig = { ptr, len };
57    chunk_t b64 = chunk_to_base64(orig, NULL);
58    if(debug) fprintf(stderr,"abac_encode_string: (%s)\n",b64.ptr);
59    ret=strndup(b64.ptr,b64.len);
60    chunk_free(&b64);
61    return ret;
62}
63
64static int _uncompress(char *string, int ilen, char *ostring)
65{
66    int olen=CHUNK;
67    int rc=uncompress(ostring,&olen,string,ilen); 
68    if(rc==Z_OK) { 
69        if(debug) fprintf(stderr,"uncompress from %d to %d\n", ilen, olen);
70        return olen;
71    }
72    return 0;
73}
74
75char* abac_decode_string(char *string)
76{
77    assert(string);
78
79    char *ret=NULL;
80    int len=strlen(string);
81    chunk_t b64 = { string, len };
82    chunk_t chk= chunk_from_base64(b64, NULL);
83
84    if(debug) fprintf(stderr,"abac_decode_string: (%s)\n",chk.ptr);
85
86    if(zip) {
87        char ostring[CHUNK];
88        int olen=_uncompress(chk.ptr, chk.len, ostring);
89        if(olen==0) { /* unable to uncompress the string!! */
90           if(debug) fprintf(stderr,"ERROR, can not uncompress the string!!\n");
91           errx(1, "ERROR, uncompress failed!!");
92        } else ret=strndup(ostring,olen);
93    } else {
94        ret=strndup(chk.ptr, chk.len);
95    }
96    chunk_free(&chk);
97    return ret;
98}
99
Note: See TracBrowser for help on using the repository browser.