~ubuntu-branches/ubuntu/saucy/mapserver/saucy-security

« back to all changes in this revision

Viewing changes to mapowscommon.c

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-12-23 14:02:06 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111223140206-n3h9t2hsa8hyslmu
Tags: 6.0.1-2
Added missed stuff for libmapscript-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: mapowscommon.c 8170 2008-12-02 17:28:34Z tomkralidis $
 
2
 * $Id: mapowscommon.c 10772 2010-11-29 18:27:02Z aboudreault $
3
3
 *
4
4
 * Project:  MapServer
5
5
 * Purpose:  OGC OWS Common Implementation for use by MapServer OGC code
42
42
#include "mapowscommon.h"
43
43
#include "maplibxml2.h"
44
44
 
45
 
MS_CVSID("$Id: mapowscommon.c 8170 2008-12-02 17:28:34Z tomkralidis $")
46
 
 
47
 
/**
48
 
 * msOWSCommonNegotiateVersion()
49
 
 *
50
 
 * returns a supported version as per subclause 7.3.2
51
 
 *
52
 
 * @param requested_version the version passed by the client
53
 
 * @param supported_versions an array of supported versions
54
 
 * @param num_supported_versions size of supported_versions
55
 
 *
56
 
 * @return supported version integer, or -1 on error
57
 
 *
58
 
 */
59
 
 
60
 
int msOWSCommonNegotiateVersion(int requested_version, int supported_versions[], int num_supported_versions) {
61
 
  int i;
62
 
 
63
 
  /* if version is not set return error */
64
 
  if (! requested_version)
65
 
    return -1;
66
 
 
67
 
  /* return the first entry that's equal to the requested version */
68
 
  for (i = 0; i < num_supported_versions; i++) {
69
 
    if (supported_versions[i] == requested_version)
70
 
      return supported_versions[i];
71
 
  }
72
 
 
73
 
  /* no match; calling code should throw an exception */
74
 
  return -1;
75
 
}
 
45
MS_CVSID("$Id: mapowscommon.c 10772 2010-11-29 18:27:02Z aboudreault $")
 
46
 
76
47
 
77
48
 
78
49
/**
487
458
    xmlNewProp(psRootNode, BAD_CAST "xml:lang", BAD_CAST language);
488
459
  }
489
460
 
490
 
  xsi_schemaLocation = strdup((char *)psNsOws->href);
 
461
  xsi_schemaLocation = msStrdup((char *)psNsOws->href);
491
462
  xsi_schemaLocation = msStringConcatenate(xsi_schemaLocation, " ");
492
463
  xsi_schemaLocation = msStringConcatenate(xsi_schemaLocation, (char *)schemas_location);
493
464
  xsi_schemaLocation = msStringConcatenate(xsi_schemaLocation, "/ows/");
563
534
  /* add attributes to the root element */
564
535
  xmlNewProp(psRootNode, BAD_CAST "crs", BAD_CAST crs);
565
536
 
566
 
  sprintf( dim_string, "%d", dimensions );
 
537
  snprintf( dim_string, sizeof(dim_string), "%d", dimensions );
567
538
  xmlNewProp(psRootNode, BAD_CAST "dimensions", BAD_CAST dim_string);
568
539
 
569
 
  sprintf(LowerCorner, "%.15g %.15g", minx, miny);
570
 
  sprintf(UpperCorner, "%.15g %.15g", maxx, maxy);
 
540
  snprintf(LowerCorner, sizeof(LowerCorner), "%.15g %.15g", minx, miny);
 
541
  snprintf(UpperCorner, sizeof(UpperCorner), "%.15g %.15g", maxx, maxy);
571
542
 
572
543
  /* add child elements */
573
544
  xmlNewChild(psRootNode, psNsOws,BAD_CAST "LowerCorner",BAD_CAST LowerCorner);
604
575
  /* create element name */
605
576
  psRootNode = xmlNewNode(psNsOws, BAD_CAST "WGS84BoundingBox");
606
577
 
607
 
  sprintf( dim_string, "%d", dimensions );
 
578
  snprintf( dim_string, sizeof(dim_string), "%d", dimensions );
608
579
  xmlNewProp(psRootNode, BAD_CAST "dimensions", BAD_CAST dim_string);
609
580
 
610
 
  sprintf(LowerCorner, "%.15g %.15g", minx, miny);
611
 
  sprintf(UpperCorner, "%.15g %.15g", maxx, maxy);
 
581
  snprintf(LowerCorner, sizeof(LowerCorner), "%.15g %.15g", minx, miny);
 
582
  snprintf(UpperCorner, sizeof(UpperCorner), "%.15g %.15g", maxx, maxy);
612
583
 
613
584
  /* add child elements */
614
585
  xmlNewChild(psRootNode, psNsOws,BAD_CAST "LowerCorner",BAD_CAST LowerCorner);
630
601
 
631
602
int _validateNamespace(xmlNsPtr psNsOws) {
632
603
  char namespace_prefix[10];
633
 
  sprintf(namespace_prefix, "%s", psNsOws->prefix);
 
604
  snprintf(namespace_prefix, sizeof(namespace_prefix), "%s", psNsOws->prefix);
634
605
  if (strcmp(namespace_prefix, MS_OWSCOMMON_OWS_NAMESPACE_PREFIX) == 0)
635
606
    return MS_SUCCESS;
636
607
  else 
637
608
    return MS_FAILURE;
638
609
}
639
610
 
 
611
xmlNodePtr msOWSCommonxmlNewChildEncoded( xmlNodePtr psParent, xmlNsPtr psNs, const char* name, 
 
612
                                          const char *content, const char *encoding)
 
613
{
 
614
    char *encoded = NULL;
 
615
    xmlNodePtr psNode;
 
616
 
 
617
    if (encoding && content)
 
618
    {
 
619
        encoded = msGetEncodedString(content, encoding);
 
620
        psNode =  xmlNewChild(psParent, psNs, BAD_CAST name, BAD_CAST encoded);
 
621
        msFree(encoded);
 
622
        return psNode;
 
623
    }
 
624
    else
 
625
      return xmlNewChild(psParent, psNs, BAD_CAST name, BAD_CAST content);
 
626
}
 
627
 
 
628
/*
 
629
 * Valid an xml string against an XML schema
 
630
 * Inpired from: http://xml.developpez.com/sources/?page=validation#validate_XSD_CppCLI_2
 
631
 * taken from tinyows.org
 
632
 */
 
633
int msOWSSchemaValidation(const char* xml_schema, const char* xml)
 
634
{
 
635
    xmlSchemaPtr schema;
 
636
    xmlSchemaParserCtxtPtr ctxt;
 
637
    xmlSchemaValidCtxtPtr validctxt;
 
638
    int ret;
 
639
    xmlDocPtr doc;
 
640
 
 
641
    if (!xml_schema || !xml)
 
642
      return MS_FAILURE;
 
643
 
 
644
    xmlInitParser();
 
645
    schema = NULL;
 
646
    ret = -1;
 
647
 
 
648
    /* Open XML Schema File */
 
649
    ctxt = xmlSchemaNewParserCtxt(xml_schema);
 
650
    /*
 
651
    else ctxt = xmlSchemaNewMemParserCtxt(xml_schema);
 
652
    */
 
653
    /*
 
654
    xmlSchemaSetParserErrors(ctxt,
 
655
                             (xmlSchemaValidityErrorFunc) libxml2_callback,
 
656
                             (xmlSchemaValidityWarningFunc) libxml2_callback, stderr);
 
657
    */
 
658
 
 
659
    schema = xmlSchemaParse(ctxt);
 
660
    xmlSchemaFreeParserCtxt(ctxt);
 
661
 
 
662
    /* If XML Schema hasn't been rightly loaded */
 
663
    if (schema == NULL) {
 
664
        xmlSchemaCleanupTypes();
 
665
        xmlMemoryDump();
 
666
        xmlCleanupParser();
 
667
        return ret;
 
668
    }
 
669
 
 
670
    doc = xmlParseDoc((xmlChar *)xml);
 
671
 
 
672
    if (doc != NULL) {
 
673
        /* Loading XML Schema content */
 
674
        validctxt = xmlSchemaNewValidCtxt(schema);
 
675
        /*
 
676
        xmlSchemaSetValidErrors(validctxt,
 
677
                                (xmlSchemaValidityErrorFunc) libxml2_callback,
 
678
                                (xmlSchemaValidityWarningFunc) libxml2_callback, stderr);
 
679
        */
 
680
        /* validation */
 
681
        ret = xmlSchemaValidateDoc(validctxt, doc);
 
682
        xmlSchemaFreeValidCtxt(validctxt);
 
683
    }
 
684
 
 
685
    xmlSchemaFree(schema);
 
686
    xmlFreeDoc(doc);
 
687
    xmlCleanupParser();
 
688
 
 
689
    return ret;
 
690
}
 
691
 
640
692
#endif /* defined(USE_LIBXML2) */
 
693
 
 
694
 
 
695
/**
 
696
 * msOWSCommonNegotiateVersion()
 
697
 *
 
698
 * returns a supported version as per subclause 7.3.2
 
699
 *
 
700
 * @param requested_version the version passed by the client
 
701
 * @param supported_versions an array of supported versions
 
702
 * @param num_supported_versions size of supported_versions
 
703
 *
 
704
 * @return supported version integer, or -1 on error
 
705
 *
 
706
 */
 
707
 
 
708
int msOWSCommonNegotiateVersion(int requested_version, int supported_versions[], int num_supported_versions) {
 
709
  int i;
 
710
 
 
711
  /* if version is not set return error */
 
712
  if (! requested_version)
 
713
    return -1;
 
714
 
 
715
  /* return the first entry that's equal to the requested version */
 
716
  for (i = 0; i < num_supported_versions; i++) {
 
717
    if (supported_versions[i] == requested_version)
 
718
      return supported_versions[i];
 
719
  }
 
720
 
 
721
  /* no match; calling code should throw an exception */
 
722
  return -1;
 
723
}