X-Git-Url: https://git.strongswan.org/?p=strongswan.git;a=blobdiff_plain;f=src%2Flibstrongswan%2Fchunk.c;h=d70e1723f2a4875daa1a8802537b672ff6431c4d;hp=f2c8a3efb2b56aa03f5b71618e2e428a83fb4c9f;hb=7cbe4a33e9d8604999cd2ce8f45e633af703c014;hpb=f27f6296e6ae5beece739342fd54528cf91e5394 diff --git a/src/libstrongswan/chunk.c b/src/libstrongswan/chunk.c index f2c8a3e..d70e172 100644 --- a/src/libstrongswan/chunk.c +++ b/src/libstrongswan/chunk.c @@ -22,9 +22,11 @@ */ #include +#include #include "chunk.h" +#include #include /** @@ -205,6 +207,45 @@ void chunk_split(chunk_t chunk, const char *mode, ...) va_end(chunks); } +/** + * Described in header. + */ +bool chunk_write(chunk_t chunk, const char *path, const char *label, mode_t mask, bool force) +{ + mode_t oldmask; + FILE *fd; + + if (!force) + { + fd = fopen(path, "r"); + if (fd) + { + fclose(fd); + DBG1(" %s file '%s' already exists", label, path); + return FALSE; + } + } + + /* set umask */ + oldmask = umask(mask); + + fd = fopen(path, "w"); + + if (fd) + { + fwrite(chunk.ptr, sizeof(u_char), chunk.len, fd); + fclose(fd); + DBG1(" written %s file '%s' (%u bytes)", label, path, chunk.len); + umask(oldmask); + return TRUE; + } + else + { + DBG1(" could not open %s file '%s' for writing", label, path); + umask(oldmask); + return FALSE; + } +} /** * Described in header. @@ -233,6 +274,21 @@ chunk_t chunk_skip(chunk_t chunk, size_t bytes) /** * Described in header. */ +int chunk_compare(chunk_t a, chunk_t b) +{ + int compare_len = a.len - b.len; + int len = (compare_len < 0)? a.len : b.len; + + if (compare_len != 0 || len == 0) + { + return compare_len; + } + return memcmp(a.ptr, b.ptr, len); +}; + +/** + * Described in header. + */ bool chunk_equals(chunk_t a, chunk_t b) { return a.ptr != NULL && b.ptr != NULL &&