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
RevLine 
[2e9455f]1/**
2**  abac_m64.c
3**/
[a9494ad]4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
[2e9455f]8#include <assert.h>
[b4b0d0a]9#include <err.h>
[a9494ad]10
[2e9455f]11#include <credentials/certificates/certificate.h>
[a9494ad]12
[2e9455f]13#include "zlib.h"
14#define CHUNK 1024 /* 16384 */
[a9494ad]15
[2e9455f]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);
[a9494ad]25*/
26
[2e9455f]27static int _compress(char *string, int ilen, char*ostring)
[a9494ad]28{
[2e9455f]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;
[a9494ad]36}
37
[2e9455f]38/* all of these are deep copy of a string */
39char* abac_encode_string(char *string)
[a9494ad]40{
[2e9455f]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 */
[b4b0d0a]49           if(debug) fprintf(stderr,"ERROR, can not compress the string!!\n");
50           errx(1, "ERROR, compress failed!!");
[a9494ad]51        } else {
[2e9455f]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}
[a9494ad]63
[2e9455f]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;
[a9494ad]71    }
[2e9455f]72    return 0;
[a9494ad]73}
74
75char* abac_decode_string(char *string)
76{
[2e9455f]77    assert(string);
[a9494ad]78
[2e9455f]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!! */
[b4b0d0a]90           if(debug) fprintf(stderr,"ERROR, can not uncompress the string!!\n");
91           errx(1, "ERROR, uncompress failed!!");
[2e9455f]92        } else ret=strndup(ostring,olen);
93    } else {
94        ret=strndup(chk.ptr, chk.len);
[a9494ad]95    }
[2e9455f]96    chunk_free(&chk);
97    return ret;
[a9494ad]98}
99
Note: See TracBrowser for help on using the repository browser.