Beispiel #1
0
static int
assert_strcasecmp (const char *s1, const char *s2, int expected)
{
  int result = raptor_strcasecmp(s1, s2);
  result = (result > 0) ? 1 : ((result <0) ? -1 : 0);

  if(result != expected)
    {
      fprintf(stderr, "FAIL strcasecmp (%s, %s) gave %d != %d\n",
              s1, s2, result, expected);
      return 1;
    }
  return 0;
}
Beispiel #2
0
/**
 * raptor_uri_uri_string_to_filename_fragment:
 * @uri_string: The file: URI to convert
 * @fragment_p: Address of pointer to store any URI fragment or NULL
 *
 * Convert a file: URI to a filename and fragment.
 * 
 * Handles the OS-specific file: URIs to filename mappings.  Returns
 * a new buffer containing the filename that the caller must free.
 *
 * If @fragment_p is given, a new string containing the URI fragment
 * is returned, or NULL if none is present
 * 
 * Return value: A newly allocated string with the filename or NULL on failure
 **/
char *
raptor_uri_uri_string_to_filename_fragment(const unsigned char *uri_string,
                                           unsigned char **fragment_p) 
{
  char *filename;
  size_t len=0;
  raptor_uri_detail *ud=NULL;
  unsigned char *from;
  char *to;
#ifdef WIN32
  unsigned char *p;
#endif

  if(!uri_string || !*uri_string)
    return NULL;
  
  ud=raptor_new_uri_detail(uri_string);
  if(!ud)
    return NULL;
  

  if(!ud->scheme || raptor_strcasecmp((const char*)ud->scheme, "file")) {
    raptor_free_uri_detail(ud);
    return NULL;
  }

  if(ud->authority) {
    if(!*ud->authority)
      ud->authority=NULL;
    else if(!raptor_strcasecmp((const char*)ud->authority, "localhost"))
      ud->authority=NULL;
  }

  /* Cannot do much if there is no path */
  if(!ud->path || (ud->path && !*ud->path)) {
    raptor_free_uri_detail(ud);
    return NULL;
  }

  /* See raptor_uri_filename_to_uri_string for details of the mapping */
#ifdef WIN32
  if(ud->authority)
    len+=ud->authority_len+3;

  p=ud->path;
  /* remove leading slash from path if there is one */
  if(*p && p[0] == '/') {
	  p++;
	  len--;
  }
  /* handle case where path starts with drive letter */
  if(*p && (p[1] == '|' || p[1] == ':')) {
    /* Either 
     *   "a:" like in file://a|/... or file://a:/... 
     * or
     *   "a:." like in file://a:./foo
     * giving device-relative path a:foo
     */
    if(p[2]=='.') {
      p[2]=*p;
      p[3]=':';
      p+= 2;
      len-= 2; /* remove 2 for ./ */
    } else
      p[1]=':';
  }
#endif


  /* add URI-escaped filename length */
  for(from=ud->path; *from ; from++) {
    len++;
    if(*from == '%')
      from+= 2;
  }


  /* Something is wrong */
  if(!len) {
    raptor_free_uri_detail(ud);
    return NULL;
  }
    
  filename=(char*)RAPTOR_MALLOC(cstring, len + sizeof(char*));
  if(!filename) {
    raptor_free_uri_detail(ud);
    return NULL;
  }


  to=filename;

#ifdef WIN32
  if(ud->authority) {
    *to++ = '\\';
    *to++ = '\\';
    from=ud->authority;
    while( (*to++ = *from++) )
      ;
    to--;
    *to++ = '\\';
  }
  
  /* copy path after all /s */
  from=p;
#else
  from=ud->path;
#endif

  while(*from) {
    char c=*from++;
#ifdef WIN32
    if(c == '/')
      *to++ ='\\';
    else
#endif
    if(c == '%') {
      if(*from && from[1]) {
        char hexbuf[3];
        char *endptr=NULL;
        hexbuf[0]=(char)*from;
        hexbuf[1]=(char)from[1];
        hexbuf[2]='\0';
        c=(char)strtol((const char*)hexbuf, &endptr, 16);
        if(endptr == &hexbuf[2])
          *to++ = c;
      }
      from+= 2;
    } else
      *to++ =c;
  }
  *to='\0';

  if(fragment_p) {
    if(ud->fragment) {
      len=ud->fragment_len;
      *fragment_p=(unsigned char*)RAPTOR_MALLOC(cstring, len + sizeof(char*));
      if(*fragment_p)
        strncpy((char*)*fragment_p, (const char*)ud->fragment, len+1);
    } else
      *fragment_p=NULL;
  }

  raptor_free_uri_detail(ud);

  return filename;
}
Beispiel #3
0
/**
 * raptor_uri_uri_string_to_filename_fragment - Convert a file: URI to a filename and fragment
 * @uri_string: The file: URI to convert
 * @fragment_p: Address of pointer to store any URI fragment or NULL
 * 
 * Handles the OS-specific file: URIs to filename mappings.  Returns
 * a new buffer containing the filename that the caller must free.
 *
 * If fragment_p is given, a new string containing the URI fragment
 * is returned, or NULL if none is present
 * 
 * Return value: A newly allocated string with the filename or NULL on failure
 **/
char *
raptor_uri_uri_string_to_filename_fragment(const unsigned char *uri_string,
                                           unsigned char **fragment_p) 
{
  unsigned char *buffer;
  char *filename;
  int uri_string_len=strlen((const char*)uri_string);
  size_t len=0;
  unsigned char *scheme, *authority, *path, *query, *fragment;
#ifdef WIN32
  unsigned char *p, *from, *to;
  int is_relative_path=0;
#endif

  buffer=(unsigned char*)RAPTOR_MALLOC(cstring, uri_string_len+1);
  if(!buffer)
    return NULL;
  
  raptor_uri_parse (uri_string, buffer, uri_string_len,
                    &scheme, &authority, &path, &query, &fragment);

  if(!scheme || raptor_strcasecmp((const char*)scheme, "file")) {
    RAPTOR_FREE(cstring, buffer);
    return NULL;
  }

  if(authority && !raptor_strcasecmp((const char*)authority, "localhost")) {
    authority=NULL;
  }

  /* See raptor_uri_filename_to_uri_string for details of the mapping */
#ifdef WIN32
  if(authority) {
    len+=strlen((const char*)authority);
    p=strchr(authority, '|');
    if(!p)
      p=strchr(authority, ':');
    if(p) {
      /* Either 
       *   "a:" like in file://a|/... or file://a:/... 
       * or
       *   "a:." like in file://a:./foo
       * giving device-relative path a:foo
       */
      if(p[1]=='.') {
        p[1]='\0';
        is_relative_path=1;
      }
      *p=':';
    } else {
      /* Otherwise UNC like "server" in file://server//share */
      len+=2; /* \\ between "server" and "share" */
    }
  } /* end if authority */
  if(!is_relative_path)
    len++;/* for \ between authority and path */
  len--; /* for removing leading / off path */
#endif
  len+=strlen((const char*)path);


  filename=(char*)RAPTOR_MALLOC(cstring, len+1);
  if(!filename) {
    RAPTOR_FREE(cstring, buffer);
    return NULL;
  }


#ifdef WIN32
  *filename='\0';
  if(authority) {
    /* p was set above to point to ':' (was '|') in authority */
    if(!p)
      strcpy(filename, "\\\\");
    strcat(filename, authority);
  }

  if(!is_relative_path)
    strcat(filename,"\\");

  /* find end of filename */
  to=filename;
  while(*to++)
    ;
  to--;
  
  /* copy path after leading \ */
  from=path+1;
  while(*from) {
    char c=*from++;
    if(c == '/')
      *to++ ='\\';
    else
      *to++ =c;
  }
  *to='\0';
#else
  strcpy((char*)filename, (const char*)path);
#endif

  if(fragment_p) {
    if(fragment) {
      len=strlen((const char*)fragment);
      *fragment_p=(unsigned char*)RAPTOR_MALLOC(cstring, len+1);
      strcpy((char*)*fragment_p, (const char*)fragment);
    } else
      *fragment_p=NULL;
  }

  RAPTOR_FREE(cstring, buffer);

  return filename;
}