• Main Page
  • Classes
  • Files
  • File List
  • File Members

aegis_common.h

Go to the documentation of this file.
00001 /* -*- mode:c; tab-width:4; c-basic-offset:4; -*-
00002  *
00003  * This file is part of Aegis crypto services
00004  *
00005  * Copyright (C) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
00006  *
00007  * Contact: Juhani Mäkelä <ext-juhani.3.makela@nokia.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00069 #ifndef AEGIS_COMMON_H
00070 #define AEGIS_COMMON_H
00071 
00072 #include <sys/types.h>
00073 #include <unistd.h>
00074 #include <stdlib.h>
00075 #include <syslog.h>
00076 
00077 #if 0
00078 
00085 #define AEGIS_DEBUG_ENABLED
00086 #endif
00087 
00096 #define RAWDATA_PTR void*
00097 
00108 #define RAWDATA_RPTR void**
00109 
00110 #ifdef  __cplusplus
00111 #include <expat.h>
00112 #include <string>
00113 #include <vector>
00114 #include <map>
00115 
00116 extern "C" {
00117 
00125     bool absolute_pathname(const char* name, std::string& to_this);
00126 
00133     int process_name_of_pid(pid_t of_pid, std::string& to_this);
00134 
00140     bool process_name(std::string& to_this);
00141 
00148     void append_hex(std::string& to_this, unsigned char* dta, unsigned len);
00149 
00150 #else // Not C++
00151 
00155      #define bool int
00156 #endif
00157 
00162     #define PATH_SEP "/"
00163 
00169     bool file_exists(const char* name);
00170 
00176     bool directory_exists(const char* name);
00177 
00187     int create_directory(const char* path, int mode);
00188 
00199     typedef int aegis_callback(int nbr, void* item, void* context);
00200 
00211     int iterate_files(const char* in_directory,
00212                       const char* matching_names,
00213                       aegis_callback* cb_func,
00214                       void* ctx);
00215 
00221     #define DLOG_TO_CONSOLE 17
00222 
00235     void init_dlogger(int enabled, int use_signal, int other_signal);
00236 
00240     void dlog_message(const char* format, ...);
00241 
00254     const char* dynhex(const void* d, size_t len);
00255 
00263     char* base64_encode(const RAWDATA_PTR data, size_t len);
00264 
00272     size_t base64_decode(const char* string, RAWDATA_RPTR to_buf);
00273 
00274 #ifdef  __cplusplus
00275 } // extern "C"
00276 
00277 
00282 namespace aegis {
00283 
00291     class c_xmlnode 
00292     {
00293     public:
00299         c_xmlnode (c_xmlnode* of_parent, const char* of_name);
00300             
00304         ~c_xmlnode ();
00305         
00311         c_xmlnode* parent();
00312         
00317         const char* name ();
00318 
00326         std::string as_string (bool add_linebreaks, 
00327                                int indent_width, 
00328                                int indent_level);
00329 
00335         void append_content (const char* data, int len);
00336 
00341         void append_content (const char* data);
00342 
00348         void append_file (const char* file_name);
00349 
00358         const char* content ();
00359 
00369         void append_attribute (const char* name, const char* value);
00370 
00379         void append_attribute (const char* name, long value);
00380             
00385         int nbrof_attributes ();
00386 
00392         const char* attribute_name (unsigned of_pos);
00393 
00399         const char* attribute_value (unsigned of_pos);
00400 
00409         const char* attribute(const char* name, 
00410                               bool required, 
00411                               const char* defval);
00412 
00419         void remove_attribute (const char* name);
00420 
00428         c_xmlnode* navigate(const char* to_xpath, bool required);
00429 
00437         c_xmlnode* navigate(const char* to_xpath, const char* default_content);
00438 
00448         c_xmlnode* append_child (const char* element_name);
00449 
00454         int nbrof_children ();
00455 
00462         c_xmlnode* child (unsigned of_pos);
00463 
00474         c_xmlnode* child (const char* of_name, bool required);
00475 
00480         void remove_child (unsigned of_pos);
00481 
00490         void set_cdata(bool to_this);
00491 
00495         void trim_whitespace();
00496 
00502         std::string xpath();
00503 
00507         void reset();
00508 
00509     private:
00510         c_xmlnode* navigate(const char* to_xpath, bool required, const char* default_content);
00511         void xml_escaped(const char *from, std::string& to);
00512         
00513         std::string m_tagname;
00514         std::string m_content;
00515         c_xmlnode* m_parent;
00516         std::map<std::string,std::string> m_attributes;
00517         std::vector<c_xmlnode*> m_children;
00518         bool is_cdata;
00519         bool cdata_ended;
00520     };
00521 
00529     class c_xmldoc
00530     {
00531     public:
00535         c_xmldoc ();
00536             
00540         ~c_xmldoc ();
00541 
00546         void parse_file (const char* file_name);
00547 
00554         void parse_string (const char* xml_as_string, int length);
00555 
00564         void release_parser();
00565 
00575         void release_string_buffer ();
00576 
00583         c_xmlnode* create (const char* root_node_name);
00584     
00590         c_xmlnode* root();
00591 
00599         std::string as_string (bool pretty_printing);
00600 
00604         void release_content(void);
00605 
00612         bool save(const char* to_file);
00613 
00614         
00615         // Expat-parser's hook routines
00616         // ----------------------------
00617         // These need to be public to be callable from the
00618         // actual, static hook routines. Do not use them directly.
00619     
00628         void xml_element_start(const char* element_name, const char** attributes);
00629 
00636         void xml_element_end(const char* element_name);
00637 
00645         void xml_character_data(const char* data, const int len);
00646             
00652         void xml_cdata_start();
00653             
00659         void xml_cdata_end();
00660 
00668         bool trim_whitespace;
00669 
00670     private:
00671         void init_parser();
00672         void xml_parsing_error();
00673         
00674         XML_Parser expat_parser;
00675         c_xmlnode* root_node;
00676         c_xmlnode* cur_node;
00677         char* xml_str_buf;
00678         std::string m_filename;
00679     };
00680 } /* namespace aegis */
00681 
00682 #endif
00683 
00684 #ifndef _STRING_H
00685 #include <string.h>
00686 #endif
00687 
00693 #define bare_file_name(s) strrchr(s,'/')?strrchr(s,'/')+1:s
00694 
00710 #ifdef AEGIS_SHOW_ERRORS
00711 #define AEGIS_ERROR(format,args...)                      \
00712     do {                                                 \
00713         fprintf(stderr, "%s(%d): ERROR: " format "\n",   \
00714                 bare_file_name(__FILE__), __LINE__       \
00715                 ,##args);                                \
00716     } while (0)
00717 #else
00718 #ifdef AEGIS_DEBUG_ENABLED
00719 #define AEGIS_ERROR(format,args...)                          \
00720     do {                                                     \
00721         dlog_message("<0>%s(%d)[%d]: ERROR " format,         \
00722                  bare_file_name(__FILE__), __LINE__,         \
00723                  getpid() ,##args);                          \
00724         syslog(LOG_ERR, "%s(%d): ERROR " format,             \
00725            bare_file_name(__FILE__), __LINE__                \
00726            ,##args);                                         \
00727     } while (0)
00728 #else
00729 #define AEGIS_ERROR(format,args...)                          \
00730     do {                                                     \
00731         syslog(LOG_ERR, "%s(%d): ERROR " format,             \
00732            bare_file_name(__FILE__), __LINE__                \
00733            ,##args);                                         \
00734     } while (0)
00735 #endif
00736 #endif
00737 
00747 #ifdef AEGIS_DEBUG_ENABLED
00748 #define AEGIS_DEBUG(level,format,args...)   \
00749     do { \
00750       dlog_message("<%d>%s(%d)[%d]: " format, level, bare_file_name(__FILE__), __LINE__, \
00751                      getpid() ,##args);                         \
00752     } while (0)
00753 
00759 #define AEGIS_ENTER \
00760     do { \
00761         dlog_message("<2>%s(%d)[%d]: => %s", bare_file_name(__FILE__), __LINE__, getpid(), \
00762                      __PRETTY_FUNCTION__);                              \
00763     } while (0)
00764 
00770 #define AEGIS_EXIT  \
00771     do { \
00772         dlog_message("<2>%s(%d)[%d]: <= %s", bare_file_name(__FILE__), __LINE__, getpid(), \
00773                      __PRETTY_FUNCTION__);                              \
00774     } while (0)
00775 #else
00776 #define AEGIS_DEBUG(level,format,args...)
00777 #define AEGIS_ENTER
00778 #define AEGIS_EXIT
00779 #endif
00780 
00787 #define GETENV(name,deflt) ({char* c = getenv(name); c?c:deflt;})
00788 
00789 #endif

Generated on Tue Jun 28 2011 14:39:10 for Aegis Crypto by  doxygen 1.7.1