diff options
Diffstat (limited to 'src/lib/libssl/src/demos/maurice/example4.c')
| -rw-r--r-- | src/lib/libssl/src/demos/maurice/example4.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/lib/libssl/src/demos/maurice/example4.c b/src/lib/libssl/src/demos/maurice/example4.c new file mode 100644 index 0000000000..d436a20019 --- /dev/null +++ b/src/lib/libssl/src/demos/maurice/example4.c | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | /* NOCW */ | ||
| 2 | /* | ||
| 3 | Please read the README file for condition of use, before | ||
| 4 | using this software. | ||
| 5 | |||
| 6 | Maurice Gittens <mgittens@gits.nl> January 1997 | ||
| 7 | |||
| 8 | */ | ||
| 9 | |||
| 10 | #include <stdio.h> | ||
| 11 | #include <fcntl.h> | ||
| 12 | #include <sys/stat.h> | ||
| 13 | #include <evp.h> | ||
| 14 | |||
| 15 | #define STDIN 0 | ||
| 16 | #define STDOUT 1 | ||
| 17 | #define BUFLEN 512 | ||
| 18 | |||
| 19 | static const char *usage = "Usage: example4 [-d]\n"; | ||
| 20 | |||
| 21 | void do_encode(void); | ||
| 22 | void do_decode(void); | ||
| 23 | |||
| 24 | int main(int argc, char *argv[]) | ||
| 25 | { | ||
| 26 | if ((argc == 1)) | ||
| 27 | { | ||
| 28 | do_encode(); | ||
| 29 | } | ||
| 30 | else if ((argc == 2) && !strcmp(argv[1],"-d")) | ||
| 31 | { | ||
| 32 | do_decode(); | ||
| 33 | } | ||
| 34 | else | ||
| 35 | { | ||
| 36 | fprintf(stderr,"%s", usage); | ||
| 37 | exit(1); | ||
| 38 | } | ||
| 39 | |||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | void do_encode() | ||
| 44 | { | ||
| 45 | char buf[BUFLEN]; | ||
| 46 | char ebuf[BUFLEN+24]; | ||
| 47 | unsigned int ebuflen, rc; | ||
| 48 | EVP_ENCODE_CTX ectx; | ||
| 49 | |||
| 50 | EVP_EncodeInit(&ectx); | ||
| 51 | |||
| 52 | while(1) | ||
| 53 | { | ||
| 54 | int readlen = read(STDIN, buf, sizeof(buf)); | ||
| 55 | |||
| 56 | if (readlen <= 0) | ||
| 57 | { | ||
| 58 | if (!readlen) | ||
| 59 | break; | ||
| 60 | else | ||
| 61 | { | ||
| 62 | perror("read"); | ||
| 63 | exit(1); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | EVP_EncodeUpdate(&ectx, ebuf, &ebuflen, buf, readlen); | ||
| 68 | |||
| 69 | write(STDOUT, ebuf, ebuflen); | ||
| 70 | } | ||
| 71 | |||
| 72 | EVP_EncodeFinal(&ectx, ebuf, &ebuflen); | ||
| 73 | |||
| 74 | write(STDOUT, ebuf, ebuflen); | ||
| 75 | } | ||
| 76 | |||
| 77 | void do_decode() | ||
| 78 | { | ||
| 79 | char buf[BUFLEN]; | ||
| 80 | char ebuf[BUFLEN+24]; | ||
| 81 | unsigned int ebuflen, rc; | ||
| 82 | EVP_ENCODE_CTX ectx; | ||
| 83 | |||
| 84 | EVP_DecodeInit(&ectx); | ||
| 85 | |||
| 86 | while(1) | ||
| 87 | { | ||
| 88 | int readlen = read(STDIN, buf, sizeof(buf)); | ||
| 89 | int rc; | ||
| 90 | |||
| 91 | if (readlen <= 0) | ||
| 92 | { | ||
| 93 | if (!readlen) | ||
| 94 | break; | ||
| 95 | else | ||
| 96 | { | ||
| 97 | perror("read"); | ||
| 98 | exit(1); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | rc = EVP_DecodeUpdate(&ectx, ebuf, &ebuflen, buf, readlen); | ||
| 103 | if (rc <= 0) | ||
| 104 | { | ||
| 105 | if (!rc) | ||
| 106 | { | ||
| 107 | write(STDOUT, ebuf, ebuflen); | ||
| 108 | break; | ||
| 109 | } | ||
| 110 | |||
| 111 | fprintf(stderr, "Error: decoding message\n"); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | write(STDOUT, ebuf, ebuflen); | ||
| 116 | } | ||
| 117 | |||
| 118 | EVP_DecodeFinal(&ectx, ebuf, &ebuflen); | ||
| 119 | |||
| 120 | write(STDOUT, ebuf, ebuflen); | ||
| 121 | } | ||
| 122 | |||
