XSD Validation on iOS - iphone

I want to validate XML files against an XSD on iOS. The documentations alludes to using NSXMLDocument to do this, but its not available on iOS =(. Are there any light weight alternatives to do this on iOS?

I ended up using the validation facilities in libxml2 since its a library already included in iOS. Following this sample code
#include <libxml/parser.h>
#include <libxml/xmlschemas.h>
int is_valid(const xmlDocPtr doc, const char *schema_filename)
{
xmlDocPtr schema_doc = xmlReadFile(schema_filename, NULL, XML_PARSE_NONET);
if (schema_doc == NULL) {
/* the schema cannot be loaded or is not well-formed */
return -1;
}
xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(schema_doc);
if (parser_ctxt == NULL) {
/* unable to create a parser context for the schema */
xmlFreeDoc(schema_doc);
return -2;
}
xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt);
if (schema == NULL) {
/* the schema itself is not valid */
xmlSchemaFreeParserCtxt(parser_ctxt);
xmlFreeDoc(schema_doc);
return -3;
}
xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema);
if (valid_ctxt == NULL) {
/* unable to create a validation context for the schema */
xmlSchemaFree(schema);
xmlSchemaFreeParserCtxt(parser_ctxt);
xmlFreeDoc(schema_doc);
return -4;
}
int is_valid = (xmlSchemaValidateDoc(valid_ctxt, doc) == 0);
xmlSchemaFreeValidCtxt(valid_ctxt);
xmlSchemaFree(schema);
xmlSchemaFreeParserCtxt(parser_ctxt);
xmlFreeDoc(schema_doc);
/* force the return value to be non-negative on success */
return is_valid ? 1 : 0;
}

It appears that it is not exactly easy to do in Objective C, but there are several ideas listed at this S.O. question: Possible to validate xml against xsd using Objc/iPhone code at runtime
It seems CodeSynthesis supports this here : http://wiki.codesynthesis.com/Using_XSDE_in_iPhone_Applications
I am really just pulling links and ideas from the Stack Overflow question at this point, though.

There is not a general schema validator. Try using XSDE as proposed above. It is very fast and very, very reliable.
Nice tutorial is here: http://amateuritsolutions.blogspot.hu/2012/10/validate-xsd-schema-in-your-ios.html

Related

What is the most efficient way to call another function in parent function to execute Palindrome function?

In programming, there are multiple ways of doing the same problem. The following problem is in regards to palindrome. Though I feel that I am on the right track, I am not able to completely solve the problem to get to requested solution.
What is a palindrome? A word written forward or backward is the same and returns true. Example, "racecar". Hence, I designed the following code in Javascript...
function palindrome(string) {
string = string.toLowerCase();
lowString = string.toLowerCase().split("").reverse().join("");
for (var i=0; i<string.length; i++) {
if (string[i] !== lowString[i]) {
return false;
}
}
}
return true;
}
The above code returns true if Palindrome exists and returns false if not.
Then, the problem says - Given various palindrome in a string or array, please return the longest palindrome. So, I wrote the following:
function longestPalindrome(newstring) {
splitString = newString.split(" ");
for (var i=0; i < splitString; i++) {
if (splitString[i] == palindrome(splitString[i]) {
console.log(splitString[i]);
}
}
}
longestPalindrome("This is a racecar ada");'
But in the above code, I am not able to get the required outcome because I believe I am not calling the function correctly.
I would appreciate clear directions or even a solution built off of my track as well as the track you deem fittest.

jquery select2 plugin how to match only beginning of word

I am using select2 in my website and i want the autocomplete to match only the beginning of the word. For example, if I type "CA" I want CAmeroun to appear and not "vatiCAn".
I figured out how to resolve this by searching in the documentation (here https://github.com/select2/select2/issues/428).
In select2 library, replace in select2.js :
matcher: function(term, text) {
return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
},
by :
matcher: function(term, text) {
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
}
},
And tadaaa. It works. I hope someone who is better in JS (99% of JS developers) could give a better answer or create a good patch.
Don't forget to minify your JS ;) !
Inspired by #IsmailH answer. I've merged this code as matchCustom in the provided example, here.
And here's my modification,
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// `params.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
var modifiedData = $.extend({}, data, true);
// You can return modified objects from here
// This includes matching the `children` how you want in nested data sets
return modifiedData;
}
// Return `null` if the term should not be displayed
return null;
};

opensc-pkcs11 not getting any token

I am writing a sample program using opensc-pkcs11.so in redhat linux. This is for pure software implementation of AES encryption/decryption. I am not using for any card.
My program intilizes the cryptoki successfully but giving CKR_TOKEN_NOT_PRESENT error.
code snippet is given.
CK_FUNCTION_LIST_PTR pFunctionList;
CK_C_Initialize pC_Initialize;
CK_RV rv;
rv = C_GetFunctionList(&pFunctionList);
if(rv == CKR_OK)
pC_Initialize = pFunctionList -> C_Initialize;
rv = (*pC_Initialize)(NULL_PTR);
CK_ULONG ulSlotCount;
CK_SLOT_ID_PTR pSlotList;
CK_C_GetSlotList pC_GetSlotList;
pC_GetSlotList = pFunctionList -> C_GetSlotList;
rv = (*pC_GetSlotList)(CK_FALSE, NULL_PTR, &ulSlotCount);
/* Get list of all slots */
//rv = C_GetSlotList(FALSE, NULL_PTR, &ulSlotCount);
if (rv == CKR_OK)
{
cout<<"ulSlotCount="<<ulSlotCount<<endl;
pSlotList =
(CK_SLOT_ID_PTR)
malloc(ulSlotCount*sizeof(CK_SLOT_ID));
//rv = C_GetSlotList(FALSE, pSlotList, &ulSlotCount);
rv = (*pC_GetSlotList)(CK_FALSE, pSlotList, &ulSlotCount);
if (rv == CKR_OK)
{
/* Now use that list of all slots */
l_lSlotId = pSlotList[0];
cerr<<"lSlotId="<<l_lSlotId<<endl;
}
CK_SLOT_INFO slotInfo;
CK_TOKEN_INFO tokenInfo;
CK_C_GetSlotInfo pC_GetSlotInfo;
pC_GetSlotInfo = pFunctionList -> C_GetSlotInfo;
/* Get slot information for first slot */
rv = (*pC_GetSlotInfo)(pSlotList[0], &slotInfo);
fprintf(stderr, "pC_GetSlotInfo: rv = 0x%.8X\n", rv);
if(rv == CKR_OK)
{
/* Get token information for first slot */
cerr<<"pC_GetSlotInfo OK"<<endl;
CK_C_GetTokenInfo pC_GetTokenInfo;
pC_GetTokenInfo = pFunctionList -> C_GetTokenInfo;
rv = (*pC_GetTokenInfo)(pSlotList[0], &tokenInfo);
}
fprintf(stderr, "pC_GetTokenInfo: rv = 0x%.8X\n", rv);
if (rv == CKR_TOKEN_NOT_PRESENT)
{
cerr<<"CKR_TOKEN_NOT_PRESENT"<<endl;
}
free(pSlotList);
}
Can anybody give idea about what is happening? I believe opensc-pkcs11 can be used for just software implementation also.
Thanks in advance.
PKCS#11 library shipped with OpenSC acts "only as a driver" for a bunch of generally available cryptographic smart cards so unless you have a physical card reader connected to your computer it won't find any slots. If you are looking for a pure software PKCS#11 implementation then I believe you should pick one from my answer to your previous question. If none of them suits your need then maybe you could use some general purpose cryptographic library such as OpenSSL, GnuTLS or Botan.

Erasing page on stm32 fails with FLASH_ERROR_WRP

I am trying to erase one page in flash on an STM32F103RB like so:
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR | FLASH_FLAG_OPTERR);
FLASHStatus = FLASH_ErasePage(Page);
However, FLASH_ErasePage fails producing FLASH_ERROR_WRP
Manually enabling/disabling write protection in the stm32-linker tool doesn't fix the problem.
Basically FLASH_ErasePage fails with WRP error without trying to do anything if there's previous WRP error in the status register.
What comes to your FLASH_ClearFlag call, at least FLASH_FLAG_BSY will cause assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)); to fail (though I'm not really sure what happens in this case).
#define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFC0FD) == 0x00000000) && ((FLAG) != 0x00000000))
What is your page address ? Which address are you trying to access ?
For instance, this example is tested on STM32F100C8 in terms of not only erasing but also writing data correctly.
http://www.ozturkibrahim.com/TR/eeprom-emulation-on-stm32/
If using the HAL driver, your code might look like this (cut'n paste from an real project)
static HAL_StatusTypeDef Erase_Main_Program ()
{
FLASH_EraseInitTypeDef ins;
uint32_t sectorerror;
ins.TypeErase = FLASH_TYPEERASE_SECTORS;
ins.Banks = FLASH_BANK_1; /* Do not care, used for mass-erase */
#warning We currently erase from sector 2 (only keep 64KB of flash for boot))
ins.Sector = FLASH_SECTOR_4;
ins.NbSectors = 4;
ins.VoltageRange = FLASH_VOLTAGE_RANGE_3; /* voltage-range defines how big blocks can be erased at the same time */
return HAL_FLASHEx_Erase (&ins, &sectorerror);
}
The internal function in the HAL driver that actually does the work
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
{
uint32_t tmp_psize = 0U;
/* Check the parameters */
assert_param(IS_FLASH_SECTOR(Sector));
assert_param(IS_VOLTAGERANGE(VoltageRange));
if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
{
tmp_psize = FLASH_PSIZE_BYTE;
}
else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
{
tmp_psize = FLASH_PSIZE_HALF_WORD;
}
else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
{
tmp_psize = FLASH_PSIZE_WORD;
}
else
{
tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
}
/* If the previous operation is completed, proceed to erase the sector */
CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
FLASH->CR |= tmp_psize;
CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
FLASH->CR |= FLASH_CR_STRT;
}
Second thing to check. Is interrupts enabled, and is there any hardware access between the unlock call and the erase call?
I hope this helps

How do I deserialize a SAML assertion in Rampart/C (Axis2/C)?

I have SAML 1.1 and SAML 1.0 responses in utf-8 char * buffers, and I would like to transform them into Rampart/C saml_response_t * objects. My question is this:
What is the correct method for creating a saml_response_t from a string?
Ideally I'd like a code sample in C that does this and then disposes of the various parser tools in the correct order, since Axis2/C has some odd ideas about how to free memory.
My efforts to do this have met with failure, resulting mainly in memory faults, but here's what I have. Keep in mind that the deserialize_buffer() method does just what it says -- converts a buffer into an axiom_node_t *, but I'm open to replacing it as well.
/* : get the raw XML from the SAML assertion */
xml_data = Tcl_GetStringFromObj (objv[2], &xml_length);
/* : parse the response XML for processing */
stream = gstream_mem_create (xml_data, xml_length);
node = Axis2_axiom_deserialize_buffer (env, stream);
/* : build a SAML response from the parsed XML */
saml_response = saml_response_create (env);
saml_response_build (saml_response, node, env);
arraylist = saml_response_get_assertions (saml_response, env);
/* : create our return value object */
obj = Tcl_NewListObj (0, NULL);
/* :- convert the SAML assertion into a string */
for (i = 0;
i < axutil_array_list_size (arraylist, env);
i++) {
saml_assertion = (saml_assertion_t *) axutil_array_list_get (arraylist, env, i);
if (saml_assertion != NULL) {
assertion_node = saml_assertion_to_om (saml_assertion, NULL, env);
element = Tcl_NewStringObj (axiom_node_to_string (node, env), -1);
Tcl_ListObjAppendElement (interp, obj, element);
axiom_node_free_tree (assertion_node, env);
}
}