#include #include #include static const char *radixN = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static int radix_encode(unsigned char inbuf[], unsigned char outbuf[], int *len, int decode) { int i,j,D=0; char *p; unsigned char c=0; if (decode) { for (i=0,j=0; inbuf[i] && inbuf[i] != '='; i++) { if ((p = strchr(radixN, inbuf[i])) == NULL) return(1); D = p - radixN; switch (i&3) { case 0: outbuf[j] = D<<2; break; case 1: outbuf[j++] |= D>>4; outbuf[j] = (D&15)<<4; break; case 2: outbuf[j++] |= D>>2; outbuf[j] = (D&3)<<6; break; case 3: outbuf[j++] |= D; } } switch (i&3) { case 1: return(3); case 2: if (D&15) return(3); if (strcmp((char *)&inbuf[i], "==")) return(2); break; case 3: if (D&3) return(3); if (strcmp((char *)&inbuf[i], "=")) return(2); } *len = j; } else { for (i=0,j=0; i < *len; i++) switch (i%3) { case 0: outbuf[j++] = radixN[inbuf[i]>>2]; c = (inbuf[i]&3)<<4; break; case 1: outbuf[j++] = radixN[c|inbuf[i]>>4]; c = (inbuf[i]&15)<<2; break; case 2: outbuf[j++] = radixN[c|inbuf[i]>>6]; outbuf[j++] = radixN[inbuf[i]&63]; c = 0; } if (i%3) outbuf[j++] = radixN[c]; switch (i%3) { case 1: outbuf[j++] = '='; case 2: outbuf[j++] = '='; } outbuf[*len = j] = '\0'; } return(0); } /* base64 error messages */ static char *radix_error(e) { switch (e) { case 0: return("Success"); case 1: return("Bad character in encoding"); case 2: return("Encoding not properly padded"); case 3: return("Decoded # of bits not a multiple of 8"); default: return("Unknown error"); } } int main (int argc,char *argv[]) { int error; int length; int i; unsigned char *in; unsigned char *out; int64_t il; int is; int ic; if (argc < 2) { printf("Error: Require HEX encoded objectSID\n"); exit(1); } printf("argc: %d argv: %s\n",argc,argv[1]); in=calloc(strlen(argv[1]),1); out=calloc(strlen(argv[1]),1); for ( i=strlen(argv[1]) ; i>=0 ; i-- ) { in[i]=argv[1][i]; if ( in[i] == '\r' || in[i] == '\n' ) in[i] = '\0'; } length=strlen(in)+1; if ((error = radix_encode(in, out, &length, 1)) != 0 ) { printf("%s\n", radix_error(error)); return 1; } ic = out[1]; il = ((int64_t)out[7]<<0) | ((int64_t)out[6]<<8) | ((int64_t)out[5]<<16) | ((int64_t)out[4]<<24) | ((int64_t)out[3]<<32) | ((int64_t)out[2]<<40); printf("S-%d-%d",out[0],il); for ( i=0;i